File: dapfs_dap.cc

package info (click to toggle)
dnprogs 2.52
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,644 kB
  • ctags: 4,021
  • sloc: ansic: 26,737; cpp: 10,666; makefile: 832; sh: 537; awk: 13
file content (561 lines) | stat: -rw-r--r-- 12,162 bytes parent folder | download | duplicates (2)
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/******************************************************************************
    (c) 2005-2008 Christine Caulfield             christine.caulfield@gmail.com

    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
    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.
 ******************************************************************************
 */

#define _FILE_OFFSET_BITS 64
#define FUSE_USE_VERSION 22

#include <fuse.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <syslog.h>
#include <dirent.h>
#include <errno.h>
#include <limits.h>
#include <sys/statfs.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <netdnet/dn.h>
#include <netdnet/dnetdb.h>
#include "dn_endian.h"
#include "connection.h"
#include "protocol.h"
#include "dapfs_dap.h"
extern "C" {
#include "filenames.h"
#include "dapfs.h"
}

// CC This isn't going to work!
static dap_connection conn(debuglevel);

static int dap_connect(dap_connection &c)
{
	char dirname[256] = {'\0'};
	if (!c.connect(prefix, dap_connection::FAL_OBJECT, dirname))
	{
		return -ENOTCONN;
	}

	// Exchange config messages
	if (!c.exchange_config())
	{
		fprintf(stderr, "Error in config: %s\n", c.get_error());
		return -ENOTCONN;
	}
	return 0;
}

static void restart_dap()
{
	syslog(LOG_INFO, "Restarting dapfs connection\n");
	conn.close();
	dap_connect(conn);
}

int get_object_info(char *command, char *reply)
{
	dap_connection dummy(0); // So we can use parse()
	struct accessdata_dn accessdata;
	char node[BUFLEN], filespec[VMSNAME_LEN];
	int sockfd;
	int status;
	struct nodeent	*np;
	struct sockaddr_dn sockaddr;
	fd_set fds;
	struct timeval tv;

	memset(&accessdata, 0, sizeof(accessdata));
	memset(&sockaddr, 0, sizeof(sockaddr));

	/* This should always succeed, otherwise we would never get here */
	if (!dummy.parse(prefix, accessdata, node, filespec))
		return -1;


	// Try very hard to get the local username for proxy access.
	// This code copied from libdap for consistency
	char *local_user = cuserid(NULL);
	if (!local_user || local_user == (char *)0xffffffff)
		local_user = getenv("LOGNAME");

	if (!local_user) local_user = getenv("USER");
	if (local_user)
	{
		strcpy((char *)accessdata.acc_acc, local_user);
		accessdata.acc_accl = strlen((char *)accessdata.acc_acc);
		makeupper((char *)accessdata.acc_acc);
	}
	else
		accessdata.acc_acc[0] = '\0';

	np = getnodebyname(node);

	if ((sockfd=socket(AF_DECnet, SOCK_SEQPACKET, DNPROTO_NSP)) == -1)
	{
		return -1;
	}

	// Provide access control and proxy information
	if (setsockopt(sockfd, DNPROTO_NSP, SO_CONACCESS, &accessdata,
		       sizeof(accessdata)) < 0)
	{
		return -1;
	}

	/* Open up object number 0 with the name of the task */
	sockaddr.sdn_family   = AF_DECnet;
	sockaddr.sdn_flags	  = 0x00;
	sockaddr.sdn_objnum	  = 0x00;
	memcpy(sockaddr.sdn_objname, "DAPFS", 5);
	sockaddr.sdn_objnamel = dn_htons(5);
	memcpy(sockaddr.sdn_add.a_addr, np->n_addr,2);
	sockaddr.sdn_add.a_len = 2;

	if (connect(sockfd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) < 0)
	{
		close(sockfd);
		return -1;
	}

// Now run the command
	if (write(sockfd, command, strlen(command)) < (int)strlen(command))
	{
		close(sockfd);
		return -1;
	}

// Wait for completion (not for ever!!)
	FD_ZERO(&fds);
	FD_SET(sockfd, &fds);
	tv.tv_usec = 0;
	tv.tv_sec = 3;
	status = select(sockfd+1, &fds, NULL, NULL, &tv);
	if (status <= 0)
	{
		close(sockfd);
		return -1;
	}
	status = read(sockfd, reply, BUFLEN);
	close (sockfd);
	return status;
}

