1/* 2 * $Id: adv1tov2.c,v 1.5 2009-10-14 01:38:28 didg 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 */ 33static 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#define MAXDESCEND 0xFFFF 62/* recursively descend subdirectories. 63 * oh the stack space we use up! */ 64static void descend(DIR *dp) 65{ 66 DIR *dpnew; 67 struct dirent *de; 68 struct stat st; 69 struct adouble ad; 70 int flags; 71 static int count = 0; 72 73 ad_init(&ad, AD_VERSION2, 0); 74 if (count++ > MAXDESCEND) { 75 fprintf(stderr, "FAILURE: too many subdirectories! possible infinite recursion."); 76 return; 77 } 78 79 putc('(', stderr); 80 for (de = readdir(dp); de; de = readdir(dp)) { 81 if (de->d_name[0] == '.') 82 continue; 83 84 if (stat(de->d_name, &st) < 0) { 85 fprintf(stderr, "FAILURE: can't stat %s\n", de->d_name); 86 continue; 87 } 88 89 /* go down subdirectory */ 90 flags = ADFLAGS_HF; 91 if (S_ISDIR(st.st_mode) && (dpnew = opendir(de->d_name))) { 92 chdir(de->d_name); 93 descend(dpnew); 94 closedir(dpnew); 95 chdir(".."); 96 flags |= ADFLAGS_DIR; 97 } 98 99 if (ad_open(de->d_name, flags, O_RDWR, 0, &ad) < 0) { 100 if (errno != ENOENT) 101 fprintf(stderr, "\nFAILURE: can't convert %s, %s\n", fullpathname(de->d_name), strerror(errno)); 102 continue; 103 } 104 ad_close(&ad, ADFLAGS_HF); 105 fputc('.', stderr); 106 } 107 putc(')', stderr); 108} 109 110 111int main(int argc, char **argv) 112{ 113 DIR *dp; 114 struct adouble ad; 115 116 ad_init(&ad, AD_VERSION2, 0); 117 if (argc != 2) { 118 fprintf(stderr, "%s <directory>\n", *argv); 119 return -1; 120 } 121 122 /* convert main directory */ 123 if (ad_open(argv[1], ADFLAGS_HF | ADFLAGS_DIR, O_RDWR, 0, 124 &ad) < 0) { 125 fprintf(stderr, "FAILURE: can't convert %s\n", argv[1]); 126 return -1; 127 } 128 129 ad_close(&ad, ADFLAGS_HF); 130 if ((dp = opendir(argv[1])) == NULL) { 131 fprintf(stderr, "%s: unable to open %s\n", *argv, argv[1]); 132 return -1; 133 } 134 135 chdir(argv[1]); 136 descend(dp); 137 closedir(dp); 138 chdir(".."); 139 140 putc('\n', stderr); 141 return 0; 142} 143 144#else /* AD_VERSION == AD_VERSION2 */ 145int main(int argc, char **argv) 146{ 147 fprintf(stderr, "%s not built for v2 AppleDouble files.\n", *argv); 148 return -1; 149} 150#endif /* AD_VERSION == AD_VERSION2 */ 151