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_blocks * buf.f_frsize);
39 }
40 
41 
42 // Returns relative disk usage of the file system that the given path
43 // is mounted at.
44 inline Try<double> usage(const std::string& path = "/")
45 {
46  struct statvfs buf;
47  if (statvfs(path.c_str(), &buf) < 0) {
48  return ErrnoError("Error invoking statvfs on '" + path + "'");
49  }
50  return (double) (buf.f_blocks - buf.f_bfree) / buf.f_blocks;
51 }
52 
53 
55  const std::string& original,
56  const std::string& link)
57 {
58  if (::symlink(original.c_str(), link.c_str()) < 0) {
59  return ErrnoError();
60  }
61  return Nothing();
62 }
63 
64 
65 // Returns a list of all files matching the given pattern. On POSIX builds this
66 // is just a wrapper on os::glob().
67 //
68 // This function was added is because glob() is not available on Windows, and we
69 // are not making use of any of the heavyweight features of ::glob() anyway.
70 //
71 // list() is meant to be a lightweight alternative to glob() - the only
72 // wildcards it should be used with are `?` and `*`, and only when they appear
73 // at the tail end of `pattern` (e.g. `/root/dir/subdir/*.txt` or
74 // `/root/dir/subdir/file?.txt`.
75 inline Try<std::list<std::string>> list(const std::string& pattern)
76 {
77  return os::glob(pattern);
78 }
79 
80 } // namespace fs {
81 
82 #endif // __STOUT_POSIX_FS_HPP__
Definition: path.hpp:26
Definition: nothing.hpp:16
Definition: check.hpp:33
Try< Nothing > symlink(const std::string &original, const std::string &link)
Definition: fs.hpp:54
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:75
Try< double > usage(const std::string &path="/")
Definition: fs.hpp:44
Definition: bytes.hpp:30
Try< std::list< std::string > > glob(const std::string &pattern)
Definition: os.hpp:260