static void add_to_stat(dap_message *m, struct stat *stbuf)
{
	switch (m->get_type())
	{
	case dap_message::NAME:
	{
		dap_name_message *nm = (dap_name_message *)m;

		// If name ends in .DIR;1 then add directory attribute
		if (nm->get_nametype() == dap_name_message::FILENAME) {
			if (strstr(nm->get_namespec(), ".DIR;1")) {
				stbuf->st_mode &= ~S_IFREG;
				stbuf->st_mode |= S_IFDIR;
			}
			else {
				stbuf->st_mode |= S_IFREG;
			}
		}
	}
	break;

	case dap_message::PROTECT:
	{
		dap_protect_message *pm = (dap_protect_message *)m;
		stbuf->st_mode |= pm->get_mode();
		stbuf->st_uid = 0;
		stbuf->st_gid = 0;
	}
	break;

	case dap_message::ATTRIB:
	{
		dap_attrib_message *am = (dap_attrib_message *)m;
		stbuf->st_size = am->get_size();
		stbuf->st_blksize = am->get_bsz();
		stbuf->st_blocks = am->get_alq();
		/* Samba needs this */
		stbuf->st_nlink = 1;
	}
	break;

	case dap_message::DATE:
	{
		dap_date_message *dm = (dap_date_message *)m;
		stbuf->st_atime = dm->get_rdt_time();
		stbuf->st_ctime = dm->get_cdt_time();
		stbuf->st_mtime = dm->get_cdt_time();
	}
	break;
	}
}

int dapfs_getattr_dap(const char *path, struct stat *stbuf)
{
	char vmsname[VMSNAME_LEN];
	char name[80];
	int ret = 0;
	int size;

	make_vms_filespec(path, vmsname, 0);

	dap_access_message acc;
	acc.set_accfunc(dap_access_message::DIRECTORY);
	acc.set_accopt(1);
	acc.set_filespec(vmsname);
	acc.set_display(dap_access_message::DISPLAY_MAIN_MASK |
			dap_access_message::DISPLAY_DATE_MASK |
			dap_access_message::DISPLAY_PROT_MASK);
	if (!acc.write(conn)) {
		restart_dap();
		return -EIO;
	}

	dap_message *m;

	// Loop through the files we find
	while ( ((m=dap_message::read_message(conn, true) )) )
	{
		add_to_stat(m, stbuf);
		if (m->get_type() == dap_message::ACCOMP) {
			delete m;
			goto finished;
		}

		if (m->get_type() == dap_message::STATUS)
		{
			dap_status_message *sm = (dap_status_message *)m;
			if (sm->get_code() == 0x4030) // Locked
			{
				dap_contran_message cm;
				cm.set_confunc(dap_contran_message::SKIP);
				if (!cm.write(conn)) {
					restart_dap();
					delete m;
					return -EIO;
				}
			}
			else
			{
				ret = -ENOENT; // TODO better error ??

				// Clean connection status.
				dap_contran_message cm;
				cm.set_confunc(dap_contran_message::SKIP);
				if (!cm.write(conn)) {
					restart_dap();
					ret = -EIO;
				}
			}
		}
		delete m;
	}
finished:
	return ret;
}

int dapfs_readdir_dap(const char *path, void *buf, fuse_fill_dir_t filler,
		      off_t offset, struct fuse_file_info *fi)
{
	dap_connection c(debuglevel);
	char vmsname[VMSNAME_LEN];
	char wildname[strlen(path)+2];
	char name[80];
	struct stat stbuf;
	int size;
	int ret;

	ret = dap_connect(c);
	if (ret)
		return ret;

	memset(&stbuf, 0, sizeof(stbuf));

	// Add wildcard to path
	if (path[strlen(path)-1] == '/') {
		sprintf(wildname, "%s*.*", path);
		path = wildname;
	}
	else {
		strcpy(wildname, path);
		strcat(wildname, "/*.*");
		path = wildname;
	}

	make_vms_filespec(path, vmsname, 0);

	dap_access_message acc;
	acc.set_accfunc(dap_access_message::DIRECTORY);
	acc.set_accopt(1);
	acc.set_filespec(vmsname);
	acc.set_display(dap_access_message::DISPLAY_MAIN_MASK |
			dap_access_message::DISPLAY_DATE_MASK |
			dap_access_message::DISPLAY_PROT_MASK);
	if (!acc.write(c)) {
		c.close();
		return -EIO;
	}

	bool name_pending = false;
	dap_message *m;
	char volname[256];

	// Loop through the files we find
	while ( ((m=dap_message::read_message(c, true) )) )
	{
		add_to_stat(m, &stbuf);

		switch (m->get_type())
		{
		case dap_message::ACK:
			// Got all the file info
			if (name_pending)
			{
				char unixname[BUFLEN];

				make_unix_filespec(unixname, name);
				if (strstr(unixname, ".dir") == unixname+strlen(unixname)-4)
				{
					char *ext = strstr(unixname, ".dir");
					if (ext) *ext = '\0';
				}

				/* Tell Fuse */
				filler(buf, unixname, &stbuf, 0);

				/* Prepare for next name */
				name_pending = false;
				memset(&stbuf, 0, sizeof(stbuf));
			}
			break;

		case dap_message::NAME:
		{
			dap_name_message *nm = (dap_name_message *)m;

			if (nm->get_nametype() == dap_name_message::VOLUME)
			{
				strcpy(volname, nm->get_namespec());
			}

			if (nm->get_nametype() == dap_name_message::FILENAME)
			{
				strcpy(name, nm->get_namespec());
				name_pending = true;
			}
		}
		break;

	        case dap_message::STATUS:
		{
			// Send a SKIP if the file is locked, else it's an error
			dap_status_message *sm = (dap_status_message *)m;
			if (sm->get_code() == 0x4030)
			{
				dap_contran_message cm;
				cm.set_confunc(dap_contran_message::SKIP);
				if (!cm.write(c))
				{
					fprintf(stderr, "Error sending skip: %s\n", c.get_error());
					delete m;
					goto finished;
				}
			}
			else
			{
				printf("Error opening %s: %s\n", vmsname, sm->get_message());
				name_pending = false;
				goto flush;
			}
			break;
		}
		case dap_message::ACCOMP:
			goto flush;
		}
		delete m;
	}

finished:
	// An error:
	fprintf(stderr, "Error: %s\n", c.get_error());
	c.close();
	return 2;

flush:
	delete m;
	if (name_pending)
	{
		char unixname[BUFLEN];
		make_unix_filespec(unixname, name);
		if (strstr(unixname, ".dir") == unixname+strlen(unixname)-4)
		{
			char *ext = strstr(unixname, ".dir");
			if (ext) *ext = '\0';
		}
		filler(buf, unixname, &stbuf, 0);
	}
	c.close();
	return 0;
}

