File: dir.cpp

package info (click to toggle)
bayonne 2.3.2-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 11,180 kB
  • ctags: 3,662
  • sloc: cpp: 38,791; sh: 9,323; ansic: 1,391; makefile: 485; perl: 471; java: 405; cs: 402; php: 354; python: 293
file content (320 lines) | stat: -rw-r--r-- 6,143 bytes parent folder | download
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright (C) 2005 Open Source Telecom Corporation.
//  
// 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 2 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, write to the Free Software 
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#include <engine.h>
#include <cc++/slog.h>
#include <cc++/socket.h>

#ifndef	WIN32
#include "private.h"

#include <sys/types.h>
#include <sys/param.h>
#ifdef HAVE_SYS_MOUNT_H
#include <sys/mount.h>
#endif
#ifdef HAVE_SYS_STATVFS_H
#include <sys/statvfs.h>
#endif
#ifdef HAVE_SYS_VFS_H
#include <sys/vfs.h>
#endif

#ifdef HAVE_SYS_STATVFS_H
#define STATFS statvfs
#else
#define STATFS statfs
#endif

#endif

namespace moduleXMLRPC {
using namespace ost;
using namespace std;

typedef	Bayonne::rpcint_t rpcint_t;
typedef	rpcint_t rpcbool_t;

static bool isValid(const char *path)
{
	if(!path)
		return false;

	if(!*path)
		return false;

	if(*path == '/' || *path == '\\' || *path == '.')
		return false;

	if(strstr("..", path))
		return false;

#ifdef	WIN32
	if(path[1] == ':')
		return false;
#endif
	return true;
}

static void purgedir(const char *dir)
{
	const char *cp;
	if(isDir(dir) && canAccess(dir))
	{
		DirTree dt(dir, 16);
		while(NULL != (cp = dt.getPath()))
			remove(cp);
		remove(dir);
	}
}

static void dir_version(BayonneRPC *rpc)
{
	if(rpc->getCount())
	{
		rpc->sendFault(3, "Invalid parameters");
		return;
	}
	rpc->buildResponse("(ii)", 
		"current", (rpcint_t)1,
		"prior", (rpcint_t)0);
}

static void dir_test(BayonneRPC *rpc)
{
	const char *path;

	if(!rpc->transport.authorized)
	{
		rpc->transportFault(401, "Not Authorized");
		return;
	}

    if(rpc->getCount() != 1)
    {
        rpc->sendFault(3, "Invalid parameters");
        return;
    }

	path = rpc->getIndexed(1);
	if(!isValid(path))
	{
		rpc->sendFault(4, "Invalid parameter value");
		return;
	}

	rpc->buildResponse("b", (rpcbool_t)isDir(path));
}

static void dir_list(BayonneRPC *rpc)
{
	Dir dir;
	const char *path;
	char pbuf[128];
	const char *cp;

	if(!rpc->transport.authorized)
	{
		rpc->transportFault(401, "Not Authorized");
		return;
	}

    if(rpc->getCount() != 1)
    {
        rpc->sendFault(3, "Invalid parameters");
        return;
    }

	path = rpc->getIndexed(1);
	if(!isValid(path))
	{
		rpc->sendFault(4, "Invalid parameter value");
		return;
	}

	if(!isDir(path) || !canAccess(path))
	{
		rpc->buildResponse("[]");
		return;
	}

	rpc->buildResponse("[");
	dir.open(path);
	while(NULL != (cp = dir.getName()))
	{
		if(*cp == '.')
			continue;

		snprintf(pbuf, sizeof(pbuf), "%s/%s", path, cp);
		rpc->buildResponse("!s", pbuf);
	}
	dir.close();

	rpc->buildResponse("]");
}

static void dir_create(BayonneRPC *rpc)
{
	const char *path;

	if(!rpc->transport.authorized)
	{
		rpc->transportFault(401, "Not Authorized");
		return;
	}

    if(rpc->getCount() != 1)
    {
        rpc->sendFault(3, "Invalid parameters");
        return;
    }

	path = rpc->getIndexed(1);
	if(!isValid(path))
	{
		rpc->sendFault(4, "Invalid parameter value");
		return;
	}

	Dir::create(path);
	rpc->buildResponse("b", (rpcbool_t)isDir(path));
}


static void dir_remove(BayonneRPC *rpc)
{
	const char *path;
	bool result = false;

	if(!rpc->transport.authorized)
	{
		rpc->transportFault(401, "Not Authorized");
		return;
	}

    if(rpc->getCount() != 1)
    {
        rpc->sendFault(3, "Invalid parameters");
        return;
    }

	path = rpc->getIndexed(1);
	if(!isValid(path))
	{
		rpc->sendFault(4, "Invalid parameter value");
		return;
	}

	if(isDir(path))
	{
		purgedir(path);
		result = true;
    }
	rpc->buildResponse("b", (rpcbool_t)result);
}

static void dir_space(BayonneRPC *rpc)
{
	const char *path;
	rpcint_t result = 0;

	if(!rpc->transport.authorized)
	{
		rpc->transportFault(401, "Not Authorized");
		return;
	}

    if(rpc->getCount() != 1)
    {
        rpc->sendFault(3, "Invalid parameters");
        return;
    }

	path = rpc->getIndexed(1);
	if(!isValid(path))
	{
		rpc->sendFault(4, "Invalid parameter value");
		return;
	}

	if(isDir(path))
	{
#ifdef	WIN32			
		ULARGE_INTEGER bavail, btotal, bfree;
		GetDiskFreeSpaceEx(path, &bavail, &btotal, &bfree);
		result = ((bavail / 1024l) / 1024l);
#else
		struct STATFS fs;
		if(0 == STATFS(path, &fs))
			result = (fs.f_bfree * (fs.f_bsize / 1024l) / 1024l);
	}
#endif
	rpc->buildResponse("i", result);
}

static void dir_move(BayonneRPC *rpc)
{
	const char *path1, *path2;
	bool result = false;

	if(!rpc->transport.authorized)
	{
		rpc->transportFault(401, "Not Authorized");
		return;
	}

    if(rpc->getCount() != 2)
    {
        rpc->sendFault(3, "Invalid parameters");
        return;
    }

	path1 = rpc->getIndexed(1);
	path2 = rpc->getIndexed(2);
	if(!isValid(path1) || !isValid(path2))
	{
		rpc->sendFault(4, "Invalid parameter value");
		return;
	}

	if(isDir(path1))
		if(!rename(path1, path2))
			result = true;

	rpc->buildResponse("b", (rpcbool_t)result);
}


static Bayonne::RPCDefine dispatch[] = {
	{"version", dir_version,
		"API version level of dir rpc subsystem", "struct"},
	{"test", dir_test,
		"Test for specified directory path", "boolean, string"},
    {"list", dir_list,
        "List files for specified directory path", "array, string"},
	{"move", dir_move,
		"Move specified directory path", "boolean, string, string"},
	{"remove", dir_remove,
		"Remove specified directory tree", "boolean, string"},
	{"create", dir_create,
		"Create specified subdirectory path", "boolean, string"},
	{"space", dir_space,
		"Return space available in megabytes", "int, string"},
	{NULL, NULL, NULL, NULL}};

static Bayonne::RPCNode xmlrpc_dir("dir", dispatch);

}; // namespace