00001 // @file paths.h 00002 // file paths and directory handling 00003 00004 /* Copyright 2010 10gen Inc. 00005 * 00006 * Licensed under the Apache License, Version 2.0 (the "License"); 00007 * you may not use this file except in compliance with the License. 00008 * You may obtain a copy of the License at 00009 * 00010 * http://www.apache.org/licenses/LICENSE-2.0 00011 * 00012 * Unless required by applicable law or agreed to in writing, software 00013 * distributed under the License is distributed on an "AS IS" BASIS, 00014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 * See the License for the specific language governing permissions and 00016 * limitations under the License. 00017 */ 00018 00019 #pragma once 00020 00021 #include "mongoutils/str.h" 00022 00023 using namespace mongoutils; 00024 00025 namespace mongo { 00026 00027 extern string dbpath; 00028 00032 struct RelativePath { 00033 string _p; 00034 00035 bool empty() const { return _p.empty(); } 00036 00037 static RelativePath fromRelativePath(string f) { 00038 RelativePath rp; 00039 rp._p = f; 00040 return rp; 00041 } 00042 00044 static RelativePath fromFullPath(path f) { 00045 path dbp(dbpath); // normalizes / and backslash 00046 string fullpath = f.string(); 00047 string relative = str::after(fullpath, dbp.string()); 00048 if( relative.empty() ) { 00049 log() << "warning file is not under db path? " << fullpath << ' ' << dbp.string() << endl; 00050 RelativePath rp; 00051 rp._p = fullpath; 00052 return rp; 00053 } 00054 /*uassert(13600, 00055 str::stream() << "file path is not under the db path? " << fullpath << ' ' << dbpath, 00056 relative != fullpath);*/ 00057 if( str::startsWith(relative, "/") || str::startsWith(relative, "\\") ) { 00058 relative.erase(0, 1); 00059 } 00060 RelativePath rp; 00061 rp._p = relative; 00062 return rp; 00063 } 00064 00065 string toString() const { return _p; } 00066 00067 bool operator!=(const RelativePath& r) const { return _p != r._p; } 00068 bool operator==(const RelativePath& r) const { return _p == r._p; } 00069 bool operator<(const RelativePath& r) const { return _p < r._p; } 00070 00071 string asFullPath() const { 00072 path x(dbpath); 00073 x /= _p; 00074 return x.string(); 00075 } 00076 00077 }; 00078 00079 }