Ejemplo n.º 1
0
        private Result WriteFile(ReadOnlySpan <byte> data)
        {
            Debug.Assert(FsClient != null);
            Debug.Assert(!string.IsNullOrWhiteSpace(FileName));

            FsClient.DeleteFile(FileName);

            Result rc = FsClient.CreateFile(FileName, data.Length);

            if (rc.IsFailure())
            {
                return(rc);
            }

            rc = FsClient.OpenFile(out FileHandle handle, FileName, OpenMode.Write);
            if (rc.IsFailure())
            {
                return(rc);
            }

            rc = FsClient.WriteFile(handle, 0, data, WriteOption.Flush);
            FsClient.CloseFile(handle);

            return(rc);
        }
Ejemplo n.º 2
0
        protected override Result DoWrite(long offset, ReadOnlySpan <byte> source)
        {
            lock (_locker)
            {
                if (source.Length == 0)
                {
                    return(Result.Success);
                }

                Result rc = UpdateSize();
                if (rc.IsFailure())
                {
                    return(rc);
                }

                if (!IsRangeValid(offset, source.Length, FileSize))
                {
                    return(ResultFs.OutOfRange.Log());
                }

                return(FsClient.WriteFile(Handle, offset, source));
            }
        }
Ejemplo n.º 3
0
        public Result Commit()
        {
            lock (Locker)
            {
                Result rc = Initialize();
                if (rc.IsFailure())
                {
                    return(rc);
                }

                rc = EnsureKvDatabaseLoaded(false);
                if (rc.IsFailure())
                {
                    return(rc);
                }

                var mount = new Mounter();

                try
                {
                    rc = mount.Initialize(FsClient, MountName, SpaceId, SaveDataId);
                    if (rc.IsFailure())
                    {
                        return(rc);
                    }

                    rc = KvDatabase.WriteDatabaseToFile();
                    if (rc.IsFailure())
                    {
                        return(rc);
                    }

                    string idFilePath = $"{MountName}:/{LastIdFileName}";

                    rc = FsClient.OpenFile(out FileHandle handle, idFilePath, OpenMode.Write);
                    if (rc.IsFailure())
                    {
                        return(rc);
                    }

                    bool fileAlreadyClosed = false;

                    try
                    {
                        ulong lastId = LastPublishedId;

                        rc = FsClient.WriteFile(handle, 0, SpanHelpers.AsByteSpan(ref lastId), WriteOption.None);
                        if (rc.IsFailure())
                        {
                            return(rc);
                        }

                        rc = FsClient.FlushFile(handle);
                        if (rc.IsFailure())
                        {
                            return(rc);
                        }

                        FsClient.CloseFile(handle);
                        fileAlreadyClosed = true;

                        return(FsClient.Commit(MountName));
                    }
                    finally
                    {
                        if (!fileAlreadyClosed)
                        {
                            FsClient.CloseFile(handle);
                        }
                    }
                }
                finally
                {
                    mount.Dispose();
                }
            }
        }