MongoDB  2.7.0
paths.h
1 // @file paths.h
2 // file paths and directory handling
3 
4 /* Copyright 2010 10gen Inc.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #pragma once
20 
21 #include <boost/filesystem/path.hpp>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 
26 #include "mongo/util/log.h"
27 #include "mongo/util/mongoutils/str.h"
28 
29 #include "mongo/db/storage_options.h"
30 
31 namespace mongo {
32 
33  using namespace mongoutils;
34 
38  struct RelativePath {
39  string _p;
40 
41  bool empty() const { return _p.empty(); }
42 
43  static RelativePath fromRelativePath(const std::string& f) {
44  RelativePath rp;
45  rp._p = f;
46  return rp;
47  }
48 
50  static RelativePath fromFullPath(boost::filesystem::path f) {
51  boost::filesystem::path dbp(storageGlobalParams.dbpath); // normalizes / and backslash
52  string fullpath = f.string();
53  string relative = str::after(fullpath, dbp.string());
54  if( relative.empty() ) {
55  log() << "warning file is not under db path? " << fullpath << ' ' << dbp.string();
56  RelativePath rp;
57  rp._p = fullpath;
58  return rp;
59  }
60  if( str::startsWith(relative, "/") || str::startsWith(relative, "\\") ) {
61  relative.erase(0, 1);
62  }
63  RelativePath rp;
64  rp._p = relative;
65  return rp;
66  }
67 
68  string toString() const { return _p; }
69 
70  bool operator!=(const RelativePath& r) const { return _p != r._p; }
71  bool operator==(const RelativePath& r) const { return _p == r._p; }
72  bool operator<(const RelativePath& r) const { return _p < r._p; }
73 
74  string asFullPath() const {
75  boost::filesystem::path x(storageGlobalParams.dbpath);
76  x /= _p;
77  return x.string();
78  }
79 
80  };
81 
82  inline dev_t getPartition(const string& path){
83  struct stat stats;
84 
85  if (stat(path.c_str(), &stats) != 0){
86  uasserted(13646, str::stream() << "stat() failed for file: " << path << " " << errnoWithDescription());
87  }
88 
89  return stats.st_dev;
90  }
91 
92  inline bool onSamePartition(const string& path1, const string& path2){
93  dev_t dev1 = getPartition(path1);
94  dev_t dev2 = getPartition(path2);
95 
96  return dev1 == dev2;
97  }
98 
99  void flushMyDirectory(const boost::filesystem::path& file);
100 
101  boost::filesystem::path ensureParentDirCreated(const boost::filesystem::path& p);
102 
103 }
the idea here is to make one liners easy.
Definition: str.h:48
boost::filesystem::path ensureParentDirCreated(const boost::filesystem::path &p)
Aliases for Win32 CRT functions.
Definition: file_allocator.cpp:69
this is very much like a boost::path.
Definition: paths.h:38
static RelativePath fromFullPath(boost::filesystem::path f)
from a full path
Definition: paths.h:50
LogstreamBuilder log()
Returns a LogstreamBuilder for logging a message with LogSeverity::Log().
Definition: log.h:69