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