Implement sudo file write

This commit is contained in:
crschnick 2024-09-25 12:25:44 +00:00
parent 83a5659ca4
commit 7458a4498a
14 changed files with 85 additions and 8 deletions

View file

@ -1,17 +1,64 @@
package io.xpipe.app.browser;
import io.xpipe.app.browser.fs.OpenFileSystemModel;
import io.xpipe.app.core.window.AppWindowHelper;
import io.xpipe.app.prefs.AppPrefs;
import io.xpipe.app.util.BooleanScope;
import io.xpipe.app.util.FileBridge;
import io.xpipe.app.util.FileOpener;
import io.xpipe.core.process.ElevationFunction;
import io.xpipe.core.process.OsType;
import io.xpipe.core.store.ConnectionFileSystem;
import io.xpipe.core.store.FileEntry;
import io.xpipe.core.store.FileInfo;
import io.xpipe.core.store.FileNames;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class BrowserFileOpener {
private static OutputStream openFileOutput(OpenFileSystemModel model, FileEntry file, long totalBytes) throws Exception {
var fileSystem = model.getFileSystem();
if (model.isClosed() || fileSystem.getShell().isEmpty()) {
return OutputStream.nullOutputStream();
}
var sc = fileSystem.getShell().get();
if (sc.getOsType() == OsType.WINDOWS) {
return fileSystem.openOutput(file.getPath(), totalBytes);
}
var info = (FileInfo.Unix) file.getInfo();
var zero = Integer.valueOf(0);
var requiresRoot = zero.equals(info.getUid()) && zero.equals(info.getGid());
if (!requiresRoot || model.getCache().isRoot()) {
return fileSystem.openOutput(file.getPath(), totalBytes);
}
var elevate = AppWindowHelper.showConfirmationAlert("app.fileWriteSudoTitle", "app.fileWriteSudoHeader", "app.fileWriteSudoContent");
if (!elevate) {
return fileSystem.openOutput(file.getPath(), totalBytes);
}
var rootSc = sc.identicalSubShell().elevated(ElevationFunction.elevated("sudo")).start();
var rootFs = new ConnectionFileSystem(rootSc);
try {
return new FilterOutputStream(rootFs.openOutput(file.getPath(), totalBytes)) {
@Override
public void close() throws IOException {
super.close();
rootFs.close();
}
};
} catch (Exception ex) {
rootFs.close();
throw ex;
}
}
public static void openWithAnyApplication(OpenFileSystemModel model, FileEntry entry) {
var file = entry.getPath();
var key = entry.getPath().hashCode() + entry.getFileSystem().hashCode();
@ -71,11 +118,7 @@ public class BrowserFileOpener {
return entry.getFileSystem().openInput(file);
},
(size) -> {
if (model.isClosed()) {
return OutputStream.nullOutputStream();
}
return entry.getFileSystem().openOutput(file, size);
return openFileOutput(model,entry, size);
},
FileOpener::openInTextEditor);
}

View file

@ -24,10 +24,8 @@ import io.xpipe.core.process.ShellDialects;
import io.xpipe.core.process.ShellOpenFunction;
import io.xpipe.core.store.*;
import io.xpipe.core.util.FailableConsumer;
import javafx.beans.binding.Bindings;
import javafx.beans.property.*;
import lombok.Getter;
import lombok.SneakyThrows;

View file

@ -523,3 +523,6 @@ yellow=Gul
blue=Blå
red=Rød
asktextAlertTitle=Spørg
fileWriteSudoTitle=Sudo filskrivning
fileWriteSudoHeader=Den fil, du forsøger at skrive, kræver root-rettigheder. Vil du skrive denne fil med sudo?
fileWriteSudoContent=Dette vil automatisk hæve sig til root med enten de angivne legitimationsoplysninger eller via en prompt.

View file

@ -517,3 +517,6 @@ yellow=Gelb
blue=Blau
red=Rot
asktextAlertTitle=Eingabeaufforderung
fileWriteSudoTitle=Sudo-Datei schreiben
fileWriteSudoHeader=Die Datei, die du zu schreiben versuchst, erfordert Root-Rechte. Willst du diese Datei mit sudo schreiben?
fileWriteSudoContent=Dadurch wird automatisch ein Root-Zugang eingerichtet, entweder mit den angegebenen Anmeldedaten oder über eine Eingabeaufforderung.

View file

