File: rfrm.c

package info (click to toggle)
lcgdm 1.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 14,044 kB
  • sloc: ansic: 149,126; sh: 13,441; perl: 11,498; python: 5,778; cpp: 5,113; sql: 1,805; makefile: 1,388; fortran: 113
file content (259 lines) | stat: -rw-r--r-- 6,059 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
/*
 * $Id: rfrm.c 3172 2010-02-15 08:51:54Z baud $
 */

/*
 * Copyright (C) 1998-2010 by CERN/IT/PDP/DM
 * All rights reserved
 */


#ifndef lint
static char sccsid[] = "@(#)$RCSfile: rfrm.c,v $ $Revision: 3172 $ $Date: 2010-02-15 09:51:54 +0100 (Mon, 15 Feb 2010) $ CERN/IT/PDP/DM Olof Barring";
#endif /* not lint */

/*
 * Remove remote file
 */
#include <limits.h>
#include <string.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <dirent.h>
#if defined(_WIN32)
#include <winsock2.h>
#endif
#define RFIO_KERNEL 1
#include <rfio.h>

struct dirstack {
  char *dir;
  struct dirstack *prev;
};

static char *ckpath();
int force;
char *getconfent();
static int rm_recursive(); 

char *cmd;
main(argc, argv) 
int argc;
char *argv[];
{
  int i, c, status;
  extern char * optarg ; 
  extern int    optind ;
  char *path,*root_path;
  int recursive = 0;
  int ask_yesno = 1;
  struct stat64 st;
#if defined(_WIN32)
  WSADATA wsadata;
#endif /* _WIN32 */

  cmd = argv[0];
  if ( argc < 2 ) {
    fprintf(stderr,"%s [-r] pathname ...\n",cmd);
    exit(1);
  }
  while ( (c = getopt(argc,argv,"fr")) != EOF ) {
    switch(c) {
    case 'f':
      force++;
      break;
    case 'r':
      recursive++;
      break;
    case '?':
      fprintf(stderr,"Usage: %s [-f] [-r] pathname ...\n",cmd);
      exit(2);
    }
  }
#if defined(_WIN32)
  if (WSAStartup (MAKEWORD (2, 0), &wsadata)) {
    fprintf (stderr, "WSAStartup unsuccessful\n");
    exit (2);
  }
#endif

  for (;optind<argc;optind++) {
    path = ckpath(argv[optind]);
    if ( recursive ) {
      root_path = (char *)malloc(strlen(path)+1);
      strcpy(root_path,path);
      /*
       * remove all files
       */
      rfio_errno = 0;
      serrno = 0;
      status = rm_recursive(root_path,&ask_yesno);
      if ( status == -1 ) {
         rfio_perror(root_path);
         exit(2);
      }
      /*
       * remove all directories. ENOENT can happen if the directory
       * was empty and removed already in previous call.
       */
      rfio_errno = 0;
      serrno = 0;
      status = rm_recursive(root_path,&ask_yesno);
      if ( (status == -1) &&
           (((rfio_errno != 0) && (rfio_errno != ENOENT)) ||
            ((rfio_errno == 0) &&
             (serrno     != 0) && (serrno     != ENOENT)) ||
            ((rfio_errno == 0) && (serrno     ==      0) &&
             (errno !=      0) && (errno != ENOENT)))
           ) {
         rfio_perror(root_path);
         exit(2);
      }
      free(root_path);
    } else {
      if ( rfio_lstat64(path,&st) ) {
        rfio_perror(path);
        exit(2);
      }
      if (st.st_mode & S_IFDIR) {
        fprintf(stderr,"%s: %s directory\n",cmd,path);
        exit(2);
      } 
      status = rfio_unlink(path);
      if ( status ) {
	rfio_perror(path);
	exit(1);
      }
    }
  }
  exit(0);
}

