File: dndel.cc

package info (click to toggle)
dnprogs 2.43.2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,936 kB
  • ctags: 3,872
  • sloc: ansic: 24,686; cpp: 10,608; makefile: 769; sh: 551; awk: 13
file content (333 lines) | stat: -rw-r--r-- 7,996 bytes parent folder | download | duplicates (3)
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
/******************************************************************************
    (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 <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netdnet/dn.h>
#include <netdnet/dnetdb.h>
#include <unistd.h>
#include <regex.h>

#include "connection.h"
#include "protocol.h"
#include "logging.h"


static void usage(void)
{
    printf("\nUSAGE: dndel [OPTIONS] 'node\"user password\"::filespec'\n\n");
    printf("NOTE: The VMS filename really should be in single quotes to\n");
    printf("      protect it from the shell\n");

    printf("\nOptions:\n");
    printf("  -i           interactive - prompt before deleting\n");
    printf("  -v           verbose - display files that have been deleted\n");
    printf("  -? -h        display this help message\n");
    printf("  -V           show version number\n");

    printf("\nExamples:\n\n");
    printf(" dndel -l 'myvax::*.*;'\n");
    printf(" dndel 'cluster\"patrick thecats\"::disk$users:[cats.pics]otto.jpg;'\n");
    printf("\n");
}


// Check for an extra STATUS message following a NAME
static bool check_status(dap_connection &conn, char *name)
{
  dap_message *m = dap_message::read_message(conn, false);
  if (m)
  {
      if (m->get_type() == dap_message::STATUS)
      {
	  dap_status_message *sm = (dap_status_message *)m;
	  fprintf(stderr, "Error deleting %s: %s\n", name, sm->get_message());
	  delete m;
	  return false;
      }

      // The file must exist if we get an ATTRIB message.
      if (m->get_type() == dap_message::ATTRIB)
      {
	  delete m;
	  return true;
      }

      fprintf(stderr, "Got unexpected message: %s\n", m->type_name());
      delete m;
  }
  return true;
}


// Send an ACCESS DIRECTORY request. If we are running non-interactively then
// we actually delete the files here instead.
static	bool dap_directory_lookup(dap_connection &c, char *dirname,
				  bool interactive)
{
    dap_access_message acc;
    if (interactive)
	acc.set_accfunc(dap_access_message::DIRECTORY);
    else
	acc.set_accfunc(dap_access_message::ERASE);
    acc.set_accopt(1);
    acc.set_filespec(dirname);
    acc.set_display(dap_access_message::DISPLAY_MAIN_MASK);
    return acc.write(c);
}

// Delete one file
static bool dap_delete_file(dap_connection &c, char *name)
{
    dap_access_message acc;
    acc.set_accfunc(dap_access_message::ERASE);
    acc.set_accopt(1);
    acc.set_filespec(name);
    acc.set_display(0);
    return acc.write(c);
}

// Get a response from the delete request
static bool dap_get_delete_ack(dap_connection &c)
{
    dap_message *m = dap_message::read_message(c, true);
    if (m)
    {
	switch (m->get_type())
	{
	case dap_message::ACCOMP:
	case dap_message::ACK:
	    return true;

	case dap_message::STATUS:
	    {
		dap_status_message *sm = (dap_status_message *)m;
		fprintf(stderr, "Error deleting file: %s\n", sm->get_message());
		return false;
	    }
	}
    }
    return false;
}

// Send CONTRAN/SKIP message. We need this if a file in the list is locked.
static bool dap_send_skip(dap_connection &conn)
{
    dap_contran_message cm;
    cm.set_confunc(dap_contran_message::SKIP);
    return cm.write(conn);
}

// Get the file information. Actually just the name
static int dap_get_dir_entry(dap_connection &c, char *name)
{
    static char volume[256];
    static char dir[256];
    static bool got_name = false;

    dap_message *m;
    while ( ((m = dap_message::read_message(c, true))) )
    {
	switch (m->get_type())
	{
	case dap_message::NAME:
	    {
		dap_name_message *nm = (dap_name_message *)m;
		switch (nm->get_nametype())
		{
		case dap_name_message::VOLUME:
		    strcpy(volume, nm->get_namespec());
		    break;

		case dap_name_message::DIRECTORY:
		    strcpy(dir, nm->get_namespec());
		    break;

		case dap_name_message::FILENAME:
		    sprintf(name, "%s%s%s", volume, dir, nm->get_namespec());
		    got_name = true;
		    delete m;
		    if (check_status(c, name))
		        return dap_message::NAME;
		    else
		        return -1;

		case dap_name_message::FILESPEC:
		    strcpy(name, nm->get_namespec());
		    got_name = true;
		    delete m;
		    if (check_status(c, name))
		        return dap_message::NAME;
		    else
		        return -1;
		}

	    }
	    break;

	case dap_message::ACCOMP:
	    delete m;
	    return -1;

	case dap_message::ACK:
	    delete m;
	    return dap_message::ACK;

	case dap_message::STATUS:
	    {
		dap_status_message *sm = (dap_status_message *)m;
		if (sm->get_code() == 0x4030) // Locked
		{
		    dap_send_skip(c);
		    break;
		}
		fprintf(stderr, "Error deleting %s: %s\n",
			got_name?name:"file",
			sm->get_message());
		delete m;
		return -1;
	    }

	default:
	    delete m;
	    return m->get_type();
	}
	delete m;
    }
    return -1;
}

// Start here...
int main(int argc, char *argv[])
{
    int	    opt,retval;
    char    name[256];
    bool    interactive = false;
    int     verbose = 0;
    int     two_links = 0;


    if (argc < 2)
    {
	usage();
	exit(0);
    }

/* Get command-line options */
    opterr = 0;
    optind = 0;
    while ((opt=getopt(argc,argv,"?hvVi")) != EOF)
    {
	switch(opt)
	{
	case 'h':
	case '?':
	    usage();
	    exit(1);

	case 'i':
	    interactive = true;
	    two_links++;
	    break;

	case 'v':
	    verbose++;
	    two_links++;
	    break;

	case 'V':
	    printf("\ndndel from dnprogs version %s\n\n", VERSION);
	    exit(1);
	    break;
	}
    }
    init_logging("dndel", 'e', false);

    dap_connection dir_conn(verbose);
    dap_connection del_conn(verbose);

    /* We open one link to get the file names and (if interactive or verbose)
       another to do the actual deletion.
       This is not a waste of resources - it's what VMS does !
     */
    if (!dir_conn.connect(argv[optind], dap_connection::FAL_OBJECT, name))
    {
	fprintf(stderr, "%s\n", dir_conn.get_error());
	return -1;
    }
    // Exchange config messages
    if (!dir_conn.exchange_config())
    {
	fprintf(stderr, "Error in config: %s\n", dir_conn.get_error());
	return -1;
    }

    /* Open the second link to actually delete the file(s) */
    if (two_links)
    {
	if (!del_conn.connect(argv[optind], dap_connection::FAL_OBJECT, name))
	{
	    fprintf(stderr, "%s\n", del_conn.get_error());
	    return -1;
	}
	// Exchange config messages
	if (!del_conn.exchange_config())
	{
	    fprintf(stderr, "Error in config: %s\n", del_conn.get_error());
	    return -1;
	}
    }

    /* If non-interactive then the next command just deletes the files */
    dap_directory_lookup(dir_conn, name, interactive || (verbose>0) );

    /* Loop through the files we find */
    while ((retval=dap_get_dir_entry(dir_conn, name)) > 0)
    {
	if (retval == dap_message::NAME)
	{
	    if (interactive)
	    {
		char response[255];

		printf("Delete %s ? ", name);
		fgets(response, sizeof(response), stdin);
		if (tolower(response[0]) == 'y')
		{
		    dap_delete_file(del_conn, name);
		    if (dap_get_delete_ack(del_conn))
			if (verbose)
			    printf("Deleted %s\n", name);
		}
	    }
	    else if (verbose)
	    {
		dap_delete_file(del_conn, name);
		if (dap_get_delete_ack(del_conn))
		    printf("Deleted %s\n", name);
	    }
	}
    }

    dir_conn.close();
    if (two_links) del_conn.close();
    return 0;
}