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>
42 #include <atalk/volume.h>
44 #include <atalk/cnid.h>
47 static const vol_opt_name_t vol_opt_names[] = {
48 {AFPVOL_A2VOL, "PRODOS"}, /* prodos volume */
49 {AFPVOL_CRLF, "CRLF"}, /* cr/lf translation */
50 {AFPVOL_NOADOUBLE, "NOADOUBLE"}, /* don't create .AppleDouble by default */
51 {AFPVOL_RO, "READONLY"}, /* read-only volume */
52 {AFPVOL_MSWINDOWS, "MSWINDOWS"}, /* deal with ms-windows yuckiness. this is going away. */
53 {AFPVOL_NOHEX, "NOHEX"}, /* don't do :hex translation */
54 {AFPVOL_USEDOTS, "USEDOTS"}, /* use real dots */
55 {AFPVOL_LIMITSIZE, "LIMITSIZE"}, /* limit size for older macs */
56 {AFPVOL_MAPASCII, "MAPASCII"}, /* map the ascii range as well */
57 {AFPVOL_DROPBOX, "DROPBOX"}, /* dropkludge dropbox support */
58 {AFPVOL_NOFILEID, "NOFILEID"}, /* don't advertise createid resolveid and deleteid calls */
59 {AFPVOL_NOSTAT, "NOSTAT"}, /* advertise the volume even if we can't stat() it
60 * maybe because it will be mounted later in preexec */
61 {AFPVOL_UNIX_PRIV, "UNIXPRIV"}, /* support unix privileges */
62 {AFPVOL_NODEV, "NODEV"}, /* always use 0 for device number in cnid calls */
63 {AFPVOL_CASEINSEN, "CASEINSENSITIVE"}, /* volume is case insensitive */
64 {AFPVOL_EILSEQ, "ILLEGALSEQ"}, /* encode illegal sequence */
65 {AFPVOL_CACHE, "CACHEID"}, /* Use adouble v2 CNID caching, default don't use it */
66 {AFPVOL_INV_DOTS, "INVISIBLEDOTS"},
67 {AFPVOL_ACLS, "ACLS"}, /* Vol supports ACLs */
68 {AFPVOL_TM, "TM"}, /* Set "kSupportsTMLockSteal" is volume attributes */
69 {AFPVOL_EJECT, "EJECT"}, /* Ejectable media eg CD -> in memory CNID db */
73 static const vol_opt_name_t vol_opt_casefold[] = {
74 {AFPVOL_MTOUUPPER, "MTOULOWER"},
75 {AFPVOL_MTOULOWER, "MTOULOWER"},
76 {AFPVOL_UTOMUPPER, "UTOMUPPER"},
77 {AFPVOL_UTOMLOWER, "UTOMLOWER"},
97 static const info_option_t info_options[] = {
98 {"MAC_CHARSET", MAC_CHARSET},
99 {"VOL_CHARSET", VOL_CHARSET},
100 {"ADOUBLE_VER", ADOUBLE_VER},
101 {"CNIDBACKEND", CNIDBACKEND},
102 {"CNIDDBDHOST", CNIDDBDHOST},
103 {"CNIDDBDPORT", CNIDDBDPORT},
104 {"CNID_DBPATH", CNID_DBPATH},
105 {"VOLUME_OPTS", VOLUME_OPTS},
106 {"VOLCASEFOLD", VOLCASEFOLD},
107 {"EXTATTRTYPE", EXTATTRTYPE},
111 static char* find_in_path( char *path, char *subdir, size_t maxlen)
116 strlcat(path, subdir, maxlen);
117 pos = strrchr(path, '/');
119 while ( stat(path, &st) != 0) {
121 if ((pos = strrchr(path, '/'))) {
123 strlcat(path, subdir, maxlen);
130 path[pos-path] = '/';
131 path[pos-path+1] = 0;
136 static char * make_path_absolute(char *path, size_t bufsize)
139 char savecwd[MAXPATHLEN];
140 char abspath[MAXPATHLEN];
143 if (stat(path, &st) != 0) {
147 strlcpy (abspath, path, sizeof(abspath));
149 if (!S_ISDIR(st.st_mode)) {
150 if (NULL == (p=strrchr(abspath, '/')) )
151 strcpy(abspath, ".");
156 if (!getcwd(savecwd, sizeof(savecwd)) || chdir(abspath) < 0)
159 if (!getcwd(abspath, sizeof(abspath)) || chdir (savecwd) < 0)
162 if (strlen(abspath) > bufsize)
165 strlcpy(path, abspath, bufsize);
169 static char * find_volumeroot(char *path, size_t maxlen)
171 char *volume = make_path_absolute(path, maxlen);
176 if (NULL == (find_in_path(volume, "/.AppleDesktop", maxlen)) )
182 int vol_load_charsets( struct volinfo *vol)
184 if ( (charset_t) -1 == ( vol->v_maccharset = add_charset(vol->v_maccodepage)) ) {
185 fprintf( stderr, "Setting codepage %s as Mac codepage failed", vol->v_maccodepage);
189 if ( (charset_t) -1 == ( vol->v_volcharset = add_charset(vol->v_volcodepage)) ) {
190 fprintf( stderr, "Setting codepage %s as volume codepage failed", vol->v_volcodepage);
197 static int parse_options (char *buf, int *flags, const vol_opt_name_t *options)
200 const vol_opt_name_t *op;
204 while ( *p != '\0') {
208 for (;op->name; op++) {
209 if ( !strcmp(op->name, q )) {
210 *flags |= op->option;
224 static int parseline ( char *buf, struct volinfo *vol)
229 const info_option_t *p = &info_options[0];
231 if (NULL == ( value = strchr(buf, ':')) )
237 if ( 0 == (len = strlen(value)) )
240 if (value[len-1] == '\n')
243 for (;p->name; p++) {
244 if ( !strcmp(p->name, buf )) {
252 if ((vol->v_maccodepage = strdup(value)) == NULL) {
253 fprintf (stderr, "strdup: %s", strerror(errno));
258 if ((vol->v_volcodepage = strdup(value)) == NULL) {
259 fprintf (stderr, "strdup: %s", strerror(errno));
264 if ((vol->v_cnidscheme = strdup(value)) == NULL) {
265 fprintf (stderr, "strdup: %s", strerror(errno));
270 if ((vol->v_dbd_host = strdup(value)) == NULL) {
271 fprintf (stderr, "strdup: %s", strerror(errno));
276 vol->v_dbd_port = atoi(value);
279 if ((vol->v_dbpath = strdup(value)) == NULL) {
280 fprintf (stderr, "strdup: %s", strerror(errno));
285 if (strcasecmp(value, "v1") == 0) {
286 vol->v_adouble = AD_VERSION1;
287 vol->ad_path = ad_path;
289 #if AD_VERSION == AD_VERSION2
290 else if (strcasecmp(value, "v2") == 0) {
291 vol->ad_path = ad_path;
292 vol->v_adouble = AD_VERSION2;
294 else if (strcasecmp(value, "osx") == 0) {
295 vol->v_adouble = AD_VERSION2_OSX;
296 vol->ad_path = ad_path_osx;
300 fprintf (stderr, "unknown adouble version: %s, %s", buf, value);
305 parse_options(value, &vol->v_flags, &vol_opt_names[0]);
308 parse_options(value, &vol->v_casefold, &vol_opt_casefold[0]);
311 if (strcasecmp(value, "AFPVOL_EA_AD") == 0)
312 vol->v_vfs_ea = AFPVOL_EA_AD;
313 else if (strcasecmp(value, "AFPVOL_EA_SYS") == 0)
314 vol->v_vfs_ea = AFPVOL_EA_SYS;
317 fprintf (stderr, "unknown volume information: %s, %s", buf, value);
326 int loadvolinfo (char *path, struct volinfo *vol)
329 char volinfofile[MAXPATHLEN];
330 char buf[MAXPATHLEN];
338 memset(vol, 0, sizeof(struct volinfo));
339 strlcpy(volinfofile, path, sizeof(volinfofile));
341 /* volinfo file is in .AppleDesktop */
342 if ( NULL == find_volumeroot(volinfofile, sizeof(volinfofile)))
345 if ((vol->v_path = strdup(volinfofile)) == NULL ) {
346 fprintf (stderr, "strdup: %s", strerror(errno));
349 strlcat(volinfofile, ".AppleDesktop/", sizeof(volinfofile));
350 strlcat(volinfofile, VOLINFOFILE, sizeof(volinfofile));
352 /* open the file read only */
353 if ( NULL == (fp = fopen( volinfofile, "r")) ) {
354 fprintf (stderr, "error opening volinfo (%s): %s", volinfofile, strerror(errno));
359 /* try to get a read lock */
361 lock.l_whence = SEEK_SET;
363 lock.l_type = F_RDLCK;
365 /* wait for read lock */
366 if (fcntl(fd, F_SETLKW, &lock) < 0) {
372 while (NULL != fgets(buf, sizeof(buf), fp)) {
377 lock.l_type = F_UNLCK;
378 fcntl(fd, F_SETLK, &lock);
380 /* Translate vol options to ad options like afp/volume.c does it */
381 vol->v_ad_options = 0;
382 if ((vol->v_flags & AFPVOL_NODEV))
383 vol->v_ad_options |= ADVOL_NODEV;
384 if ((vol->v_flags & AFPVOL_CACHE))
385 vol->v_ad_options |= ADVOL_CACHE;
386 if ((vol->v_flags & AFPVOL_UNIX_PRIV))
387 vol->v_ad_options |= ADVOL_UNIXPRIV;
388 if ((vol->v_flags & AFPVOL_INV_DOTS))
389 vol->v_ad_options |= ADVOL_INVDOTS;
396 * Save the volume options to a file, used by shell utilities. Writing the file
397 * everytime a volume is opened is unnecessary, but it shouldn't hurt much.
399 int savevolinfo(const struct vol *vol, const char *Cnid_srv, const char *Cnid_port)
402 char item[MAXPATHLEN];
406 const vol_opt_name_t *op = &vol_opt_names[0];
407 const vol_opt_name_t *cf = &vol_opt_casefold[0];
409 strlcpy (item, vol->v_path, sizeof(item));
410 strlcat (item, "/.AppleDesktop/", sizeof(item));
411 strlcat (item, VOLINFOFILE, sizeof(item));
413 if ((fd = open( item, O_RDWR | O_CREAT , 0666)) <0 ) {
414 LOG(log_debug, logtype_afpd,"Error opening %s: %s", item, strerror(errno));
418 /* try to get a lock */
420 lock.l_whence = SEEK_SET;
422 lock.l_type = F_WRLCK;
424 if (fcntl(fd, F_SETLK, &lock) < 0) {
425 if (errno == EACCES || errno == EAGAIN) {
426 /* ignore, other process already writing the file */
429 LOG(log_error, logtype_cnid, "savevoloptions: cannot get lock: %s", strerror(errno));
434 /* write volume options */
435 snprintf(buf, sizeof(buf), "MAC_CHARSET:%s\n", vol->v_maccodepage);
436 snprintf(item, sizeof(item), "VOL_CHARSET:%s\n", vol->v_volcodepage);
437 strlcat(buf, item, sizeof(buf));
439 switch (vol->v_adouble) {
441 strlcat(buf, "ADOUBLE_VER:v1\n", sizeof(buf));
444 strlcat(buf, "ADOUBLE_VER:v2\n", sizeof(buf));
446 case AD_VERSION2_OSX:
447 strlcat(buf, "ADOUBLE_VER:osx\n", sizeof(buf));
449 case AD_VERSION1_SFM:
450 strlcat(buf, "ADOUBLE_VER:sfm\n", sizeof(buf));
454 strlcat(buf, "CNIDBACKEND:", sizeof(buf));
455 strlcat(buf, vol->v_cnidscheme, sizeof(buf));
456 strlcat(buf, "\n", sizeof(buf));
458 strlcat(buf, "CNIDDBDHOST:", sizeof(buf));
459 strlcat(buf, Cnid_srv, sizeof(buf));
460 strlcat(buf, "\n", sizeof(buf));
462 strlcat(buf, "CNIDDBDPORT:", sizeof(buf));
463 strlcat(buf, Cnid_port, sizeof(buf));
464 strlcat(buf, "\n", sizeof(buf));
466 strcpy(item, "CNID_DBPATH:");
468 strlcat(item, vol->v_dbpath, sizeof(item));
470 strlcat(item, vol->v_path, sizeof(item));
471 strlcat(item, "\n", sizeof(item));
472 strlcat(buf, item, sizeof(buf));
475 strcpy(item, "VOLUME_OPTS:");
476 for (;op->name; op++) {
477 if ( (vol->v_flags & op->option) ) {
478 strlcat(item, op->name, sizeof(item));
479 strlcat(item, " ", sizeof(item));
482 strlcat(item, "\n", sizeof(item));
483 strlcat(buf, item, sizeof(buf));
486 strcpy(item, "VOLCASEFOLD:");
487 for (;cf->name; cf++) {
488 if ( (vol->v_casefold & cf->option) ) {
489 strlcat(item, cf->name, sizeof(item));
490 strlcat(item, " ", sizeof(item));
493 strlcat(item, "\n", sizeof(item));
494 strlcat(buf, item, sizeof(buf));
496 /* ExtendedAttributes */
497 strcpy(item, "EXTATTRTYPE:");
498 switch (vol->v_vfs_ea) {
500 strlcat(item, "AFPVOL_EA_SYS\n", sizeof(item));
503 strlcat(item, "AFPVOL_EA_AD\n", sizeof(item));
506 strlcat(item, "AFPVOL_EA_NONE\n", sizeof(item));
509 strlcat(item, "AFPVOL_EA_UNKNOWN\n", sizeof(item));
512 strlcat(buf, item, sizeof(buf));
514 if (strlen(buf) >= sizeof(buf)-1)
515 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: buffer too small, %s", buf);
516 if (write( fd, buf, strlen(buf)) < 0 || ftruncate(fd, strlen(buf)) < 0 ) {
517 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: %s", strerror(errno));
520 lock.l_type = F_UNLCK;
521 fcntl(fd, F_SETLK, &lock);