public void WrapsIOExceptionsDuringWrite()
        {
            MockFileSystem      fs  = new MockFileSystem();
            FileSystemTaskQueue dut = CreateFileBasedQueue(fs, Item1EntryText);

            dut.IsEmpty.ShouldBeFalse();

            fs.File.TruncateWrites = true;

            Assert.Throws <FileBasedCollectionException>(() => dut.EnqueueAndFlush(Item2Payload));

            fs.File.TruncateWrites = false;
            fs.File.ReadAt(fs.File.Length - 2, 2).ShouldNotEqual("\r\n", "Bad Test: The file is supposed to be corrupt.");

            string error;

            FileSystemTaskQueue.TryCreate(null, MockEntryFileName, fs, out dut, out error).ShouldEqual(true);
            dut.IsEmpty.ShouldBeFalse();
            using (dut)
            {
                FileSystemTask output;
                dut.TryPeek(out output).ShouldEqual(true);
                output.ShouldEqual(Item1Payload);
                dut.DequeueAndFlush(output);
            }

            dut.IsEmpty.ShouldBeTrue();
        }
        private static FileSystemTaskQueue CreateFileBasedQueue(MockFileSystem fs, string initialContents)
        {
            fs.File         = new ReusableMemoryStream(initialContents);
            fs.ExpectedPath = MockEntryFileName;

            string error;
            FileSystemTaskQueue dut;

            FileSystemTaskQueue.TryCreate(null, MockEntryFileName, fs, out dut, out error).ShouldEqual(true, error);
            dut.ShouldNotBeNull();
            return(dut);
        }
        public void ReturnsFalseWhenOpenFails()
        {
            MockFileSystem fs = new MockFileSystem();

            fs.File            = new ReusableMemoryStream(string.Empty);
            fs.ThrowDuringOpen = true;

            string error;
            FileSystemTaskQueue dut;

            FileSystemTaskQueue.TryCreate(null, MockEntryFileName, fs, out dut, out error).ShouldEqual(false);
            dut.ShouldBeNull();
            error.ShouldNotBeNull();
        }
        public override IssueType HasIssue(List <string> messages)
        {
            string error;
            FileSystemTaskQueue instance;

            if (!FileSystemTaskQueue.TryCreate(
                    this.Tracer,
                    this.dataPath,
                    new PhysicalFileSystem(),
                    out instance,
                    out error))
            {
                messages.Add("Failed to read background operations: " + error);
                return(IssueType.CantFix);
            }

            return(IssueType.None);
        }
        private bool UpdateBackgroundOperations(ITracer tracer, string dotGVFSRoot)
        {
            string esentBackgroundOpsFolder = Path.Combine(dotGVFSRoot, EsentBackgroundOpsFolder);

            if (Directory.Exists(esentBackgroundOpsFolder))
            {
                string newBackgroundOpsFolder = Path.Combine(dotGVFSRoot, GVFSConstants.DotGVFS.Databases.BackgroundFileSystemTasks);
                try
                {
                    using (PersistentDictionary <long, GVFltCallbacks.BackgroundGitUpdate> oldBackgroundOps =
                               new PersistentDictionary <long, GVFltCallbacks.BackgroundGitUpdate>(esentBackgroundOpsFolder))
                    {
                        string error;
                        FileSystemTaskQueue newBackgroundOps;
                        if (!FileSystemTaskQueue.TryCreate(
                                tracer,
                                newBackgroundOpsFolder,
                                new PhysicalFileSystem(),
                                out newBackgroundOps,
                                out error))
                        {
                            tracer.RelatedError("Failed to create new background operations folder: " + error);
                            return(false);
                        }

                        using (newBackgroundOps)
                        {
                            foreach (KeyValuePair <long, GVFltCallbacks.BackgroundGitUpdate> kvp in oldBackgroundOps)
                            {
                                tracer.RelatedInfo("Copying ESENT entry: {0} = {1}", kvp.Key, kvp.Value);
                                newBackgroundOps.EnqueueAndFlush(
                                    new FileSystemTask(
                                        (FileSystemTask.OperationType)kvp.Value.Operation,
                                        kvp.Value.VirtualPath,
                                        kvp.Value.OldVirtualPath));
                            }
                        }
                    }
                }
                catch (IOException ex)
                {
                    tracer.RelatedError("Could not write to new background operations: " + ex.Message);
                    return(false);
                }
                catch (EsentException ex)
                {
                    tracer.RelatedError("BackgroundOperations appears to be from an older version of GVFS and corrupted: " + ex.Message);
                    return(false);
                }

                string backupName;
                if (this.TryRenameFolderForDelete(tracer, esentBackgroundOpsFolder, out backupName))
                {
                    // If this fails, we leave behind cruft, but there's no harm because we renamed.
                    this.TryDeleteFolder(tracer, backupName);
                    return(true);
                }
                else
                {
                    // To avoid double upgrading, we should rollback if we can't rename the old data
                    this.TryDeleteFile(tracer, RepoMetadata.Instance.DataFilePath);
                    return(false);
                }
            }

            return(true);
        }