-
Notifications
You must be signed in to change notification settings - Fork 0
/
permissions.cpp
41 lines (30 loc) · 993 Bytes
/
permissions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
* permissions.cpp
*
* Created on: 23.01.2014
* Author: milo
*/
#include "permissions.hpp"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
namespace SimpleTar {
int getFilePermissions(const boost::filesystem::path& path) {
//boost::filesystem 1.48 had not file_status.permissions() yet
struct stat buf;
if (stat(path.c_str(), &buf)) {
boost::system::error_code ec(errno, boost::system::system_category());
throw boost::system::system_error (ec, "Cannot stat file");
}
permissions_t perms = 0;
perms |= (buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
return perms;
}
void setPermissions(const boost::filesystem::path& path, permissions_t perms) {
mode_t mode = perms;
if (chmod(path.c_str(), mode) < 0) {
boost::system::error_code ec(errno, boost::system::system_category());
throw boost::system::system_error (ec, "Cannot set permissions to the file");
}
}
} //namespace SimpleTar