Apache Mesos
fs.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_POSIX_FS_HPP__
14 #define __STOUT_POSIX_FS_HPP__
15 
16 #include <unistd.h> // For symlink.
17 #include <sys/statvfs.h>
18 
19 #include <string>
20 
21 #include <stout/bytes.hpp>
22 #include <stout/error.hpp>
23 #include <stout/nothing.hpp>
24 #include <stout/os.hpp>
25 #include <stout/try.hpp>
26 
27 // TODO(bmahler): Merge available() and usage() into df() that returns
28 // a struct, and move this back into os.hpp.
29 namespace fs {
30 
31 // Returns the total disk size in bytes.
32 inline Try<Bytes> size(const std::string& path = "/")
33 {
34  struct statvfs buf;
35  if (::statvfs(path.c_str(), &buf) < 0) {
36  return ErrnoError();
37  }
38  return Bytes(buf.f_frsize) * buf.f_blocks;
39 }
40 
41 
42 // Returns the amount of disk space used in bytes.
43 inline Try<Bytes> used(const std::string& path = "/")
44 {
45  struct statvfs buf;
46  if (::statvfs(path.c_str(), &buf) < 0) {
47  return ErrnoError();
48  }
49  return Bytes(buf.f_frsize) * (buf.f_blocks - buf.f_bfree);
50 }
51 
52 
53 // Returns relative disk usage of the file system that the given path
54 // is mounted at.
55 inline Try<double> usage(const std::string& path = "/")
56 {
57  struct statvfs buf;
58  if (statvfs(path.c_str(), &buf) < 0) {
59  return ErrnoError("Error invoking statvfs on '" + path + "'");
60  }
61  return (double) (buf.f_blocks - buf.f_bfree) / buf.f_blocks;
62 }
63 
64 
66  const std::string& original,
67  const std::string& link)
68 {
69  if (::symlink(original.c_str(), link.c_str()) < 0) {
70  return ErrnoError();
71  }
72  return Nothing();
73 }
74 
75 
76 // Returns a list of all files matching the given pattern. On POSIX builds this
77 // is just a wrapper on os::glob().
78 //
79 // This function was added is because glob() is not available on Windows, and we
80 // are not making use of any of the heavyweight features of ::glob() anyway.
81 //
82 // list() is meant to be a lightweight alternative to glob() - the only
83 // wildcards it should be used with are `?` and `*`, and only when they appear
84 // at the tail end of `pattern` (e.g. `/root/dir/subdir/*.txt` or
85 // `/root/dir/subdir/file?.txt`.
86 inline Try<std::list<std::string>> list(const std::string& pattern)
87 {
88  return os::glob(pattern);
89 }
90 
91 } // namespace fs {
92 
93 #endif // __STOUT_POSIX_FS_HPP__
Definition: path.hpp:29
Definition: nothing.hpp:16
Definition: check.hpp:33
Try< Nothing > symlink(const std::string &original, const std::string &link)
Definition: fs.hpp:65
Definition: errorbase.hpp:50
Try< Bytes > size(const std::string &path="/")
Definition: fs.hpp:32
Definition: fs.hpp:29
Try< std::list< std::string > > list(const std::string &pattern)
Definition: fs.hpp:86
Try< Bytes > used(const std::string &path="/")
Definition: fs.hpp:43
Try< double > usage(const std::string &path="/")
Definition: fs.hpp:55
Definition: bytes.hpp:30
Try< std::list< std::string > > glob(const std::string &pattern)
Definition: os.hpp:239