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 */
72 static const vol_opt_name_t vol_opt_casefold[] = {
73 {AFPVOL_MTOUUPPER, "MTOULOWER"},
74 {AFPVOL_MTOULOWER, "MTOULOWER"},
75 {AFPVOL_UTOMUPPER, "UTOMUPPER"},
76 {AFPVOL_UTOMLOWER, "UTOMLOWER"},
96 static const info_option_t info_options[] = {
97 {"MAC_CHARSET", MAC_CHARSET},
98 {"VOL_CHARSET", VOL_CHARSET},
99 {"ADOUBLE_VER", ADOUBLE_VER},
100 {"CNIDBACKEND", CNIDBACKEND},
101 {"CNIDDBDHOST", CNIDDBDHOST},
102 {"CNIDDBDPORT", CNIDDBDPORT},
103 {"CNID_DBPATH", CNID_DBPATH},
104 {"VOLUME_OPTS", VOLUME_OPTS},
105 {"VOLCASEFOLD", VOLCASEFOLD},
106 {"EXTATTRTYPE", EXTATTRTYPE},
110 static char* find_in_path( char *path, char *subdir, size_t maxlen)
115 strlcat(path, subdir, maxlen);
116 pos = strrchr(path, '/');
118 while ( stat(path, &st) != 0) {
120 if ((pos = strrchr(path, '/'))) {
122 strlcat(path, subdir, maxlen);
129 path[pos-path] = '/';
130 path[pos-path+1] = 0;
135 static char * make_path_absolute(char *path, size_t bufsize)
138 char savecwd[MAXPATHLEN];
139 char abspath[MAXPATHLEN];
142 strlcpy(abspath, path, sizeof(abspath));
144 /* we might be called from `ad cp ...` with non existing target */
145 if (stat(abspath, &st) != 0) {
149 if (NULL == (p = strrchr(abspath, '/')) )
150 /* single component `ad cp SOURCEFILE TARGETFILE`, use "." instead */
151 strcpy(abspath, ".");
153 /* try without the last path element */
156 if (stat(abspath, &st) != 0) {
161 if (S_ISREG(st.st_mode)) {
162 /* single file copy SOURCE */
163 if (NULL == (p = strrchr(abspath, '/')) )
164 /* no path, use "." instead */
165 strcpy(abspath, ".");
167 /* try without the last path element */
171 if (!getcwd(savecwd, sizeof(savecwd)) || chdir(abspath) < 0)
174 if (!getcwd(abspath, sizeof(abspath)) || chdir (savecwd) < 0)
177 if (strlen(abspath) > bufsize)
180 strlcpy(path, abspath, bufsize);
184 static char * find_volumeroot(char *path, size_t maxlen)
186 char *volume = make_path_absolute(path, maxlen);
191 if (NULL == (find_in_path(volume, "/.AppleDesktop", maxlen)) )
197 int vol_load_charsets( struct volinfo *vol)
199 if ( (charset_t) -1 == ( vol->v_maccharset = add_charset(vol->v_maccodepage)) ) {
200 fprintf( stderr, "Setting codepage %s as Mac codepage failed", vol->v_maccodepage);
204 if ( (charset_t) -1 == ( vol->v_volcharset = add_charset(vol->v_volcodepage)) ) {
205 fprintf( stderr, "Setting codepage %s as volume codepage failed", vol->v_volcodepage);
212 static int parse_options (char *buf, int *flags, const vol_opt_name_t *options)
215 const vol_opt_name_t *op;
219 while ( *p != '\0') {
223 for (;op->name; op++) {
224 if ( !strcmp(op->name, q )) {
225 *flags |= op->option;
239 static int parseline ( char *buf, struct volinfo *vol)
244 const info_option_t *p = &info_options[0];
246 if (NULL == ( value = strchr(buf, ':')) )
252 if ( 0 == (len = strlen(value)) )
255 if (value[len-1] == '\n')
258 for (;p->name; p++) {
259 if ( !strcmp(p->name, buf )) {
267 if ((vol->v_maccodepage = strdup(value)) == NULL) {
268 fprintf (stderr, "strdup: %s", strerror(errno));
273 if ((vol->v_volcodepage = strdup(value)) == NULL) {
274 fprintf (stderr, "strdup: %s", strerror(errno));
279 if ((vol->v_cnidscheme = strdup(value)) == NULL) {
280 fprintf (stderr, "strdup: %s", strerror(errno));
285 if ((vol->v_dbd_host = strdup(value)) == NULL) {
286 fprintf (stderr, "strdup: %s", strerror(errno));
291 if ((vol->v_dbd_port = strdup(value)) == NULL) {
292 fprintf (stderr, "strdup: %s", strerror(errno));
297 if ((vol->v_dbpath = malloc(MAXPATHLEN+1)) == NULL)
299 strcpy(vol->v_dbpath, value);
302 if (strcasecmp(value, "v1") == 0) {
303 vol->v_adouble = AD_VERSION1;
304 vol->ad_path = ad_path;
306 #if AD_VERSION == AD_VERSION2
307 else if (strcasecmp(value, "v2") == 0) {
308 vol->ad_path = ad_path;
309 vol->v_adouble = AD_VERSION2;
311 else if (strcasecmp(value, "osx") == 0) {
312 vol->v_adouble = AD_VERSION2_OSX;
313 vol->ad_path = ad_path_osx;
317 fprintf (stderr, "unknown adouble version: %s, %s", buf, value);
322 parse_options(value, &vol->v_flags, &vol_opt_names[0]);
325 parse_options(value, &vol->v_casefold, &vol_opt_casefold[0]);
328 if (strcasecmp(value, "AFPVOL_EA_AD") == 0)
329 vol->v_vfs_ea = AFPVOL_EA_AD;
330 else if (strcasecmp(value, "AFPVOL_EA_SYS") == 0)
331 vol->v_vfs_ea = AFPVOL_EA_SYS;
334 fprintf (stderr, "unknown volume information: %s, %s", buf, value);
343 int loadvolinfo (char *path, struct volinfo *vol)
346 char volinfofile[MAXPATHLEN];
347 char buf[MAXPATHLEN];
355 memset(vol, 0, sizeof(struct volinfo));
356 strlcpy(volinfofile, path, sizeof(volinfofile));
358 /* volinfo file is in .AppleDesktop */
359 if ( NULL == find_volumeroot(volinfofile, sizeof(volinfofile)))
362 if ((vol->v_path = strdup(volinfofile)) == NULL ) {
363 fprintf (stderr, "strdup: %s", strerror(errno));
366 /* Remove trailing slashes */
367 len = strlen(vol->v_path);
368 while (len && (vol->v_path[len-1] == '/')) {
369 vol->v_path[len-1] = 0;
373 strlcat(volinfofile, ".AppleDesktop/", sizeof(volinfofile));
374 strlcat(volinfofile, VOLINFOFILE, sizeof(volinfofile));
376 /* open the file read only */
377 if ( NULL == (fp = fopen( volinfofile, "r")) ) {
378 fprintf (stderr, "error opening volinfo (%s): %s", volinfofile, strerror(errno));
383 /* try to get a read lock */
385 lock.l_whence = SEEK_SET;
387 lock.l_type = F_RDLCK;
389 /* wait for read lock */
390 if (fcntl(fd, F_SETLKW, &lock) < 0) {
396 while (NULL != fgets(buf, sizeof(buf), fp)) {
401 lock.l_type = F_UNLCK;
402 fcntl(fd, F_SETLK, &lock);
404 /* Translate vol options to ad options like afp/volume.c does it */
405 vol->v_ad_options = 0;
406 if ((vol->v_flags & AFPVOL_NODEV))
407 vol->v_ad_options |= ADVOL_NODEV;
408 if ((vol->v_flags & AFPVOL_CACHE))
409 vol->v_ad_options |= ADVOL_CACHE;
410 if ((vol->v_flags & AFPVOL_UNIX_PRIV))
411 vol->v_ad_options |= ADVOL_UNIXPRIV;
412 if ((vol->v_flags & AFPVOL_INV_DOTS))
413 vol->v_ad_options |= ADVOL_INVDOTS;
415 vol->retaincount = 1;
422 * Allocate a struct volinfo object for refcounting usage with retain and close, and
423 * call loadvolinfo with it
425 struct volinfo *allocvolinfo(char *path)
427 struct volinfo *p = malloc(sizeof(struct volinfo));
431 if (loadvolinfo(path, p) == -1)
439 void retainvolinfo(struct volinfo *vol)
445 * Decrement retain count, free resources when retaincount reaches 0
447 int closevolinfo(struct volinfo *volinfo)
449 if (volinfo->retaincount <= 0)
452 volinfo->retaincount--;
454 if (volinfo->retaincount == 0) {
455 free(volinfo->v_name); volinfo->v_name = NULL;
456 free(volinfo->v_path); volinfo->v_path = NULL;
457 free(volinfo->v_cnidscheme); volinfo->v_cnidscheme = NULL;
458 free(volinfo->v_dbpath); volinfo->v_dbpath = NULL;
459 free(volinfo->v_volcodepage); volinfo->v_volcodepage = NULL;
460 free(volinfo->v_maccodepage); volinfo->v_maccodepage = NULL;
461 free(volinfo->v_dbd_host); volinfo->v_dbd_host = NULL;
462 free(volinfo->v_dbd_port); volinfo->v_dbd_port = NULL;
463 if (volinfo->malloced) {
464 volinfo->malloced = 0;
473 * Save the volume options to a file, used by shell utilities. Writing the file
474 * everytime a volume is opened is unnecessary, but it shouldn't hurt much.
476 int savevolinfo(const struct vol *vol, const char *Cnid_srv, const char *Cnid_port)
479 char item[MAXPATHLEN];
483 const vol_opt_name_t *op = &vol_opt_names[0];
484 const vol_opt_name_t *cf = &vol_opt_casefold[0];
486 strlcpy (item, vol->v_path, sizeof(item));
487 strlcat (item, "/.AppleDesktop/", sizeof(item));
488 strlcat (item, VOLINFOFILE, sizeof(item));
490 if ((fd = open( item, O_RDWR | O_CREAT , 0666)) <0 ) {
491 LOG(log_debug, logtype_afpd,"Error opening %s: %s", item, strerror(errno));
495 /* try to get a lock */
497 lock.l_whence = SEEK_SET;
499 lock.l_type = F_WRLCK;
501 if (fcntl(fd, F_SETLK, &lock) < 0) {
502 if (errno == EACCES || errno == EAGAIN) {
503 /* ignore, other process already writing the file */
506 LOG(log_error, logtype_cnid, "savevoloptions: cannot get lock: %s", strerror(errno));
511 /* write volume options */
512 snprintf(buf, sizeof(buf), "MAC_CHARSET:%s\n", vol->v_maccodepage);
513 snprintf(item, sizeof(item), "VOL_CHARSET:%s\n", vol->v_volcodepage);
514 strlcat(buf, item, sizeof(buf));
516 switch (vol->v_adouble) {
518 strlcat(buf, "ADOUBLE_VER:v1\n", sizeof(buf));
521 strlcat(buf, "ADOUBLE_VER:v2\n", sizeof(buf));
523 case AD_VERSION2_OSX:
524 strlcat(buf, "ADOUBLE_VER:osx\n", sizeof(buf));
526 case AD_VERSION1_SFM:
527 strlcat(buf, "ADOUBLE_VER:sfm\n", sizeof(buf));
531 strlcat(buf, "CNIDBACKEND:", sizeof(buf));
532 strlcat(buf, vol->v_cnidscheme, sizeof(buf));
533 strlcat(buf, "\n", sizeof(buf));
535 strlcat(buf, "CNIDDBDHOST:", sizeof(buf));
536 strlcat(buf, Cnid_srv, sizeof(buf));
537 strlcat(buf, "\n", sizeof(buf));
539 strlcat(buf, "CNIDDBDPORT:", sizeof(buf));
540 strlcat(buf, Cnid_port, sizeof(buf));
541 strlcat(buf, "\n", sizeof(buf));
543 strcpy(item, "CNID_DBPATH:");
545 strlcat(item, vol->v_dbpath, sizeof(item));
547 strlcat(item, vol->v_path, sizeof(item));
548 strlcat(item, "\n", sizeof(item));
549 strlcat(buf, item, sizeof(buf));
552 strcpy(item, "VOLUME_OPTS:");
553 for (;op->name; op++) {
554 if ( (vol->v_flags & op->option) ) {
555 strlcat(item, op->name, sizeof(item));
556 strlcat(item, " ", sizeof(item));
559 strlcat(item, "\n", sizeof(item));
560 strlcat(buf, item, sizeof(buf));
563 strcpy(item, "VOLCASEFOLD:");
564 for (;cf->name; cf++) {
565 if ( (vol->v_casefold & cf->option) ) {
566 strlcat(item, cf->name, sizeof(item));
567 strlcat(item, " ", sizeof(item));
570 strlcat(item, "\n", sizeof(item));
571 strlcat(buf, item, sizeof(buf));
573 /* ExtendedAttributes */
574 strcpy(item, "EXTATTRTYPE:");
575 switch (vol->v_vfs_ea) {
577 strlcat(item, "AFPVOL_EA_SYS\n", sizeof(item));
580 strlcat(item, "AFPVOL_EA_AD\n", sizeof(item));
583 strlcat(item, "AFPVOL_EA_NONE\n", sizeof(item));
586 strlcat(item, "AFPVOL_EA_UNKNOWN\n", sizeof(item));
589 strlcat(buf, item, sizeof(buf));
591 if (strlen(buf) >= sizeof(buf)-1)
592 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: buffer too small, %s", buf);
593 if (write( fd, buf, strlen(buf)) < 0 || ftruncate(fd, strlen(buf)) < 0 ) {
594 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: %s", strerror(errno));
597 lock.l_type = F_UNLCK;
598 fcntl(fd, F_SETLK, &lock);