-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremoteattr.cpp
119 lines (104 loc) · 3.34 KB
/
remoteattr.cpp
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
Copyright 2010 Toshiyuki Terashita.
fuse-autohttpfs is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This software 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this software. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include "remoteattr.h"
#include "curlaccessor.h"
#include "filestat.h"
#include "ext/time_iso8601.h"
// RemoteAttr class implements.
void RemoteAttr::store(UrlStat& stat, const char* path, mode_t mode, std::string x_filestat, uint64_t content_length)
{
if(!x_filestat.empty()) {
try {
FileStat fs(x_filestat);
stat = UrlStat(fs.mode, fs.size, fs.mtime);
m_cache.add(path, stat);
}
catch(std::string e) {
m_cache.add(path, stat);
throw e;
}
} else {
stat = UrlStat(mode, content_length, time(NULL));
m_cache.add(path, stat);
}
}
int RemoteAttr::get_attr(Log& logger, const char* path, UrlStat& stat)
{
// check 'root' directory.
if(strcmp(path, "/")==0) {
stat = UrlStat(S_IFDIR);
return 0;
}
// check dot.path.
if(strncmp(path, "/.", 2)==0) {
return -ENOENT;
}
// check cache.
if(m_cache.find(path, stat)) {
TimeIso8601 t(stat.mtime);
logger(Log::DEBUG, " RemoteAttr::get_attr(%s:FIND): %05o %s\n", path, stat.mode, ((std::string)t).c_str());
return 0;
}
// challenge "path/" to directory.
{
CurlAccessor ca(path, true);
ca.add_header("Accept", "text/json");
int res = ca.head(logger);
if((res==200) || (res==403)) {
// path should be directory.
try{ store(stat, path, S_IFDIR, ca.x_filestat(), ca.content_length()); }
catch(std::string e) {
logger(Log::WARN, " RemoteAttr::get_attr(%s:DIR): %s\n", path, e.c_str());
}
return 0;
}
}
// challenge "path" to regular file.
{
CurlAccessor ca(path);
ca.add_header("Accept", "text/json");
int res = ca.head(logger);
if(res==200) {
// path is regular file.
try{ store(stat, path, S_IFREG, ca.x_filestat(), ca.content_length()); }
catch(std::string e) {
logger(Log::WARN, " RemoteAttr::get_attr(%s:REG): %s\n", path, e.c_str());
}
return 0;
}
}
// challenge "path" without follow location to use without mod_index_json.
{
CurlAccessor ca(path, false, false);
int res = ca.head(logger);
if(res==200) {
// path is regular file.
try{ store(stat, path, S_IFREG, ca.x_filestat(), ca.content_length()); }
catch(std::string e) {
logger(Log::WARN, " RemoteAttr::get_attr(%s:REG): %s\n", path, e.c_str());
}
return 0;
} else if(res==301) {
// path should be directory.
try{ store(stat, path, S_IFDIR, ca.x_filestat(), ca.content_length()); }
catch(std::string e) {
logger(Log::WARN, " RemoteAttr::get_attr(%s:DIR): %s\n", path, e.c_str());
}
return 0;
}
}
return -ENOENT;
}
// vim: sw=2 sts=2 ts=4 expandtab :