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 #ifdef __WINDOWS__
64  VLOG(2) << "`os::permissions` has been called, but is a stub on Windows";
65  return Permissions(0);
66 #else
67  struct stat status;
68  if (::stat(path.c_str(), &status) < 0) {
69  return ErrnoError();
70  }
71 
72  return Permissions(status.st_mode);
73 #endif // __WINDOWS__
74 }
75 
76 
77 } // namespace os {
78 
79 
80 #endif // __STOUT_OS_PERMISSIONS_HPP__
Definition: path.hpp:29
const mode_t S_IXOTH
Definition: windows.hpp:323
const mode_t S_IRGRP
Definition: windows.hpp:313
Definition: check.hpp:33
const mode_t S_IWUSR
Definition: windows.hpp:306
struct os::Permissions::@21 group
Result< ProcessStatus > status(pid_t pid)
Definition: proc.hpp:166
Definition: errorbase.hpp:50
Definition: posix_signalhandler.hpp:23
const mode_t S_IRUSR
Definition: windows.hpp:305
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:324
const mode_t S_ISVTX
Definition: windows.hpp:330
int mode_t
Definition: windows.hpp:177
const mode_t S_IXGRP
Definition: windows.hpp:315
bool r
Definition: permissions.hpp:49
const mode_t S_IXUSR
Definition: windows.hpp:307
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:308
const mode_t S_IWGRP
Definition: windows.hpp:314
const mode_t S_ISUID
Definition: windows.hpp:328
Definition: grp.hpp:26
const mode_t S_IRWXG
Definition: windows.hpp:316
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:168
const mode_t S_IWOTH
Definition: windows.hpp:322
const mode_t S_ISGID
Definition: windows.hpp:329
bool x
Definition: permissions.hpp:51
const mode_t S_IROTH
Definition: windows.hpp:321
Permissions(mode_t mode)
Definition: permissions.hpp:28