2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
18 #include <sys/param.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
25 #include <atalk/dsi.h>
26 #include <atalk/adouble.h>
27 #include <atalk/afp.h>
28 #include <atalk/util.h>
29 #include <atalk/logger.h>
30 #include <atalk/vfs.h>
31 #include <atalk/uuid.h>
33 #include <atalk/bstrlib.h>
34 #include <atalk/bstradd.h>
35 #include <atalk/ftw.h>
36 #include <atalk/globals.h>
37 #include <atalk/fce_api.h>
38 #include <atalk/errchk.h>
39 #include <atalk/iniparser.h>
40 #include <atalk/unix.h>
41 #include <atalk/netatalk_conf.h>
44 #include <atalk/cnid.h>
47 #include "directory.h"
58 extern int afprun(int root, char *cmd, int *outfd);
61 * Read band-size info from Info.plist XML file of an TM sparsebundle
63 * @param path (r) path to Info.plist file
64 * @return band-size in bytes, -1 on error
66 static long long int get_tm_bandsize(const char *path)
71 long long int bandsize = -1;
73 EC_NULL_LOGSTR( file = fopen(path, "r"),
74 "get_tm_bandsize(\"%s\"): %s",
75 path, strerror(errno) );
77 while (fgets(buf, sizeof(buf), file) != NULL) {
78 if (strstr(buf, "band-size") == NULL)
81 if (fscanf(file, " <integer>%lld</integer>", &bandsize) != 1) {
82 LOG(log_error, logtype_afpd, "get_tm_bandsize(\"%s\"): can't parse band-size", path);
91 LOG(log_debug, logtype_afpd, "get_tm_bandsize(\"%s\"): bandsize: %lld", path, bandsize);
96 * Return number on entries in a directory
98 * @param path (r) path to dir
99 * @return number of entries, -1 on error
101 static long long int get_tm_bands(const char *path)
104 long long int count = 0;
106 const struct dirent *entry;
108 EC_NULL( dir = opendir(path) );
110 while ((entry = readdir(dir)) != NULL)
112 count -= 2; /* All OSens I'm aware of return "." and "..", so just substract them, avoiding string comparison in loop */
123 * Calculate used size of a TimeMachine volume
125 * This assumes that the volume is used only for TimeMachine.
127 * 1) readdir(path of volume)
128 * 2) for every element that matches regex "\(.*\)\.sparsebundle$" :
129 * 3) parse "\1.sparsebundle/Info.plist" and read the band-size XML key integer value
130 * 4) readdir "\1.sparsebundle/bands/" counting files
131 * 5) calculate used size as: (file_count - 1) * band-size
133 * The result of the calculation is returned in "volume->v_tm_used".
134 * "volume->v_appended" gets reset to 0.
135 * "volume->v_tm_cachetime" is updated with the current time from time(NULL).
137 * "volume->v_tm_used" is cached for TM_USED_CACHETIME seconds and updated by
138 * "volume->v_appended". The latter is increased by X every time the client
139 * appends X bytes to a file (in fork.c).
141 * @param vol (rw) volume to calculate
142 * @return 0 on success, -1 on error
144 #define TM_USED_CACHETIME 60 /* cache for 60 seconds */
145 static int get_tm_used(struct vol * restrict vol)
148 long long int bandsize;
150 bstring infoplist = NULL;
151 bstring bandsdir = NULL;
153 const struct dirent *entry;
157 time_t now = time(NULL);
159 if (vol->v_tm_cachetime
160 && ((vol->v_tm_cachetime + TM_USED_CACHETIME) >= now)) {
161 if (vol->v_tm_used == -1)
163 vol->v_tm_used += vol->v_appended;
165 LOG(log_debug, logtype_afpd, "getused(\"%s\"): cached: %" PRIu64 " bytes",
166 vol->v_path, vol->v_tm_used);
170 vol->v_tm_cachetime = now;
172 EC_NULL( dir = opendir(vol->v_path) );
174 while ((entry = readdir(dir)) != NULL) {
175 if (((p = strstr(entry->d_name, "sparsebundle")) != NULL)
176 && (strlen(entry->d_name) == (p + strlen("sparsebundle") - entry->d_name))) {
178 EC_NULL_LOG( infoplist = bformat("%s/%s/%s", vol->v_path, entry->d_name, "Info.plist") );
180 if ((bandsize = get_tm_bandsize(cfrombstr(infoplist))) == -1)
183 EC_NULL_LOG( bandsdir = bformat("%s/%s/%s/", vol->v_path, entry->d_name, "bands") );
185 if ((links = get_tm_bands(cfrombstr(bandsdir))) == -1)
188 used += (links - 1) * bandsize;
189 LOG(log_debug, logtype_afpd, "getused(\"%s\"): bands: %" PRIu64 " bytes",
190 cfrombstr(bandsdir), used);
194 vol->v_tm_used = used;
204 LOG(log_debug, logtype_afpd, "getused(\"%s\"): %" PRIu64 " bytes", vol->v_path, vol->v_tm_used);
209 static int getvolspace(const AFPObj *obj, struct vol *vol,
210 uint32_t *bfree, uint32_t *btotal,
211 VolSpace *xbfree, VolSpace *xbtotal, uint32_t *bsize)
216 #ifndef NO_QUOTA_SUPPORT
217 VolSpace qfree, qtotal;
220 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
221 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
222 maxsize = (obj->afp_version < 22) ? 0x7fffffffL : 0xffffffffL;
225 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
226 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
227 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
228 goto getvolspace_done;
233 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal, bsize)) != AFP_OK ) {
237 #ifndef NO_QUOTA_SUPPORT
238 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
239 if ( uquota_getvolspace(obj, vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
240 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
241 *xbfree = MIN(*xbfree, qfree);
242 *xbtotal = MIN(*xbtotal, qtotal);
243 goto getvolspace_done;
247 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
250 if (vol->v_limitsize) {
251 if (get_tm_used(vol) != 0)
254 *xbtotal = MIN(*xbtotal, (vol->v_limitsize * 1024 * 1024));
255 *xbfree = MIN(*xbfree, *xbtotal < vol->v_tm_used ? 0 : *xbtotal - vol->v_tm_used);
257 LOG(log_debug, logtype_afpd,
258 "volparams: total: %" PRIu64 ", used: %" PRIu64 ", free: %" PRIu64 " bytes",
259 *xbtotal, vol->v_tm_used, *xbfree);
262 *bfree = MIN(*xbfree, maxsize);
263 *btotal = MIN(*xbtotal, maxsize);
267 /* -----------------------
268 * set volume creation date
269 * avoid duplicate, well at least it tries
271 static void vol_setdate(uint16_t id, struct adouble *adp, time_t date)
274 struct vol *vol = getvolumes();
276 for ( volume = getvolumes(); volume; volume = volume->v_next ) {
277 if (volume->v_vid == id) {
280 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
282 volume = getvolumes(); /* restart */
285 vol->v_ctime = AD_DATE_FROM_UNIX(date);
286 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
289 /* ----------------------- */
290 static int getvolparams(const AFPObj *obj, uint16_t bitmap, struct vol *vol, struct stat *st, char *buf, size_t *buflen)
293 int bit = 0, isad = 1;
296 uint32_t bfree, btotal, bsize;
297 VolSpace xbfree, xbtotal; /* extended bytes */
298 char *data, *nameoff = NULL;
301 LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
303 /* courtesy of jallison@whistle.com:
304 * For MacOS8.x support we need to create the
305 * .Parent file here if it doesn't exist. */
307 /* Convert adouble:v2 to adouble:ea on the fly */
308 (void)ad_convert(vol->v_path, st, vol, NULL);
311 if (ad_open(&ad, vol->v_path, ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_RDWR | ADFLAGS_CREATE, 0666) != 0 ) {
313 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
315 } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
316 slash = strrchr( vol->v_path, '/' );
321 if (ad_getentryoff(&ad, ADEID_NAME)) {
322 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
323 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
324 ad_getentrylen( &ad, ADEID_NAME ));
326 vol_setdate(vol->v_vid, &ad, st->st_mtime);
330 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
331 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
336 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
337 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
338 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
339 if ( getvolspace(obj, vol, &bfree, &btotal, &xbfree, &xbtotal,
340 &bsize) != AFP_OK ) {
342 ad_close( &ad, ADFLAGS_HF );
344 return( AFPERR_PARAM );
349 while ( bitmap != 0 ) {
350 while (( bitmap & 1 ) == 0 ) {
358 /* check for read-only.
359 * NOTE: we don't actually set the read-only flag unless
360 * it's passed in that way as it's possible to mount
361 * a read-write filesystem under a read-only one. */
362 if ((vol->v_flags & AFPVOL_RO) ||
363 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
364 ashort |= VOLPBIT_ATTR_RO;
366 /* prior 2.1 only VOLPBIT_ATTR_RO is defined */
367 if (obj->afp_version > 20) {
368 if (vol->v_cdb != NULL && (vol->v_cdb->flags & CNID_FLAG_PERSISTENT))
369 ashort |= VOLPBIT_ATTR_FILEID;
370 ashort |= VOLPBIT_ATTR_CATSEARCH;
372 if (obj->afp_version >= 30) {
373 ashort |= VOLPBIT_ATTR_UTF8;
374 if (vol->v_flags & AFPVOL_UNIX_PRIV)
375 ashort |= VOLPBIT_ATTR_UNIXPRIV;
376 if (vol->v_flags & AFPVOL_TM)
377 ashort |= VOLPBIT_ATTR_TM;
378 if (vol->v_flags & AFPVOL_NONETIDS)
379 ashort |= VOLPBIT_ATTR_NONETIDS;
380 if (obj->afp_version >= 32) {
382 ashort |= VOLPBIT_ATTR_EXT_ATTRS;
383 if (vol->v_flags & AFPVOL_ACLS)
384 ashort |= VOLPBIT_ATTR_ACLS;
388 ashort = htons(ashort);
389 memcpy(data, &ashort, sizeof( ashort ));
390 data += sizeof( ashort );
394 ashort = htons( AFPVOLSIG_DEFAULT );
395 memcpy(data, &ashort, sizeof( ashort ));
396 data += sizeof( ashort );
401 memcpy(data, &aint, sizeof( aint ));
402 data += sizeof( aint );
406 if ( st->st_mtime > vol->v_mtime ) {
407 vol->v_mtime = st->st_mtime;
409 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
410 memcpy(data, &aint, sizeof( aint ));
411 data += sizeof( aint );
415 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
416 aint = AD_DATE_START;
417 memcpy(data, &aint, sizeof( aint ));
418 data += sizeof( aint );
422 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
423 data += sizeof( vol->v_vid );
427 bfree = htonl( bfree );
428 memcpy(data, &bfree, sizeof( bfree ));
429 data += sizeof( bfree );
432 case VOLPBIT_BTOTAL :
433 btotal = htonl( btotal );
434 memcpy(data, &btotal, sizeof( btotal ));
435 data += sizeof( btotal );
438 #ifndef NO_LARGE_VOL_SUPPORT
439 case VOLPBIT_XBFREE :
440 xbfree = hton64( xbfree );
441 memcpy(data, &xbfree, sizeof( xbfree ));
442 data += sizeof( xbfree );
445 case VOLPBIT_XBTOTAL :
446 xbtotal = hton64( xbtotal );
447 memcpy(data, &xbtotal, sizeof( xbtotal ));
448 data += sizeof( xbfree );
450 #endif /* ! NO_LARGE_VOL_SUPPORT */
454 data += sizeof( uint16_t );
457 case VOLPBIT_BSIZE: /* block size */
458 bsize = htonl(bsize);
459 memcpy(data, &bsize, sizeof(bsize));
460 data += sizeof(bsize);
465 ad_close( &ad, ADFLAGS_HF );
467 return( AFPERR_BITMAP );
473 ashort = htons( data - buf );
474 memcpy(nameoff, &ashort, sizeof( ashort ));
475 /* name is always in mac charset */
476 aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
486 ad_close(&ad, ADFLAGS_HF);
488 *buflen = data - buf;
492 /* ------------------------- */
493 static int stat_vol(const AFPObj *obj, uint16_t bitmap, struct vol *vol, char *rbuf, size_t *rbuflen)
499 if ( stat( vol->v_path, &st ) < 0 ) {
501 return( AFPERR_PARAM );
503 /* save the volume device number */
504 vol->v_dev = st.st_dev;
506 buflen = *rbuflen - sizeof( bitmap );
507 if (( ret = getvolparams(obj, bitmap, vol, &st,
508 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
512 *rbuflen = buflen + sizeof( bitmap );
513 bitmap = htons( bitmap );
514 memcpy(rbuf, &bitmap, sizeof( bitmap ));
519 /* ------------------------------- */
520 int afp_getsrvrparms(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
530 load_volumes(obj, closevol);
533 for ( vcnt = 0, volume = getvolumes(); volume; volume = volume->v_next ) {
534 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
537 if ( stat( volume->v_path, &st ) < 0 ) {
538 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
539 volume->v_path, strerror(errno) );
540 continue; /* can't access directory */
542 if (!S_ISDIR(st.st_mode)) {
543 continue; /* not a dir */
545 accessmode(obj, volume, volume->v_path, &ma, NULL, &st);
546 if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
547 continue; /* no r-x access */
551 if (utf8_encoding(obj)) {
552 len = ucs2_to_charset_allocate(CH_UTF8_MAC, &namebuf, volume->v_u8mname);
554 len = ucs2_to_charset_allocate(obj->options.maccharset, &namebuf, volume->v_macname);
557 if (len == (size_t)-1)
560 /* set password bit if there's a volume password */
561 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
563 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
565 memcpy(data, namebuf, len );
571 *rbuflen = data - rbuf;
573 if ( gettimeofday( &tv, NULL ) < 0 ) {
574 LOG(log_error, logtype_afpd, "afp_getsrvrparms(%s): gettimeofday: %s", volume->v_path, strerror(errno) );
578 tv.tv_sec = AD_DATE_FROM_UNIX(tv.tv_sec);
579 memcpy(data, &tv.tv_sec, sizeof( uint32_t));
580 data += sizeof( uint32_t);
585 /* ------------------------- */
586 static int volume_codepage(AFPObj *obj, struct vol *volume)
588 struct charset_functions *charset;
591 if (!volume->v_volcodepage)
592 volume->v_volcodepage = strdup("UTF8");
594 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
595 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
599 if ( NULL == (charset = find_charset_functions(volume->v_volcodepage)) || charset->flags & CHARSET_ICONV ) {
600 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
603 if (!volume->v_maccodepage)
604 volume->v_maccodepage = strdup(obj->options.maccodepage);
606 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
607 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
611 if ( NULL == ( charset = find_charset_functions(volume->v_maccodepage)) || ! (charset->flags & CHARSET_CLIENT) ) {
612 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
615 volume->v_kTextEncoding = htonl(charset->kTextEncoding);
619 /* ------------------------- */
620 static int volume_openDB(const AFPObj *obj, struct vol *volume)
624 if ((volume->v_flags & AFPVOL_NODEV)) {
625 flags |= CNID_FLAG_NODEV;
628 LOG(log_debug, logtype_afpd, "CNID server: %s:%s", volume->v_cnidserver, volume->v_cnidport);
630 volume->v_cdb = cnid_open(volume->v_path,
632 volume->v_cnidscheme,
634 volume->v_cnidserver,
637 if ( ! volume->v_cdb && ! (flags & CNID_FLAG_MEMORY)) {
638 /* The first attempt failed and it wasn't yet an attempt to open in-memory */
639 LOG(log_error, logtype_afpd, "Can't open volume \"%s\" CNID backend \"%s\" ",
640 volume->v_path, volume->v_cnidscheme);
641 LOG(log_error, logtype_afpd, "Reopen volume %s using in memory temporary CNID DB.",
643 flags |= CNID_FLAG_MEMORY;
644 volume->v_cdb = cnid_open (volume->v_path, volume->v_umask, "tdb", flags, NULL, NULL);
646 /* kill ourself with SIGUSR2 aka msg pending */
648 setmessage("Something wrong with the volume's CNID DB, using temporary CNID DB instead."
649 "Check server messages for details!");
650 kill(getpid(), SIGUSR2);
651 /* deactivate cnid caching/storing in AppleDouble files */
656 return (!volume->v_cdb)?-1:0;
659 /* -------------------------
660 * we are the user here
662 int afp_openvol(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
678 memcpy(&bitmap, ibuf, sizeof( bitmap ));
679 bitmap = ntohs( bitmap );
680 ibuf += sizeof( bitmap );
683 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
684 return AFPERR_BITMAP;
687 len = (unsigned char)*ibuf++;
688 volname = obj->oldtmp;
690 if ((volname_tmp = strchr(volname,'+')) != NULL)
691 volname = volname_tmp+1;
693 if (utf8_encoding(obj)) {
694 namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
696 namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
704 if ((len + 1) & 1) /* pad to an even boundary */
707 load_volumes(obj, closevol);
709 for ( volume = getvolumes(); volume; volume = volume->v_next ) {
710 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
715 if ( volume == NULL ) {
719 /* check for a volume password */
720 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
721 return AFPERR_ACCESS;
724 if (( volume->v_flags & AFPVOL_OPEN ) ) {
725 /* the volume is already open */
726 return stat_vol(obj, bitmap, volume, rbuf, rbuflen);
729 if (volume->v_root_preexec) {
730 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
731 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
736 if (volume->v_preexec) {
737 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
738 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
743 if ( stat( volume->v_path, &st ) < 0 ) {
747 if ( chdir( volume->v_path ) < 0 ) {
751 if (volume_codepage(obj, volume) < 0) {
756 /* initialize volume variables
759 if (utf8_encoding(obj)) {
760 volume->max_filename = UTF8FILELEN_EARLY;
763 volume->max_filename = MACFILELEN;
766 volume->v_flags |= AFPVOL_OPEN;
767 volume->v_cdb = NULL;
769 if (utf8_encoding(obj)) {
770 len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
772 len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
774 if ( !vol_mname || len <= 0) {
779 if ((vol_uname = strrchr(volume->v_path, '/')) == NULL)
780 vol_uname = volume->v_path;
781 else if (vol_uname[1] != '\0')
784 if ((dir = dir_new(vol_mname,
789 bfromcstr(volume->v_path),
793 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
798 volume->v_root = dir;
801 if (volume_openDB(obj, volume) < 0) {
802 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
803 volume->v_path, volume->v_cnidscheme);
808 ret = stat_vol(obj, bitmap, volume, rbuf, rbuflen);
812 * If you mount a volume twice, the second time the trash appears on
813 * the desk-top. That's because the Mac remembers the DID for the
814 * trash (even for volumes in different zones, on different servers).
815 * Just so this works better, we prime the DID cache with the trash,
816 * fixing the trash at DID 17.
817 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
819 if ((volume->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
821 /* FIXME find db time stamp */
822 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
823 LOG (log_error, logtype_afpd,
824 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
832 if ((msg = iniparser_getstring(obj->iniconfig, volume->v_configname, "login message", NULL)) != NULL)
839 if (volume->v_root) {
840 dir_free( volume->v_root );
841 volume->v_root = NULL;
844 volume->v_flags &= ~AFPVOL_OPEN;
845 if (volume->v_cdb != NULL) {
846 cnid_close(volume->v_cdb);
847 volume->v_cdb = NULL;
853 void closevol(const AFPObj *obj, struct vol *vol)
858 vol->v_flags &= ~AFPVOL_OPEN;
860 of_closevol(obj, vol);
862 dir_free( vol->v_root );
864 if (vol->v_cdb != NULL) {
865 cnid_close(vol->v_cdb);
869 if (vol->v_postexec) {
870 afprun(0, vol->v_postexec, NULL);
872 if (vol->v_root_postexec) {
873 afprun(1, vol->v_root_postexec, NULL);
877 /* ------------------------- */
878 void close_all_vol(const AFPObj *obj)
882 for ( ovol = getvolumes(); ovol; ovol = ovol->v_next ) {
883 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
884 ovol->v_flags &= ~AFPVOL_OPEN;
890 /* ------------------------- */
891 int afp_closevol(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
898 memcpy(&vid, ibuf, sizeof( vid ));
899 if (NULL == ( vol = getvolbyvid( vid )) ) {
900 return( AFPERR_PARAM );
910 /* --------------------------
911 poll if a volume is changed by other processes.
913 0 no attention msg sent
915 -1 error (socket closed)
917 Note: if attention return -1 no packet has been
918 sent because the buffer is full, we don't care
919 either there's no reader or there's a lot of
920 traffic and another pollvoltime will follow
922 int pollvoltime(AFPObj *obj)
929 if (!(obj->afp_version > 21 && obj->options.flags & OPTION_SERVERNOTIF))
932 if ( gettimeofday( &tv, NULL ) < 0 )
935 for ( vol = getvolumes(); vol; vol = vol->v_next ) {
936 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
937 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
938 vol->v_mtime = st.st_mtime;
939 if (!obj->attention(obj->dsi, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
948 /* ------------------------- */
949 void setvoltime(AFPObj *obj, struct vol *vol)
953 if ( gettimeofday( &tv, NULL ) < 0 ) {
954 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
957 if( utime( vol->v_path, NULL ) < 0 ) {
958 /* write of time failed ... probably a read only filesys,
959 * where no other users can interfere, so there's no issue here
963 /* a little granularity */
964 if (vol->v_mtime < tv.tv_sec) {
965 vol->v_mtime = tv.tv_sec;
966 /* or finder doesn't update free space
967 * AFP 3.2 and above clients seem to be ok without so many notification
969 if (obj->afp_version < 32 && obj->options.flags & OPTION_SERVERNOTIF) {
970 obj->attention(obj->dsi, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
975 /* ------------------------- */
976 int afp_getvolparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_,char *rbuf, size_t *rbuflen)
979 uint16_t vid, bitmap;
982 memcpy(&vid, ibuf, sizeof( vid ));
983 ibuf += sizeof( vid );
984 memcpy(&bitmap, ibuf, sizeof( bitmap ));
985 bitmap = ntohs( bitmap );
987 if (NULL == ( vol = getvolbyvid( vid )) ) {
989 return( AFPERR_PARAM );
992 return stat_vol(obj, bitmap, vol, rbuf, rbuflen);
995 /* ------------------------- */
996 int afp_setvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1000 uint16_t vid, bitmap;
1006 memcpy(&vid, ibuf, sizeof( vid ));
1007 ibuf += sizeof( vid );
1008 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1009 bitmap = ntohs( bitmap );
1010 ibuf += sizeof(bitmap);
1012 if (( vol = getvolbyvid( vid )) == NULL ) {
1013 return( AFPERR_PARAM );
1016 if ((vol->v_flags & AFPVOL_RO))
1017 return AFPERR_VLOCK;
1019 /* we can only set the backup date. */
1020 if (bitmap != (1 << VOLPBIT_BDATE))
1021 return AFPERR_BITMAP;
1024 if ( ad_open(&ad, vol->v_path, ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_RDWR) < 0 ) {
1026 return AFPERR_VLOCK;
1028 return AFPERR_ACCESS;
1031 memcpy(&aint, ibuf, sizeof(aint));
1032 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1034 ad_close(&ad, ADFLAGS_HF);