Ejemplo n.º 1
0
    protected async Task InternalInitAsync(CancellationToken cancel = default)
    {
        cancel.ThrowIfCancellationRequested();

        try
        {
            bool newFille = false;

            LargeFileSystem.ParsedPath?lastFileParsed = InitialRelatedFiles.OrderBy(x => x.FileNumber).LastOrDefault();

            if (lastFileParsed == null)
            {
                // New file
                CurrentFileSize = 0;
                newFille        = true;

                if (FileParams.Mode == FileMode.Open || FileParams.Mode == FileMode.Truncate)
                {
                    throw new IOException($"The file '{FileParams.Path}' not found.");
                }
            }
            else
            {
                // File exists
                if (FileParams.Mode == FileMode.CreateNew)
                {
                    throw new IOException($"The file '{FileParams.Path}' already exists.");
                }

                checked
                {
                    long sizeOfLastFile = (await UnderlayFileSystem.GetFileMetadataAsync(lastFileParsed.PhysicalFilePath, FileMetadataGetFlags.NoAlternateStream | FileMetadataGetFlags.NoSecurity, cancel)).Size;
                    sizeOfLastFile  = Math.Min(sizeOfLastFile, LargeFileSystem.Params.MaxSinglePhysicalFileSize);
                    CurrentFileSize = lastFileParsed.FileNumber * LargeFileSystem.Params.MaxSinglePhysicalFileSize + sizeOfLastFile;
                }
            }

            if (FileParams.Mode == FileMode.Create || FileParams.Mode == FileMode.CreateNew || FileParams.Mode == FileMode.Truncate)
            {
                if (lastFileParsed != null)
                {
                    // Delete the files first
                    await LargeFileSystem.DeleteFileAsync(FileParams.Path, FileFlags.ForceClearReadOnlyOrHiddenBitsOnNeed, cancel);

                    lastFileParsed  = null;
                    CurrentFileSize = 0;

                    newFille = true;
                }
            }

            long currentPosition = 0;
            if (FileParams.Mode == FileMode.Append)
            {
                currentPosition = this.CurrentFileSize;
            }

            InitAndCheckFileSizeAndPosition(currentPosition, await GetFileSizeImplAsync(cancel), cancel);

            if (newFille)
            {
                await using (var handle = await GetUnderlayRandomAccessHandle(0, cancel))
                {
                }
            }
        }
        catch
        {
            throw;
        }
    }