public string ReadFile(string path, string userIdentity) { string result = String.Empty; if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0)) { if (FileExists(path)) { NSFileHandle fileHandle = null; NSError error = null; Exception exception = null; try { UIApplication.SharedApplication.InvokeOnMainThread(() => { _fileProtectionManagerService.DecryptFile(path); fileHandle = NSFileHandle.OpenReadUrl(NSUrl.CreateFileUrl(new[] { path }), out error); if (fileHandle != null) { var nsStringResult = NSString.FromData(fileHandle.ReadDataToEndOfFile(), NSStringEncoding.UTF8); if (nsStringResult != null) { result = nsStringResult.ToString(); } } _fileProtectionManagerService.EncryptFile(path, userIdentity); }); } catch (Exception ex) { exception = ex; } finally { if (fileHandle != null) { fileHandle.CloseFile(); } } if (error != null) { _loggingService.LogError(typeof(FileSystemService), new Exception(error.DebugDescription), error.Description); throw new NSErrorException(error); } else if (exception != null) { _loggingService.LogError(typeof(FileSystemService), exception, exception.Message); throw exception; } } } return(result); }
private Stream PlatformGetInputStream(string path) { if (FileExists(path)) { NSFileHandle handle = NSFileHandle.OpenReadUrl(NSUrl.CreateFileUrl(new[] { path }), out NSError error); if (error != null) { throw new NSErrorException(error); } else { NSData result = handle.ReadDataToEndOfFile(); handle.CloseFile(); return result.AsStream(); } } throw new FileNotFoundException(); }
private string PlatformReadFile(string path) { if (FileExists(path)) { NSFileHandle handle = NSFileHandle.OpenReadUrl(NSUrl.CreateFileUrl(new[] { path }), out NSError error); if (error != null) { throw new NSErrorException(error); } else { NSString result = NSString.FromData(handle.ReadDataToEndOfFile(), NSStringEncoding.UTF8); handle.CloseFile(); return result?.ToString() ?? ""; } } throw new FileNotFoundException(); }
public override void ShellSyncCore(string path, string[] arguments, string autoWriteStdin, out string stdout, out string stderr, out int exitCode) { if (autoWriteStdin != "") { throw new Exception("ShellSyncCore::AutoWriteStdin not supported in macOS"); // Never need yet, used only in Linux } try { var pipeOut = new NSPipe(); var pipeErr = new NSPipe(); var t = new NSTask(); t.LaunchPath = path; t.Arguments = arguments; t.StandardOutput = pipeOut; t.StandardError = pipeErr; t.Launch(); t.WaitUntilExit(); //t.Release(); t.Dispose(); NSFileHandle fileOut = pipeOut.ReadHandle; stdout = fileOut.ReadDataToEndOfFile().ToString(); fileOut.CloseFile(); NSFileHandle fileErr = pipeErr.ReadHandle; stderr = fileErr.ReadDataToEndOfFile().ToString(); fileErr.CloseFile(); exitCode = t.TerminationStatus; } catch (Exception ex) { stdout = ""; stderr = "Error: " + ex.Message; exitCode = -1; } }
public Stream GetInputStream(string path, string userIdentity) { NSFileHandle fileHandle = null; NSError error = null; NSData result = null; Exception exception = null; try { UIApplication.SharedApplication.InvokeOnMainThread(() => { _fileProtectionManagerService.DecryptFile(path); fileHandle = NSFileHandle.OpenReadUrl(NSUrl.CreateFileUrl(new[] { path }), out error); result = fileHandle.ReadDataToEndOfFile(); _fileProtectionManagerService.EncryptFile(path, userIdentity); }); } catch (Exception ex) { exception = ex; } finally { if (fileHandle != null) { fileHandle.CloseFile(); } } if (error != null) { throw new NSErrorException(error); } else if (exception != null) { throw exception; } return(result?.AsStream() ?? null); }
private void PlatformWriteFile(string path, string contents) { if (FileExists(path)) { NSFileHandle handle = NSFileHandle.OpenUpdateUrl(NSUrl.CreateFileUrl(new[] { path }), out NSError error); if (error != null) { throw new NSErrorException(error); } else { handle.SeekToEndOfFile(); handle.WriteData(NSData.FromString(contents)); handle.CloseFile(); } } else { CreateFile(path, NSData.FromString(contents)); } }
public override void ShellSync(string path, string[] arguments, out string stdout, out string stderr, out int exitCode) { try { var pipeOut = new NSPipe(); var pipeErr = new NSPipe(); var t = new NSTask(); t.LaunchPath = path; t.Arguments = arguments; t.StandardOutput = pipeOut; t.StandardError = pipeErr; t.Launch(); t.WaitUntilExit(); //t.Release(); t.Dispose(); NSFileHandle fileOut = pipeOut.ReadHandle; stdout = fileOut.ReadDataToEndOfFile().ToString(); fileOut.CloseFile(); NSFileHandle fileErr = pipeErr.ReadHandle; stderr = fileErr.ReadDataToEndOfFile().ToString(); fileErr.CloseFile(); exitCode = t.TerminationStatus; } catch (Exception ex) { stdout = ""; stderr = "Error: " + ex.Message; exitCode = -1; } }
public void WriteFile(string path, string userIdentity, string contents) { if (!string.IsNullOrEmpty(path) || !string.IsNullOrEmpty(contents)) { if (!NSFileManager.DefaultManager.FileExists(path)) { var dict = new NSMutableDictionary(); UIApplication.SharedApplication.InvokeOnMainThread(() => { NSFileManager.DefaultManager.CreateFile(path, NSData.FromString(contents), dict); }); _fileProtectionManagerService.ProtectFile(path, userIdentity); _fileProtectionManagerService.EncryptFile(path, userIdentity); } else { NSFileHandle fileHandle = null; NSError error = null; Exception exception = null; try { UIApplication.SharedApplication.InvokeOnMainThread(() => { _fileProtectionManagerService.DecryptFile(path); fileHandle = NSFileHandle.OpenUpdateUrl(NSUrl.CreateFileUrl(new[] { path }), out error); fileHandle.SeekToEndOfFile(); fileHandle.WriteData(NSData.FromString(contents)); _fileProtectionManagerService.ProtectFile(path, userIdentity); _fileProtectionManagerService.EncryptFile(path, userIdentity); }); } catch (Exception ex) { exception = ex; } finally { if (fileHandle != null) { UIApplication.SharedApplication.InvokeOnMainThread(() => { fileHandle.CloseFile(); }); } } if (error != null) { _loggingService.LogError(typeof(FileSystemService), new Exception(error.DebugDescription), error.Description); throw new NSErrorException(error); } else if (exception != null) { _loggingService.LogError(typeof(FileSystemService), exception, exception.Message); throw exception; } } } }