Apache Mesos
permissions.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_OS_PERMISSIONS_HPP__
14 #define __STOUT_OS_PERMISSIONS_HPP__
15 
16 #include <sys/stat.h>
17 
18 #include <string>
19 
20 #include <stout/error.hpp>
21 #include <stout/try.hpp>
22 
23 
24 namespace os {
25 
27 {
29  {
30  owner.r = (mode & S_IRUSR) != 0;
31  owner.w = (mode & S_IWUSR) != 0;
32  owner.x = (mode & S_IXUSR) != 0;
33  owner.rwx = (mode & S_IRWXU) != 0;
34  group.r = (mode & S_IRGRP) != 0;
35  group.w = (mode & S_IWGRP) != 0;
36  group.x = (mode & S_IXGRP) != 0;
37  group.rwx = (mode & S_IRWXG) != 0;
38  others.r = (mode & S_IROTH) != 0;
39  others.w = (mode & S_IWOTH) != 0;
40  others.x = (mode & S_IXOTH) != 0;
41  others.rwx = (mode & S_IRWXO) != 0;
42  setuid = (mode & S_ISUID) != 0;
43  setgid = (mode & S_ISGID) != 0;
44  sticky = (mode & S_ISVTX) != 0;
45  }
46 
47  struct
48  {
49  bool r;
50  bool w;
51  bool x;
52  bool rwx;
53  } owner, group, others;
54 
55  bool setuid;
56  bool setgid;
57  bool sticky;
58 };
59 
60 
61 inline Try<Permissions> permissions(const std::string& path)
62 {
63  struct stat status;
64  if (::stat(path.c_str(), &status) < 0) {
65  return ErrnoError();
66  }
67 
68  return Permissions(status.st_mode);
69 }
70 
71 
72 } // namespace os {
73 
74 
75 #endif // __STOUT_OS_PERMISSIONS_HPP__
const mode_t S_IXOTH
Definition: windows.hpp:329
const mode_t S_IRGRP
Definition: windows.hpp:319
Definition: try.hpp:34
const mode_t S_IWUSR
Definition: windows.hpp:312
struct os::Permissions::@21 group
Result< ProcessStatus > status(pid_t pid)
Definition: proc.hpp:166
Definition: errorbase.hpp:49
const mode_t S_IRUSR
Definition: windows.hpp:311
Try< Permissions > permissions(const std::string &path)
Definition: permissions.hpp:61
bool setgid
Definition: permissions.hpp:56
bool rwx
Definition: permissions.hpp:52
const mode_t S_IRWXO
Definition: windows.hpp:330
const mode_t S_ISVTX
Definition: windows.hpp:336
int mode_t
Definition: windows.hpp:183
const mode_t S_IXGRP
Definition: windows.hpp:321
bool r
Definition: permissions.hpp:49
const mode_t S_IXUSR
Definition: windows.hpp:313
struct os::Permissions::@21 others
Try< hashmap< std::string, uint64_t > > stat(const std::string &hierarchy, const std::string &cgroup, const std::string &file)
Definition: permissions.hpp:26
const mode_t S_IRWXU
Definition: windows.hpp:314
const mode_t S_IWGRP
Definition: windows.hpp:320
const mode_t S_ISUID
Definition: windows.hpp:334
Definition: grp.hpp:26
const mode_t S_IRWXG
Definition: windows.hpp:322
struct os::Permissions::@21 owner
bool w
Definition: permissions.hpp:50
bool sticky
Definition: permissions.hpp:57
bool setuid
Definition: permissions.hpp:55
Try< mode_t > mode(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:126
const mode_t S_IWOTH
Definition: windows.hpp:328
const mode_t S_ISGID
Definition: windows.hpp:335
bool x
Definition: permissions.hpp:51
const mode_t S_IROTH
Definition: windows.hpp:327
Permissions(mode_t mode)
Definition: permissions.hpp:28