File: dpm-Ls.c

package info (click to toggle)
dpm-postgres 1.7.4.7-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 13,788 kB
  • ctags: 10,782
  • sloc: ansic: 146,136; sh: 13,362; perl: 11,142; python: 5,529; cpp: 5,113; sql: 1,790; makefile: 955; fortran: 113
file content (412 lines) | stat: -rw-r--r-- 9,203 bytes parent folder | download | duplicates (8)
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
/*
 * Copyright (C) 1999-2007 by CERN/IT/PDP/DM
 * All rights reserved
 */
 
#ifndef lint
static char sccsid[] = "@(#)$RCSfile: dpm-Ls.c,v $ $Revision: 1.2 $ $Date: 2007/07/19 15:53:24 $ CERN IT-PDP/DM Jean-Philippe Baud";
#endif /* not lint */

// $Id: dpm-Ls.c,v 1.2 2007/07/19 15:53:24 grodid Exp $

/*	dpm-Ls - list DPM directory/file entries recursively when older than a given threshold */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#if defined(_WIN32)
#define W_OK 2
#include <winsock2.h>
#else
#include <unistd.h>
#endif
#include "Cns.h"
//#include "Cns_api.h"
#include "dpns_api.h"
#include "dpm_api.h"
#include "serrno.h"
extern	char	*getenv();
extern	int	optind;
time_t current_time;
#if sgi
extern char *strdup _PROTO((CONST char *));
#endif
int errflg;
int fflag;
int iflag;
int rflag;

#include <signal.h>   /* catch signals */
// gnu opts
#include "dpmgopts.h"
#include "dpmutils.h"

