public override async Task <bool> FileExists(string filename) { var exists = false; using (var process = CreateSUProcess()) using (var input = new DataInputStream(process.InputStream)) using (var output = new DataOutputStream(process.OutputStream)) { await output.WriteBytesAsync($"[ -f \"{filename}\" ] && echo 1 || echo 0\n"); await output.FlushAsync(); await output.WriteBytesAsync("exit\n"); await output.FlushAsync(); var res = await input.ReadLineAsync(); exists = res == "1"; await process.WaitForAsync(); } return(exists); }
public override async Task <bool> CheckDeviceRooted() { try { using (var process = CreateSUProcess()) using (var input = new DataInputStream(process.InputStream)) using (var output = new DataOutputStream(process.OutputStream)) { await output.WriteBytesAsync("id\n"); await output.FlushAsync(); var id = await input.ReadLineAsync(); var res = id != null && id.Contains("uid=0"); await output.WriteBytesAsync("exit\n"); await output.FlushAsync(); await process.WaitForAsync(); return(res); } } catch { return(false); } }
private async Task DoBackup(Uri uri, string password) { var backup = new Backup( _authSource.GetAll(), _categorySource.GetAll(), _authSource.CategoryBindings, _customIconSource.GetAll() ); var dataToWrite = backup.ToBytes(password); // This is the only way of reliably writing files using SAF on Xamarin. // A file output stream will usually create 0 byte files on virtual storage such as Google Drive var output = ContentResolver.OpenOutputStream(uri, "rwt"); var dataStream = new DataOutputStream(output); try { await dataStream.WriteAsync(dataToWrite); await dataStream.FlushAsync(); } finally { dataStream.Close(); output.Close(); } }
public override async Task ExecuteRootCommand(string cmd) { using (var process = CreateSUProcess()) using (var output = new DataOutputStream(process.OutputStream)) { await output.WriteBytesAsync(cmd + "\n"); await output.FlushAsync(); await output.WriteBytesAsync("exit\n"); await output.FlushAsync(); await process.WaitForAsync(); } }
public static async Task WriteFile(Context context, Uri uri, byte[] data) { // Run backup on separate thread, file writing on the main thread fails when using Nextcloud await Task.Run(async delegate { // This is the only way of reliably writing binary files using SAF on Xamarin. // A file output stream will usually create 0 byte files on virtual storage such as Google Drive Stream output = null; DataOutputStream dataStream = null; try { output = context.ContentResolver.OpenOutputStream(uri); dataStream = new DataOutputStream(output); await dataStream.WriteAsync(data); await dataStream.FlushAsync(); } finally { dataStream?.Close(); output?.Close(); } }); }
public override async Task <Stream> OpenFile(string filename) { Stream outStream = null; using (var process = CreateSUProcess()) using (var input = new DataInputStream(process.InputStream)) using (var output = new DataOutputStream(process.OutputStream)) { await output.WriteBytesAsync($"[ -f \"{filename}\" ] && echo 1 || echo 0\n"); await output.FlushAsync(); var exists = await input.ReadLineAsync(); if (exists == "1") { var tempDirectory = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "Android", "data", AppInfo.PackageName, "cache"); var outputFile = Path.Combine(tempDirectory, Path.GetFileName(filename)); if (!Directory.Exists(tempDirectory)) { Directory.CreateDirectory(tempDirectory); } await output.WriteBytesAsync($"cp {filename} {outputFile}\n"); await output.FlushAsync(); outStream = new FileInfo(outputFile).Open(FileMode.Open); } await output.WriteBytesAsync("exit\n"); await output.FlushAsync(); await process.WaitForAsync(); } return(outStream); }