source: trunk/athena/bin/delete/directories.c @ 23665

Revision 23665, 15.6 KB checked in by broder, 15 years ago (diff)
In delete: * Apply patches from jik: - Change the "st_chtime" structure member to "st_ctim" to avoid a potential header file conflict.
Line 
1/*
2 * $Id: directories.c,v 1.28 1999-01-22 23:08:54 ghudson Exp $
3 *
4 * This program is part of a package including delete, undelete,
5 * lsdel, expunge and purge.  The software suite is meant as a
6 * replacement for rm which allows for file recovery.
7 *
8 * Copyright (c) 1989 by the Massachusetts Institute of Technology.
9 * For copying and distribution information, see the file "mit-copying.h."
10 */
11
12#if !defined(lint) && !defined(SABER)
13     static char rcsid_directories_c[] = "$Id: directories.c,v 1.28 1999-01-22 23:08:54 ghudson Exp $";
14#endif
15
16#include <math.h>
17#include <stdio.h>
18#include <sys/types.h>
19#include <sys/param.h>
20#include <dirent.h>
21#include <string.h>
22#include <time.h>
23#include <errno.h>
24#include <com_err.h>
25#include "delete_errs.h"
26#include "util.h"
27#include "directories.h"
28#include "mit-copying.h"
29#include "errors.h"
30
31static filerec root_tree;
32static filerec cwd_tree;
33
34void free_leaf();
35
36 /* These are not static because external routines need to be able to */
37 /* access them.                                                      */
38time_t current_time;
39
40
41static filerec default_cwd = {
42     "",
43     (filerec *) NULL,
44     (filerec *) NULL,
45     (filerec *) NULL,
46     (filerec *) NULL,
47     (filerec *) NULL,
48     False,
49     False,
50     {0}
51};
52
53static filerec default_root = {
54     "/",
55     (filerec *) NULL,
56     (filerec *) NULL,
57     (filerec *) NULL,
58     (filerec *) NULL,
59     (filerec *) NULL,
60     False,
61     False,
62     {0}
63};
64
65static filerec default_directory = {
66     "",
67     (filerec *) NULL,
68     (filerec *) NULL,
69     (filerec *) NULL,
70     (filerec *) NULL,
71     (filerec *) NULL,
72     False,
73     False,
74     {0}
75};
76
77static filerec default_file = {
78     "",
79     (filerec *) NULL,
80     (filerec *) NULL,
81     (filerec *) NULL,
82     (filerec *) NULL,
83     (filerec *) NULL,
84     False,
85     False,
86     {0}
87};
88
89
90filerec *get_root_tree()
91{
92     return(&root_tree);
93}
94
95
96
97filerec *get_cwd_tree()
98{
99     return(&cwd_tree);
100}
101
102
103int initialize_tree()
104{
105     int retval;
106     
107     root_tree = default_root;
108     cwd_tree = default_cwd;
109
110     current_time = time((time_t *)0);
111     if (retval = get_specs(".", &cwd_tree.specs, FOLLOW_LINKS)) {
112          error("get_specs on .");
113          return retval;
114     }
115     if (retval = get_specs("/", &root_tree.specs, FOLLOW_LINKS)) {
116          error("get_specs on /");
117          return retval;
118     }
119     return 0;
120}
121
122
123int add_path_to_tree(path, leaf)
124char *path;
125filerec **leaf;
126{
127     filerec *parent;
128     char next_name[MAXNAMLEN];
129     char lpath[MAXPATHLEN], built_path[MAXPATHLEN], *ptr;
130     struct mystat specs;
131     int retval;
132     
133     if (retval = get_specs(path, &specs, DONT_FOLLOW_LINKS)) {
134          char error_buf[MAXPATHLEN+14];
135
136          (void) sprintf(error_buf, "get_specs on %s", path);
137          error(error_buf);
138          return retval;
139     }
140     
141     (void) strcpy(lpath, path); /* we don't want to damage the user's */
142                                 /* string */
143     ptr = lpath;
144     if (*ptr == '/') {
145          parent = &root_tree;
146          ptr++;
147          (void) strcpy(built_path, "/");
148     }
149     else if (! strncmp(ptr, "./", 2)) {
150          parent = &cwd_tree;
151          ptr += 2;
152          *built_path = '\0';
153     }
154     else {
155          parent = &cwd_tree;
156          *built_path = '\0';
157     }
158     
159     (void) strcpy(next_name, firstpart(ptr, ptr));
160     while (*ptr) {
161          (void) strcat(built_path, next_name);
162          if (retval = add_directory_to_parent(parent, next_name, False,
163                                               &parent)) {
164               error("add_directory_to_parent");
165               return retval;
166          }
167          (void) strcpy(next_name, firstpart(ptr, ptr));
168          if (retval = get_specs(built_path, &parent->specs, FOLLOW_LINKS)) {
169               char error_buf[MAXPATHLEN+14];
170
171               (void) sprintf(error_buf, "get_specs on %s", built_path);
172               error(error_buf);
173               return retval;
174          }
175          (void) strcat(built_path, "/");
176     }
177     if ((specs.st_mode & S_IFMT) == S_IFDIR) {
178          retval = add_directory_to_parent(parent, next_name, True, leaf);
179          if (retval) {
180               error("add_directory_to_parent");
181               return retval;
182          }
183     }
184     else {
185          retval = add_file_to_parent(parent, next_name, True, leaf);
186          if (retval) {
187               error("add_file_to_parent");
188               return retval;
189          }
190     }         
191
192     (*leaf)->specs = specs;
193
194     return 0;
195}
196
197
198
199int get_specs(path, specs, follow)
200char *path;
201struct mystat *specs;
202int follow; /* follow symlinks or not? */
203{
204     int status;
205     struct stat realspecs;
206     
207     if (strlen(path)) if ((path[strlen(path) - 1] == '/') &&
208                           (strlen(path) != 1))
209          path[strlen(path) - 1] = '\0';
210     if (follow == FOLLOW_LINKS)
211          status = stat(path, &realspecs);
212     else
213          status = lstat(path, &realspecs);
214
215     if (status) {
216          set_error(errno);
217          error(path);
218          return error_code;
219     }
220
221     specs->st_dev = realspecs.st_dev;
222     specs->st_ino = realspecs.st_ino;
223     specs->st_mode = realspecs.st_mode;
224     specs->st_size = realspecs.st_size;
225     specs->st_ctim = realspecs.st_ctime;
226     specs->st_blocks = realspecs.st_blocks;
227
228     return 0;
229}
230
231
232
233filerec *next_leaf(leaf)
234filerec *leaf;
235{
236     filerec *new;
237
238     if ((leaf->specs.st_mode & S_IFMT) == S_IFDIR) {
239          new = first_in_directory(leaf);
240          if (new)
241               return(new);
242          new = next_directory(leaf);
243          return(new);
244     }
245     else {
246          new = next_in_directory(leaf);
247          return(new);
248     }
249}
250
251
252filerec *next_specified_leaf(leaf)
253filerec *leaf;
254{
255     while (leaf = next_leaf(leaf))
256     if (leaf->specified)
257          return(leaf);
258     return((filerec *) NULL);
259}
260
261
262filerec *next_directory(leaf)
263filerec *leaf;
264{
265     filerec *ret;
266     if ((leaf->specs.st_mode & S_IFMT) != S_IFDIR)
267          leaf = leaf->parent;
268     if (leaf)
269          ret = leaf->next;
270     else
271          ret = (filerec *) NULL;
272     if (ret) if (ret->freed)
273          ret = next_directory(ret);
274     return(ret);
275}
276
277
278filerec *next_specified_directory(leaf)
279filerec *leaf;
280{
281     while (leaf = next_directory(leaf))
282          if (leaf->specified)
283               return(leaf);
284     return ((filerec *) NULL);
285}
286
287
288
289filerec *next_in_directory(leaf)
290filerec *leaf;
291{
292     filerec *ret;
293
294     if (leaf->next)
295          ret = leaf->next;
296     else if (((leaf->specs.st_mode & S_IFMT) != S_IFDIR) && leaf->parent)
297          ret = leaf->parent->dirs;
298     else
299          ret = (filerec *) NULL;
300     if (ret) if (ret->freed)
301          ret = next_in_directory(ret);
302     return (ret);
303}
304
305
306
307
308filerec *next_specified_in_directory(leaf)
309filerec *leaf;
310{
311     while (leaf = next_in_directory(leaf))
312          if (leaf->specified)
313               return(leaf);
314     return ((filerec *) NULL);
315}
316
317
318
319filerec *first_in_directory(leaf)
320filerec *leaf;
321{
322     filerec *ret;
323
324     if ((leaf->specs.st_mode & S_IFMT) != S_IFDIR)
325          ret = (filerec *) NULL;
326     else if (leaf->files)
327          ret = leaf->files;
328     else if (leaf->dirs)
329          ret =  leaf->dirs;
330     else
331          ret = (filerec *) NULL;
332     if (ret) if (ret->freed)
333          ret = next_in_directory(ret);
334     return(ret);
335}
336
337
338filerec *first_specified_in_directory(leaf)
339filerec *leaf;
340{
341     leaf = first_in_directory(leaf);
342     if (! leaf)
343          return((filerec *) NULL);
344     
345     if (leaf->specified)
346          return(leaf);
347     else
348          leaf = next_specified_in_directory(leaf);
349     return (leaf);
350}
351
352
353void print_paths_from(leaf)
354filerec *leaf;
355{
356     /*
357      * This is static to prevent multiple copies of it when calling
358      * recursively.
359      */
360     static char buf[MAXPATHLEN];
361
362     printf("%s\n", get_leaf_path(leaf, buf));
363     if (leaf->dirs)
364          print_paths_from(leaf->dirs);
365     if (leaf->files)
366          print_paths_from(leaf->files);
367     if (leaf->next)
368          print_paths_from(leaf->next);
369}
370
371
372void print_specified_paths_from(leaf)
373filerec *leaf;
374{
375     /*
376      * This is static to prevent multiple copies of it when calling
377      * recursively.
378      */
379     static char buf[MAXPATHLEN];
380
381     if (leaf->specified)
382          printf("%s\n", get_leaf_path(leaf, buf));
383     if (leaf->dirs)
384          print_specified_paths_from(leaf->dirs);
385     if (leaf->files)
386          print_specified_paths_from(leaf->files);
387     if (leaf->next)
388          print_specified_paths_from(leaf->next);
389}
390     
391
392int add_file_to_parent(parent, name, specified, last)
393filerec *parent, **last;
394char *name;
395Boolean specified;
396{
397     filerec *files;
398
399     *last = files = (filerec *) NULL;
400     files = parent->files;
401     while (files) {
402          if (! strcmp(files->name, name))
403               break;
404          *last = files;
405          files = files->next;
406     }
407     if (files) {
408          files->specified = (files->specified || specified);
409          *last = files;
410          return 0;
411     }
412     if (*last) {
413          (*last)->next = (filerec *) Malloc((unsigned) sizeof(filerec));
414          if (! (*last)->next) {
415               set_error(errno);
416               error("Malloc");
417               return error_code;
418          }
419          *(*last)->next = default_file;
420          (*last)->next->previous = *last;
421          (*last)->next->parent = parent;
422          (*last) = (*last)->next;
423     }
424     else {
425          parent->files = (filerec *) Malloc(sizeof(filerec));
426          if (! parent->files) {
427               set_error(errno);
428               error("Malloc");
429               return error_code;
430          }
431          *parent->files = default_file;
432          parent->files->parent = parent;
433          parent->files->previous = (filerec *) NULL;
434          *last = parent->files;
435     }
436     (void) strcpy((*last)->name, name);
437     (*last)->specified = specified;
438     return 0;
439}
440
441
442
443
444
445int add_directory_to_parent(parent, name, specified, last)
446filerec *parent, **last;
447char *name;
448Boolean specified;
449{
450     filerec *directories;
451
452     *last = (filerec *) NULL;
453     directories = parent->dirs;
454     while (directories) {
455          if (! strcmp(directories->name, name))
456               break;
457          (*last) = directories;
458          directories = directories->next;
459     }
460     if (directories) {
461          directories->specified = (directories->specified || specified);
462          *last = directories;
463          return 0;
464     }
465     if (*last) {
466          (*last)->next = (filerec *) Malloc(sizeof(filerec));
467          if (! (*last)->next) {
468               set_error(errno);
469               error("Malloc");
470               return error_code;
471          }
472          *(*last)->next = default_directory;
473          (*last)->next->previous = *last;
474          (*last)->next->parent = parent;
475          (*last) = (*last)->next;
476     }
477     else {
478          parent->dirs = (filerec *) Malloc(sizeof(filerec));
479          if (! parent->dirs) {
480               set_error(errno);
481               error("Malloc");
482               return error_code;
483          }
484          *parent->dirs = default_directory;
485          parent->dirs->parent = parent;
486          parent->dirs->previous = (filerec *) NULL;
487          (*last) = parent->dirs;
488     }
489     (void) strcpy((*last)->name, name);
490     (*last)->specified = specified;
491     return 0;
492}
493
494
495
496
497
498void free_leaf(leaf)
499filerec *leaf;
500{
501     leaf->freed = True;
502     if (! (leaf->dirs || leaf->files)) {
503          if (leaf->previous)
504               leaf->previous->next = leaf->next;
505          if (leaf->next)
506               leaf->next->previous = leaf->previous;
507          if (leaf->parent) {
508               if ((leaf->specs.st_mode & S_IFMT) == S_IFDIR) {
509                    if (leaf->parent->dirs == leaf) {
510                         leaf->parent->dirs = leaf->next;
511                         if (leaf->parent->freed)
512                              free_leaf(leaf->parent);
513                    }
514               }
515               else {
516                    if (leaf->parent->files == leaf) {
517                         leaf->parent->files = leaf->next;
518                         if (leaf->parent->freed)
519                              free_leaf(leaf->parent);
520                    }
521               }
522               free((char *) leaf);
523          }
524     }
525}     
526
527
528
529int find_child(directory, name, child)
530filerec *directory, **child;
531char *name;
532{
533     filerec *ptr;
534     
535     *child = (filerec *) NULL;
536     if ((directory->specs.st_mode & S_IFMT) != S_IFDIR)
537          return DIR_NOT_DIRECTORY;
538     ptr = directory->dirs;
539     while (ptr)
540          if (strcmp(ptr->name, name))
541               ptr = ptr->next;
542          else
543               break;
544     if (ptr) {
545          *child = ptr;
546          return DIR_MATCH;
547     }
548     ptr = directory->files;
549     while (ptr)
550          if (strcmp(ptr->name, name))
551               ptr = ptr->next;
552          else
553               break;
554     if (ptr) {
555          *child = ptr;
556          return DIR_MATCH;
557     }
558     set_status(DIR_NO_MATCH);
559     return DIR_NO_MATCH;
560}
561
562
563
564
565
566int change_path(old_path, new_path)
567char *old_path, *new_path;
568{
569     char next_old[MAXNAMLEN], next_new[MAXNAMLEN];
570     char rest_old[MAXPATHLEN], rest_new[MAXPATHLEN];
571     int retval;
572     filerec *current;
573     
574     if (*old_path == '/') {
575          current = &root_tree;
576          old_path++;
577          new_path++;
578     }
579     else if (! strncmp(old_path, "./", 2)) {
580          current = &cwd_tree;
581          old_path += 2;
582          new_path += 2;
583     }
584     else
585          current = &cwd_tree;
586     
587     (void) strcpy(next_old, firstpart(old_path, rest_old));
588     (void) strcpy(next_new, firstpart(new_path, rest_new));
589     while (*next_old && *next_new) {
590          retval = find_child(current, next_old, &current);
591          if (retval == DIR_MATCH) {
592               if (current) {
593                    (void) strcpy(current->name, next_new);
594                    current->specified = False;
595               }
596               else {
597                    set_error(INTERNAL_ERROR);
598                    error("change_path");
599                    return error_code;
600               }
601          }
602          else {
603               error("change_path");
604               return retval;
605          }
606         
607          (void) strcpy(next_old, firstpart(rest_old, rest_old));
608          (void) strcpy(next_new, firstpart(rest_new, rest_new));
609     }
610     return 0;
611}
612
613
614int get_leaf_path(leaf, leaf_buf)
615filerec *leaf;
616char leaf_buf[]; /* RETURN */
617{
618     char *name_ptr;
619
620     name_ptr = Malloc(1);
621     if (! name_ptr) {
622          set_error(errno);
623          error("Malloc");
624          *leaf_buf = '\0';
625          return error_code;
626     }
627     *name_ptr = '\0';
628     do {
629          name_ptr = realloc((char *) name_ptr, (unsigned)
630                             (strlen(leaf->name) + strlen(name_ptr) + 2));
631          if (! name_ptr) {
632               set_error(errno);
633               *leaf_buf = '\0';
634               error("realloc");
635               return error_code;
636          }
637          (void) strcpy(leaf_buf, name_ptr);
638          *name_ptr = '\0';
639          if (leaf->parent) if (leaf->parent->parent)
640               (void) strcat(name_ptr, "/");
641          (void) strcat(name_ptr, leaf->name);
642          (void) strcat(name_ptr, leaf_buf);
643          leaf = leaf->parent;
644     } while (leaf);
645     (void) strcpy(leaf_buf, name_ptr);
646     return 0;
647}
648
649
650
651
652
653int accumulate_names(leaf, in_strings, num)
654filerec *leaf;
655char ***in_strings;
656int *num;
657{
658     char **strings;
659     int retval;
660     
661     strings = *in_strings;
662     if (leaf->specified) {
663          /*
664           * This array is static so that only one copy of it is allocated,
665           * rather than one copy on the stack for each recursive
666           * invocation of accumulate_names.
667           */
668          static char newname[MAXPATHLEN];
669
670          *num += 1;
671          strings = (char **) realloc((char *) strings, (unsigned)
672                                      (sizeof(char *) * (*num)));
673          if (! strings) {
674               set_error(errno);
675               error("realloc");
676               return error_code;
677          }
678          if (retval = get_leaf_path(leaf, newname)) {
679               error("get_leaf_path");
680               return retval;
681          }
682          (void) convert_to_user_name(newname, newname);
683          strings[*num - 1] = Malloc((unsigned) (strlen(newname) + 1));
684          if (! strings[*num - 1]) {
685               set_error(errno);
686               error("Malloc");
687               return error_code;
688          }
689          (void) strcpy(strings[*num - 1], newname);
690     }
691     if (leaf->files) if (retval = accumulate_names(leaf->files, &strings,
692                                                    num)) {
693          error("accumulate_names");
694          return retval;
695     }
696     if (leaf->dirs) if (retval = accumulate_names(leaf->dirs, &strings,
697                                                   num)) {
698          error("accumulate_names");
699          return retval;
700     }
701     if (leaf->next) if (retval = accumulate_names(leaf->next, &strings,
702                                                   num)) {
703          error("accumulate_names");
704          return retval;
705     }
706
707     *in_strings = strings;
708     return 0;
709}
710
711char *bytes_to_friendly(off_t bytes)
712{
713  double size = bytes;
714  char *output;
715
716  size /= 1024;
717  if (size < 1024) {
718    if (! (output = malloc(7))) {
719      set_error(errno);
720      error("malloc");
721      return "";
722    }
723    sprintf(output, "%.0fKB", ceil(size));
724    return(output);
725  }
726  size /= 1024;
727  if (size < 1024) {
728    if (! (output = malloc(9))) {
729      set_error(errno);
730      error("malloc");
731      return "";
732    }
733    sprintf(output, "%.1fMB", size);
734    return(output);
735  }
736  size /= 1024;
737  if (! (output = malloc((int)log10(size)+6))) {
738    set_error(errno);
739    error("malloc");
740    return "";
741  }
742  sprintf(output, "%.2fGB", size);
743  return(output);
744}
Note: See TracBrowser for help on using the repository browser.