int main(argc, argv)
int argc;
char **argv;
{
	int c;
	char fullpath[CA_MAXPATHLEN+1];
	int i;
	char *p;
	char *path;
	char *pn;
	char *pm;
	struct Cns_filestat statbuf;
	time_t ltime;
#if defined(_WIN32)
	WSADATA wsadata;
#endif

#define _DPMOPT yes

#ifndef _DPMOPT
	while ((c = getopt (argc, argv, "fiRr")) != EOF) {
		switch (c) {
		case 'f':
			fflag++;
			break;
		case 'i':
			iflag++;
			break;
		case 'R':
		case 'r':
			rflag++;
			break;
		case '?':
			errflg++;
			break;
		default:
			break;
		}
	}
	if (optind >= argc) {
		fprintf (stderr,
		    "usage: %s [-f] [-i] file...\n\t%s [-f] [-i] -r dirname...\n",
		    argv[0], argv[0]);
		exit (USERR);
	}
#if defined(_WIN32)
	if (WSAStartup (MAKEWORD (2, 0), &wsadata)) {
		fprintf (stderr, NS052);
		exit (SYERR);
	}
#endif
#else

	struct dpm_filestatus *filestatuses = NULL;
	int nbfiles;
	int nbreplies;
	char **dpmrmargs = NULL;

	int thisarg;
	int nbargs = 0;
	char **remargs = NULL;
	char hostenvm[256];
	char hostenvn[256];

#include "dpmgdebug.h"
	if (argc < 2) {
		fprintf (stderr, "usage: %s --dpmolder=age list-of-SURLs and/or list-of-SURL-of-DIRs\n", argv[0]);
		exit (USERR);
	}

	// gnu opts
	reset_global();  /* must be before parser */
	prog_gname = strdup(argv[0]);

	//P printf (" before arg_parse \n");
	thisarg = arg_parse(argc, argv);
	//P printf ("\n after arg_parse %s %d %d \n", argv[thisarg], argc, thisarg);

	if ( argc > thisarg ) {
	  remargs = realloc(remargs, sizeof(char*)*(argc-thisarg));
	}
	while ( thisarg < argc ) {
	  remargs[nbargs++] = strdup(argv[thisarg++]);
	}


	if ( dpm_host ) {
	  printf ("    host: %s\n", dpm_host);
	  strcpy(hostenvm, "DPM_HOST=");
	  strcat(hostenvm, dpm_host);
	  putenv(hostenvm);
	  strcpy(hostenvn, "DPNS_HOST=");
	  strcat(hostenvn, dpm_host);
	  putenv(hostenvn);
	}
	(void) time (&current_time);
	if ((pn = getenv ("DPNS_HOST")) && (pm = getenv ("DPM_HOST")) && strcmp(pn, pm)) {
	  printf ( " DPNS_HOST: %s and DPM_HOST: %s mismatch\n", pn, pm);
	  exit(USERR);
	} else {
	  printf ("DPM_HOST: %s\n", pm);
	}
	rflag++;
	fflag++;
	if ( ! dpm_older ) {
	  perror (" Older option is mandatory: --dpmolder=age (sec.) ");
	  exit(1);
	}
#endif

#if defined(_DPMOPT)
	for (i = 0; i < nbargs; i++) {
		path = remargs[i];
#else
	for (i = optind; i < argc; i++) {
		path = argv[i];
#endif
		printf ("path: %s\n", path);
		if (*path != '/' && strstr (path, ":/") == NULL) {
			if ((p = getenv (CNS_HOME_ENV)) == NULL ||
			    strlen (p) + strlen (path) + 1 > CA_MAXPATHLEN) {
				fprintf (stderr, "%s: invalid path\n", path);
				errflg++;
				continue;
			} else
				sprintf (fullpath, "%s/%s", p, path);
		} else {
			if (strlen (path) > CA_MAXPATHLEN) {
				fprintf (stderr, "%s: %s\n", path,
				    sstrerror(SENAMETOOLONG));
				errflg++;
				continue;
			} else
				strcpy (fullpath, path);
		}
		if ((c = Cns_lstat (fullpath, &statbuf)) == 0) {
			if (statbuf.filemode & S_IFDIR) {
				if (rflag) {
					c = listdir (fullpath);
				} else {
					serrno = EISDIR;
					c = -1;
				}
			} else {
				if ((statbuf.filemode & S_IFLNK) != S_IFLNK &&
				    ! fflag && Cns_access (fullpath, W_OK) &&
				    isatty (fileno (stdin))) {
					printf ("override write protection for %s? ", fullpath);
					if (! isyes())
						continue;
				} else if (iflag) {
					printf ("list %s? ", fullpath);
					if (! isyes())
						continue;
				}
#if 0
				c = Cns_unlink (fullpath);
#else
				ltime = statbuf.atime;
				if ( current_time-ltime > dpm_older ) {
				  printf ("listING file: %s %d\n", fullpath, current_time-ltime);
				  // To replace with dpm_rm ...
				  // c = Cns_unlink (fullpath);

#ifdef _REMOVAL
				  nbfiles = 1;
				  dpmrmargs = realloc(dpmrmargs, sizeof(char*)*nbfiles);
				  dpmrmargs[0] = strdup(fullpath);
				  if ((c = dpm_rm (nbfiles, 
						   dpmrmargs,
						   &nbreplies, 
						   &filestatuses
						   )) < 0) {
				    fprintf (stderr, "dpm_rm: %s\n", sstrerror(serrno));
				    //nb_file_err++;
				  }
				  nbreplies = 0;
				  if (dpmrmargs)
				    free(dpmrmargs);
				  if (filestatuses)
				    free (filestatuses);
#endif
				} else {
				  printf (" excludING file: %s %d\n", fullpath, current_time-ltime);
				}
#endif
			}
		}
		if (c && (serrno != ENOENT || fflag == 0)) {
			fprintf (stderr, "%s: %s\n", path,
			    sstrerror(serrno));
			errflg++;
		}
	}
#if defined(_WIN32)
	WSACleanup();
#endif
	if (errflg)
		exit (USERR);
	exit (0);
}

isyes()
{
	int c;
	int fchar;

	fchar = c = getchar();
	while (c != '\n' && c != EOF)
		c = getchar();
	return (fchar == 'y');
}

listdir (dir)
char *dir;
{
	char curdir[CA_MAXPATHLEN+1];
	struct dirlist {
		char *d_name;
		struct dirlist *next;
	};
	Cns_DIR *dirp;
	struct dirlist *dlc;		/* pointer to current directory in the list */
	struct dirlist *dlf = NULL;	/* pointer to first directory in the list */
	struct dirlist *dll;		/* pointer to last directory in the list */
	struct Cns_direnstat *dxp;
	char fullpath[CA_MAXPATHLEN+1];

	struct dpm_filestatus *filestatuses = NULL;
	int nbfiles = 0;
	//int nb_file_err = 0;
	int nbreplies;
	char **dpmrmargs = NULL;

	int c;
	struct Cns_filestat statbuf;
	time_t ltime;

	if (! fflag && Cns_access (dir, W_OK) &&
	    isatty (fileno (stdin))) {
		printf ("override write protection for %s? ", dir);
		if (! isyes())
			return (0);
	} else if (iflag) {
		printf ("list files in %s? ", dir);
		if (! isyes())
			return (0);
	}
	if ((dirp = Cns_opendir (dir)) == NULL)
		return (-1);

	if (Cns_chdir (dir) < 0)
		return (-1);
	while ((dxp = Cns_readdirx (dirp)) != NULL) {
		if (dxp->filemode & S_IFDIR) {
			if ((dlc = (struct dirlist *)
			    malloc (sizeof(struct dirlist))) == NULL ||
			    (dlc->d_name = strdup (dxp->d_name)) == NULL) {
				serrno = errno;
				return (-1);
			}
			dlc->next = 0;
			if (dlf == NULL)
				dlf = dlc;
			else
				dll->next = dlc;
			dll = dlc;
		} else {
			sprintf (fullpath, "%s/%s", dir, dxp->d_name);
			if ((dxp->filemode & S_IFLNK) != S_IFLNK &&
			    ! fflag && Cns_access (fullpath, W_OK) &&
			    isatty (fileno (stdin))) {
				printf ("override write protection for %s? ", fullpath);
				if (! isyes())
					continue;
			} else if (iflag) {
				printf ("list %s? ", fullpath);
				if (! isyes())
					continue;
			}
#if 0
			if (Cns_unlink (dxp->d_name)) {
				fprintf (stderr, "%s/%s: %s\n", dir,
				    dxp->d_name, sstrerror(serrno));
				errflg++;
			}
#else
			if ((c = Cns_lstat (fullpath, &statbuf)) == 0) {
			  ltime = statbuf.atime;
			  if ( current_time-ltime > dpm_older ) {
			    printf ("listING file: %s %d\n", fullpath, current_time-ltime);
#ifdef _REMOVAL
			    // Putting this file on the list stack ...
				  nbfiles++;
				  dpmrmargs = realloc(dpmrmargs, sizeof(char*)*nbfiles);
				  dpmrmargs[nbfiles-1] = strdup(fullpath);
#endif
			  } else {
			    printf (" excludING file: %s %d\n", fullpath, current_time-ltime);
			  }
			} else {
			  // No info found on this file ...
			  //NO printf ("listING file: %s\n", fullpath);
			}
#endif
		}
	}

#ifdef _REMOVAL
	if (nbfiles) {
	                          printf ("listING nbfiles: %d\n", nbfiles);
				  if ((c = dpm_rm (nbfiles, 
						   dpmrmargs,
						   &nbreplies, 
						   &filestatuses
						   )) < 0) {
				    fprintf (stderr, "listdir dpm_rm: %s\n", sstrerror(serrno));
				    errflg++;
				  } 
				  nbfiles = 0;
				  nbreplies = 0;
				  if (dpmrmargs)
				    free(dpmrmargs);
				  if (filestatuses)
				    free (filestatuses);
	}
#endif


	(void) Cns_closedir (dirp);
	while (dlf) {
		sprintf (curdir, "%s/%s", dir, dlf->d_name);
		if (listdir (curdir) < 0)
			fprintf (stderr, "%s: %s\n", curdir, sstrerror(serrno));
		free (dlf->d_name);
		dlc = dlf;
		dlf = dlf->next;
		free (dlc);
	}
	if (Cns_chdir ("..") < 0)
		return (-1);
	if (iflag) {
		printf ("list %s? ", dir);
		if (! isyes())
			return (0);
	}
#if 0
	if (Cns_rmdir (dir)) {
		fprintf (stderr, "%s: %s\n", dir, (serrno == EEXIST) ?
		    "Directory not empty" : sstrerror(serrno));
		errflg++;
	}
#else
	printf ("listING  dir: %s\n", dir);
	
#ifdef _REMOVAL
	if (Cns_rmdir (dir)) {
		fprintf (stderr, "%s: %s\n", dir, (serrno == EEXIST) ?
		    "Directory not empty" : sstrerror(serrno));
		errflg++;
	}
#endif
	
#endif
	return (0);
}