forked from ax003d/gridfs-fuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
88 lines (77 loc) · 1.84 KB
/
utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* Copyright 2009 Michael Stephens
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __UTILS_H
#define __UTILS_H
#include <ctime>
#include <string>
#include <cstring>
inline const char* fuse_to_mongo_path(const char* path)
{
if(path[0] == '/') {
return path + 1;
} else {
return path;
}
}
inline const bool is_leaf(const char* path) {
int pp = -1;
int sp = -1;
for(int i=0; i<strlen(path); i++) {
if(path[i] == '/') sp = i;
if(path[i] == '.') pp = i;
}
return pp > sp;
}
inline const int path_depth(const char* path) {
int sc = 0;
for(int i=0; i<strlen(path); i++) {
if(path[i] == '/') sc++;
}
return sc;
}
inline time_t mongo_time_to_unix_time(unsigned long long mtime)
{
return mtime / 1000;
}
inline time_t unix_time_to_mongo_time(unsigned long long utime)
{
return utime * 1000;
}
inline time_t mongo_time()
{
return unix_time_to_mongo_time(time(NULL));
}
inline std::string namespace_xattr(const std::string name)
{
#ifdef __linux__
return "user." + name;
#else
return name;
#endif
}
inline const char* unnamespace_xattr(const char* name) {
#ifdef __linux__
if(std::strstr(name, "user.") == name) {
return name + 5;
} else {
return NULL;
}
#else
return name;
#endif
}
#endif