@ -522,4 +522,7 @@ green=Green
yellow=Yellow
blue=Blue
red=Red
asktextAlertTitle=Prompt
asktextAlertTitle=Prompt
fileWriteSudoTitle=Sudo file write
fileWriteSudoHeader=The file you are trying to write requires root privileges. Do you want to write this file with sudo?
fileWriteSudoContent=This will automatically elevate to root with either the provided credentials or via a prompt.

View file

@ -504,3 +504,6 @@ yellow=Amarillo
blue=Azul
red=Rojo
asktextAlertTitle=Pregunta
fileWriteSudoTitle=Escritura de archivos Sudo
fileWriteSudoHeader=El archivo que intentas escribir requiere privilegios de root. ¿Quieres escribir este archivo con sudo?
fileWriteSudoContent=Esto elevará automáticamente a root con las credenciales proporcionadas o a través de un prompt.

View file

@ -504,3 +504,6 @@ yellow=Jaune
blue=Bleu
red=Rouge
asktextAlertTitle=Invite
fileWriteSudoTitle=Sudo file write
fileWriteSudoHeader=Le fichier que tu essaies d'écrire nécessite les privilèges de root. Veux-tu écrire ce fichier avec sudo ?
fileWriteSudoContent=Cette opération permet d'accéder automatiquement à la fonction de super-utilisateur à l'aide des informations d'identification fournies ou d'un message d'invite.

View file

@ -504,3 +504,6 @@ yellow=Giallo
blue=Blu
red=Rosso
asktextAlertTitle=Prompt
fileWriteSudoTitle=Scrittura di file Sudo
fileWriteSudoHeader=Il file che stai cercando di scrivere richiede i privilegi di root. Vuoi scrivere questo file con sudo?
fileWriteSudoContent=In questo modo si eleva automaticamente a root con le credenziali fornite o tramite un prompt.

View file

@ -504,3 +504,6 @@ yellow=黄色
blue=ブルー
red=赤い
asktextAlertTitle=プロンプト
fileWriteSudoTitle=須藤ファイル書き込み
fileWriteSudoHeader=書き込もうとしているファイルにはroot権限が必要だ。このファイルをsudoで書き込むか
fileWriteSudoContent=これは、提供された認証情報またはプロンプト経由で自動的にrootに昇格する。

View file

@ -504,3 +504,6 @@ yellow=Geel
blue=Blauw
red=Rood
asktextAlertTitle=Prompt
fileWriteSudoTitle=Sudo bestand schrijven
fileWriteSudoHeader=Het bestand dat je probeert te schrijven vereist rootrechten. Wil je dit bestand schrijven met sudo?
fileWriteSudoContent=Dit zal automatisch verheffen naar root met de verstrekte inloggegevens of via een prompt.

View file

@ -504,3 +504,6 @@ yellow=Amarelo
blue=Azul
red=Vermelho
asktextAlertTitle=Prompt
fileWriteSudoTitle=Escreve um ficheiro Sudo
fileWriteSudoHeader=O ficheiro que estás a tentar escrever requer privilégios de root. Queres escrever este ficheiro com sudo?
fileWriteSudoContent=Isto irá elevar automaticamente para a raiz com as credenciais fornecidas ou através de um prompt.

View file

@ -504,3 +504,6 @@ yellow=Желтый
blue=Синий
red=Красный
asktextAlertTitle=Prompt
fileWriteSudoTitle=Запись файла Sudo
fileWriteSudoHeader=Файл, который ты пытаешься записать, требует привилегий root. Хочешь ли ты записать этот файл с помощью sudo?
fileWriteSudoContent=Это автоматически повысит статус до root либо с помощью предоставленных учетных данных, либо через приглашение.

View file

@ -505,3 +505,6 @@ yellow=Sarı
blue=Mavi
red=Kırmızı
asktextAlertTitle=İstem
fileWriteSudoTitle=Sudo dosya yazma
fileWriteSudoHeader=Yazmaya çalıştığınız dosya root ayrıcalıkları gerektiriyor. Bu dosyayı sudo ile mi yazmak istiyorsunuz?
fileWriteSudoContent=Bu, sağlanan kimlik bilgileriyle veya bir komut istemi aracılığıyla otomatik olarak kök dizine yükseltme yapacaktır.

View file

@ -504,3 +504,6 @@ yellow=黄色
blue=蓝色
red=红色
asktextAlertTitle=提示
fileWriteSudoTitle=Sudo 文件写入
fileWriteSudoHeader=您要写入的文件需要 root 权限。你想用 sudo 来写这个文件吗?
fileWriteSudoContent=这将使用提供的凭据或通过提示自动提升为根用户。