Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DesktopFileUtil: add_exec_permission: Return bool instead of int #157

Merged
merged 2 commits into from
Feb 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions src/Util/DesktopFileUtil.vala
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,46 @@

namespace Util.DesktopFileUtil {
/**
* The mask bit to get permission info from mode_t (stat.st_mode).
* The mask bit to get permission info (``st_mode``) from ``mode_t``.
*
* Refer to ``man stat.3`` for more info.
*/
public const Posix.mode_t PERMISSION_BIT = Posix.S_IRWXU | Posix.S_IRWXG | Posix.S_IRWXO;

/**
* Add executable permission to the given file at path.
* Add execute permission to the given path.
*
* If the current user already has execute permission to the given path,
* this method doesn't change its permission and returns true.
*
* @return 0 when succeed, 1 otherwise.
* @param path the path to add execute permission for the current user
* @return true when succeed, false otherwise
*/
public int add_exec_permission (string path) {
public bool add_exec_permission (string path) {
int ret;
Posix.Stat sbuf;

ret = Posix.stat (path, out sbuf);
if (ret != 0) {
warning ("Failed to get the current mode of '%s': %s",
path, Posix.strerror (Posix.errno));
return 1;
warning ("add_exec_permission: Failed to get the current mode of \"%s\": %s",
path, Posix.strerror (Posix.errno));
return false;
}

Posix.mode_t cur_perm = sbuf.st_mode & PERMISSION_BIT;
Posix.mode_t current_permission = sbuf.st_mode & PERMISSION_BIT;

/*
* If the current user already has exec permission to the specified file,
* do nothing.
*/
if ((cur_perm & Posix.S_IXUSR) == Posix.S_IXUSR) {
return 0;
// Do nothing if the current user already has execute permission
if ((current_permission & Posix.S_IXUSR) == Posix.S_IXUSR) {
return true;
}

ret = Posix.chmod (path, cur_perm | Posix.S_IXUSR);
ret = Posix.chmod (path, current_permission | Posix.S_IXUSR);
if (ret != 0) {
warning ("Failed to give exec permission to '%s': %s",
path, Posix.strerror (Posix.errno));
return 1;
warning ("add_exec_permission: Failed to give exec permission to \"%s\": %s",
path, Posix.strerror (Posix.errno));
return false;
}

return 0;
return true;
}
}