/* Path already has version number appended to it -- this may be a mistake :) */
int dap_delete_file(const char *path)
{
	char vmsname[VMSNAME_LEN];
	char name[80];
	int ret;
	int size;

	make_vms_filespec(path, vmsname, 0);

	if (vmsname[strlen(vmsname)-1] == '.')
		vmsname[strlen(vmsname)-1] = '\0';

	dap_access_message acc;
	acc.set_accfunc(dap_access_message::ERASE);
	acc.set_accopt(1);
	acc.set_filespec(vmsname);
	acc.set_display(0);
        if (!acc.write(conn)) {
		restart_dap();
		return -EIO;
	}

	// Wait for ACK or status
	ret = 0;
	while(1) {
		dap_message *m = dap_message::read_message(conn, true);

		switch (m->get_type())
		{
		case dap_message::ACCOMP:
			delete m;
			goto end;
		break;

		case dap_message::STATUS:
		{
			dap_status_message *sm = (dap_status_message *)m;
			ret = -EPERM; // Default error!
			delete m;
			goto end;
		}
		break;
		}
		delete m;
	}

	end:
	return ret;
}

int dap_rename_file(const char *from, const char *to)
{
	char vmsfrom[VMSNAME_LEN];
	char vmsto[VMSNAME_LEN];
	char dirname[BUFLEN];
	int ret;
	int size;
	struct stat stbuf;

	// If it's a directory then add .DIR to the name
	if ( (ret = dapfs_getattr_dap(from, &stbuf))) {
		strcpy(dirname, from);
		strcat(dirname, ".dir");
		if ( (ret = dapfs_getattr_dap(dirname, &stbuf)))
			return ret;
		from = dirname;
	}

	make_vms_filespec(from, vmsfrom, 0);
	make_vms_filespec(to, vmsto, 0);

	if (from == dirname) {

		// Set prot=RWED on 'from' directory or we can't rename it.
		// Don't regard this as fatal, subsequent calls will return -EPERM
		// anyway if it's really wrong.
		char setprot[BUFLEN];
		char reply[BUFLEN];
		int len;

		sprintf(setprot, "SETPROT %s O:RWED", vmsfrom);
		len = get_object_info(setprot, reply);
		if (len != 2) // "OK"
			syslog(LOG_WARNING, "dapfs: can't set protection on directory %s", vmsfrom);

		// Fix the TO name too.
		// TODO Odd dotting here, not sure why. just work around it for now
		if (vmsto[strlen(vmsto)-1] == '.')
			strcat(vmsto, "DIR");
		else
			strcat(vmsto, ".DIR");
	}

	dap_access_message acc;
	acc.set_accfunc(dap_access_message::RENAME);
	acc.set_accopt(1);
	acc.set_filespec(vmsfrom);
	acc.set_display(0);
        if (!acc.write(conn)) {
		restart_dap();
		return -EIO;
	}

	dap_name_message nam;
	nam.set_nametype(dap_name_message::FILESPEC);
	nam.set_namespec(vmsto);
	if (!nam.write(conn)) {
		restart_dap();
		return -EIO;
	}

	// Wait for ACK or status
	ret = 0;
	while (1) {
		dap_message *m = dap_message::read_message(conn, true);
		switch (m->get_type())
		{
		case dap_message::ACCOMP:
			goto end;

		case dap_message::STATUS:
		{
			dap_status_message *sm = (dap_status_message *)m;
			ret = -EPERM; // Default error!
			goto end;
		}
		}
	}
	end:
	return ret;
}


int dap_init()
{
	struct accessdata_dn accessdata;
	char node[BUFLEN], filespec[VMSNAME_LEN];

	if (dap_connect(conn))
		return -ENOTCONN;

	return 0;
}