]> arthur.barton.de Git - netatalk.git/blob - bin/adv1tov2/adv1tov2.c
warnings removal patch (#420300) from Sebastian Rittau (srittau)
[netatalk.git] / bin / adv1tov2 / adv1tov2.c
1 /* v1tov2: given a root directory, run down and convert all the
2  * files/directories into appledouble v2.  */
3
4 #ifdef HAVE_CONFIG_H
5 #include "config.h"
6 #endif
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <dirent.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <ctype.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <sys/param.h>
17
18 #include <atalk/adouble.h>
19
20 #if AD_VERSION == AD_VERSION2
21 /* translate characters */
22 static int xlate(char *name, int flags) {
23   static const char hexdig[] = "0123456789abcdef";
24   char upath[MAXPATHLEN + 1];
25   char *m, *u;
26   int doit = 0;
27
28   m = name;
29   u = upath;
30   while (*m != '\0') {
31     if (isascii(*m) && !isprint(*m)) {
32       doit = 1;
33       *u++ = ':';
34       *u++ = hexdig[(*m & 0xf0) >> 4];
35       *u++ = hexdig[*m & 0x0f];
36     } else
37       *u++ = *m;
38     m++;
39   }
40   
41   if (doit) {
42     *u = '\0';
43     rename(name, upath);
44     if ((flags & ADFLAGS_DIR) == 0)
45       rename(ad_path(name, flags), ad_path(upath, flags)); /* rename rfork */
46   }
47 }
48
49
50 #define MAXDESCEND 0xFFFF
51 /* recursively descend subdirectories. 
52  * oh the stack space we use up! */
53 void descend(DIR *dp)
54 {
55   DIR *dpnew;
56   struct dirent *de;
57   unsigned char *m, *u;
58   struct stat st;
59   struct adouble ad;
60   int flags;
61   static int count = 0;
62
63   if (count++ > MAXDESCEND) {
64     fprintf(stderr, "FAILURE: too many subdirectories! possible infinite recursion.");
65     return;
66   }
67     
68   putc('(', stderr);
69   for (de = readdir(dp); de; de = readdir(dp)) {
70     if (de->d_name[0] == '.') 
71       continue;
72
73     if (stat(de->d_name, &st) < 0) {
74       fprintf(stderr, "FAILURE: can't stat %s\n", de->d_name);
75       continue;
76     }
77
78     /* go down subdirectory */
79     flags = ADFLAGS_HF;
80     if (S_ISDIR(st.st_mode) && (dpnew = opendir(de->d_name))) {
81       chdir(de->d_name);
82       descend(dpnew);
83       closedir(dpnew);
84       chdir("..");
85       flags |= ADFLAGS_DIR;
86     }
87
88     if (ad_open(de->d_name, flags, O_RDWR, 0, &ad) < 0) {
89       fprintf(stderr, "FAILURE: can't convert %s\n", de->d_name);
90       continue;
91     }
92     ad_close(&ad, ADFLAGS_HF);
93     xlate(de->d_name, flags);
94     fputc('.', stderr);
95   }
96   putc(')', stderr);
97 }
98
99
100 int main(int argc, char **argv)
101 {
102   DIR           *dp;
103   struct adouble ad;
104  
105   if (argc != 2) {
106     fprintf(stderr, "%s <directory>\n", *argv);
107     return -1;
108   }
109     
110   /* convert main directory */
111   if (ad_open(argv[1], ADFLAGS_HF | ADFLAGS_DIR, O_RDWR, 0, 
112               &ad) < 0) {
113     fprintf(stderr, "FAILURE: can't convert %s\n", argv[1]);
114     return -1;
115   }
116
117   ad_close(&ad, ADFLAGS_HF);
118   if ((dp = opendir(argv[1])) == NULL) {
119     fprintf(stderr, "%s: unable to open %s\n", *argv, argv[1]);
120     return -1;
121   }
122
123   chdir(argv[1]);
124   descend(dp);
125   closedir(dp);
126   chdir("..");
127
128   xlate(argv[1], ADFLAGS_HF | ADFLAGS_DIR);
129   putc('\n', stderr);
130   return 0;
131 }
132
133 #else
134 int main(int argc, char **argv)
135 {
136   fprintf(stderr, "%s not built for v2 AppleDouble files.\n", *argv);
137   return -1;
138 }
139 #endif