Skip to content

Commit

Permalink
Add isTrusted
Browse files Browse the repository at this point in the history
  • Loading branch information
FreeSlave committed Apr 14, 2016
1 parent b9b10e7 commit 1f2e7a4
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 11 deletions.
2 changes: 1 addition & 1 deletion dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"copyright": "Copyright © 2015-2016, Roman Chistokhodov",
"authors": ["Roman Chistokhodov"],
"dependencies": {
"inilike": "~>0.6.0",
"inilike": "~>0.6.1",
"xdgpaths" : "~>0.2.1"
},
"targetName" : "desktopfile",
Expand Down
2 changes: 1 addition & 1 deletion dub.selections.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"fileVersion": 1,
"versions": {
"isfreedesktop": "0.1.0",
"inilike": "0.6.0",
"inilike": "0.6.1",
"xdgpaths": "0.2.1"
}
}
2 changes: 1 addition & 1 deletion examples/desktoptest/dub.selections.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"versions": {
"standardpaths": "0.4.0",
"isfreedesktop": "0.1.0",
"inilike": "0.6.0",
"inilike": "0.6.1",
"xdgpaths": "0.2.1"
}
}
2 changes: 1 addition & 1 deletion examples/desktoputil/dub.selections.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"fileVersion": 1,
"versions": {
"isfreedesktop": "0.1.0",
"inilike": "0.6.0",
"inilike": "0.6.1",
"xdgpaths": "0.2.1"
}
}
1 change: 1 addition & 0 deletions examples/desktoputil/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ void main(string[] args)
if (df.type() == DesktopFile.Type.Application) {
writeln("Exec: ", df.execString());
writeln("In terminal: ", df.terminal());
writeln("Trusted: ", isTrusted(df.fileName));
}
if (df.type() == DesktopFile.Type.Link) {
writeln("URL: ", df.url());
Expand Down
2 changes: 1 addition & 1 deletion examples/shootdesktop/dub.selections.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"fileVersion": 1,
"versions": {
"isfreedesktop": "0.1.0",
"inilike": "0.6.0",
"inilike": "0.6.1",
"xdgpaths": "0.2.1"
}
}
19 changes: 17 additions & 2 deletions source/desktopfile/file.d
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ private @trusted void validateKeyValueImpl(string key, string value) {
}
}

private @safe string escapeIfNeeded(string value) nothrow {
private @trusted string escapeIfNeeded(string value) pure {
if (value.needEscaping()) {
return value.escapeValue();
return value.replace("\r", `\r`).replace("\n", `\n`);
} else {
return value;
}
Expand Down Expand Up @@ -1315,4 +1315,19 @@ Name=Name2`;
assert(df.genericName() == "Program");
df.comment = "Do\nthings";
assert(df.comment() == `Do\nthings`);

df.execString = "utilname";
assert(df.execString() == "utilname");

df.noDisplay = true;
assert(df.noDisplay());
df.hidden = true;
assert(df.hidden());
df.dbusActivable = true;
assert(df.dbusActivable());
df.startupNotify = true;
assert(df.startupNotify());

df.url = "/some/url";
assert(df.url == "/some/url");
}
38 changes: 34 additions & 4 deletions source/desktopfile/utils.d
Original file line number Diff line number Diff line change
Expand Up @@ -612,18 +612,18 @@ unittest
File(tempXdgTerminalFile, "w");
scope(exit) remove(tempXdgTerminalFile);
changeMod(tempXdgTerminalFile, octal!755);
assert(getTerminalCommand() == [buildPath(tempPath, "xdg-terminal")]);
enforce(getTerminalCommand() == [buildPath(tempPath, "xdg-terminal")]);

changeMod(tempXdgTerminalFile, octal!644);
assert(getTerminalCommand() == ["xterm", "-e"]);
enforce(getTerminalCommand() == ["xterm", "-e"]);

File(tempXTerminalEmulatorFile, "w");
scope(exit) remove(tempXTerminalEmulatorFile);
changeMod(tempXTerminalEmulatorFile, octal!755);
assert(getTerminalCommand() == [buildPath(tempPath, "x-terminal-emulator"), "-e"]);
enforce(getTerminalCommand() == [buildPath(tempPath, "x-terminal-emulator"), "-e"]);

environment["PATH"] = ":";
assert(getTerminalCommand() == ["xterm", "-e"]);
enforce(getTerminalCommand() == ["xterm", "-e"]);

} catch(Exception e) {

Expand Down Expand Up @@ -956,3 +956,33 @@ static if (isFreedesktop)
return desktopId(fileName, applicationsPaths());
}
}


/**
* Check if .desktop file is trusted. This is not actually part of Desktop File Specification but many file managers has this concept.
* The trusted .desktop file is a file the current user has executable access to or the owner of which is root.
* This function should be applicable only to desktop files of Application type.
* Note: Always returns true on non-posix systems.
*/
@trusted bool isTrusted(string appFileName) nothrow
{
version(Posix) {
import core.sys.posix.sys.stat;
import core.sys.posix.unistd;

try { // try for outdated compilers
auto namez = toStringz(appFileName);
if (access(namez, X_OK) == 0) {
return true;
}

stat_t statbuf;
auto result = stat(namez, &statbuf);
return (result == 0 && statbuf.st_uid == 0);
} catch(Exception e) {
return false;
}
} else {
return true;
}
}

0 comments on commit 1f2e7a4

Please sign in to comment.