1 // @file paths.h
2 // file paths and directory handling
3 
4 
5 /**
6  *    Copyright (C) 2018-present MongoDB, Inc.
7  *
8  *    This program is free software: you can redistribute it and/or modify
9  *    it under the terms of the Server Side Public License, version 1,
10  *    as published by MongoDB, Inc.
11  *
12  *    This program is distributed in the hope that it will be useful,
13  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *    Server Side Public License for more details.
16  *
17  *    You should have received a copy of the Server Side Public License
18  *    along with this program. If not, see
19  *    <http://www.mongodb.com/licensing/server-side-public-license>.
20  *
21  *    As a special exception, the copyright holders give permission to link the
22  *    code of portions of this program with the OpenSSL library under certain
23  *    conditions as described in each individual source file and distribute
24  *    linked combinations including the program with the OpenSSL library. You
25  *    must comply with the Server Side Public License in all respects for
26  *    all of the code used other than as permitted herein. If you modify file(s)
27  *    with this exception, you may extend this exception to your version of the
28  *    file(s), but you are not obligated to do so. If you do not wish to do so,
29  *    delete this exception statement from your version. If you delete this
30  *    exception statement from all source files in the program, then also delete
31  *    it in the license file.
32  */
33 
34 #pragma once
35 
36 #include <boost/filesystem/path.hpp>
37 #include <fcntl.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40 
41 #include "mongo/util/mongoutils/str.h"
42 
43 #include "mongo/db/storage/storage_options.h"
44 
45 namespace mongo {
46 
47 using namespace mongoutils;
48 
49 /** this is very much like a boost::path.  however, we define a new type to get some type
50     checking.  if you want to say 'my param MUST be a relative path", use this.
51 */
52 struct RelativePath {
53     std::string _p;
54 
emptyRelativePath55     bool empty() const {
56         return _p.empty();
57     }
58 
fromRelativePathRelativePath59     static RelativePath fromRelativePath(const std::string& f) {
60         RelativePath rp;
61         rp._p = f;
62         return rp;
63     }
64 
65     /**
66      * Returns path relative to 'dbpath' from a full path 'f'.
67      */
68     static RelativePath fromFullPath(boost::filesystem::path dbpath, boost::filesystem::path f);
69 
toStringRelativePath70     std::string toString() const {
71         return _p;
72     }
73 
74     bool operator!=(const RelativePath& r) const {
75         return _p != r._p;
76     }
77     bool operator==(const RelativePath& r) const {
78         return _p == r._p;
79     }
80     bool operator<(const RelativePath& r) const {
81         return _p < r._p;
82     }
83 
asFullPathRelativePath84     std::string asFullPath() const {
85         boost::filesystem::path x(storageGlobalParams.dbpath);
86         x /= _p;
87         return x.string();
88     }
89 };
90 
91 dev_t getPartition(const std::string& path);
92 
onSamePartition(const std::string & path1,const std::string & path2)93 inline bool onSamePartition(const std::string& path1, const std::string& path2) {
94     dev_t dev1 = getPartition(path1);
95     dev_t dev2 = getPartition(path2);
96 
97     return dev1 == dev2;
98 }
99 
100 void flushMyDirectory(const boost::filesystem::path& file);
101 
102 boost::filesystem::path ensureParentDirCreated(const boost::filesystem::path& p);
103 }
104