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 if (stat(path, &st) != 0) {
146 strlcpy (abspath, path, sizeof(abspath));
148 if (!S_ISDIR(st.st_mode)) {
149 if (NULL == (p=strrchr(abspath, '/')) )
150 strcpy(abspath, ".");
155 if (!getcwd(savecwd, sizeof(savecwd)) || chdir(abspath) < 0)
158 if (!getcwd(abspath, sizeof(abspath)) || chdir (savecwd) < 0)
161 if (strlen(abspath) > bufsize)
164 strlcpy(path, abspath, bufsize);
168 static char * find_volumeroot(char *path, size_t maxlen)
170 char *volume = make_path_absolute(path, maxlen);
175 if (NULL == (find_in_path(volume, "/.AppleDesktop", maxlen)) )
181 int vol_load_charsets( struct volinfo *vol)
183 if ( (charset_t) -1 == ( vol->v_maccharset = add_charset(vol->v_maccodepage)) ) {
184 fprintf( stderr, "Setting codepage %s as Mac codepage failed", vol->v_maccodepage);
188 if ( (charset_t) -1 == ( vol->v_volcharset = add_charset(vol->v_volcodepage)) ) {
189 fprintf( stderr, "Setting codepage %s as volume codepage failed", vol->v_volcodepage);
196 static int parse_options (char *buf, int *flags, const vol_opt_name_t *options)
199 const vol_opt_name_t *op;
203 while ( *p != '\0') {
207 for (;op->name; op++) {
208 if ( !strcmp(op->name, q )) {
209 *flags |= op->option;
223 static int parseline ( char *buf, struct volinfo *vol)
228 const info_option_t *p = &info_options[0];
230 if (NULL == ( value = strchr(buf, ':')) )
236 if ( 0 == (len = strlen(value)) )
239 if (value[len-1] == '\n')
242 for (;p->name; p++) {
243 if ( !strcmp(p->name, buf )) {
251 if ((vol->v_maccodepage = strdup(value)) == NULL) {
252 fprintf (stderr, "strdup: %s", strerror(errno));
257 if ((vol->v_volcodepage = strdup(value)) == NULL) {
258 fprintf (stderr, "strdup: %s", strerror(errno));
263 if ((vol->v_cnidscheme = strdup(value)) == NULL) {
264 fprintf (stderr, "strdup: %s", strerror(errno));
269 if ((vol->v_dbd_host = strdup(value)) == NULL) {
270 fprintf (stderr, "strdup: %s", strerror(errno));
275 vol->v_dbd_port = atoi(value);
278 if ((vol->v_dbpath = strdup(value)) == NULL) {
279 fprintf (stderr, "strdup: %s", strerror(errno));
284 if (strcasecmp(value, "v1") == 0) {
285 vol->v_adouble = AD_VERSION1;
286 vol->ad_path = ad_path;
288 #if AD_VERSION == AD_VERSION2
289 else if (strcasecmp(value, "v2") == 0) {
290 vol->ad_path = ad_path;
291 vol->v_adouble = AD_VERSION2;
293 else if (strcasecmp(value, "osx") == 0) {
294 vol->v_adouble = AD_VERSION2_OSX;
295 vol->ad_path = ad_path_osx;
299 fprintf (stderr, "unknown adouble version: %s, %s", buf, value);
304 parse_options(value, &vol->v_flags, &vol_opt_names[0]);
307 parse_options(value, &vol->v_casefold, &vol_opt_casefold[0]);
310 if (strcasecmp(value, "AFPVOL_EA_AD") == 0)
311 vol->v_vfs_ea = AFPVOL_EA_AD;
312 else if (strcasecmp(value, "AFPVOL_EA_SYS") == 0)
313 vol->v_vfs_ea = AFPVOL_EA_SYS;
316 fprintf (stderr, "unknown volume information: %s, %s", buf, value);
325 int loadvolinfo (char *path, struct volinfo *vol)
328 char volinfofile[MAXPATHLEN];
329 char buf[MAXPATHLEN];
337 memset(vol, 0, sizeof(struct volinfo));
338 strlcpy(volinfofile, path, sizeof(volinfofile));
340 /* volinfo file is in .AppleDesktop */
341 if ( NULL == find_volumeroot(volinfofile, sizeof(volinfofile)))
344 if ((vol->v_path = strdup(volinfofile)) == NULL ) {
345 fprintf (stderr, "strdup: %s", strerror(errno));
348 strlcat(volinfofile, ".AppleDesktop/", sizeof(volinfofile));
349 strlcat(volinfofile, VOLINFOFILE, sizeof(volinfofile));
351 /* open the file read only */
352 if ( NULL == (fp = fopen( volinfofile, "r")) ) {
353 fprintf (stderr, "error opening volinfo (%s): %s", volinfofile, strerror(errno));
358 /* try to get a read lock */
360 lock.l_whence = SEEK_SET;
362 lock.l_type = F_RDLCK;
364 /* wait for read lock */
365 if (fcntl(fd, F_SETLKW, &lock) < 0) {
371 while (NULL != fgets(buf, sizeof(buf), fp)) {
376 lock.l_type = F_UNLCK;
377 fcntl(fd, F_SETLK, &lock);
379 /* Translate vol options to ad options like afp/volume.c does it */
380 vol->v_ad_options = 0;
381 if ((vol->v_flags & AFPVOL_NODEV))
382 vol->v_ad_options |= ADVOL_NODEV;
383 if ((vol->v_flags & AFPVOL_CACHE))
384 vol->v_ad_options |= ADVOL_CACHE;
385 if ((vol->v_flags & AFPVOL_UNIX_PRIV))
386 vol->v_ad_options |= ADVOL_UNIXPRIV;
387 if ((vol->v_flags & AFPVOL_INV_DOTS))
388 vol->v_ad_options |= ADVOL_INVDOTS;
395 * Save the volume options to a file, used by shell utilities. Writing the file
396 * everytime a volume is opened is unnecessary, but it shouldn't hurt much.
398 int savevolinfo(const struct vol *vol, const char *Cnid_srv, const char *Cnid_port)
401 char item[MAXPATHLEN];
405 const vol_opt_name_t *op = &vol_opt_names[0];
406 const vol_opt_name_t *cf = &vol_opt_casefold[0];
408 strlcpy (item, vol->v_path, sizeof(item));
409 strlcat (item, "/.AppleDesktop/", sizeof(item));
410 strlcat (item, VOLINFOFILE, sizeof(item));
412 if ((fd = open( item, O_RDWR | O_CREAT , 0666)) <0 ) {
413 LOG(log_debug, logtype_afpd,"Error opening %s: %s", item, strerror(errno));
417 /* try to get a lock */
419 lock.l_whence = SEEK_SET;
421 lock.l_type = F_WRLCK;
423 if (fcntl(fd, F_SETLK, &lock) < 0) {
424 if (errno == EACCES || errno == EAGAIN) {
425 /* ignore, other process already writing the file */
428 LOG(log_error, logtype_cnid, "savevoloptions: cannot get lock: %s", strerror(errno));
433 /* write volume options */
434 snprintf(buf, sizeof(buf), "MAC_CHARSET:%s\n", vol->v_maccodepage);
435 snprintf(item, sizeof(item), "VOL_CHARSET:%s\n", vol->v_volcodepage);
436 strlcat(buf, item, sizeof(buf));
438 switch (vol->v_adouble) {
440 strlcat(buf, "ADOUBLE_VER:v1\n", sizeof(buf));
443 strlcat(buf, "ADOUBLE_VER:v2\n", sizeof(buf));
445 case AD_VERSION2_OSX:
446 strlcat(buf, "ADOUBLE_VER:osx\n", sizeof(buf));
448 case AD_VERSION1_SFM:
449 strlcat(buf, "ADOUBLE_VER:sfm\n", sizeof(buf));
453 strlcat(buf, "CNIDBACKEND:", sizeof(buf));
454 strlcat(buf, vol->v_cnidscheme, sizeof(buf));
455 strlcat(buf, "\n", sizeof(buf));
457 strlcat(buf, "CNIDDBDHOST:", sizeof(buf));
458 strlcat(buf, Cnid_srv, sizeof(buf));
459 strlcat(buf, "\n", sizeof(buf));
461 strlcat(buf, "CNIDDBDPORT:", sizeof(buf));
462 strlcat(buf, Cnid_port, sizeof(buf));
463 strlcat(buf, "\n", sizeof(buf));
465 strcpy(item, "CNID_DBPATH:");
467 strlcat(item, vol->v_dbpath, sizeof(item));
469 strlcat(item, vol->v_path, sizeof(item));
470 strlcat(item, "\n", sizeof(item));
471 strlcat(buf, item, sizeof(buf));
474 strcpy(item, "VOLUME_OPTS:");
475 for (;op->name; op++) {
476 if ( (vol->v_flags & op->option) ) {
477 strlcat(item, op->name, sizeof(item));
478 strlcat(item, " ", sizeof(item));
481 strlcat(item, "\n", sizeof(item));
482 strlcat(buf, item, sizeof(buf));
485 strcpy(item, "VOLCASEFOLD:");
486 for (;cf->name; cf++) {
487 if ( (vol->v_casefold & cf->option) ) {
488 strlcat(item, cf->name, sizeof(item));
489 strlcat(item, " ", sizeof(item));
492 strlcat(item, "\n", sizeof(item));
493 strlcat(buf, item, sizeof(buf));
495 /* ExtendedAttributes */
496 strcpy(item, "EXTATTRTYPE:");
497 switch (vol->v_vfs_ea) {
499 strlcat(item, "AFPVOL_EA_SYS\n", sizeof(item));
502 strlcat(item, "AFPVOL_EA_AD\n", sizeof(item));
505 strlcat(item, "AFPVOL_EA_NONE\n", sizeof(item));
508 strlcat(item, "AFPVOL_EA_UNKNOWN\n", sizeof(item));
511 strlcat(buf, item, sizeof(buf));
513 if (strlen(buf) >= sizeof(buf)-1)
514 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: buffer too small, %s", buf);
515 if (write( fd, buf, strlen(buf)) < 0 || ftruncate(fd, strlen(buf)) < 0 ) {
516 LOG(log_debug, logtype_afpd,"Error writing .volinfo file: %s", strerror(errno));
519 lock.l_type = F_UNLCK;
520 fcntl(fd, F_SETLK, &lock);