-
Notifications
You must be signed in to change notification settings - Fork 0
/
proc.cpp
243 lines (186 loc) · 5.45 KB
/
proc.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
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 <stdlib.h>
#include <string.h>
#include "proc.h"
#include "log.h"
#include "context.h"
#include "int64format.h"
static std::string uint64_to_str(uint64_t v)
{
char t[64];
snprintf(t, sizeof(t), "%"FINT64"u\n", v);
return std::string(t);
}
// Proc_Dir class implements.
int Proc_Dir::getattr(Log& logger, struct stat& stbuf)
{
stbuf.st_mode = S_IFDIR|S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
stbuf.st_atime = stbuf.st_ctime = stbuf.st_mtime = time(NULL);
return 0;
}
int Proc_Dir::opendir(Log& logger, ProcAbstract*& self)
{
self = (ProcAbstract*)this;
return 0;
}
int Proc_Dir::readdir(Log& logger, void *buf, fuse_fill_dir_t filler, off_t offset)
{
std::vector<std::string>::iterator it;
for(it = m_dirs.begin(); it!=m_dirs.end(); it++) {
filler(buf, (*it).c_str(), NULL, 0);
}
return 0;
}
// Proc_StringStream class implements.
int Proc_StringStream::getattr(Log& logger, struct stat& stbuf)
{
stbuf.st_mode = S_IFREG|S_IRUSR|S_IRGRP|S_IROTH;
stbuf.st_atime = stbuf.st_ctime = stbuf.st_mtime = time(NULL);
stbuf.st_size = m_string.size();
return 0;
}
int Proc_StringStream::read(Log& logger, char* buf, size_t size, off_t offset)
{
if(m_string.size()<size+offset) {
size = ((off_t)m_string.size()>offset)? m_string.size()-offset: 0;
}
if(size>0) memcpy(buf, m_string.c_str()+offset, size);
return size;
}
// Proc_StringStreamIO class implements.
int Proc_StringStreamIO::getattr(Log& logger, struct stat& stbuf)
{
Proc_StringStream::getattr(logger, stbuf);
stbuf.st_mode |= S_IWUSR;
return 0;
}
int Proc_StringStreamIO::write(Log& logger, const char* buf, size_t size, off_t offset)
{
// Can append or replace only.
if(offset==0) m_string.resize(0);
if((off_t)m_string.size()==offset) {
m_string.append(buf, size);
m_wrote += size;
return size;
}
return -EINVAL;
}
// Proc_CacheEnable class implements.
int Proc_CacheEnable::open(Log& logger, ProcAbstract*& self)
{
m_string = uint64_to_str(AUTOHTTPFSCONTEXTS.remote_attr().cache().enabled());
m_wrote = 0;
self = this;
return 0;
}
int Proc_CacheEnable::release(Log& logger)
{
if(m_wrote) {
int64_t mode = strtoll(m_string.c_str(), NULL, 10);
AUTOHTTPFSCONTEXTS.remote_attr().cache().enabled(mode);
logger(Log::NOTE, "Set cache::enable to %"FINT64"d\n", mode);
}
Proc_StringStream::release(logger);
return 0;
}
// Proc_CacheMaxEntries class implements.
int Proc_CacheMaxEntries::open(Log& logger, ProcAbstract*& self)
{
m_string = uint64_to_str(AUTOHTTPFSCONTEXTS.remote_attr().cache().max_entries());
m_wrote = 0;
self = this;
return 0;
}
int Proc_CacheMaxEntries::release(Log& logger)
{
if(m_wrote) {
int64_t size = strtoll(m_string.c_str(), NULL, 10);
AUTOHTTPFSCONTEXTS.remote_attr().cache().max_entries(size);
logger(Log::NOTE, "Set cache::max_entries to %"FINT64"d\n", size);
}
Proc_StringStream::release(logger);
return 0;
}
// Proc_CacheEntries class implements.
int Proc_CacheEntries::open(Log& logger, ProcAbstract*& self)
{
m_string = uint64_to_str(AUTOHTTPFSCONTEXTS.remote_attr().cache().size());
self = this;
return 0;
}
// Proc_CacheExpire class implements.
int Proc_CacheExpire::open(Log& logger, ProcAbstract*& self)
{
m_string = uint64_to_str(AUTOHTTPFSCONTEXTS.remote_attr().cache().expire());
m_wrote = 0;
self = this;
return 0;
}
int Proc_CacheExpire::release(Log& logger)
{
if(m_wrote) {
int64_t sec = strtoll(m_string.c_str(), NULL, 10);
AUTOHTTPFSCONTEXTS.remote_attr().cache().expire(sec);
logger(Log::NOTE, "Set cache::expire to %"FINT64"d\n", sec);
}
Proc_StringStream::release(logger);
return 0;
}
// Proc_LogLevel class implements.
int Proc_LogLevel::open(Log& logger, ProcAbstract*& self)
{
m_string = uint64_to_str(logger.loglevel());
m_wrote = 0;
self = this;
return 0;
}
int Proc_LogLevel::release(Log& logger)
{
if(m_wrote) {
int64_t lev = strtoll(m_string.c_str(), NULL, 10);
logger.loglevel((Log::LOGLEVEL)lev);
logger(Log::NOTE, "Set loglevel to %"FINT64"d\n", lev);
}
Proc_StringStream::release(logger);
return 0;
}
// Proc_Benchmark class implements.
int Proc_BenchmarkNull::getattr(Log& logger, struct stat& stbuf)
{
stbuf.st_mode = S_IFREG|S_IRUSR|S_IRGRP|S_IROTH;
stbuf.st_atime = stbuf.st_ctime = stbuf.st_mtime = time(NULL);
stbuf.st_size = m_size;
return 0;
}
int Proc_BenchmarkNull::open(Log& logger, ProcAbstract*& self)
{
self = this;
return 0;
}
int Proc_BenchmarkNull::read(Log& logger, char* buf, size_t size, off_t offset)
{
return size;
}
int Proc_Benchmark::open(Log& logger, ProcAbstract*& self)
{
self = this;
return 0;
}
int Proc_Benchmark::read(Log& logger, char* buf, size_t size, off_t offset)
{
memset(buf, offset & 0xff, size);
return size;
}
// vim: sw=2 sts=2 ts=4 expandtab :