2 This program is free software; you can redistribute it and/or modify
3 it under the terms of the GNU General Public License as published by
4 the Free Software Foundation; either version 2 of the License, or
5 (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 .volinfo file handling, command line utilities
17 copyright Bjoern Fernhomberg, 2004
22 #endif /* HAVE_CONFIG_H */
36 #include <sys/param.h>
38 #include <atalk/adouble.h>
39 #include <atalk/util.h>
40 #include <atalk/logger.h>
41 #include <atalk/volinfo.h>
43 #include <atalk/cnid.h>
56 typedef struct _info_option {
61 static const _info_option info_options[] = {
62 {"MAC_CHARSET", MAC_CHARSET},
63 {"VOL_CHARSET", VOL_CHARSET},
64 {"ADOUBLE_VER", ADOUBLE_VER},
65 {"CNIDBACKEND", CNIDBACKEND},
66 {"CNIDDBDHOST", CNIDDBDHOST},
67 {"CNIDDBDPORT", CNIDDBDPORT},
68 {"CNID_DBPATH", CNID_DBPATH},
69 {"VOLUME_OPTS", VOLUME_OPTS},
70 {"VOLCASEFOLD", VOLCASEFOLD},
74 typedef struct _vol_opt_name {
75 const u_int32_t option;
79 static const _vol_opt_name vol_opt_names[] = {
80 {AFPVOL_A2VOL, "PRODOS"}, /* prodos volume */
81 {AFPVOL_CRLF, "CRLF"}, /* cr/lf translation */
82 {AFPVOL_NOADOUBLE, "NOADOUBLE"}, /* don't create .AppleDouble by default */
83 {AFPVOL_RO, "READONLY"}, /* read-only volume */
84 {AFPVOL_MSWINDOWS, "MSWINDOWS"}, /* deal with ms-windows yuckiness. this is going away. */
85 {AFPVOL_NOHEX, "NOHEX"}, /* don't do :hex translation */
86 {AFPVOL_USEDOTS, "USEDOTS"}, /* use real dots */
87 {AFPVOL_LIMITSIZE, "LIMITSIZE"}, /* limit size for older macs */
88 {AFPVOL_MAPASCII, "MAPASCII"}, /* map the ascii range as well */
89 {AFPVOL_DROPBOX, "DROPBOX"}, /* dropkludge dropbox support */
90 {AFPVOL_NOFILEID, "NOFILEID"}, /* don't advertise createid resolveid and deleteid calls */
91 {AFPVOL_NOSTAT, "NOSTAT"}, /* advertise the volume even if we can't stat() it
92 * maybe because it will be mounted later in preexec */
93 {AFPVOL_UNIX_PRIV, "UNIXPRIV"}, /* support unix privileges */
94 {AFPVOL_NODEV, "NODEV"}, /* always use 0 for device number in cnid calls */
95 {AFPVOL_CASEINSEN, "CASEINSENSITIVE"}, /* volume is case insensitive */
96 {AFPVOL_EILSEQ, "ILLEGALSEQ"}, /* encode illegal sequence */
97 {AFPVOL_CACHE, "CACHEID"}, /* Use adouble v2 CNID caching, default don't use it */
98 {AFPVOL_INV_DOTS, "INVISIBLEDOTS"},
99 {AFPVOL_ACLS, "ACLS"}, /* Vol supports ACLs */
100 {AFPVOL_TM, "TM"}, /* Set "kSupportsTMLockSteal" is volume attributes */
104 static const _vol_opt_name vol_opt_casefold[] = {
105 {AFPVOL_MTOUUPPER, "MTOULOWER"},
106 {AFPVOL_MTOULOWER, "MTOULOWER"},
107 {AFPVOL_UTOMUPPER, "UTOMUPPER"},
108 {AFPVOL_UTOMLOWER, "UTOMLOWER"},
112 static char* find_in_path( char *path, char *subdir, size_t maxlen)
117 strlcat(path, subdir, maxlen);
118 pos = strrchr(path, '/');
120 while ( stat(path, &st) != 0) {
122 if ((pos = strrchr(path, '/'))) {
124 strlcat(path, subdir, maxlen);
131 path[pos-path] = '/';
132 path[pos-path+1] = 0;
137 static char * make_path_absolute(char *path, size_t bufsize)
140 char savecwd[MAXPATHLEN];
141 char abspath[MAXPATHLEN];
144 if (stat(path, &st) != 0) {
148 strlcpy (abspath, path, sizeof(abspath));
150 if (!S_ISDIR(st.st_mode)) {
151 if (NULL == (p=strrchr(abspath, '/')) )
152 strcpy(abspath, ".");
157 if (!getcwd(savecwd, sizeof(savecwd)) || chdir(abspath) < 0)
160 if (!getcwd(abspath, sizeof(abspath)) || chdir (savecwd) < 0)
163 if (strlen(abspath) > bufsize)
166 strlcpy(path, abspath, bufsize);
170 static char * find_volumeroot(char *path, size_t maxlen)
172 char *volume = make_path_absolute(path, maxlen);
177 if (NULL == (find_in_path(volume, "/.AppleDesktop", maxlen)) )
183 int vol_load_charsets( struct volinfo *vol)
185 if ( (charset_t) -1 == ( vol->v_maccharset = add_charset(vol->v_maccodepage)) ) {
186 fprintf( stderr, "Setting codepage %s as Mac codepage failed", vol->v_maccodepage);
190 if ( (charset_t) -1 == ( vol->v_volcharset = add_charset(vol->v_volcodepage)) ) {
191 fprintf( stderr, "Setting codepage %s as volume codepage failed", vol->v_volcodepage);
198 static int parse_options (char *buf, int *flags, const _vol_opt_name* options)
201 const _vol_opt_name *op;
205 while ( *p != '\0') {
209 for (;op->name; op++) {
210 if ( !strcmp(op->name, q )) {
211 *flags |= op->option;
225 static int parseline ( char *buf, struct volinfo *vol)
230 const _info_option *p = &info_options[0];
232 if (NULL == ( value = strchr(buf, ':')) )
238 if ( 0 == (len = strlen(value)) )
241 if (value[len-1] == '\n')
244 for (;p->name; p++) {
245 if ( !strcmp(p->name, buf )) {
253 if ((vol->v_maccodepage = strdup(value)) == NULL) {
254 fprintf (stderr, "strdup: %s", strerror(errno));
259 if ((vol->v_volcodepage = strdup(value)) == NULL) {
260 fprintf (stderr, "strdup: %s", strerror(errno));
265 if ((vol->v_cnidscheme = strdup(value)) == NULL) {
266 fprintf (stderr, "strdup: %s", strerror(errno));
271 if ((vol->v_dbd_host = strdup(value)) == NULL) {
272 fprintf (stderr, "strdup: %s", strerror(errno));
277 vol->v_dbd_port = atoi(value);
280 if ((vol->v_dbpath = strdup(value)) == NULL) {
281 fprintf (stderr, "strdup: %s", strerror(errno));
286 if (strcasecmp(value, "v1") == 0) {
287 vol->v_adouble = AD_VERSION1;
288 vol->ad_path = ad_path;
290 #if AD_VERSION == AD_VERSION2
291 else if (strcasecmp(value, "v2") == 0) {
292 vol->ad_path = ad_path;
293 vol->v_adouble = AD_VERSION2;
295 else if (strcasecmp(value, "osx") == 0) {
296 vol->v_adouble = AD_VERSION2_OSX;
297 vol->ad_path = ad_path_osx;
301 fprintf (stderr, "unknown adouble version: %s, %s", buf, value);
306 parse_options(value, &vol->v_flags, &vol_opt_names[0]);
309 parse_options(value, &vol->v_casefold, &vol_opt_casefold[0]);
312 fprintf (stderr, "unknown volume information: %s, %s", buf, value);
321 int loadvolinfo (char *path, struct volinfo *vol)
324 char volinfofile[MAXPATHLEN];
325 char buf[MAXPATHLEN];
333 memset(vol, 0, sizeof(struct volinfo));
334 strlcpy(volinfofile, path, sizeof(volinfofile));
336 /* volinfo file is in .AppleDesktop */
337 if ( NULL == find_volumeroot(volinfofile, sizeof(volinfofile)))
340 if ((vol->v_path = strdup(volinfofile)) == NULL ) {
341 fprintf (stderr, "strdup: %s", strerror(errno));
344 strlcat(volinfofile, ".AppleDesktop/", sizeof(volinfofile));
345 strlcat(volinfofile, VOLINFOFILE, sizeof(volinfofile));
347 /* open the file read only */
348 if ( NULL == (fp = fopen( volinfofile, "r")) ) {
349 fprintf (stderr, "error opening volinfo (%s): %s", volinfofile, strerror(errno));
354 /* try to get a read lock */
356 lock.l_whence = SEEK_SET;
358 lock.l_type = F_RDLCK;
360 /* wait for read lock */
361 if (fcntl(fd, F_SETLKW, &lock) < 0) {
367 while (NULL != fgets(buf, sizeof(buf), fp)) {
372 lock.l_type = F_UNLCK;
373 fcntl(fd, F_SETLK, &lock);
375 /* Translate vol options to ad options like afp/volume.c does it */
376 vol->v_ad_options = 0;
377 if ((vol->v_flags & AFPVOL_NODEV))
378 vol->v_ad_options |= ADVOL_NODEV;
379 if ((vol->v_flags & AFPVOL_CACHE))
380 vol->v_ad_options |= ADVOL_CACHE;
381 if ((vol->v_flags & AFPVOL_UNIX_PRIV))
382 vol->v_ad_options |= ADVOL_UNIXPRIV;
383 if ((vol->v_flags & AFPVOL_INV_DOTS))
384 vol->v_ad_options |= ADVOL_INVDOTS;