File: dnetfile.cc

package info (click to toggle)
dnprogs 2.18-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,896 kB
  • ctags: 3,051
  • sloc: ansic: 18,586; cpp: 9,436; makefile: 669; sh: 502; awk: 13
file content (432 lines) | stat: -rw-r--r-- 9,427 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
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
/******************************************************************************
    (c) 1998-1999 P.J. Caulfield               patrick@tykepenguin.cix.co.uk
    
    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.
 ******************************************************************************
 */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <netdnet/dn.h>
#include <netdnet/dnetdb.h>
#include <sys/types.h>
#include <regex.h>
#include "connection.h"
#include "protocol.h"
#include "logging.h"

#include "file.h"
#include "dnetfile.h"

// Assume the file name is a DECnet name if it has two consecutive colons in it
bool dnetfile::isMine(char *name)
{
    if (strstr(name, "::"))
	return TRUE;
    else
	return FALSE;
}

// Constructor.
dnetfile::dnetfile(char *n, int verbosity):
    conn(verbosity)
{
    isOpen = FALSE;
    verbose = verbosity;
    lasterror = NULL;
    strcpy(fname, n);
    strcpy(name, n);

    // Is this a wildcard filename?
    if (strchr(name, '*') || 
	strchr(name, '%'))
	wildcard = TRUE;
    else
	wildcard = FALSE;
}

dnetfile::~dnetfile()
{
    dap_close_link();
}

int dnetfile::setup_link(unsigned int bufsize, int rfm, int rat, int xfer_mode)
{
// If there was a parse error in the file name then fail here
    if (lasterror)
	return -1;

    init_logging("dncopy", 'e', false);

// Save these for later
    user_rfm = rfm;
    user_rat = rat;
    transfer_mode = xfer_mode;
    user_bufsize = bufsize;

    struct accessdata_dn accessdata;
    memset(&accessdata, 0, sizeof(accessdata));
    if (!conn.parse(fname, accessdata, node, name))
    {
	lasterror = conn.get_error();
	return -1;
    }


    strcpy(user, (char *)accessdata.acc_user);
    strcpy(password, (char *)accessdata.acc_pass);

    if (!conn.connect(node, user, password, dap_connection::FAL_OBJECT))
    {
	lasterror = conn.get_error();
	return -1;
    }
    if (!conn.exchange_config())
    {
	lasterror = conn.get_error();
	return -1;
    }

    strcpy(filname, name);

    return 0;
}

// Open a file for writing
int dnetfile::open(char *filename,char *mode)
{
    // Add the remote end's directory spec to the filename.
    strcpy(filname, name);
    strcat(filname, filename);
    return open(mode);
}

// Open a file already named 
int dnetfile::open(char *mode)
{
    int real_rfm, real_rat;
    int status;

    switch (mode[0])
    {
    case 'r':
	writing = FALSE;
	break;

    case 'a':   // Not supported yet (if ever!)
	lasterror ="append unsupported";
	return -1;
	break;

    case 'w':
	writing = TRUE;
	break;
    }

    if (!isOpen) // Open connection for the first time
    {
	file_fsz = 0;
	if (writing)
	{
	    status = dap_send_attributes();
	    
	    // Send default file name
	    if (wildcard)
	    {
		status = dap_send_name();
		if (status) return status;
	    }
	    status = dap_send_access();
	    if (status) return status;

	    status = dap_get_file_entry(&real_rfm, &real_rat);
	    if (status) return status;
	}
	else
	{
	    status = dap_send_access();
	    if (status) return status;
	}

	if (!writing)
	{
	    status = dap_get_file_entry(&real_rfm, &real_rat);
	    if (status) return status;
	    if (verbose > 1) printf("File attributes: rfm: %d, rat: %d\n",
				    real_rfm, real_rat);

// Use the file's real attributes if the user just wants defaults.
	    if (user_rfm == RFM_DEFAULT)  file_rfm = real_rfm;
	    if (user_rat == RAT_DEFAULT)  file_rat = real_rat;
	    
// Get the file's real name in case the user specified a wildcard
	    strcpy(name, filname);
	}

	isOpen = TRUE;
	ateof = FALSE;
    }
    else if (writing) // new file to create on already open link
    {
        dap_send_attributes();
        dap_send_access();
	status = dap_get_file_entry(&real_rfm, &real_rat);
    }
    status = dap_send_connect();
    if (status) return status;

    status = dap_send_get_or_put();
    return status;
}

// Close the file but leave the link open in case there are any more to
// read/write
int dnetfile::close()
{
    return dap_send_accomp();
}

