protected override void Dispose(bool disposing) { Exception exc = null; if (handle != MonoIO.InvalidHandle) { try { FlushBuffer(); } catch (Exception e) { exc = e; } if (owner) { MonoIOError error; MonoIO.Close(handle, out error); if (error != MonoIOError.ERROR_SUCCESS) { // don't leak the path information for isolated storage throw MonoIO.GetException(GetSecureFileName(name), error); } handle = MonoIO.InvalidHandle; } } canseek = false; access = 0; if (disposing && buf != null) { if (buf.Length == DefaultBufferSize && buf_recycle == null) { lock (buf_recycle_lock) { if (buf_recycle == null) { buf_recycle = buf; } } } buf = null; GC.SuppressFinalize(this); } if (exc != null) { throw exc; } }
public static void Delete(string path) { Path.Validate(path); if (Environment.IsRunningOnWindows && path == ":") { throw new NotSupportedException("Only ':' In path"); } SecurityManager.EnsureElevatedPermissions(); // this is a no-op outside moonlight MonoIOError error; bool success; if (MonoIO.ExistsSymlink(path, out error)) { /* RemoveDirectory maps to rmdir() * which fails on symlinks (ENOTDIR) */ success = MonoIO.DeleteFile(path, out error); } else { success = MonoIO.RemoveDirectory(path, out error); } if (!success) { /* * FIXME: * In io-layer/io.c rmdir returns error_file_not_found if directory does not exists. * So maybe this could be handled somewhere else? */ if (error == MonoIOError.ERROR_FILE_NOT_FOUND) { if (File.Exists(path)) { throw new IOException("Directory does not exist, but a file of the same name exists."); } else { throw new DirectoryNotFoundException("Directory does not exist."); } } else { throw MonoIO.GetException(path, error); } } }
public static Exception GetException(MonoIOError error) { if (error == MonoIOError.ERROR_ACCESS_DENIED) { return(new UnauthorizedAccessException("Access to the path is denied.")); } if (error != MonoIOError.ERROR_FILE_EXISTS) { return(MonoIO.GetException(string.Empty, error)); } string message = "Cannot create a file that already exist."; return(new IOException(message, (int)((MonoIOError)(-2147024896) | error))); }
internal static String InternalCopy(String sourceFileName, String destFileName, bool overwrite, bool checkHost) { String fullSourceFileName = Path.GetFullPathInternal(sourceFileName); String fullDestFileName = Path.GetFullPathInternal(destFileName); MonoIOError error; if (!MonoIO.CopyFile(fullSourceFileName, fullDestFileName, overwrite, out error)) { string p = Locale.GetText("{0}\" or \"{1}", sourceFileName, destFileName); throw MonoIO.GetException(p, error); } return(fullDestFileName); }
public static void SetLastWriteTime(string path, DateTime lastWriteTime) { MonoIOError error; Path.Validate(path); if (!MonoIO.Exists(path, out error)) { throw MonoIO.GetException(path, error); } if (!MonoIO.SetLastWriteTime(path, lastWriteTime, out error)) { throw MonoIO.GetException(path, error); } }
public static FileAttributes GetAttributes(string path) { Path.Validate(path); SecurityManager.EnsureElevatedPermissions(); // this is a no-op outside moonlight MonoIOError error; FileAttributes attrs; attrs = MonoIO.GetFileAttributes(path, out error); if (error != MonoIOError.ERROR_SUCCESS) { throw MonoIO.GetException(path, error); } return(attrs); }
static internal IEnumerable <FileSystemInfo> EnumerateFileSystemInfos(string full, string searchPattern, SearchOption searchOption) { string path_with_pattern = Path.Combine(full, searchPattern); IntPtr handle; MonoIOError error; FileAttributes rattr; bool subdirs = searchOption == SearchOption.AllDirectories; Path.Validate(full); string s = MonoIO.FindFirst(full, path_with_pattern, out rattr, out error, out handle); if (s == null) { yield break; } if (error != 0) { throw MonoIO.GetException(Path.GetDirectoryName(path_with_pattern), (MonoIOError)error); } try { do { if (((rattr & FileAttributes.ReparsePoint) == 0)) { if ((rattr & FileAttributes.Directory) != 0) { yield return(new DirectoryInfo(s)); } else { yield return(new FileInfo(s)); } } if (((rattr & FileAttributes.Directory) != 0) && subdirs) { foreach (FileSystemInfo child in EnumerateFileSystemInfos(s, searchPattern, searchOption)) { yield return(child); } } } while ((s = MonoIO.FindNext(handle, out rattr, out error)) != null); } finally { MonoIO.FindClose(handle); } }
public virtual void Flush(bool flushToDisk) { if (safeHandle.IsClosed) { throw new ObjectDisposedException("Stream has been closed"); } FlushBuffer(); // This does the fsync if (flushToDisk) { MonoIOError error; MonoIO.Flush(safeHandle, out error); } }
private void Init(SafeFileHandle safeHandle, FileAccess access, bool ownsHandle, int bufferSize, bool isAsync, bool isZeroSize) { if (access < FileAccess.Read || access > FileAccess.ReadWrite) { throw new ArgumentOutOfRangeException("access"); } MonoIOError error; MonoFileType ftype = MonoIO.GetFileType(safeHandle, out error); if (error != MonoIOError.ERROR_SUCCESS) { throw MonoIO.GetException(name, error); } if (ftype == MonoFileType.Unknown) { throw new IOException("Invalid handle."); } else if (ftype == MonoFileType.Disk) { this.canseek = true; } else { this.canseek = false; } this.safeHandle = safeHandle; ExposeHandle(); this.access = access; this.owner = ownsHandle; this.async = isAsync; this.anonymous = false; if (canseek) { buf_start = MonoIO.Seek(safeHandle, 0, SeekOrigin.Current, out error); if (error != MonoIOError.ERROR_SUCCESS) { throw MonoIO.GetException(name, error); } } /* Can't set append mode */ this.append_startpos = 0; }
// private // Does the common validation, searchPattern has already been checked for not-null static string ValidateDirectoryListing(string path, string searchPattern, out bool stop) { Path.Validate(path); string wild = Path.Combine(path, searchPattern); string wildpath = Path.GetDirectoryName(wild); if (wildpath.IndexOfAny(Path.InvalidPathChars) != -1) { throw new ArgumentException("Pattern contains invalid characters", "pattern"); } MonoIOError error; if (!MonoIO.ExistsDirectory(wildpath, out error)) { if (error == MonoIOError.ERROR_SUCCESS) { MonoIOError file_error; if (MonoIO.ExistsFile(wildpath, out file_error)) { stop = true; return(wildpath); } } if (error != MonoIOError.ERROR_PATH_NOT_FOUND) { throw MonoIO.GetException(wildpath, error); } if (wildpath.IndexOfAny(SearchPattern.WildcardChars) == -1) { throw new DirectoryNotFoundException("Directory '" + wildpath + "' not found."); } if (path.IndexOfAny(SearchPattern.WildcardChars) == -1) { throw new ArgumentException("Pattern is invalid", "searchPattern"); } throw new ArgumentException("Path is invalid", "path"); } stop = false; return(wild); }
/// <summary>Permanently deletes a file.</summary> /// <exception cref="T:System.IO.IOException">The target file is open or memory-mapped on a computer running Microsoft Windows NT. </exception> /// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission. </exception> /// <exception cref="T:System.UnauthorizedAccessException">The path is a directory. </exception> /// <filterpriority>1</filterpriority> /// <PermissionSet> /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" /> /// </PermissionSet> public override void Delete() { MonoIOError error; if (!MonoIO.Exists(this.FullPath, out error)) { return; } if (MonoIO.ExistsDirectory(this.FullPath, out error)) { throw new UnauthorizedAccessException("Access to the path \"" + this.FullPath + "\" is denied."); } if (!MonoIO.DeleteFile(this.FullPath, out error)) { throw MonoIO.GetException(this.OriginalPath, error); } }
public static bool Exists(string path) { // For security reasons no exceptions are // thrown, only false is returned if there is // any problem with the path or permissions. // Minimizes what information can be // discovered by using this method. if (path == null || path.Trim().Length == 0 || path.IndexOfAny(Path.InvalidPathChars) >= 0) { return(false); } MonoIOError error; return(MonoIO.ExistsFile(path, out error)); }
public static void Move(string sourceDirName, string destDirName) { if (sourceDirName == null) { throw new ArgumentNullException("sourceDirName"); } if (destDirName == null) { throw new ArgumentNullException("destDirName"); } if (sourceDirName.Trim().Length == 0 || sourceDirName.IndexOfAny(Path.InvalidPathChars) != -1) { throw new ArgumentException("Invalid source directory name: " + sourceDirName, "sourceDirName"); } if (destDirName.Trim().Length == 0 || destDirName.IndexOfAny(Path.InvalidPathChars) != -1) { throw new ArgumentException("Invalid target directory name: " + destDirName, "destDirName"); } if (sourceDirName == destDirName) { throw new IOException("Source and destination path must be different."); } SecurityManager.EnsureElevatedPermissions(); // this is a no-op outside moonlight if (Exists(destDirName)) { throw new IOException(destDirName + " already exists."); } if (!Exists(sourceDirName) && !File.Exists(sourceDirName)) { throw new DirectoryNotFoundException(sourceDirName + " does not exist"); } MonoIOError error; if (!MonoIO.MoveFile(sourceDirName, destDirName, out error)) { throw MonoIO.GetException(error); } }
public void MoveTo(String destFileName) { if (destFileName == null) { throw new ArgumentNullException("destFileName"); } if (destFileName.Length == 0) { throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "destFileName"); } Contract.EndContractBlock(); String fullDestFileName = Path.GetFullPathInternal(destFileName); #if !MONO #if FEATURE_CORECLR FileSecurityState sourceState = new FileSecurityState(FileSecurityStateAccess.Write | FileSecurityStateAccess.Read, DisplayPath, FullPath); FileSecurityState destState = new FileSecurityState(FileSecurityStateAccess.Write, destFileName, fullDestFileName); sourceState.EnsureState(); destState.EnsureState(); #else FileIOPermission.QuickDemand(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, FullPath, false, false); FileIOPermission.QuickDemand(FileIOPermissionAccess.Write, fullDestFileName, false, false); #endif #endif #if MONO MonoIOError error; if (!MonoIO.MoveFile(FullPath, fullDestFileName, out error)) { __Error.WinIOError((int)error, String.Empty); } #else if (!Win32Native.MoveFile(FullPath, fullDestFileName)) { __Error.WinIOError(); } #endif FullPath = fullDestFileName; OriginalPath = destFileName; _name = Path.GetFileName(fullDestFileName); DisplayPath = GetDisplayPath(destFileName); // Flush any cached information about the file. _dataInitialised = -1; }
public static void SetAttributes(string path, FileAttributes fileAttributes) { #if MONO if (((uint)fileAttributes & 0x80000000) != 0) { MonoIOError error; Path.Validate(path); if (!MonoIO.SetFileAttributes(path, fileAttributes, out error)) { throw MonoIO.GetException(path, error); } return; } #endif string fullPath = Path.GetFullPath(path); FileSystem.SetAttributes(fullPath, fileAttributes); }
public static string GetCurrentDirectory() { MonoIOError error; string result = MonoIO.GetCurrentDirectory(out error); if (error != MonoIOError.ERROR_SUCCESS) { throw MonoIO.GetException(error); } #if !NET_2_1 && !DISABLE_SECURITY if ((result != null) && (result.Length > 0) && SecurityManager.SecurityEnabled) { new FileIOPermission(FileIOPermissionAccess.PathDiscovery, result).Demand(); } #endif return(result); }
protected virtual void Dispose(bool disposing) #endif { Exception exc = null; if (handle != MonoIO.InvalidHandle) { try { FlushBuffer(); } catch (Exception e) { exc = e; } if (owner) { MonoIOError error; MonoIO.Close(handle, out error); if (error != MonoIOError.ERROR_SUCCESS) { // don't leak the path information for isolated storage throw MonoIO.GetException(GetSecureFileName(name), error); } handle = MonoIO.InvalidHandle; } } canseek = false; access = 0; if (disposing) { buf = null; } if (disposing) { GC.SuppressFinalize(this); } if (exc != null) { throw exc; } }
// file methods public override void Delete() { MonoIOError error; if (!MonoIO.Exists(FullPath, out error)) { // a weird MS.NET behaviour return; } if (MonoIO.ExistsDirectory(FullPath, out error)) { throw new UnauthorizedAccessException("Access to the path \"" + FullPath + "\" is denied."); } if (!MonoIO.DeleteFile(FullPath, out error)) { throw MonoIO.GetException(OriginalPath, error); } }
// internal internal void Refresh(bool force) { if (valid && !force) { return; } MonoIOError error; MonoIO.GetFileStat(FullName, out stat, out error); /* Don't throw on error here, too much other * stuff relies on it not doing so... */ valid = true; InternalRefresh(); }
void FlushBuffer(Stream st) { if (buf_dirty) { MonoIOError error; if (CanSeek == true && safeHandle == null) { MonoIO.Seek(handle, buf_start, SeekOrigin.Begin, out error); if (error != MonoIOError.ERROR_SUCCESS) { // don't leak the path information for isolated storage throw MonoIO.GetException(GetSecureFileName(name), error); } } if (st == null) { int wcount = buf_length; int offset = 0; while (wcount > 0) { int n = MonoIO.Write(handle, buf, 0, buf_length, out error); if (error != MonoIOError.ERROR_SUCCESS) { // don't leak the path information for isolated storage throw MonoIO.GetException(GetSecureFileName(name), error); } wcount -= n; offset += n; } } else { st.Write(buf, 0, buf_length); } } buf_start += buf_offset; buf_offset = buf_length = 0; buf_dirty = false; }
public static string GetCurrentDirectory() { MonoIOError error; SecurityManager.EnsureElevatedPermissions(); // this is a no-op outside moonlight string result = MonoIO.GetCurrentDirectory(out error); if (error != MonoIOError.ERROR_SUCCESS) { throw MonoIO.GetException(error); } #if !MOONLIGHT if ((result != null) && (result.Length > 0) && SecurityManager.SecurityEnabled) { new FileIOPermission(FileIOPermissionAccess.PathDiscovery, result).Demand(); } #endif return(result); }
public static bool Exists(string path) { if (path == null) { return(false); } // on Moonlight this does not throw but returns false if (!SecurityManager.CheckElevatedPermissions()) { return(false); } MonoIOError error; bool exists; exists = MonoIO.ExistsDirectory(path, out error); /* This should not throw exceptions */ return(exists); }
internal static int FillAttributeInfo(String path, ref MonoIOStat data, bool tryagain, bool returnErrorOnNotFound) { if (tryagain) { throw new NotImplementedException(); } MonoIOError error; MonoIO.GetFileStat(path, out data, out error); if (!returnErrorOnNotFound && (error == MonoIOError.ERROR_FILE_NOT_FOUND || error == MonoIOError.ERROR_PATH_NOT_FOUND || error == MonoIOError.ERROR_NOT_READY)) { data = default(MonoIOStat); data.fileAttributes = (FileAttributes)(-1); return(0); } return((int)error); }
void WriteInternal(byte [] src, int offset, int count) { if (count > buf_size) { // shortcut for long writes MonoIOError error; FlushBuffer(); int wcount = count; while (wcount > 0) { int n = MonoIO.Write(safeHandle, src, offset, wcount, out error); if (error != MonoIOError.ERROR_SUCCESS) { throw MonoIO.GetException(GetSecureFileName(name), error); } wcount -= n; offset += n; } buf_start += count; } else { int copied = 0; while (count > 0) { int n = WriteSegment(src, offset + copied, count); copied += n; count -= n; if (count == 0) { break; } FlushBuffer(); } } }
// file methods public override void Delete() { MonoIOError error; SecurityManager.EnsureElevatedPermissions(); // this is a no-op outside moonlight if (!MonoIO.Exists(FullPath, out error)) { // a weird MS.NET behaviour return; } if (MonoIO.ExistsDirectory(FullPath, out error)) { throw new UnauthorizedAccessException("Access to the path \"" + FullPath + "\" is denied."); } if (!MonoIO.DeleteFile(FullPath, out error)) { throw MonoIO.GetException(OriginalPath, error); } }
public static DateTime GetLastWriteTime(string path) { MonoIOStat stat; MonoIOError error; Path.Validate(path); SecurityManager.EnsureElevatedPermissions(); // this is a no-op outside moonlight if (!MonoIO.GetFileStat(path, out stat, out error)) { if (error == MonoIOError.ERROR_PATH_NOT_FOUND || error == MonoIOError.ERROR_FILE_NOT_FOUND) { return(DefaultLocalFileTime); } else { throw new IOException(path); } } return(DateTime.FromFileTime(stat.LastWriteTime)); }
public override void Delete() { #if FEATURE_MONO_CAS #if FEATURE_CORECLR FileSecurityState state = new FileSecurityState(FileSecurityStateAccess.Write, DisplayPath, FullPath); state.EnsureState(); #else // For security check, path should be resolved to an absolute path. FileIOPermission.QuickDemand(FileIOPermissionAccess.Write, FullPath, false, false); #endif #endif #if MONO MonoIOError error; if (MonoIO.ExistsDirectory(FullPath, out error)) { __Error.WinIOError(Win32Native.ERROR_ACCESS_DENIED, DisplayPath); } if (!MonoIO.DeleteFile(FullPath, out error)) { int hr = (int)error; #else bool r = Win32Native.DeleteFile(FullPath); if (!r) { int hr = Marshal.GetLastWin32Error(); #endif if (hr == Win32Native.ERROR_FILE_NOT_FOUND) { return; } else { __Error.WinIOError(hr, DisplayPath); } } }
public static bool Exists(string path) { // For security reasons no exceptions are // thrown, only false is returned if there is // any problem with the path or permissions. // Minimizes what information can be // discovered by using this method. if (String.IsNullOrWhiteSpace(path) || path.IndexOfAny(Path.InvalidPathChars) >= 0) { return(false); } // on Moonlight this does not throw but returns false if (!SecurityManager.CheckElevatedPermissions()) { return(false); } MonoIOError error; return(MonoIO.ExistsFile(path, out error)); }
public override void SetLength(long value) { if (safeHandle.IsClosed) { throw new ObjectDisposedException("Stream has been closed"); } if (CanSeek == false) { throw new NotSupportedException("The stream does not support seeking"); } if (CanWrite == false) { throw new NotSupportedException("The stream does not support writing"); } if (value < 0) { throw new ArgumentOutOfRangeException("value is less than 0"); } FlushBuffer(); MonoIOError error; MonoIO.SetLength(safeHandle, value, out error); if (error != MonoIOError.ERROR_SUCCESS) { // don't leak the path information for isolated storage throw MonoIO.GetException(GetSecureFileName(name), error); } if (Position > value) { Position = value; } }
static void RecursiveDelete(string path) { MonoIOError error; foreach (string dir in GetDirectories(path)) { if (MonoIO.ExistsSymlink(dir, out error)) { MonoIO.DeleteFile(dir, out error); } else { RecursiveDelete(dir); } } foreach (string file in GetFiles(path)) { File.Delete(file); } Directory.Delete(path); }