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); }
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 bool DuplicateHandle(HandleRef hSourceProcessHandle, HandleRef hSourceHandle, HandleRef hTargetProcess, out SafeProcessHandle targetHandle, int dwDesiredAccess, bool bInheritHandle, int dwOptions) { MonoIOError error; IntPtr nakedTargetHandle; bool ret = MonoIO.DuplicateHandle(hSourceProcessHandle.Handle, hSourceHandle.Handle, hTargetProcess.Handle, out nakedTargetHandle, dwDesiredAccess, bInheritHandle ? 1 : 0, dwOptions, out error); if (error != MonoIOError.ERROR_SUCCESS) { throw MonoIO.GetException(error); } targetHandle = new SafeProcessHandle(nakedTargetHandle, true); return(ret); }
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); }
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 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)); }
bool StartWithCreateProcess(ProcessStartInfo startInfo) { if (startInfo.StandardOutputEncoding != null && !startInfo.RedirectStandardOutput) { throw new InvalidOperationException(SR.GetString(SR.StandardOutputEncodingNotAllowed)); } if (startInfo.StandardErrorEncoding != null && !startInfo.RedirectStandardError) { throw new InvalidOperationException(SR.GetString(SR.StandardErrorEncodingNotAllowed)); } if (this.disposed) { throw new ObjectDisposedException(GetType().Name); } var procInfo = new ProcInfo(); if (startInfo.HaveEnvVars) { List <string> envVariables = null; StringBuilder sb = null; foreach (DictionaryEntry de in startInfo.EnvironmentVariables) { if (de.Value == null) { continue; } if (envVariables == null) { envVariables = new List <string> (); } if (sb == null) { sb = new StringBuilder(); } else { sb.Clear(); } sb.Append((string)de.Key); sb.Append('='); sb.Append((string)de.Value); envVariables.Add(sb.ToString()); } procInfo.envVariables = envVariables?.ToArray(); } MonoIOError error; IntPtr stdin_read = IntPtr.Zero, stdin_write = IntPtr.Zero; IntPtr stdout_read = IntPtr.Zero, stdout_write = IntPtr.Zero; IntPtr stderr_read = IntPtr.Zero, stderr_write = IntPtr.Zero; try { if (startInfo.RedirectStandardInput) { CreatePipe(out stdin_read, out stdin_write, true); } else { stdin_read = MonoIO.ConsoleInput; stdin_write = IntPtr.Zero; } if (startInfo.RedirectStandardOutput) { CreatePipe(out stdout_read, out stdout_write, false); } else { stdout_read = IntPtr.Zero; stdout_write = MonoIO.ConsoleOutput; } if (startInfo.RedirectStandardError) { CreatePipe(out stderr_read, out stderr_write, false); } else { stderr_read = IntPtr.Zero; stderr_write = MonoIO.ConsoleError; } FillUserInfo(startInfo, ref procInfo); // // FIXME: For redirected pipes we need to send descriptors of // stdin_write, stdout_read, stderr_read to child process and // close them there (fork makes exact copy of parent's descriptors) // if (!CreateProcess_internal(startInfo, stdin_read, stdout_write, stderr_write, ref procInfo)) { throw new Win32Exception(-procInfo.pid, "ApplicationName='" + startInfo.FileName + "', CommandLine='" + startInfo.Arguments + "', CurrentDirectory='" + startInfo.WorkingDirectory + "', Native error= " + Win32Exception.GetErrorMessage(-procInfo.pid)); } } catch { if (startInfo.RedirectStandardInput) { if (stdin_read != IntPtr.Zero) { MonoIO.Close(stdin_read, out error); } if (stdin_write != IntPtr.Zero) { MonoIO.Close(stdin_write, out error); } } if (startInfo.RedirectStandardOutput) { if (stdout_read != IntPtr.Zero) { MonoIO.Close(stdout_read, out error); } if (stdout_write != IntPtr.Zero) { MonoIO.Close(stdout_write, out error); } } if (startInfo.RedirectStandardError) { if (stderr_read != IntPtr.Zero) { MonoIO.Close(stderr_read, out error); } if (stderr_write != IntPtr.Zero) { MonoIO.Close(stderr_write, out error); } } throw; } finally { if (procInfo.Password != IntPtr.Zero) { Marshal.ZeroFreeBSTR(procInfo.Password); procInfo.Password = IntPtr.Zero; } } SetProcessHandle(new SafeProcessHandle(procInfo.process_handle, true)); SetProcessId(procInfo.pid); #pragma warning disable 618 if (startInfo.RedirectStandardInput) { MonoIO.Close(stdin_read, out error); #if MOBILE var stdinEncoding = Encoding.Default; #else var stdinEncoding = Console.InputEncoding; #endif standardInput = new StreamWriter(new FileStream(stdin_write, FileAccess.Write, true, 8192), stdinEncoding) { AutoFlush = true }; } if (startInfo.RedirectStandardOutput) { MonoIO.Close(stdout_write, out error); Encoding stdoutEncoding = startInfo.StandardOutputEncoding ?? Console.OutputEncoding; standardOutput = new StreamReader(new FileStream(stdout_read, FileAccess.Read, true, 8192), stdoutEncoding, true); } if (startInfo.RedirectStandardError) { MonoIO.Close(stderr_write, out error); Encoding stderrEncoding = startInfo.StandardErrorEncoding ?? Console.OutputEncoding; standardError = new StreamReader(new FileStream(stderr_read, FileAccess.Read, true, 8192), stderrEncoding, true); } #pragma warning restore return(true); }
protected override bool ReleaseHandle() { MonoIOError error; return(MonoIO.Close(handle, out error)); }
bool StartWithCreateProcess(ProcessStartInfo startInfo) { if (startInfo.StandardOutputEncoding != null && !startInfo.RedirectStandardOutput) { throw new InvalidOperationException(SR.GetString(SR.StandardOutputEncodingNotAllowed)); } if (startInfo.StandardErrorEncoding != null && !startInfo.RedirectStandardError) { throw new InvalidOperationException(SR.GetString(SR.StandardErrorEncodingNotAllowed)); } if (this.disposed) { throw new ObjectDisposedException(GetType().Name); } var proc_info = new ProcInfo(); if (startInfo.HaveEnvVars) { string [] strs = new string [startInfo.EnvironmentVariables.Count]; startInfo.EnvironmentVariables.Keys.CopyTo(strs, 0); proc_info.envKeys = strs; strs = new string [startInfo.EnvironmentVariables.Count]; startInfo.EnvironmentVariables.Values.CopyTo(strs, 0); proc_info.envValues = strs; } MonoIOError error; IntPtr stdin_read = IntPtr.Zero, stdin_write = IntPtr.Zero; IntPtr stdout_read = IntPtr.Zero, stdout_write = IntPtr.Zero; IntPtr stderr_read = IntPtr.Zero, stderr_write = IntPtr.Zero; try { if (startInfo.RedirectStandardInput) { CreatePipe(out stdin_read, out stdin_write, true); } else { stdin_read = MonoIO.ConsoleInput; stdin_write = IntPtr.Zero; } if (startInfo.RedirectStandardOutput) { CreatePipe(out stdout_read, out stdout_write, false); } else { stdout_read = IntPtr.Zero; stdout_write = MonoIO.ConsoleOutput; } if (startInfo.RedirectStandardError) { CreatePipe(out stderr_read, out stderr_write, false); } else { stderr_read = IntPtr.Zero; stderr_write = MonoIO.ConsoleError; } FillUserInfo(startInfo, ref proc_info); // // FIXME: For redirected pipes we need to send descriptors of // stdin_write, stdout_read, stderr_read to child process and // close them there (fork makes exact copy of parent's descriptors) // if (!CreateProcess_internal(startInfo, stdin_read, stdout_write, stderr_write, ref proc_info)) { throw new Win32Exception(-proc_info.pid, "ApplicationName='" + startInfo.FileName + "', CommandLine='" + startInfo.Arguments + "', CurrentDirectory='" + startInfo.WorkingDirectory + "', Native error= " + Win32Exception.W32ErrorMessage(-proc_info.pid)); } } catch { if (startInfo.RedirectStandardInput) { if (stdin_read != IntPtr.Zero) { MonoIO.Close(stdin_read, out error); } if (stdin_write != IntPtr.Zero) { MonoIO.Close(stdin_write, out error); } } if (startInfo.RedirectStandardOutput) { if (stdout_read != IntPtr.Zero) { MonoIO.Close(stdout_read, out error); } if (stdout_write != IntPtr.Zero) { MonoIO.Close(stdout_write, out error); } } if (startInfo.RedirectStandardError) { if (stderr_read != IntPtr.Zero) { MonoIO.Close(stderr_read, out error); } if (stderr_write != IntPtr.Zero) { MonoIO.Close(stderr_write, out error); } } throw; } finally { if (proc_info.Password != IntPtr.Zero) { Marshal.ZeroFreeBSTR(proc_info.Password); proc_info.Password = IntPtr.Zero; } } SetProcessHandle(new SafeProcessHandle(proc_info.process_handle, true)); SetProcessId(proc_info.pid); if (startInfo.RedirectStandardInput) { // // FIXME: The descriptor needs to be closed but due to wapi io-layer // not coping with duplicated descriptors any StandardInput write fails // // MonoIO.Close (stdin_read, out error); #if MOBILE var stdinEncoding = Encoding.Default; #else var stdinEncoding = Console.InputEncoding; #endif standardInput = new StreamWriter(new FileStream(stdin_write, FileAccess.Write, true, 8192), stdinEncoding) { AutoFlush = true }; } if (startInfo.RedirectStandardOutput) { MonoIO.Close(stdout_write, out error); Encoding stdoutEncoding = startInfo.StandardOutputEncoding ?? Console.Out.Encoding; standardOutput = new StreamReader(new FileStream(stdout_read, FileAccess.Read, true, 8192), stdoutEncoding, true); } if (startInfo.RedirectStandardError) { MonoIO.Close(stderr_write, out error); Encoding stderrEncoding = startInfo.StandardErrorEncoding ?? Console.Out.Encoding; standardError = new StreamReader(new FileStream(stderr_read, FileAccess.Read, true, 8192), stderrEncoding, true); } return(true); }
public static void Replace(string sourceFileName, string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors) { MonoIOError error; if (sourceFileName == null) { throw new ArgumentNullException("sourceFileName"); } if (destinationFileName == null) { throw new ArgumentNullException("destinationFileName"); } if (sourceFileName.Trim().Length == 0 || sourceFileName.IndexOfAny(Path.InvalidPathChars) != -1) { throw new ArgumentException("sourceFileName"); } if (destinationFileName.Trim().Length == 0 || destinationFileName.IndexOfAny(Path.InvalidPathChars) != -1) { throw new ArgumentException("destinationFileName"); } string fullSource = Path.GetFullPath(sourceFileName); string fullDest = Path.GetFullPath(destinationFileName); if (MonoIO.ExistsDirectory(fullSource, out error)) { throw new IOException(Locale.GetText("{0} is a directory", sourceFileName)); } if (MonoIO.ExistsDirectory(fullDest, out error)) { throw new IOException(Locale.GetText("{0} is a directory", destinationFileName)); } if (!Exists(fullSource)) { throw new FileNotFoundException(Locale.GetText("{0} does not exist", sourceFileName), sourceFileName); } if (!Exists(fullDest)) { throw new FileNotFoundException(Locale.GetText("{0} does not exist", destinationFileName), destinationFileName); } if (fullSource == fullDest) { throw new IOException(Locale.GetText("Source and destination arguments are the same file.")); } string fullBackup = null; if (destinationBackupFileName != null) { if (destinationBackupFileName.Trim().Length == 0 || destinationBackupFileName.IndexOfAny(Path.InvalidPathChars) != -1) { throw new ArgumentException("destinationBackupFileName"); } fullBackup = Path.GetFullPath(destinationBackupFileName); if (MonoIO.ExistsDirectory(fullBackup, out error)) { throw new IOException(Locale.GetText("{0} is a directory", destinationBackupFileName)); } if (fullSource == fullBackup) { throw new IOException(Locale.GetText("Source and backup arguments are the same file.")); } if (fullDest == fullBackup) { throw new IOException(Locale.GetText( "Destination and backup arguments are the same file.")); } } var attrs = GetAttributes(fullDest); // TODO: Should be done in wapi, win32 api handles this already if ((attrs & FileAttributes.ReadOnly) != 0) { throw MonoIO.GetException(MonoIOError.ERROR_ACCESS_DENIED); } if (!MonoIO.ReplaceFile(fullSource, fullDest, fullBackup, ignoreMetadataErrors, out error)) { throw MonoIO.GetException(error); } }
public static void Copy(string sourceFileName, string destFileName, bool overwrite) { MonoIOError error; if (sourceFileName == null) { throw new ArgumentNullException("sourceFileName"); } if (destFileName == null) { throw new ArgumentNullException("destFileName"); } if (sourceFileName.Length == 0) { throw new ArgumentException("An empty file name is not valid.", "sourceFileName"); } if (sourceFileName.Trim().Length == 0 || sourceFileName.IndexOfAny(Path.InvalidPathChars) != -1) { throw new ArgumentException("The file name is not valid."); } if (destFileName.Length == 0) { throw new ArgumentException("An empty file name is not valid.", "destFileName"); } if (destFileName.Trim().Length == 0 || destFileName.IndexOfAny(Path.InvalidPathChars) != -1) { throw new ArgumentException("The file name is not valid."); } SecurityManager.EnsureElevatedPermissions(); // this is a no-op outside moonlight if (!MonoIO.Exists(sourceFileName, out error)) { throw new FileNotFoundException(Locale.GetText("{0} does not exist", sourceFileName), sourceFileName); } if ((GetAttributes(sourceFileName) & FileAttributes.Directory) == FileAttributes.Directory) { throw new ArgumentException(Locale.GetText("{0} is a directory", sourceFileName)); } if (MonoIO.Exists(destFileName, out error)) { if ((GetAttributes(destFileName) & FileAttributes.Directory) == FileAttributes.Directory) { throw new ArgumentException(Locale.GetText("{0} is a directory", destFileName)); } if (!overwrite) { throw new IOException(Locale.GetText("{0} already exists", destFileName)); } } string DirName = Path.GetDirectoryName(destFileName); if (DirName != String.Empty && !Directory.Exists(DirName)) { throw new DirectoryNotFoundException(Locale.GetText("Destination directory not found: {0}", DirName)); } if (!MonoIO.CopyFile(sourceFileName, destFileName, overwrite, out error)) { string p = Locale.GetText("{0}\" or \"{1}", sourceFileName, destFileName); throw MonoIO.GetException(p, error); } }
public static void Move(string sourceFileName, string destFileName) { if (sourceFileName == null) { throw new ArgumentNullException("sourceFileName"); } if (destFileName == null) { throw new ArgumentNullException("destFileName"); } if (sourceFileName.Length == 0) { throw new ArgumentException("An empty file name is not valid.", "sourceFileName"); } if (sourceFileName.Trim().Length == 0 || sourceFileName.IndexOfAny(Path.InvalidPathChars) != -1) { throw new ArgumentException("The file name is not valid."); } if (destFileName.Length == 0) { throw new ArgumentException("An empty file name is not valid.", "destFileName"); } if (destFileName.Trim().Length == 0 || destFileName.IndexOfAny(Path.InvalidPathChars) != -1) { throw new ArgumentException("The file name is not valid."); } SecurityManager.EnsureElevatedPermissions(); // this is a no-op outside moonlight MonoIOError error; if (!MonoIO.Exists(sourceFileName, out error)) { throw new FileNotFoundException(Locale.GetText("{0} does not exist", sourceFileName), sourceFileName); } // Don't check for this error here to allow the runtime // to check if sourceFileName and destFileName are equal. // Comparing sourceFileName and destFileName is not enough. //if (MonoIO.Exists (destFileName, out error)) // throw new IOException (Locale.GetText ("{0} already exists", destFileName)); string DirName; DirName = Path.GetDirectoryName(destFileName); if (DirName != String.Empty && !Directory.Exists(DirName)) { throw new DirectoryNotFoundException(Locale.GetText("Could not find a part of the path.")); } if (!MonoIO.MoveFile(sourceFileName, destFileName, out error)) { if (error == MonoIOError.ERROR_ALREADY_EXISTS) { throw MonoIO.GetException(error); } else if (error == MonoIOError.ERROR_SHARING_VIOLATION) { throw MonoIO.GetException(sourceFileName, error); } throw MonoIO.GetException(error); } }