public override void MoveDirectory(string sourceFullPath, string destFullPath) { // Windows doesn't care if you try and copy a file via "MoveDirectory"... if (FileExists(sourceFullPath)) { // ... but it doesn't like the source to have a trailing slash ... // On Windows we end up with ERROR_INVALID_NAME, which is // "The filename, directory name, or volume label syntax is incorrect." // // This surfaces as a IOException, if we let it go beyond here it would // give DirectoryNotFound. if (PathHelpers.EndsInDirectorySeparator(sourceFullPath)) { throw new IOException(SR.Format(SR.IO_PathNotFound_Path, sourceFullPath)); } // ... but it doesn't care if the destination has a trailing separator. destFullPath = PathHelpers.TrimEndingDirectorySeparator(destFullPath); } if (Interop.Sys.Rename(sourceFullPath, destFullPath) < 0) { Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo(); switch (errorInfo.Error) { case Interop.Error.EACCES: // match Win32 exception throw new IOException(SR.Format(SR.UnauthorizedAccess_IODenied_Path, sourceFullPath), errorInfo.RawErrno); default: throw Interop.GetExceptionForIoErrno(errorInfo, sourceFullPath, isDirectory: true); } } }
public override bool FileExists(string fullPath) { Interop.ErrorInfo ignored; // Windows doesn't care about the trailing separator return(FileExists(PathHelpers.TrimEndingDirectorySeparator(fullPath), Interop.Sys.FileTypes.S_IFREG, out ignored)); }
private static String GetDirName(String fullPath) { Debug.Assert(fullPath != null); return(PathHelpers.IsRoot(fullPath) ? fullPath : Path.GetFileName(PathHelpers.TrimEndingDirectorySeparator(fullPath))); }
public override bool FileExists(string fullPath) { Interop.ErrorInfo ignored; // Input allows trailing separators in order to match Windows behavior // Unix does not accept trailing separators, so must be trimmed return(FileExists(PathHelpers.TrimEndingDirectorySeparator(fullPath), Interop.Sys.FileTypes.S_IFREG, out ignored)); }
private void Init(string originalPath, string fullPath = null, string fileName = null, bool isNormalized = false) { // Want to throw the original argument name OriginalPath = originalPath ?? throw new ArgumentNullException("path"); fullPath = fullPath ?? originalPath; fullPath = isNormalized ? fullPath : Path.GetFullPath(fullPath); _name = fileName ?? (PathHelpers.IsRoot(fullPath) ? fullPath : Path.GetFileName(PathHelpers.TrimEndingDirectorySeparator(fullPath))); FullPath = fullPath; }
private void Init(string originalPath, string fullPath = null, string fileName = null, bool isNormalized = false) { // Want to throw the original argument name OriginalPath = originalPath ?? throw new ArgumentNullException("path"); fullPath = fullPath ?? originalPath; Debug.Assert(!isNormalized || !PathInternal.IsPartiallyQualified(fullPath), $"'{fullPath}' should be fully qualified if normalized"); fullPath = isNormalized ? fullPath : Path.GetFullPath(fullPath); _name = fileName ?? (PathHelpers.IsRoot(fullPath) ? fullPath : Path.GetFileName(PathHelpers.TrimEndingDirectorySeparator(fullPath))); FullPath = fullPath; }
public override void DeleteFile(string fullPath) { if (Interop.Sys.Unlink(fullPath) < 0) { Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo(); switch (errorInfo.Error) { case Interop.Error.ENOENT: // ENOENT means it already doesn't exist; nop return; case Interop.Error.EROFS: // EROFS means the file system is read-only // Need to manually check file existence // github.com/dotnet/corefx/issues/21273 Interop.ErrorInfo fileExistsError; // Input allows trailing separators in order to match Windows behavior // Unix does not accept trailing separators, so must be trimmed if (!FileExists(PathHelpers.TrimEndingDirectorySeparator(fullPath), Interop.Sys.FileTypes.S_IFREG, out fileExistsError) && fileExistsError.Error == Interop.Error.ENOENT) { return; } goto default; case Interop.Error.EISDIR: errorInfo = Interop.Error.EACCES.Info(); goto default; default: throw Interop.GetExceptionForIoErrno(errorInfo, fullPath); } } }
private void EnsureStatInitialized() { if (_fileinfoInitialized == -1) { Refresh(); } if (_fileinfoInitialized != 0) { int errno = _fileinfoInitialized; _fileinfoInitialized = -1; var errorInfo = new Interop.ErrorInfo(errno); // Windows distinguishes between whether the directory or the file isn't found, // and throws a different exception in these cases. We attempt to approximate that // here; there is a race condition here, where something could change between // when the error occurs and our checks, but it's the best we can do, and the // worst case in such a race condition (which could occur if the file system is // being manipulated concurrently with these checks) is that we throw a // FileNotFoundException instead of DirectoryNotFoundexception. // directoryError is true only if a FileNotExists error was provided and the parent // directory of the file represented by _fullPath is nonexistent bool directoryError = (errorInfo.Error == Interop.Error.ENOENT && !Directory.Exists(Path.GetDirectoryName(PathHelpers.TrimEndingDirectorySeparator(_fullPath)))); // The destFile's path is invalid throw Interop.GetExceptionForIoErrno(errorInfo, _fullPath, directoryError); } }