예제 #1
0
 /// <summary>
 /// Checks whether the file is accessible for this type of FileAccess.
 /// MockfileData can be configured to have FileShare.None, which indicates it is locked by a 'different process'.
 ///
 /// If the file is 'locked by a different process', an IOException will be thrown.
 /// </summary>
 /// <param name="path">The path is used in the IOException message to match the message in real life situations</param>
 /// <param name="access">The access type to check</param>
 internal void CheckFileAccess(string path, FileAccess access)
 {
     if (!AllowedFileShare.HasFlag((FileShare)access))
     {
         throw CommonExceptions.ProcessCannotAccessFileInUse(path);
     }
 }
        public override void Delete(string path)
        {
            mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");

            // We mimic exact behavior of the standard File.Delete() method
            // which throws exception only if the folder does not exist,
            // but silently returns if deleting a non-existing file in an existing folder.
            VerifyDirectoryExists(path);

            var file = mockFileDataAccessor.GetFile(path);

            if (file != null && !file.AllowedFileShare.HasFlag(FileShare.Delete))
            {
                throw CommonExceptions.ProcessCannotAccessFileInUse(path);
            }

            mockFileDataAccessor.RemoveFile(path);
        }
        public override void Move(string sourceFileName, string destFileName)
        {
            if (sourceFileName == null)
            {
                throw CommonExceptions.FilenameCannotBeNull(nameof(sourceFileName));
            }

            if (destFileName == null)
            {
                throw CommonExceptions.FilenameCannotBeNull(nameof(destFileName));
            }

            mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(sourceFileName, nameof(sourceFileName));
            mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(destFileName, nameof(destFileName));

            if (mockFileDataAccessor.GetFile(destFileName) != null)
            {
                if (destFileName.Equals(sourceFileName))
                {
                    return;
                }
                else
                {
                    throw new IOException("A file can not be created if it already exists.");
                }
            }


            var sourceFile = mockFileDataAccessor.GetFile(sourceFileName);

            if (sourceFile == null)
            {
                throw CommonExceptions.FileNotFound(sourceFileName);
            }
            if (!sourceFile.AllowedFileShare.HasFlag(FileShare.Delete))
            {
                throw CommonExceptions.ProcessCannotAccessFileInUse();
            }
            VerifyDirectoryExists(destFileName);

            mockFileDataAccessor.AddFile(destFileName, new MockFileData(sourceFile));
            mockFileDataAccessor.RemoveFile(sourceFileName);
        }