static char *ckpath(path)
char *path;
{
  char *cp;
  static char newpath[BUFSIZ];
 /* Special treatment for filenames starting with /scratch/... */
  if (!strncmp ("/scratch/", path, 9) &&
      (cp = getconfent ("SHIFT", "SCRATCH", 0)) != NULL) {
    strcpy (newpath, cp);
    strcat (newpath, path+9);
  } else 
    strcpy(newpath,path);
  return(newpath);
}

static int rfio_pushdir(ds,dir)
struct dirstack **ds;
char *dir;
{
  struct dirstack *tmp;
  if ( ds == NULL || dir == NULL ) return(0);
  tmp = (struct dirstack *)malloc(sizeof(struct dirstack));
  tmp->prev = *ds;
  tmp->dir = (char *)malloc((strlen(dir)+1)*sizeof(char));
  strcpy(tmp->dir,dir);
  *ds = tmp;
  return(0);
}
static struct dirstack *rfio_popdir(ds)
struct dirstack **ds;
{
   struct dirstack *tmp;
   if ( ds == NULL ) return(NULL);
   tmp = *ds;
   *ds = (*ds)->prev;
   free(tmp->dir);
   free(tmp);
   return(*ds);
}

static int read_yesno() {
    int i, rc, retval;
    i =0;
    retval = 'n';
    do {
        rc = fgetc(stdin);
        if ( rc == ' ' ) continue;
        if ( i == 0 ) {
            retval = rc;
            i++;
        }
    } while ( rc != EOF && rc != '\n');
    return(retval);
}

static int rm_recursive(path, yesno) 
char *path;
int *yesno;
{
  DIR *dirp;
  struct dirent *de;
  struct stat64 st;
  char *p;
  struct dirstack *ds = NULL;
  int ask_yesno = 1;
  int empty = 1;
  
  if ( !rfio_lstat64(path,&st) ) {
    if ( S_ISDIR(st.st_mode) ) {
      if ( force == 0 && (yesno == NULL || *yesno) ) {
        printf("%s: descend into directory `%s'? ",cmd,path);
        if ( read_yesno() != 'y' ) return(-1);
        if ( yesno != NULL ) *yesno = 0;
      }

      dirp = (DIR *)rfio_opendir(path);
      while ( ( de = (struct dirent *)rfio_readdir((RDIR *)dirp) ) != NULL ) {
        if ( strcmp(de->d_name,".") && strcmp(de->d_name,"..") ) {
          empty = 0;
          p = (char *)malloc(strlen(path)+strlen(de->d_name)+2);
          strcpy(p,path);
          strcat(p,"/");
          strcat(p,de->d_name);
          if ( rfio_lstat64(p,&st) == -1 ) {
            fprintf(stderr,"%s: %s\n",p,rfio_serror());
            free(p);
          } else {
            if ( S_ISDIR(st.st_mode) ) {
              rfio_pushdir(&ds,p);
            } else {
              if ( rfio_unlink(p) ) {
                fprintf(stderr,"unlink(%s): %s\n",p,rfio_serror());
                exit(1);
              }
              free(p);
            } 
          }
        }
      }
      rfio_closedir((RDIR *)dirp);
      if ( empty ) {
        if ( force == 0 ) {
          printf("%s: remove directory `%s'? ",cmd,path);
          if ( read_yesno() != 'y' ) return(-1);
	}
        if ( rfio_rmdir(path) == -1 ) {
            fprintf(stderr,"rmdir(%s): %s\n",path,rfio_serror());
            exit(2);
        }
      }
    } else { /* if ( S_ISDIR(st.st_mode) ) ... */
      if ( rfio_unlink(path) ) {
        fprintf(stderr,"unlink(%s): %s\n",path,rfio_serror());
        exit(1);
      }
    }
    while ( ds != NULL ) {
      while ( rm_recursive(ds->dir,&ask_yesno) == 0 );
      rfio_popdir(&ds);
    }
  } else { /* if ( !rfio_lstat64(path,&st) ) .... */
    return(-1);
  }
  return(0);
}