create script to write to file without changing ownership

This commit is contained in:
joshuaboud 2021-10-04 13:16:41 -03:00
parent 45b72df38b
commit f269505719
No known key found for this signature in database
GPG Key ID: 17EFB59E2A8BF50E

View File

@ -20,7 +20,7 @@
import { NavEntry } from "./NavEntry.js"; import { NavEntry } from "./NavEntry.js";
import { NavDownloader } from "./NavDownloader.js"; import { NavDownloader } from "./NavDownloader.js";
import { NavWindow } from "./NavWindow.js"; import { NavWindow } from "./NavWindow.js";
import { property_entry_html } from "../functions.js"; import { property_entry_html, simple_spawn } from "../functions.js";
export class NavFile extends NavEntry { export class NavFile extends NavEntry {
/** /**
@ -128,12 +128,9 @@ export class NavFile extends NavEntry {
async write_to_file() { async write_to_file() {
var new_contents = document.getElementById("nav-edit-contents-textarea").value; var new_contents = document.getElementById("nav-edit-contents-textarea").value;
try { try {
if (new_contents.length) await simple_spawn(["/usr/share/cockpit/navigator/scripts/write-to-file.py3", this.path_str()], new_contents);
await cockpit.file(this.path_str(), {superuser: "try"}).replace(new_contents);
else
await cockpit.script("echo -n > $1", [this.path_str()], {superuser: "try"});
} catch (e) { } catch (e) {
this.nav_window_ref.modal_prompt.alert(e.message); this.nav_window_ref.modal_prompt.alert(e);
} }
this.nav_window_ref.refresh(); this.nav_window_ref.refresh();
this.hide_edit_file_contents(); this.hide_edit_file_contents();
@ -229,9 +226,9 @@ export class NavFileLink extends NavFile{
var target_path = this.get_link_target_path(); var target_path = this.get_link_target_path();
var new_contents = document.getElementById("nav-edit-contents-textarea").value; var new_contents = document.getElementById("nav-edit-contents-textarea").value;
try { try {
await cockpit.file(target_path, {superuser: "try"}).replace(new_contents); await simple_spawn(["/usr/share/cockpit/navigator/scripts/write-to-file.py3", target_path], new_contents);
} catch (e) { } catch (e) {
this.nav_window_ref.modal_prompt.alert(e.message); this.nav_window_ref.modal_prompt.alert(e);
} }
this.nav_window_ref.refresh(); this.nav_window_ref.refresh();
this.hide_edit_file_contents(); this.hide_edit_file_contents();