Apache Mesos
find.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_FIND_HPP__
14 #define __STOUT_OS_FIND_HPP__
15 
16 #include <stout/foreach.hpp>
17 #include <list>
18 #include <string>
19 
20 #include <stout/error.hpp>
21 #include <stout/path.hpp>
22 #include <stout/try.hpp>
23 
24 #include <stout/os/ls.hpp>
25 #include <stout/os/stat.hpp>
26 
27 
28 namespace os {
29 
30 // Return the list of file paths that match the given pattern by recursively
31 // searching the given directory. A match is successful if the pattern is a
32 // substring of the file name.
33 // NOTE: Directory path should not end with '/'.
34 // NOTE: Symbolic links are not followed.
35 // TODO(vinod): Support regular expressions for pattern.
36 // TODO(vinod): Consider using ftw or a non-recursive approach.
38  const std::string& directory,
39  const std::string& pattern)
40 {
41  std::list<std::string> results;
42 
43  if (!stat::isdir(directory)) {
44  return Error("'" + directory + "' is not a directory");
45  }
46 
47  Try<std::list<std::string>> entries = ls(directory);
48  if (entries.isSome()) {
49  foreach (const std::string& entry, entries.get()) {
50  std::string path = path::join(directory, entry);
51  // If it's a directory, recurse.
52  if (stat::isdir(path) && !stat::islink(path)) {
53  Try<std::list<std::string>> matches = find(path, pattern);
54  if (matches.isError()) {
55  return matches;
56  }
57  foreach (const std::string& match, matches.get()) {
58  results.push_back(match);
59  }
60  } else {
61  if (entry.find(pattern) != std::string::npos) {
62  results.push_back(path); // Matched the file pattern!
63  }
64  }
65  }
66  }
67 
68  return results;
69 }
70 
71 } // namespace os {
72 
73 #endif // __STOUT_OS_FIND_HPP__
Definition: path.hpp:26
Definition: errorbase.hpp:35
T & get()&
Definition: try.hpp:73
bool islink(const std::string &path)
Definition: stat.hpp:67
Definition: check.hpp:33
Definition: posix_signalhandler.hpp:23
std::string join(const std::string &path1, const std::string &path2, const char _separator=os::PATH_SEPARATOR)
Definition: path.hpp:56
bool isSome() const
Definition: try.hpp:70
bool isError() const
Definition: try.hpp:71
bool isdir(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:78
Try< std::list< std::string > > find(const std::string &directory, const std::string &pattern)
Definition: find.hpp:37
Try< std::list< std::string > > ls(const std::string &directory)
Definition: ls.hpp:29