OS.File set chown and chmod (creating trusted laun

2019-09-01 07:29发布

I'm trying to create a shortcut that launches a profile on the desktop in Linux.

I'm using this code:

Cu.import('resource://gre/modules/FileUtils.jsm'); var exe = FileUtils.getFile('XREExeF', []); var args = '-P -dev -no-remote';

var name = "mysc";
var target = exe;
var cmd = "[Desktop Entry]\n";
cmd += "Name=" + name + "\n";
cmd += "Type=Application\n";
cmd += "Comment=Web Application\n";
cmd += "Exec=" + target.path + " " + args + "\n";
//cmd += "Icon=" + icon.path + "\n";

Cu.import("resource://gre/modules/osfile.jsm")
var path = OS.Path.join(OS.Constants.Path.desktopDir, 'mysc.desktop');
Services.ww.activeWindow.alert(path)

let encoder = new TextEncoder();
let array = encoder.encode(cmd);
let promise = OS.File.writeAtomic(path, array, {tmpPath: "file.txt.tmp"});
promise.then(
  function(aVal) {
    Services.ww.activeWindow.alert('success aVal = ' + uneval(aVal));
  },
  function(aRejReas) {
    Services.ww.activeWindow.alert('rejected for reason: ' + uneval(aRejReas))
  }
)

How can i set chown and chmod?

I found this code https://github.com/johnshih/releases-mozilla-aurora/blob/dc29053a4b1765cf94a8562130865036c373038e/toolkit/components/osfile/osfile_unix_back.jsm#L218

UnixFile.chmod =
    declareFFI("chmod", ctypes.default_abi,
        /*return*/
        Types.negativeone_or_nothing,
        /*path*/
        Types.path,
        /*mode*/
        Types.mode_t);
 UnixFile.chown =
    declareFFI("chown", ctypes.default_abi,
        /*return*/
        Types.negativeone_or_nothing,
        /*path*/
        Types.path,
        /*uid*/
        Types.uid_t,
        /*gid*/
        Types.gid_t);

Someone told me to set chown noi and chmod +x on the file because my path to desktop is: /home/noi/Desktop/mysc.desktop will fix this error here:

Maybe OS.File.setPermissions?

I tried this but it didnt work:

    var promise2 = OS.File.setPermissions(path, {
        unixMode: OS.Constants.libc.S_IRWXO
    });
    promise2.then(
        function(aVal) {
            console.log('promise2 success', 'aVal:', aVal);
        },
        function(aReason) {
            console.warn('promise2 rejected', 'aReason:', aReason);
        }
    );

1条回答
淡お忘
2楼-- · 2019-09-01 07:48

I didn't figure out how to set chown and chmod but I was able to create the file as trusted. It was along the lines of OS.File.setPermission but for some reason using unixMod and the flags of OSConstants.libc.S_I* like the docs recommended was not right.

This is how I made a trusted desktop shortcut:

Cu.import('resource://gre/modules/FileUtils.jsm');
Cu.import("resource://gre/modules/osfile.jsm")

var exe = FileUtils.getFile('XREExeF', []);
var args = '-P dev -no-remote';

var name = "Mozilla Firefox - dev";
var target = exe;
var icon_path = OS.Path.join(OS.Constants.Path.desktopDir, 'beta.png');
var cmd = "[Desktop Entry]\n";
cmd += "Name=" + name + "\n";
cmd += "Type=Application\n";
cmd += "Comment=Web Application\n";
cmd += "Exec=" + target.path + " " + args + "\n";
cmd += "Icon=" + icon_path + "\n";


var path = OS.Path.join(OS.Constants.Path.desktopDir, 'mysc.desktop');

let encoder = new TextEncoder();
let array = encoder.encode(cmd);
let promise = OS.File.writeAtomic(path, array, {tmpPath: "file.txt.tmp"});
promise.then(
  function(aVal) {
    console.log('aVal', aVal);
    var promise2 = OS.File.setPermissions(path, {unixMode: 0o4777});
    promise2.then(
     function(aVal) {
       console.log('promise2 success', 'aVal:', aVal);
     },
      function(aReason) {
        console.warn('promise2 rejected', 'aReason:', aReason);
      }
    );
  },
  function(aRejReas) {
    Services.ww.activeWindow.alert('rejected for reason: ' + uneval(aRejReas))
  }
);

Notice the 0o4777 instead of OS.Constants.libc.S_IRWXO, its weird, I didn't expect this at all and just got lucky searching github code base.

查看更多
登录 后发表回答