// Read a block or a record
int dnetfile::read(char *buf, int len)
{
    int retlen = dap_get_record(buf, len);
    if (retlen < 0) return retlen; // Empty record.

// If the file has implied carriage control then add an LF to the end of the
// line
    if ((file_rfm != RFM_STMLF) && 
	(file_rat & RAT_CR || file_rat & RAT_PRN))
	buf[retlen++] = '\n';

// Print files have a two-byte header indicating the line length.
    if (file_rat & RAT_PRN) 
    {
	memmove(buf, buf+file_fsz, retlen-file_fsz);
	retlen -= file_fsz;
    }

// FORTRAN files have a leading character that indicates carriage control
    if (file_rat & RAT_FTN)
    {
	switch (buf[0]) 
	{
	  
	case '+': // No new line
	  buf[0] = '\r';
	  break;
	  
	case '1': // Form Feed
	  buf[0] = '\f';
	  break;

	case '0': // Two new lines
	  memmove(buf+1, buf, retlen+1);
	  buf[0] = '\n';
	  buf[1] = '\n';
	  retlen++;
	  break;


	case ' ': // new line
	default:  // Default to a new line. This seems to be what VMS does.
	  buf[0] = '\n';
	  break;
	}
    }
    return retlen;
}

// Send a block/record
int dnetfile::write(char *buf, int len)
{
    return dap_put_record(buf, len);
}

/* Get the next filename in a wildcard list */
int dnetfile::next()
{
    int status;
    int real_rfm, real_rat;

    if (!wildcard) 
    {
	return FALSE;
    }

    if ( ((status = dap_get_file_entry(&real_rfm, &real_rat))) )
    {
	if (status != -2) perror("last file");
	return FALSE; // No more files.
    }
    else
    {

// Use the file's real attributes if the user just wants defaults.
        if (user_rfm == RFM_DEFAULT)  file_rfm = real_rfm;
        if (user_rat == RAT_DEFAULT)  file_rat = real_rat;
        if (verbose > 1) printf("File attributes: rfm: %d, rat: %d\n",
                                 real_rfm, real_rat);

// Get the file's real name in case the user specified a wildcard
        strcpy(name, filname);

        ateof = FALSE;
  	return TRUE;
    }
}

void dnetfile::perror(char *msg)
{
    if (lasterror)
	fprintf(stderr, "%s: %s\n", msg, lasterror);
    else
	fprintf(stderr, "%s: %s\n", msg, strerror(errno));
}

char *dnetfile::get_basename(int keep_version)
{
    make_basename(keep_version);
    return &basename[0];
}

// Returns TRUE if the target filename looks like a directory
bool dnetfile::isdirectory()
{
    // If the last character of the filename is ']' or ':' then we assume
    // the name is a directory. This means that logical names for
    // directories MUST have a colon at the end.
    if (fname[strlen(fname)-1] == ':' ||
	fname[strlen(fname)-1] == ']')
	return TRUE;
    else
	return FALSE;
}

bool dnetfile::eof()
{
    return ateof;
}

bool dnetfile::iswildcard()
{
    return wildcard;
}

/* Work out the base name so that when we copy VMS->Unix we don't get the
   device and version numbers in the final filename */
void dnetfile::make_basename(int keep_version)
{
    char        *start;
    char        *end;
    unsigned int i;
		    
    /* Find the start of the name */
    start = rindex(name, ']');
    if (!start) start = rindex(name, ':');
    if (!start) start = name-1;
    strcpy(basename, start+1);

    /* Remove a version number */
    if (!keep_version)
    {
	end = rindex(basename, ';');
	if (end) *end = '\0';
    }

    /* make all lower case */
    for (i=0; i < strlen(basename); i++)
    {
	basename[i] = tolower(basename[i]);
    }
}

// Return a pretty-printed version of the file name for confirmation and
// logging
char *dnetfile::get_printname(char *filename)
{
    static char pname[1024];
    
    strcpy(pname, node);
    if (*user) 
    {
	strcat(pname, "\"");
	strcat(pname, user);
	if (*password) strcat(pname, " password");
	strcat(pname, "\"");
    }
    strcat(pname, "::");
    strcat(pname, volname);

    if (filename) // Use passed-in filename if there is one
    {
        strcat(pname, filename);
    }
    else
    {
        strcat(pname, dirname);
        strcat(pname, filname);
    }
    return pname;
}

char *dnetfile::get_printname()
{
    return get_printname((char *)NULL);
}

// Return a string telling the user whether we transferred blocks or
// records or what.
char *dnetfile::get_format_name()
{
    switch (transfer_mode)
    {
    case MODE_DEFAULT:
	switch (file_rat)
	{
	case RAT_NONE:
	    return "blocks";
	    
	default:
	    return "records";
	}
	break;

    case MODE_RECORD:
	return "records";
    case MODE_BLOCK:
	return "blocks";
    }
    return "things";
}


int dnetfile::get_umask()
{
    if (verbose > 1) printf("protection = %o\n", prot);
    return prot;
}

int dnetfile::set_umask(int mask)
{
// TODO Set the protection - this may have to be done at $CREATE time in
// which case dncopy needs rejigging (clang!)
    return 0;
}


// Return the largest buffer size less than 'biggest'
int dnetfile::max_buffersize(int biggest)
{
    return biggest>conn.get_blocksize()?conn.get_blocksize():biggest;
}