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 */
25 #include <sys/types.h>
37 #include <sys/param.h>
39 #include <atalk/adouble.h>
40 #include <atalk/util.h>
41 #include <atalk/logger.h>
42 #include <atalk/volinfo.h>
43 #include <atalk/volume.h>
45 #include <atalk/cnid.h>
48 static const vol_opt_name_t vol_opt_names[] = {
49 {AFPVOL_A2VOL, "PRODOS"}, /* prodos volume */
50 {AFPVOL_CRLF, "CRLF"}, /* cr/lf translation */
51 {AFPVOL_NOADOUBLE, "NOADOUBLE"}, /* don't create .AppleDouble by default */
52 {AFPVOL_RO, "READONLY"}, /* read-only volume */
53 {AFPVOL_MSWINDOWS, "MSWINDOWS"}, /* deal with ms-windows yuckiness. this is going away. */
54 {AFPVOL_NOHEX, "NOHEX"}, /* don't do :hex translation */
55 {AFPVOL_USEDOTS, "USEDOTS"}, /* use real dots */
56 {AFPVOL_LIMITSIZE, "LIMITSIZE"}, /* limit size for older macs */
57 {AFPVOL_MAPASCII, "MAPASCII"}, /* map the ascii range as well */
58 {AFPVOL_DROPBOX, "DROPBOX"}, /* dropkludge dropbox support */
59 {AFPVOL_NOFILEID, "NOFILEID"}, /* don't advertise createid resolveid and deleteid calls */
60 {AFPVOL_NOSTAT, "NOSTAT"}, /* advertise the volume even if we can't stat() it
61 * maybe because it will be mounted later in preexec */
62 {AFPVOL_UNIX_PRIV, "UNIXPRIV"}, /* support unix privileges */
63 {AFPVOL_NODEV, "NODEV"}, /* always use 0 for device number in cnid calls */
64 {AFPVOL_CASEINSEN, "CASEINSENSITIVE"}, /* volume is case insensitive */
65 {AFPVOL_EILSEQ, "ILLEGALSEQ"}, /* encode illegal sequence */
66 {AFPVOL_CACHE, "CACHEID"}, /* Use adouble v2 CNID caching, default don't use it */
67 {AFPVOL_INV_DOTS, "INVISIBLEDOTS"},
68 {AFPVOL_ACLS, "ACLS"}, /* Vol supports ACLs */
69 {AFPVOL_TM, "TM"}, /* Set "kSupportsTMLockSteal" is volume attributes */
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 strlcpy(abspath, path, sizeof(abspath));
145 /* we might be called from `ad cp ...` with non existing target */
146 if (stat(abspath, &st) != 0) {
150 if (NULL == (p = strrchr(abspath, '/')) )
151 /* single component `ad cp SOURCEFILE TARGETFILE`, use "." instead */
152 strcpy(abspath, ".");
154 /* try without the last path element */
157 if (stat(abspath, &st) != 0) {
162 if (S_ISREG(st.st_mode)) {
163 /* single file copy SOURCE */
164 if (NULL == (p = strrchr(abspath, '/')) )
165 /* no path, use "." instead */
166 strcpy(abspath, ".");
168 /* try without the last path element */
172 if (!getcwd(savecwd, sizeof(savecwd)) || chdir(abspath) < 0)
175 if (!getcwd(abspath, sizeof(abspath)) || chdir (savecwd) < 0)
178 if (strlen(abspath) > bufsize)
181 strlcpy(path, abspath, bufsize);
185 static char * find_volumeroot(char *path, size_t maxlen)
187 char *volume = make_path_absolute(path, maxlen);
192 if (NULL == (find_in_path(volume, "/.AppleDesktop", maxlen)) )
198 int vol_load_charsets( struct volinfo *vol)
200 if ( (charset_t) -1 == ( vol->v_maccharset = add_charset(vol->v_maccodepage)) ) {
201 fprintf( stderr, "Setting codepage %s as Mac codepage failed", vol->v_maccodepage);
205 if ( (charset_t) -1 == ( vol->v_volcharset = add_charset(vol->v_volcodepage)) ) {
206 fprintf( stderr, "Setting codepage %s as volume codepage failed", vol->v_volcodepage);
213 static int parse_options (char *buf, int *flags, const vol_opt_name_t *options)
216 const vol_opt_name_t *op;
220 while ( *p != '\0') {
224 for (;op->name; op++) {
225 if ( !strcmp(op->name, q )) {
226 *flags |= op->option;
240 static int parseline ( char *buf, struct volinfo *vol)
245 const info_option_t *p = &info_options[0];
247 if (NULL == ( value = strchr(buf, ':')) )
253 if ( 0 == (len = strlen(value)) )
256 if (value[len-1] == '\n')
259 for (;p->name; p++) {
260 if ( !strcmp(p->name, buf )) {
268 if ((vol->v_maccodepage = strdup(value)) == NULL) {
269 fprintf (stderr, "strdup: %s", strerror(errno));
274 if ((vol->v_volcodepage = strdup(value)) == NULL) {
275 fprintf (stderr, "strdup: %s", strerror(errno));
280 if ((vol->v_cnidscheme = strdup(value)) == NULL) {
281 fprintf (stderr, "strdup: %s", strerror(errno));
286 if ((vol->v_dbd_host = strdup(value)) == NULL) {
287 fprintf (stderr, "strdup: %s", strerror(errno));
292 if ((vol->v_dbd_port = strdup(value)) == NULL) {
293 fprintf (stderr, "strdup: %s", strerror(errno));
298 if ((vol->v_dbpath = malloc(MAXPATHLEN+1)) == NULL)
300 strcpy(vol->v_dbpath, value);
303 if (strcasecmp(value, "v1") == 0) {
304 vol->v_adouble = AD_VERSION1;
305 vol->ad_path = ad_path;
307 #if AD_VERSION == AD_VERSION2
308 else if (strcasecmp(value, "v2") == 0) {
309 vol->ad_path = ad_path;
310 vol->v_adouble = AD_VERSION2;
312 else if (strcasecmp(value, "osx") == 0) {
313 vol->v_adouble = AD_VERSION2_OSX;
314 vol->ad_path = ad_path_osx;
318 fprintf (stderr, "unknown adouble version: %s, %s", buf, value);
323 parse_options(value, &vol->v_flags, &vol_opt_names[0]);
326 parse_options(value, &vol->v_casefold, &vol_opt_casefold[0]);
329 if (strcasecmp(value, "AFPVOL_EA_AD") == 0)
330 vol->v_vfs_ea = AFPVOL_EA_AD;
331 else if (strcasecmp(value, "AFPVOL_EA_SYS") == 0)
332 vol->v_vfs_ea = AFPVOL_EA_SYS;
335 fprintf (stderr, "unknown volume information: %s, %s", buf, value);
344 int loadvolinfo (char *path, struct volinfo *vol)
347 char volinfofile[MAXPATHLEN];
348 char buf[MAXPATHLEN];
356 memset(vol, 0, sizeof(struct volinfo));
357 strlcpy(volinfofile, path, sizeof(volinfofile));
359 /* volinfo file is in .AppleDesktop */
360 if ( NULL == find_volumeroot(volinfofile, sizeof(volinfofile)))
363 if ((vol->v_path = strdup(volinfofile)) == NULL ) {
364 fprintf (stderr, "strdup: %s", strerror(errno));
367 /* Remove trailing slashes */
368 len = strlen(vol->v_path);
369 while (len && (vol->v_path[len-1] == '/')) {
370 vol->v_path[len-1] = 0;
374 strlcat(volinfofile, ".AppleDesktop/", sizeof(volinfofile));
375 strlcat(volinfofile, VOLINFOFILE, sizeof(volinfofile));
377 /* open the file read only */
378 if ( NULL == (fp = fopen( volinfofile, "r")) ) {
379 fprintf (stderr, "error opening volinfo (%s): %s", volinfofile, strerror(errno));
384 /* try to get a read lock */
386 lock.l_whence = SEEK_SET;
388 lock.l_type = F_RDLCK;
390 /* wait for read lock */
391 if (fcntl(fd, F_SETLKW, &lock) < 0) {
397 while (NULL != fgets(buf, sizeof(buf), fp)) {
402 lock.l_type = F_UNLCK;
403 fcntl(fd, F_SETLK, &lock);
405 /* Translate vol options to ad options like afp/volume.c does it */
406 vol->v_ad_options = 0;
407 if ((vol->v_flags & AFPVOL_NODEV))
408 vol->v_ad_options |= ADVOL_NODEV;
409 if ((vol->v_flags & AFPVOL_CACHE))
410 vol->v_ad_options |= ADVOL_CACHE;
411 if ((vol->v_flags & AFPVOL_UNIX_PRIV))
412 vol->v_ad_options |= ADVOL_UNIXPRIV;
413 if ((vol->v_flags & AFPVOL_INV_DOTS))
414 vol->v_ad_options |= ADVOL_INVDOTS;
416 vol->retaincount = 1;
423 * Allocate a struct volinfo object for refcounting usage with retain and close, and
424 * call loadvolinfo with it
426 struct volinfo *allocvolinfo(char *path)
428 struct volinfo *p = malloc(sizeof(struct volinfo));
432 if (loadvolinfo(path, p) == -1)
440 void retainvolinfo(struct volinfo *vol)
446 * Decrement retain count, free resources when retaincount reaches 0
448 int closevolinfo(struct volinfo *volinfo)
450 if (volinfo->retaincount <= 0)
453 volinfo->retaincount--;
455 if (volinfo->retaincount == 0) {
456 free(volinfo->v_name); volinfo->v_name = NULL;
457 free(volinfo->v_path); volinfo->v_path = NULL;
458 free(volinfo->v_cnidscheme); volinfo->v_cnidscheme = NULL;
459 free(volinfo->v_dbpath); volinfo->v_dbpath = NULL;
460 free(volinfo->v_volcodepage); volinfo->v_volcodepage = NULL;
461 free(volinfo->v_maccodepage); volinfo->v_maccodepage = NULL;
462 free(volinfo->v_dbd_host); volinfo->v_dbd_host = NULL;
463 free(volinfo->v_dbd_port); volinfo->v_dbd_port = NULL;
464 if (volinfo->malloced) {
465 volinfo->malloced = 0;
474 * Save the volume options to a file, used by shell utilities. Writing the file
475 * everytime a volume is opened is unnecessary, but it shouldn't hurt much.
477 int savevolinfo(const struct vol *vol, const char *Cnid_srv, const char *Cnid_port)
481 char item[MAXPATHLEN];
485 const vol_opt_name_t *op = &vol_opt_names[0];
486 const vol_opt_name_t *cf = &vol_opt_casefold[0];
488 strlcpy (item, vol->v_path, sizeof(item));
489 strlcat (item, "/.AppleDesktop/", sizeof(item));
490 strlcat (item, VOLINFOFILE, sizeof(item));
492 process_uid = geteuid();
494 if (seteuid(0) == -1) {
499 if ((fd = open(item, O_RDWR | O_CREAT , 0666)) <0 ) {
500 LOG(log_debug, logtype_default,"Error opening %s: %s", item, strerror(errno));
502 if (seteuid(process_uid) == -1) {
503 LOG(log_error, logtype_default, "can't seteuid back %s", strerror(errno));
511 if (seteuid(process_uid) == -1) {
512 LOG(log_error, logtype_default, "can't seteuid back %s", strerror(errno));
517 /* try to get a lock */
519 lock.l_whence = SEEK_SET;
521 lock.l_type = F_WRLCK;
523 if (fcntl(fd, F_SETLK, &lock) < 0) {
524 if (errno == EACCES || errno == EAGAIN) {
525 /* ignore, other process already writing the file */
528 LOG(log_error, logtype_default, "savevoloptions: cannot get lock: %s", strerror(errno));
533 /* write volume options */
534 snprintf(buf, sizeof(buf), "MAC_CHARSET:%s\n", vol->v_maccodepage);
535 snprintf(item, sizeof(item), "VOL_CHARSET:%s\n", vol->v_volcodepage);
536 strlcat(buf, item, sizeof(buf));
538 switch (vol->v_adouble) {
540 strlcat(buf, "ADOUBLE_VER:v1\n", sizeof(buf));
543 strlcat(buf, "ADOUBLE_VER:v2\n", sizeof(buf));
545 case AD_VERSION2_OSX:
546 strlcat(buf, "ADOUBLE_VER:osx\n", sizeof(buf));
548 case AD_VERSION1_SFM:
549 strlcat(buf, "ADOUBLE_VER:sfm\n", sizeof(buf));
553 strlcat(buf, "CNIDBACKEND:", sizeof(buf));
554 strlcat(buf, vol->v_cnidscheme, sizeof(buf));
555 strlcat(buf, "\n", sizeof(buf));
557 strlcat(buf, "CNIDDBDHOST:", sizeof(buf));
558 strlcat(buf, Cnid_srv, sizeof(buf));
559 strlcat(buf, "\n", sizeof(buf));
561 strlcat(buf, "CNIDDBDPORT:", sizeof(buf));
562 strlcat(buf, Cnid_port, sizeof(buf));
563 strlcat(buf, "\n", sizeof(buf));
565 strcpy(item, "CNID_DBPATH:");
567 strlcat(item, vol->v_dbpath, sizeof(item));
569 strlcat(item, vol->v_path, sizeof(item));
570 strlcat(item, "\n", sizeof(item));
571 strlcat(buf, item, sizeof(buf));
574 strcpy(item, "VOLUME_OPTS:");
575 for (;op->name; op++) {
576 if ( (vol->v_flags & op->option) ) {
577 strlcat(item, op->name, sizeof(item));
578 strlcat(item, " ", sizeof(item));
581 strlcat(item, "\n", sizeof(item));
582 strlcat(buf, item, sizeof(buf));
585 strcpy(item, "VOLCASEFOLD:");
586 for (;cf->name; cf++) {
587 if ( (vol->v_casefold & cf->option) ) {
588 strlcat(item, cf->name, sizeof(item));
589 strlcat(item, " ", sizeof(item));
592 strlcat(item, "\n", sizeof(item));
593 strlcat(buf, item, sizeof(buf));
595 /* ExtendedAttributes */
596 strcpy(item, "EXTATTRTYPE:");
597 switch (vol->v_vfs_ea) {
599 strlcat(item, "AFPVOL_EA_SYS\n", sizeof(item));
602 strlcat(item, "AFPVOL_EA_AD\n", sizeof(item));
605 strlcat(item, "AFPVOL_EA_NONE\n", sizeof(item));
608 strlcat(item, "AFPVOL_EA_UNKNOWN\n", sizeof(item));
611 strlcat(buf, item, sizeof(buf));
613 if (strlen(buf) >= sizeof(buf)-1)
614 LOG(log_debug, logtype_default, "Error writing .volinfo file: buffer too small, %s", buf);
615 if (write( fd, buf, strlen(buf)) < 0 || ftruncate(fd, strlen(buf)) < 0 ) {
616 LOG(log_debug, logtype_default, "Error writing .volinfo file: %s", strerror(errno));
619 lock.l_type = F_UNLCK;
620 fcntl(fd, F_SETLK, &lock);