]> arthur.barton.de Git - netatalk.git/blob - libatalk/util/volinfo.c
New volume option "eject" for eg CD-ROM. Forces in-memory tdb CNID backend.
[netatalk.git] / libatalk / util / volinfo.c
1 /*
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.
6
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.
11
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.
15
16    .volinfo file handling, command line utilities
17    copyright Bjoern Fernhomberg, 2004
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif /* HAVE_CONFIG_H */
23
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #ifdef HAVE_STRINGS_H
31 #include <strings.h>
32 #endif
33 #ifdef STDC_HEADERS
34 #include <string.h>
35 #endif
36 #include <sys/param.h>
37
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>
43 #ifdef CNID_DB
44 #include <atalk/cnid.h>
45 #endif /* CNID_DB*/
46
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 */
70     {0, NULL}
71 };
72
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"},
78     {0, NULL}
79 };
80
81 typedef struct {
82     const char *name;
83     int type;
84 } info_option_t;
85
86 #define MAC_CHARSET 0
87 #define VOL_CHARSET 1
88 #define ADOUBLE_VER 2
89 #define CNIDBACKEND 3
90 #define CNIDDBDHOST 4
91 #define CNIDDBDPORT 5
92 #define CNID_DBPATH 6
93 #define VOLUME_OPTS 7
94 #define VOLCASEFOLD 8
95 #define EXTATTRTYPE 9
96
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},
108    {NULL, 0}
109 };
110
111 static char* find_in_path( char *path, char *subdir, size_t maxlen)
112 {
113     char        *pos;
114     struct stat st;
115
116     strlcat(path, subdir, maxlen);
117     pos = strrchr(path, '/');
118
119     while ( stat(path, &st) != 0) {
120         path[pos-path]=0;
121         if ((pos = strrchr(path, '/'))) {
122             path[pos-path]=0;
123             strlcat(path, subdir, maxlen);
124         }
125         else {
126             return NULL;
127         }
128     }
129
130     path[pos-path] = '/';
131     path[pos-path+1] = 0;
132
133     return path;
134 }
135
136 static char * make_path_absolute(char *path, size_t bufsize)
137 {
138     struct      stat st;
139     char        savecwd[MAXPATHLEN];
140     char        abspath[MAXPATHLEN];
141     char        *p;
142
143     if (stat(path, &st) != 0) {
144         return NULL;
145     }
146
147     strlcpy (abspath, path, sizeof(abspath));
148
149     if (!S_ISDIR(st.st_mode)) {
150         if (NULL == (p=strrchr(abspath, '/')) )
151             strcpy(abspath, ".");
152         else
153             *p = '\0';
154     }
155
156     if (!getcwd(savecwd, sizeof(savecwd)) || chdir(abspath) < 0)        
157         return NULL;
158
159     if (!getcwd(abspath, sizeof(abspath)) || chdir (savecwd) < 0)
160         return NULL;
161     
162     if (strlen(abspath) > bufsize)
163         return NULL;
164
165     strlcpy(path, abspath, bufsize);
166     return path;
167 }
168
169 static char * find_volumeroot(char *path, size_t maxlen)
170 {
171     char *volume = make_path_absolute(path, maxlen);
172         
173     if (volume == NULL)
174        return NULL;
175
176     if (NULL == (find_in_path(volume, "/.AppleDesktop", maxlen)) )
177        return NULL;
178
179     return volume;
180 }
181
182 int vol_load_charsets( struct volinfo *vol)
183 {
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);
186         return (-1);
187     }
188
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);
191         return (-1);
192     }
193
194     return 0;
195 }
196
197 static int parse_options (char *buf, int *flags, const vol_opt_name_t *options)
198 {
199     char *p, *q;
200     const vol_opt_name_t *op;
201
202     q = p = buf; 
203
204     while ( *p != '\0') {
205         if (*p == ' ') {
206             *p = '\0';
207             op = options;
208             for (;op->name; op++) {
209                 if ( !strcmp(op->name, q )) {
210                     *flags |= op->option;
211                     break;
212                 }
213             }
214             q = p+1;
215         }
216         p++;
217     }
218
219     return 0;
220
221             
222
223
224 static int parseline ( char *buf, struct volinfo *vol)
225 {
226     char *value;
227     size_t len;
228     int  option=-1;
229     const info_option_t  *p  = &info_options[0];
230
231     if (NULL == ( value = strchr(buf, ':')) )
232         return 1;
233
234     *value = '\0';
235     value++;
236
237     if ( 0 == (len = strlen(value)) )
238         return 0;
239
240     if (value[len-1] == '\n')
241         value[len-1] = '\0';
242
243     for (;p->name; p++) {
244         if ( !strcmp(p->name, buf )) {
245             option=p->type;
246             break;
247         }
248     }
249
250     switch (option) {
251       case MAC_CHARSET:
252         if ((vol->v_maccodepage = strdup(value)) == NULL) {
253             fprintf (stderr, "strdup: %s", strerror(errno));
254             return -1;
255         }
256         break;
257       case VOL_CHARSET:
258         if ((vol->v_volcodepage = strdup(value)) == NULL) {
259             fprintf (stderr, "strdup: %s", strerror(errno));
260             return -1;
261         }
262         break;
263       case CNIDBACKEND:
264         if ((vol->v_cnidscheme = strdup(value)) == NULL) {
265             fprintf (stderr, "strdup: %s", strerror(errno));
266             return -1;
267         }
268         break;
269       case CNIDDBDHOST:
270         if ((vol->v_dbd_host = strdup(value)) == NULL) {
271             fprintf (stderr, "strdup: %s", strerror(errno));
272             return -1;
273         }
274         break;
275       case CNIDDBDPORT:
276         vol->v_dbd_port = atoi(value);
277         break;
278       case CNID_DBPATH:
279         if ((vol->v_dbpath = strdup(value)) == NULL) {
280             fprintf (stderr, "strdup: %s", strerror(errno));
281             return -1;
282         }
283         break;
284       case ADOUBLE_VER:
285         if (strcasecmp(value, "v1") == 0) {
286             vol->v_adouble = AD_VERSION1;
287             vol->ad_path = ad_path;
288         }
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;
293         }
294         else if (strcasecmp(value, "osx") == 0) {
295             vol->v_adouble = AD_VERSION2_OSX;
296             vol->ad_path = ad_path_osx;
297         }
298 #endif
299         else  {
300             fprintf (stderr, "unknown adouble version: %s, %s", buf, value);
301             return -1;
302         }
303         break;
304       case VOLUME_OPTS:
305         parse_options(value, &vol->v_flags, &vol_opt_names[0]);
306         break;
307       case VOLCASEFOLD:
308         parse_options(value, &vol->v_casefold, &vol_opt_casefold[0]);
309         break;
310     case EXTATTRTYPE:
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;
315         break;
316       default:
317         fprintf (stderr, "unknown volume information: %s, %s", buf, value);
318         return (-1);
319         break;
320     }
321         
322     return 0;
323 }
324     
325
326 int loadvolinfo (char *path, struct volinfo *vol)
327 {
328
329     char   volinfofile[MAXPATHLEN];
330     char   buf[MAXPATHLEN];
331     struct flock lock;
332     int    fd;
333     FILE   *fp;
334
335     if ( !path || !vol)
336         return -1;
337
338     memset(vol, 0, sizeof(struct volinfo));
339     strlcpy(volinfofile, path, sizeof(volinfofile));
340
341     /* volinfo file is in .AppleDesktop */ 
342     if ( NULL == find_volumeroot(volinfofile, sizeof(volinfofile)))
343         return -1;
344
345     if ((vol->v_path = strdup(volinfofile)) == NULL ) {
346         fprintf (stderr, "strdup: %s", strerror(errno));
347         return (-1);
348     }
349     strlcat(volinfofile, ".AppleDesktop/", sizeof(volinfofile));
350     strlcat(volinfofile, VOLINFOFILE, sizeof(volinfofile));
351
352     /* open the file read only */
353     if ( NULL == (fp = fopen( volinfofile, "r")) )  {
354         fprintf (stderr, "error opening volinfo (%s): %s", volinfofile, strerror(errno));
355         return (-1);
356     }
357     fd = fileno(fp); 
358
359     /* try to get a read lock */ 
360     lock.l_start  = 0;
361     lock.l_whence = SEEK_SET;
362     lock.l_len    = 0;
363     lock.l_type   = F_RDLCK;
364
365     /* wait for read lock */
366     if (fcntl(fd, F_SETLKW, &lock) < 0) {
367         fclose(fp);
368         return (-1);
369     }
370
371     /* read the file */
372     while (NULL != fgets(buf, sizeof(buf), fp)) {
373         parseline(buf, vol);
374     }
375
376     /* unlock file */
377     lock.l_type = F_UNLCK;
378     fcntl(fd, F_SETLK, &lock);
379
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;
390
391     fclose(fp);
392     return 0;
393 }
394
395 /*
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.
398  */
399 int savevolinfo(const struct vol *vol, const char *Cnid_srv, const char *Cnid_port)
400 {
401     char buf[16348];
402     char item[MAXPATHLEN];
403     int fd;
404     int ret = 0;
405     struct flock lock;
406     const vol_opt_name_t *op = &vol_opt_names[0];
407     const vol_opt_name_t *cf = &vol_opt_casefold[0];
408
409     strlcpy (item, vol->v_path, sizeof(item));
410     strlcat (item, "/.AppleDesktop/", sizeof(item));
411     strlcat (item, VOLINFOFILE, sizeof(item));
412
413     if ((fd = open( item, O_RDWR | O_CREAT , 0666)) <0 ) {
414         LOG(log_debug, logtype_afpd,"Error opening %s: %s", item, strerror(errno));
415         return (-1);
416     }
417
418     /* try to get a lock */
419     lock.l_start  = 0;
420     lock.l_whence = SEEK_SET;
421     lock.l_len    = 0;
422     lock.l_type   = F_WRLCK;
423
424     if (fcntl(fd, F_SETLK, &lock) < 0) {
425         if (errno == EACCES || errno == EAGAIN) {
426             /* ignore, other process already writing the file */
427             return 0;
428         } else {
429             LOG(log_error, logtype_cnid, "savevoloptions: cannot get lock: %s", strerror(errno));
430             return (-1);
431         }
432     }
433
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));
438
439     switch (vol->v_adouble) {
440         case AD_VERSION1:
441             strlcat(buf, "ADOUBLE_VER:v1\n", sizeof(buf));
442             break;
443         case AD_VERSION2:
444             strlcat(buf, "ADOUBLE_VER:v2\n", sizeof(buf));
445             break;
446         case AD_VERSION2_OSX:
447             strlcat(buf, "ADOUBLE_VER:osx\n", sizeof(buf));
448             break;
449         case AD_VERSION1_SFM:
450             strlcat(buf, "ADOUBLE_VER:sfm\n", sizeof(buf));
451             break;
452     }
453
454     strlcat(buf, "CNIDBACKEND:", sizeof(buf));
455     strlcat(buf, vol->v_cnidscheme, sizeof(buf));
456     strlcat(buf, "\n", sizeof(buf));
457
458     strlcat(buf, "CNIDDBDHOST:", sizeof(buf));
459     strlcat(buf, Cnid_srv, sizeof(buf));
460     strlcat(buf, "\n", sizeof(buf));
461
462     strlcat(buf, "CNIDDBDPORT:", sizeof(buf));
463     strlcat(buf, Cnid_port, sizeof(buf));
464     strlcat(buf, "\n", sizeof(buf));
465
466     strcpy(item, "CNID_DBPATH:");
467     if (vol->v_dbpath)
468         strlcat(item, vol->v_dbpath, sizeof(item));
469     else
470         strlcat(item, vol->v_path, sizeof(item));
471     strlcat(item, "\n", sizeof(item));
472     strlcat(buf, item, sizeof(buf));
473
474     /* volume flags */
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));
480         }
481     }
482     strlcat(item, "\n", sizeof(item));
483     strlcat(buf, item, sizeof(buf));
484
485     /* casefold flags */
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));
491         }
492     }
493     strlcat(item, "\n", sizeof(item));
494     strlcat(buf, item, sizeof(buf));
495
496     /* ExtendedAttributes */
497     strcpy(item, "EXTATTRTYPE:");
498     switch (vol->v_vfs_ea) {
499     case AFPVOL_EA_SYS:
500         strlcat(item, "AFPVOL_EA_SYS\n", sizeof(item));
501         break;
502     case AFPVOL_EA_AD:
503         strlcat(item, "AFPVOL_EA_AD\n", sizeof(item));
504         break;
505     case AFPVOL_EA_NONE:
506         strlcat(item, "AFPVOL_EA_NONE\n", sizeof(item));
507         break;
508     default:
509         strlcat(item, "AFPVOL_EA_UNKNOWN\n", sizeof(item));
510     }
511
512     strlcat(buf, item, sizeof(buf));
513
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));
518    }
519
520    lock.l_type = F_UNLCK;
521    fcntl(fd, F_SETLK, &lock);
522    close (fd);
523    return ret;
524 }