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 #include <stout/os/int_fd.hpp>
26 
27 
28 namespace os {
29 
30 namespace stat {
31 
32 // Specify whether symlink path arguments should be followed or
33 // not. APIs in the os::stat family that take a FollowSymlink
34 // argument all provide FollowSymlink::FOLLOW_SYMLINK as the default value,
35 // so they will follow symlinks unless otherwise specified.
36 enum class FollowSymlink
37 {
40 };
41 
42 
43 namespace internal {
44 
46  const std::string& path,
47  const FollowSymlink follow)
48 {
49  struct ::stat s;
50 
51  switch (follow) {
53  if (::lstat(path.c_str(), &s) < 0) {
54  return ErrnoError("Failed to lstat '" + path + "'");
55  }
56  return s;
58  if (::stat(path.c_str(), &s) < 0) {
59  return ErrnoError("Failed to stat '" + path + "'");
60  }
61  return s;
62  }
63 
64  UNREACHABLE();
65 }
66 
67 
68 inline Try<struct ::stat> stat(const int_fd fd)
69 {
70  struct ::stat s;
71 
72  if (::fstat(fd, &s) < 0) {
73  return ErrnoError();
74  }
75  return s;
76 }
77 
78 } // namespace internal {
79 
80 inline bool islink(const std::string& path)
81 {
82  // By definition, you don't follow symlinks when trying
83  // to find whether a path is a link. If you followed it,
84  // it wouldn't ever be a link.
87  return s.isSome() && S_ISLNK(s->st_mode);
88 }
89 
90 
91 inline bool isdir(
92  const std::string& path,
94 {
95  Try<struct ::stat> s = internal::stat(path, follow);
96  return s.isSome() && S_ISDIR(s->st_mode);
97 }
98 
99 
100 // TODO(andschwa): Share logic with other overload.
101 inline bool isdir(const int_fd fd)
102 {
104  return s.isSome() && S_ISDIR(s->st_mode);
105 }
106 
107 
108 inline bool isfile(
109  const std::string& path,
111 {
112  Try<struct ::stat> s = internal::stat(path, follow);
113  return s.isSome() && S_ISREG(s->st_mode);
114 }
115 
116 
117 // Returns the size in Bytes of a given file system entry. When
118 // applied to a symbolic link with `follow` set to
119 // `DO_NOT_FOLLOW_SYMLINK`, this will return the length of the entry
120 // name (strlen).
122  const std::string& path,
124 {
125  Try<struct ::stat> s = internal::stat(path, follow);
126  if (s.isError()) {
127  return Error(s.error());
128  }
129 
130  return Bytes(s->st_size);
131 }
132 
133 
134 // TODO(andschwa): Share logic with other overload.
135 inline Try<Bytes> size(const int_fd fd)
136 {
138  if (s.isError()) {
139  return Error(s.error());
140  }
141 
142  return Bytes(s->st_size);
143 }
144 
145 
147  const std::string& path,
149 {
150  Try<struct ::stat> s = internal::stat(path, follow);
151  if (s.isError()) {
152  return Error(s.error());
153  }
154 
155  return s->st_mtime;
156 }
157 
158 
160  const std::string& path,
162 {
163  Try<struct ::stat> s = internal::stat(path, follow);
164  if (s.isError()) {
165  return Error(s.error());
166  }
167 
168  return s->st_mode;
169 }
170 
171 
173  const std::string& path,
175 {
176  Try<struct ::stat> s = internal::stat(path, follow);
177  if (s.isError()) {
178  return Error(s.error());
179  }
180 
181  return s->st_dev;
182 }
183 
184 
186  const std::string& path,
188 {
189  Try<struct ::stat> s = internal::stat(path, follow);
190  if (s.isError()) {
191  return Error(s.error());
192  }
193 
194  if (!S_ISCHR(s->st_mode) && !S_ISBLK(s->st_mode)) {
195  return Error("Not a special file: " + path);
196  }
197 
198  return s->st_rdev;
199 }
200 
201 
203  const std::string& path,
205 {
206  Try<struct ::stat> s = internal::stat(path, follow);
207  if (s.isError()) {
208  return Error(s.error());
209  }
210 
211  return s->st_ino;
212 }
213 
214 
216  const std::string& path,
218 {
219  Try<struct ::stat> s = internal::stat(path, follow);
220  if (s.isError()) {
221  return Error(s.error());
222  }
223 
224  return s->st_uid;
225 }
226 
227 } // namespace stat {
228 
229 } // namespace os {
230 
231 #endif // __STOUT_OS_POSIX_STAT_HPP__
Definition: path.hpp:26
Try< uid_t > uid(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:215
bool isfile(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:108
Definition: errorbase.hpp:36
Try< Bytes > size(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:121
bool S_ISDIR(const int mode)
Definition: windows.hpp:200
bool islink(const std::string &path)
Definition: stat.hpp:80
Definition: check.hpp:33
bool S_ISREG(const int mode)
Definition: windows.hpp:206
Definition: errorbase.hpp:50
Try< long > mtime(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:146
Definition: posix_signalhandler.hpp:23
Try< ino_t > inode(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:202
FollowSymlink
Definition: reparsepoint.hpp:35
Try< dev_t > dev(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:172
Try< dev_t > rdev(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:185
bool isSome() const
Definition: try.hpp:70
Try< struct::stat > stat(const int_fd fd)
Definition: stat.hpp:68
bool S_ISBLK(const int mode)
Definition: windows.hpp:224
static Try error(const E &e)
Definition: try.hpp:42
#define UNREACHABLE()
Definition: unreachable.hpp:22
Definition: attributes.hpp:24
bool isError() const
Definition: try.hpp:71
bool isdir(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:91
bool S_ISLNK(const int mode)
Definition: windows.hpp:236
Try< mode_t > mode(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:159
bool S_ISCHR(const int mode)
Definition: windows.hpp:212
Definition: bytes.hpp:30
int int_fd
Definition: int_fd.hpp:35
Try< struct::stat > stat(const std::string &path, const FollowSymlink follow)
Definition: stat.hpp:45