private bool VerifyExistingResourceFiles()

in csharp/NativeUtils/ResourceLoader.cs [818:913]


        private bool VerifyExistingResourceFiles(string deploymentPath, Opt options)
        {
            // Verify automatically fails if overwrite mode is forced
            if (options.HasFlag(Opt.AlwaysOverwrite))
            {
                Dbg("last.verifyExisting", "OverwriteAnyway");
                return false;
            }

            // Make sure all resource files are closed
            if (!options.HasFlag(Opt.ReusePartiallyDeployed))
                DisposeResourceFiles();

            int timeout = _retryTimeoutMs;
            int numExpected = _resources.Count;
            do
            {
                int numFound = 0, numOpened = 0;
                foreach (var resource in _resources)
                {
                    string filePath = resource.FullPath(deploymentPath);

                    if (File.Exists(filePath))
                    {
                        ++numFound;
                        DbgLast("filesExisted", numFound);
                        if (null == resource.File)
                        {
                            DbgLast("readLockTries", numFound);
                            try
                            {
                                if (LogLevelLeast(DBG))
                                    Log($"Found existing, taking read lock: {filePath}");

                                resource.ReadLock(filePath);
                            }
                            catch (IOException)
                            {
                                // On simple IOException (Also returned due to failed locks), but no lockfile, we will wait until timeout
                                continue;
                            }

                            if (0 != (options & (Opt.VerifyContent | Opt.VerifyLength)))
                            {
                                long fileLength = new FileInfo(filePath).Length;
                                // TODO: Must cache file size and hash for verification. Loading from zstd every time is wrong
                                //if (options.HasFlag(Opt.VerifyLength))
                                //{
                                //	if (fileLength == (resource.IsZstd ? (long)ZStdDecompress.GetDecompressedSize(inputData) : length))
                                //		continue;
                                //}
                            }
                        }

                        ++numOpened;
                    }
                }

                // Nothing found? Fail immediately
                if (0 == numFound)
                {
                    Dbg("last.verifyExisting", "Nothing");
                    break;
                }

                // All found? Return success
                if (numOpened == numExpected)
                {
                    Dbg("last.verifyExisting", "All");
                    return true;
                }

                // Able to open some of the files
                if (numOpened == numFound)
                {
                    Dbg("last.verifyExisting", "Some");
                    break;
                }

                // If there is a lock file, fail
                if (FileJanitor.LockFileExists(deploymentPath))
                {
                    Dbg("last.verifyExisting", "Locked");
                    break;
                }

                // Ok, we can't open _some_ of the files we found and there is no lock file. Probably being written, but not by this class.
                // Wait and retry until timeout
                timeout -= RandomSleep(timeout);
            } while (timeout > 0);

            if (!options.HasFlag(Opt.ReusePartiallyDeployed))
                DisposeResourceFiles();

            return false;
        }