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 */
31 #include <sys/param.h>
33 #include <atalk/adouble.h>
34 #include <atalk/util.h>
35 #include <atalk/logger.h>
36 #include <atalk/volinfo.h>
37 #include <atalk/volume.h>
38 #include <atalk/compat.h>
40 #include <atalk/cnid.h>
43 static const vol_opt_name_t vol_opt_names[] = {
44 {AFPVOL_A2VOL, "PRODOS"}, /* prodos volume */
45 {AFPVOL_CRLF, "CRLF"}, /* cr/lf translation */
46 {AFPVOL_NOADOUBLE, "NOADOUBLE"}, /* don't create .AppleDouble by default */
47 {AFPVOL_RO, "READONLY"}, /* read-only volume */
48 {AFPVOL_MSWINDOWS, "MSWINDOWS"}, /* deal with ms-windows yuckiness. this is going away. */
49 {AFPVOL_NOHEX, "NOHEX"}, /* don't do :hex translation */
50 {AFPVOL_USEDOTS, "USEDOTS"}, /* use real dots */
51 {AFPVOL_LIMITSIZE, "LIMITSIZE"}, /* limit size for older macs */
52 {AFPVOL_MAPASCII, "MAPASCII"}, /* map the ascii range as well */
53 {AFPVOL_NOFILEID, "NOFILEID"}, /* don't advertise createid resolveid and deleteid calls */
54 {AFPVOL_NOSTAT, "NOSTAT"}, /* advertise the volume even if we can't stat() it
55 * maybe because it will be mounted later in preexec */
56 {AFPVOL_UNIX_PRIV, "UNIXPRIV"}, /* support unix privileges */
57 {AFPVOL_NODEV, "NODEV"}, /* always use 0 for device number in cnid calls */
58 {AFPVOL_CASEINSEN, "CASEINSENSITIVE"}, /* volume is case insensitive */
59 {AFPVOL_EILSEQ, "ILLEGALSEQ"}, /* encode illegal sequence */
60 {AFPVOL_CACHE, "CACHEID"}, /* Use adouble v2 CNID caching, default don't use it */
61 {AFPVOL_INV_DOTS, "INVISIBLEDOTS"},
62 {AFPVOL_ACLS, "ACLS"}, /* Vol supports ACLs */
63 {AFPVOL_TM, "TM"}, /* Set "kSupportsTMLockSteal" is volume attributes */
67 static const vol_opt_name_t vol_opt_casefold[] = {
68 {AFPVOL_MTOUUPPER, "MTOULOWER"},
69 {AFPVOL_MTOULOWER, "MTOULOWER"},
70 {AFPVOL_UTOMUPPER, "UTOMUPPER"},
71 {AFPVOL_UTOMLOWER, "UTOMLOWER"},
91 static const info_option_t info_options[] = {
92 {"MAC_CHARSET", MAC_CHARSET},
93 {"VOL_CHARSET", VOL_CHARSET},
94 {"ADOUBLE_VER", ADOUBLE_VER},
95 {"CNIDBACKEND", CNIDBACKEND},
96 {"CNIDDBDHOST", CNIDDBDHOST},
97 {"CNIDDBDPORT", CNIDDBDPORT},
98 {"CNID_DBPATH", CNID_DBPATH},
99 {"VOLUME_OPTS", VOLUME_OPTS},
100 {"VOLCASEFOLD", VOLCASEFOLD},
101 {"EXTATTRTYPE", EXTATTRTYPE},
105 static char* find_in_path( char *path, char *subdir, size_t maxlen)
110 strlcat(path, subdir, maxlen);
111 pos = strrchr(path, '/');
113 while ( stat(path, &st) != 0) {
115 if ((pos = strrchr(path, '/'))) {
117 strlcat(path, subdir, maxlen);
124 path[pos-path] = '/';
125 path[pos-path+1] = 0;
130 static char * make_path_absolute(char *path, size_t bufsize)
133 char savecwd[MAXPATHLEN];
134 char abspath[MAXPATHLEN];
137 strlcpy(abspath, path, sizeof(abspath));
139 /* we might be called from `ad cp ...` with non existing target */
140 if (stat(abspath, &st) != 0) {
144 if (NULL == (p = strrchr(abspath, '/')) )
145 /* single component `ad cp SOURCEFILE TARGETFILE`, use "." instead */
146 strcpy(abspath, ".");
148 /* try without the last path element */
151 if (stat(abspath, &st) != 0) {
156 if (S_ISREG(st.st_mode)) {
157 /* single file copy SOURCE */
158 if (NULL == (p = strrchr(abspath, '/')) )
159 /* no path, use "." instead */
160 strcpy(abspath, ".");
162 /* try without the last path element */
166 if (!getcwd(savecwd, sizeof(savecwd)) || chdir(abspath) < 0)
169 if (!getcwd(abspath, sizeof(abspath)) || chdir (savecwd) < 0)
172 if (strlen(abspath) > bufsize)
175 strlcpy(path, abspath, bufsize);
179 static char * find_volumeroot(char *path, size_t maxlen)
181 char *volume = make_path_absolute(path, maxlen);
186 if (NULL == (find_in_path(volume, "/.AppleDesktop", maxlen)) )
192 int vol_load_charsets( struct volinfo *vol)
194 if ( (charset_t) -1 == ( vol->v_maccharset = add_charset(vol->v_maccodepage)) ) {
195 fprintf( stderr, "Setting codepage %s as Mac codepage failed", vol->v_maccodepage);
199 if ( (charset_t) -1 == ( vol->v_volcharset = add_charset(vol->v_volcodepage)) ) {
200 fprintf( stderr, "Setting codepage %s as volume codepage failed", vol->v_volcodepage);
207 static int parse_options (char *buf, int *flags, const vol_opt_name_t *options)
210 const vol_opt_name_t *op;
214 while ( *p != '\0') {
218 for (;op->name; op++) {
219 if ( !strcmp(op->name, q )) {
220 *flags |= op->option;
234 static int parseline ( char *buf, struct volinfo *vol)
239 const info_option_t *p = &info_options[0];
241 if (NULL == ( value = strchr(buf, ':')) )
247 if ( 0 == (len = strlen(value)) )
250 if (value[len-1] == '\n')
253 for (;p->name; p++) {
254 if ( !strcmp(p->name, buf )) {
262 if ((vol->v_maccodepage = strdup(value)) == NULL) {
263 fprintf (stderr, "strdup: %s", strerror(errno));
268 if ((vol->v_volcodepage = strdup(value)) == NULL) {
269 fprintf (stderr, "strdup: %s", strerror(errno));
274 if ((vol->v_cnidscheme = strdup(value)) == NULL) {
275 fprintf (stderr, "strdup: %s", strerror(errno));
280 if ((vol->v_dbd_host = strdup(value)) == NULL) {
281 fprintf (stderr, "strdup: %s", strerror(errno));
286 if ((vol->v_dbd_port = strdup(value)) == NULL) {
287 fprintf (stderr, "strdup: %s", strerror(errno));
292 if ((vol->v_dbpath = malloc(MAXPATHLEN+1)) == NULL)
294 strcpy(vol->v_dbpath, value);
297 if (strcasecmp(value, "v2") == 0) {
298 vol->ad_path = ad_path;
299 vol->v_adouble = AD_VERSION2;
300 } else if (strcasecmp(value, "ea") == 0) {
301 vol->ad_path = ad_path_ea;
302 vol->v_adouble = AD_VERSION_EA;
305 fprintf (stderr, "unknown adouble version: %s, %s", buf, value);
310 parse_options(value, &vol->v_flags, &vol_opt_names[0]);
313 parse_options(value, &vol->v_casefold, &vol_opt_casefold[0]);
316 if (strcasecmp(value, "AFPVOL_EA_AD") == 0)
317 vol->v_vfs_ea = AFPVOL_EA_AD;
318 else if (strcasecmp(value, "AFPVOL_EA_SYS") == 0)
319 vol->v_vfs_ea = AFPVOL_EA_SYS;
322 fprintf (stderr, "unknown volume information: %s, %s", buf, value);
331 int loadvolinfo (char *path, struct volinfo *vol)
334 char volinfofile[MAXPATHLEN];
335 char buf[MAXPATHLEN];
343 memset(vol, 0, sizeof(struct volinfo));
344 strlcpy(volinfofile, path, sizeof(volinfofile));
346 /* volinfo file is in .AppleDesktop */
347 if ( NULL == find_volumeroot(volinfofile, sizeof(volinfofile)))
350 if ((vol->v_path = strdup(volinfofile)) == NULL ) {
351 fprintf (stderr, "strdup: %s", strerror(errno));
354 /* Remove trailing slashes */
355 len = strlen(vol->v_path);
356 while (len && (vol->v_path[len-1] == '/')) {
357 vol->v_path[len-1] = 0;
361 strlcat(volinfofile, ".AppleDesktop/", sizeof(volinfofile));
362 strlcat(volinfofile, VOLINFOFILE, sizeof(volinfofile));
364 /* open the file read only */
365 if ( NULL == (fp = fopen( volinfofile, "r")) ) {
366 fprintf (stderr, "error opening volinfo (%s): %s", volinfofile, strerror(errno));
371 /* try to get a read lock */
373 lock.l_whence = SEEK_SET;
375 lock.l_type = F_RDLCK;
377 /* wait for read lock */
378 if (fcntl(fd, F_SETLKW, &lock) < 0) {
384 while (NULL != fgets(buf, sizeof(buf), fp)) {
389 lock.l_type = F_UNLCK;
390 fcntl(fd, F_SETLK, &lock);
392 /* Translate vol options to ad options like afp/volume.c does it */
393 vol->v_ad_options = 0;
394 if ((vol->v_flags & AFPVOL_NODEV))
395 vol->v_ad_options |= ADVOL_NODEV;
396 if ((vol->v_flags & AFPVOL_CACHE))
397 vol->v_ad_options |= ADVOL_CACHE;
398 if ((vol->v_flags & AFPVOL_UNIX_PRIV))
399 vol->v_ad_options |= ADVOL_UNIXPRIV;
400 if ((vol->v_flags & AFPVOL_INV_DOTS))
401 vol->v_ad_options |= ADVOL_INVDOTS;
403 vol->retaincount = 1;
410 * Allocate a struct volinfo object for refcounting usage with retain and close, and
411 * call loadvolinfo with it
413 struct volinfo *allocvolinfo(char *path)
415 struct volinfo *p = malloc(sizeof(struct volinfo));
419 if (loadvolinfo(path, p) == -1)
427 void retainvolinfo(struct volinfo *vol)
433 * Decrement retain count, free resources when retaincount reaches 0
435 int closevolinfo(struct volinfo *volinfo)
437 if (volinfo->retaincount <= 0)
440 volinfo->retaincount--;
442 if (volinfo->retaincount == 0) {
443 free(volinfo->v_name); volinfo->v_name = NULL;
444 free(volinfo->v_path); volinfo->v_path = NULL;
445 free(volinfo->v_cnidscheme); volinfo->v_cnidscheme = NULL;
446 free(volinfo->v_dbpath); volinfo->v_dbpath = NULL;
447 free(volinfo->v_volcodepage); volinfo->v_volcodepage = NULL;
448 free(volinfo->v_maccodepage); volinfo->v_maccodepage = NULL;
449 free(volinfo->v_dbd_host); volinfo->v_dbd_host = NULL;
450 free(volinfo->v_dbd_port); volinfo->v_dbd_port = NULL;
451 if (volinfo->malloced) {
452 volinfo->malloced = 0;
461 * Save the volume options to a file, used by shell utilities. Writing the file
462 * everytime a volume is opened is unnecessary, but it shouldn't hurt much.
464 int savevolinfo(const struct vol *vol, const char *Cnid_srv, const char *Cnid_port)
467 char item[MAXPATHLEN];
471 const vol_opt_name_t *op = &vol_opt_names[0];
472 const vol_opt_name_t *cf = &vol_opt_casefold[0];
474 strlcpy (item, vol->v_path, sizeof(item));
475 strlcat (item, "/.AppleDesktop/", sizeof(item));
476 strlcat (item, VOLINFOFILE, sizeof(item));
478 if ((fd = open( item, O_RDWR | O_CREAT , 0666)) <0 ) {
479 LOG(log_debug, logtype_afpd,"Error opening %s: %s", item, strerror(errno));
483 /* try to get a lock */
485 lock.l_whence = SEEK_SET;
487 lock.l_type = F_WRLCK;
489 if (fcntl(fd, F_SETLK, &lock) < 0) {
490 if (errno == EACCES || errno == EAGAIN) {
491 /* ignore, other process already writing the file */
494 LOG(log_error, logtype_cnid, "savevoloptions: cannot get lock: %s", strerror(errno));
499 /* write volume options */
500 snprintf(buf, sizeof(buf), "MAC_CHARSET:%s\n", vol->v_maccodepage);
501 snprintf(item, sizeof(item), "VOL_CHARSET:%s\n", vol->v_volcodepage);
502 strlcat(buf, item, sizeof(buf));
504 switch (vol->v_adouble) {
506 strlcat(buf, "ADOUBLE_VER:v2\n", sizeof(buf));
509 strlcat(buf, "ADOUBLE_VER:ea\n", sizeof(buf));
513 strlcat(buf, "CNIDBACKEND:", sizeof(buf));
514 strlcat(buf, vol->v_cnidscheme, sizeof(buf));
515 strlcat(buf, "\n", sizeof(buf));
517 strlcat(buf, "CNIDDBDHOST:", sizeof(buf));
518 strlcat(buf, Cnid_srv, sizeof(buf));
519 strlcat(buf, "\n", sizeof(buf));
521 strlcat(buf, "CNIDDBDPORT:", sizeof(buf));
522 strlcat(buf, Cnid_port, sizeof(buf));
523 strlcat(buf, "\n", sizeof(buf));
525 strcpy(item, "CNID_DBPATH:");
527 strlcat(item, vol->v_dbpath, sizeof(item));
529 strlcat(item, vol->v_path, sizeof(item));
530 strlcat(item, "\n", sizeof(item));
531 strlcat(buf, item, sizeof(buf));
534 strcpy(item, "VOLUME_OPTS:");
535 for (;op->name; op++) {
536 if ( (vol->v_flags & op->option) ) {
537 strlcat(item, op->name, sizeof(item));
538 strlcat(item, " ", sizeof(item));
541 strlcat(item, "\n", sizeof(item));
542 strlcat(buf, item, sizeof(buf));
545 strcpy(item, "VOLCASEFOLD:");
546 for (;cf->name; cf++) {
547 if ( (vol->v_casefold & cf->option) ) {
548 strlcat(item, cf->name, sizeof(item));
549 strlcat(item, " ", sizeof(item));
552 strlcat(item, "\n", sizeof(item));
553 strlcat(buf, item, sizeof(buf));
555 /* ExtendedAttributes */
556 strcpy(item, "EXTATTRTYPE:");
557 switch (vol->v_vfs_ea) {
559 strlcat(item, "AFPVOL_EA_SYS\n", sizeof(item));
562 strlcat(item, "AFPVOL_EA_AD\n", sizeof(item));
565 strlcat(item, "AFPVOL_EA_NONE\n", sizeof(item));
568 strlcat(item, "AFPVOL_EA_UNKNOWN\n", sizeof(item));
571 strlcat(buf, item, sizeof(buf));
573 if (strlen(buf) >= sizeof(buf)-1)
574 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: buffer too small, %s", buf);
575 if (write( fd, buf, strlen(buf)) < 0 || ftruncate(fd, strlen(buf)) < 0 ) {
576 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: %s", strerror(errno));
579 lock.l_type = F_UNLCK;
580 fcntl(fd, F_SETLK, &lock);