Apache Mesos
stat.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_POSIX_STAT_HPP__
14 #define __STOUT_OS_POSIX_STAT_HPP__
15 
16 #include <sys/stat.h>
17 #include <sys/statvfs.h>
18 
19 #include <string>
20 
21 #include <stout/bytes.hpp>
22 #include <stout/try.hpp>
23 #include <stout/unreachable.hpp>
24 
25 
26 namespace os {
27 
28 namespace stat {
29 
30 // Specify whether symlink path arguments should be followed or
31 // not. APIs in the os::stat family that take a FollowSymlink
32 // argument all provide FollowSymlink::FOLLOW_SYMLINK as the default value,
33 // so they will follow symlinks unless otherwise specified.
34 enum class FollowSymlink
35 {
38 };
39 
40 
41 namespace internal {
42 
44  const std::string& path,
45  const FollowSymlink follow)
46 {
47  struct ::stat s;
48 
49  switch (follow) {
51  if (::lstat(path.c_str(), &s) < 0) {
52  return ErrnoError("Failed to lstat '" + path + "'");
53  }
54  return s;
56  if (::stat(path.c_str(), &s) < 0) {
57  return ErrnoError("Failed to stat '" + path + "'");
58  }
59  return s;
60  }
61 
62  UNREACHABLE();
63 }
64 
65 } // namespace internal {
66 
67 inline bool islink(const std::string& path)
68 {
69  // By definition, you don't follow symlinks when trying
70  // to find whether a path is a link. If you followed it,
71  // it wouldn't ever be a link.
74  return s.isSome() && S_ISLNK(s->st_mode);
75 }
76 
77 
78 inline bool isdir(
79  const std::string& path,
81 {
82  Try<struct ::stat> s = internal::stat(path, follow);
83  return s.isSome() && S_ISDIR(s->st_mode);
84 }
85 
86 
87 inline bool isfile(
88  const std::string& path,
90 {
91  Try<struct ::stat> s = internal::stat(path, follow);
92  return s.isSome() && S_ISREG(s->st_mode);
93 }
94 
95 
96 // Returns the size in Bytes of a given file system entry. When
97 // applied to a symbolic link with `follow` set to
98 // `DO_NOT_FOLLOW_SYMLINK`, this will return the length of the entry
99 // name (strlen).
101  const std::string& path,
103 {
104  Try<struct ::stat> s = internal::stat(path, follow);
105  if (s.isError()) {
106  return Error(s.error());
107  }
108 
109  return Bytes(s->st_size);
110 }
111 
112 
114  const std::string& path,
116 {
117  Try<struct ::stat> s = internal::stat(path, follow);
118  if (s.isError()) {
119  return Error(s.error());
120  }
121 
122  return s->st_mtime;
123 }
124 
125 
127  const std::string& path,
129 {
130  Try<struct ::stat> s = internal::stat(path, follow);
131  if (s.isError()) {
132  return Error(s.error());
133  }
134 
135  return s->st_mode;
136 }
137 
138 
140  const std::string& path,
142 {
143  Try<struct ::stat> s = internal::stat(path, follow);
144  if (s.isError()) {
145  return Error(s.error());
146  }
147 
148  return s->st_dev;
149 }
150 
151 
153  const std::string& path,
155 {
156  Try<struct ::stat> s = internal::stat(path, follow);
157  if (s.isError()) {
158  return Error(s.error());
159  }
160 
161  if (!S_ISCHR(s->st_mode) && !S_ISBLK(s->st_mode)) {
162  return Error("Not a special file: " + path);
163  }
164 
165  return s->st_rdev;
166 }
167 
168 
170  const std::string& path,
172 {
173  Try<struct ::stat> s = internal::stat(path, follow);
174  if (s.isError()) {
175  return Error(s.error());
176  }
177 
178  return s->st_ino;
179 }
180 
181 
183  const std::string& path,
185 {
186  Try<struct ::stat> s = internal::stat(path, follow);
187  if (s.isError()) {
188  return Error(s.error());
189  }
190 
191  return s->st_uid;
192 }
193 
194 } // namespace stat {
195 
196 } // namespace os {
197 
198 #endif // __STOUT_OS_POSIX_STAT_HPP__
Try< uid_t > uid(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:182
bool isfile(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:87
Definition: errorbase.hpp:35
Try< Bytes > size(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:100
bool S_ISDIR(const int mode)
Definition: windows.hpp:206
bool islink(const std::string &path)
Definition: stat.hpp:67
Definition: try.hpp:34
bool S_ISREG(const int mode)
Definition: windows.hpp:212
Definition: errorbase.hpp:49
Try< long > mtime(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:113
Try< ino_t > inode(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:169
FollowSymlink
Definition: reparsepoint.hpp:35
Try< dev_t > dev(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:139
Try< dev_t > rdev(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:152
bool isSome() const
Definition: try.hpp:70
bool S_ISBLK(const int mode)
Definition: windows.hpp:230
Try< hashmap< std::string, uint64_t > > stat(const std::string &hierarchy, const std::string &cgroup, const std::string &file)
static Try error(const E &e)
Definition: try.hpp:42
#define UNREACHABLE()
Definition: unreachable.hpp:22
bool isError() const
Definition: try.hpp:71
bool isdir(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:78
bool S_ISLNK(const int mode)
Definition: windows.hpp:242
Try< mode_t > mode(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:126
bool S_ISCHR(const int mode)
Definition: windows.hpp:218
Definition: bytes.hpp:30
Try< struct::stat > stat(const std::string &path, const FollowSymlink follow)
Definition: stat.hpp:43