Beispiel #1
0
        /// <summary>
        /// Creates an empty file at a given path.
        /// </summary>
        public static void CreateEmptyFile(this IAbsFileSystem fileSystem, AbsolutePath path)
        {
            Contract.RequiresNotNull(path);
            Contract.RequiresNotNull(path.Parent);

            fileSystem.CreateDirectory(path.Parent);
            using (fileSystem.TryOpen(path, FileAccess.Write, FileMode.Create, FileShare.None, FileOptions.None, bufferSize: 1))
            {
            }
        }
Beispiel #2
0
        /// <summary>
        /// Tries to read the content from a file <paramref name="absolutePath"/>.
        /// </summary>
        /// <returns>Returns <code>null</code> if file does not exist, or content of the file otherwise.</returns>
        /// <exception cref="Exception">Throws if the IO operation fails.</exception>
        public static async Task <string?> TryReadFileAsync(this IAbsFileSystem fileSystem, AbsolutePath absolutePath, FileShare fileShare = FileShare.ReadWrite)
        {
            using Stream? readLockFile = fileSystem.TryOpen(absolutePath, FileAccess.Read, FileMode.Open, fileShare);
            if (readLockFile != null)
            {
                using var reader = new StreamReader(readLockFile);
                return(await reader.ReadToEndAsync().ConfigureAwait(false));
            }

            return(null);
        }
            static async Task openAndClose(IAbsFileSystem fileSystem, AbsolutePath path)
            {
                await Task.Yield();

                try
                {
                    // This operation may fail with UnauthorizedAccessException because
                    // the test tries to delete the file in the process.
                    using var f = fileSystem.TryOpen(path, FileAccess.Read, FileMode.Open, FileShare.Read | FileShare.Delete);
                }
                catch (UnauthorizedAccessException)
                { }
            }
Beispiel #4
0
        /// <summary>
        /// Open the named file asynchronously for reading.
        /// </summary>
        /// <exception cref="FileNotFoundException">Throws if the file is not found.</exception>
        /// <exception cref="DirectoryNotFoundException">Throws if the directory is not found.</exception>
        /// <remarks>
        /// Unlike <see cref="IAbsFileSystem.OpenAsync(AbsolutePath, FileAccess, FileMode, FileShare, FileOptions, int)"/> this function throws an exception if the file is missing.
        /// </remarks>
        public static StreamWithLength Open(this IAbsFileSystem fileSystem, AbsolutePath path, FileAccess fileAccess, FileMode fileMode, FileShare share, FileOptions options = FileOptions.None, int bufferSize = FileSystemDefaults.DefaultFileStreamBufferSize)
        {
            var stream = fileSystem.TryOpen(path, fileAccess, fileMode, share, options, bufferSize);

            if (stream == null)
            {
                // Stream is null when the file or a directory is not found.
                if (fileSystem.DirectoryExists(path.Parent !))
                {
                    throw new FileNotFoundException($"The file '{path}' does not exist.");
                }

                throw new DirectoryNotFoundException($"The directory '{path.Parent}' does not exist.");
            }

            return(stream.Value);
        }
Beispiel #5
0
        /// <summary>
        /// Open the named file asynchronously for reading and sets the expected file length for performance reasons if available.
        /// </summary>
        /// <remarks>
        /// Setting the file length up-front leads to 20-30% performance improvements.
        /// </remarks>
        public static StreamWithLength?TryOpenForWrite(
            this IAbsFileSystem fileSystem,
            AbsolutePath path,
            long?expectingLength,
            FileMode fileMode,
            FileShare share,
            FileOptions options = FileOptions.None,
            int bufferSize      = FileSystemDefaults.DefaultFileStreamBufferSize)
        {
            var stream = fileSystem.TryOpen(path, FileAccess.Write, fileMode, share, options, bufferSize);

            if (stream == null)
            {
                return(null);
            }

            if (expectingLength != null)
            {
                stream.Value.Stream.SetLength(expectingLength.Value);
            }

            return(stream.Value);
        }
Beispiel #6
0
 /// <summary>
 /// Creates a stream to an existing file.
 /// Note that the permissions are such that it is readable, but not
 /// writable by the caller.
 /// </summary>
 public static StreamWithLength?TryOpenReadOnly(this IAbsFileSystem fileSystem, AbsolutePath path, FileShare share)
 {
     return(fileSystem.TryOpen(path, FileAccess.Read, FileMode.Open, share));
 }
Beispiel #7
0
 /// <summary>
 /// Calls <see cref="IAbsFileSystem.OpenAsync(AbsolutePath, FileAccess, FileMode, FileShare, FileOptions, int)" /> with the default buffer stream size.
 /// </summary>
 public static StreamWithLength?TryOpen(this IAbsFileSystem fileSystem, AbsolutePath path, FileAccess fileAccess, FileMode fileMode, FileShare share)
 {
     return(fileSystem.TryOpen(path, fileAccess, fileMode, share, FileOptions.None, DefaultFileStreamBufferSize));
 }