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>
42 #include <atalk/server_ipc.h>
45 #include <atalk/cnid.h>
48 #include "directory.h"
59 extern int afprun(int root, char *cmd, int *outfd);
62 * Read band-size info from Info.plist XML file of an TM sparsebundle
64 * @param path (r) path to Info.plist file
65 * @return band-size in bytes, -1 on error
67 static long long int get_tm_bandsize(const char *path)
72 long long int bandsize = -1;
74 EC_NULL_LOGSTR( file = fopen(path, "r"),
75 "get_tm_bandsize(\"%s\"): %s",
76 path, strerror(errno) );
78 while (fgets(buf, sizeof(buf), file) != NULL) {
79 if (strstr(buf, "band-size") == NULL)
82 if (fscanf(file, " <integer>%lld</integer>", &bandsize) != 1) {
83 LOG(log_error, logtype_afpd, "get_tm_bandsize(\"%s\"): can't parse band-size", path);
92 LOG(log_debug, logtype_afpd, "get_tm_bandsize(\"%s\"): bandsize: %lld", path, bandsize);
97 * Return number on entries in a directory
99 * @param path (r) path to dir
100 * @return number of entries, -1 on error
102 static long long int get_tm_bands(const char *path)
105 long long int count = 0;
107 const struct dirent *entry;
109 EC_NULL( dir = opendir(path) );
111 while ((entry = readdir(dir)) != NULL)
113 count -= 2; /* All OSens I'm aware of return "." and "..", so just substract them, avoiding string comparison in loop */
124 * Calculate used size of a TimeMachine volume
126 * This assumes that the volume is used only for TimeMachine.
128 * 1) readdir(path of volume)
129 * 2) for every element that matches regex "\(.*\)\.sparsebundle$" :
130 * 3) parse "\1.sparsebundle/Info.plist" and read the band-size XML key integer value
131 * 4) readdir "\1.sparsebundle/bands/" counting files
132 * 5) calculate used size as: (file_count - 1) * band-size
134 * The result of the calculation is returned in "volume->v_tm_used".
135 * "volume->v_appended" gets reset to 0.
136 * "volume->v_tm_cachetime" is updated with the current time from time(NULL).
138 * "volume->v_tm_used" is cached for TM_USED_CACHETIME seconds and updated by
139 * "volume->v_appended". The latter is increased by X every time the client
140 * appends X bytes to a file (in fork.c).
142 * @param vol (rw) volume to calculate
143 * @return 0 on success, -1 on error
145 #define TM_USED_CACHETIME 60 /* cache for 60 seconds */
146 static int get_tm_used(struct vol * restrict vol)
149 long long int bandsize;
151 bstring infoplist = NULL;
152 bstring bandsdir = NULL;
154 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) {
185 EC_NULL_LOG( bandsdir = bformat("%s/%s/%s/", vol->v_path, entry->d_name, "bands") );
187 if ((links = get_tm_bands(cfrombstr(bandsdir))) == -1) {
193 used += (links - 1) * bandsize;
194 LOG(log_debug, logtype_afpd, "getused(\"%s\"): bands: %" PRIu64 " bytes",
195 cfrombstr(bandsdir), used);
199 vol->v_tm_used = used;
209 LOG(log_debug, logtype_afpd, "getused(\"%s\"): %" PRIu64 " bytes", vol->v_path, vol->v_tm_used);
214 static int getvolspace(const AFPObj *obj, struct vol *vol,
215 uint32_t *bfree, uint32_t *btotal,
216 VolSpace *xbfree, VolSpace *xbtotal, uint32_t *bsize)
220 #ifndef NO_QUOTA_SUPPORT
221 VolSpace qfree, qtotal;
224 spaceflag = AFPVOL_GVSMASK & vol->v_flags;
225 /* report up to 2GB if afp version is < 2.2 (4GB if not) */
226 maxsize = (obj->afp_version < 22) ? 0x7fffffffL : 0xffffffffL;
229 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_AFSGVS ) {
230 if ( afs_getvolspace( vol, xbfree, xbtotal, bsize ) == AFP_OK ) {
231 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_AFSGVS;
232 goto getvolspace_done;
237 if (( rc = ustatfs_getvolspace( vol, xbfree, xbtotal, bsize)) != AFP_OK ) {
241 #ifndef NO_QUOTA_SUPPORT
242 if ( spaceflag == AFPVOL_NONE || spaceflag == AFPVOL_UQUOTA ) {
243 if ( uquota_getvolspace(obj, vol, &qfree, &qtotal, *bsize ) == AFP_OK ) {
244 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_UQUOTA;
245 *xbfree = MIN(*xbfree, qfree);
246 *xbtotal = MIN(*xbtotal, qtotal);
247 goto getvolspace_done;
251 vol->v_flags = ( ~AFPVOL_GVSMASK & vol->v_flags ) | AFPVOL_USTATFS;
254 if (vol->v_limitsize) {
255 if (get_tm_used(vol) != 0)
258 *xbtotal = MIN(*xbtotal, (vol->v_limitsize * 1024 * 1024));
259 *xbfree = MIN(*xbfree, *xbtotal < vol->v_tm_used ? 0 : *xbtotal - vol->v_tm_used);
261 LOG(log_debug, logtype_afpd,
262 "volparams: total: %" PRIu64 ", used: %" PRIu64 ", free: %" PRIu64 " bytes",
263 *xbtotal, vol->v_tm_used, *xbfree);
266 *bfree = MIN(*xbfree, maxsize);
267 *btotal = MIN(*xbtotal, maxsize);
271 /* -----------------------
272 * set volume creation date
273 * avoid duplicate, well at least it tries
275 static void vol_setdate(uint16_t id, struct adouble *adp, time_t date)
278 struct vol *vol = getvolumes();
280 for ( volume = getvolumes(); volume; volume = volume->v_next ) {
281 if (volume->v_vid == id) {
284 else if ((time_t)(AD_DATE_FROM_UNIX(date)) == volume->v_ctime) {
286 volume = getvolumes(); /* restart */
289 vol->v_ctime = AD_DATE_FROM_UNIX(date);
290 ad_setdate(adp, AD_DATE_CREATE | AD_DATE_UNIX, date);
293 /* ----------------------- */
294 static int getvolparams(const AFPObj *obj, uint16_t bitmap, struct vol *vol, struct stat *st, char *buf, size_t *buflen)
297 int bit = 0, isad = 1;
300 uint32_t bfree, btotal, bsize;
301 VolSpace xbfree, xbtotal; /* extended bytes */
302 char *data, *nameoff = NULL;
305 LOG(log_debug, logtype_afpd, "getvolparams: Volume '%s'", vol->v_localname);
307 /* courtesy of jallison@whistle.com:
308 * For MacOS8.x support we need to create the
309 * .Parent file here if it doesn't exist. */
311 /* Convert adouble:v2 to adouble:ea on the fly */
312 (void)ad_convert(vol->v_path, st, vol, NULL);
315 if (ad_open(&ad, vol->v_path, ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_RDWR | ADFLAGS_CREATE, 0666) != 0 ) {
317 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
319 } else if (ad_get_MD_flags( &ad ) & O_CREAT) {
320 slash = strrchr( vol->v_path, '/' );
325 if (ad_getentryoff(&ad, ADEID_NAME)) {
326 ad_setentrylen( &ad, ADEID_NAME, strlen( slash ));
327 memcpy(ad_entry( &ad, ADEID_NAME ), slash,
328 ad_getentrylen( &ad, ADEID_NAME ));
330 vol_setdate(vol->v_vid, &ad, st->st_mtime);
334 if (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0)
335 vol->v_ctime = AD_DATE_FROM_UNIX(st->st_mtime);
340 if (( bitmap & ( (1<<VOLPBIT_BFREE)|(1<<VOLPBIT_BTOTAL) |
341 (1<<VOLPBIT_XBFREE)|(1<<VOLPBIT_XBTOTAL) |
342 (1<<VOLPBIT_BSIZE)) ) != 0 ) {
343 if ( getvolspace(obj, vol, &bfree, &btotal, &xbfree, &xbtotal,
344 &bsize) != AFP_OK ) {
346 ad_close( &ad, ADFLAGS_HF );
348 return( AFPERR_PARAM );
353 while ( bitmap != 0 ) {
354 while (( bitmap & 1 ) == 0 ) {
362 /* check for read-only.
363 * NOTE: we don't actually set the read-only flag unless
364 * it's passed in that way as it's possible to mount
365 * a read-write filesystem under a read-only one. */
366 if ((vol->v_flags & AFPVOL_RO) ||
367 ((utime(vol->v_path, NULL) < 0) && (errno == EROFS))) {
368 ashort |= VOLPBIT_ATTR_RO;
370 /* prior 2.1 only VOLPBIT_ATTR_RO is defined */
371 if (obj->afp_version > 20) {
372 if (vol->v_cdb != NULL && (vol->v_cdb->cnid_db_flags & CNID_FLAG_PERSISTENT))
373 ashort |= VOLPBIT_ATTR_FILEID;
374 ashort |= VOLPBIT_ATTR_CATSEARCH;
376 if (obj->afp_version >= 30) {
377 ashort |= VOLPBIT_ATTR_UTF8;
378 if (vol->v_flags & AFPVOL_UNIX_PRIV)
379 ashort |= VOLPBIT_ATTR_UNIXPRIV;
380 if (vol->v_flags & AFPVOL_TM)
381 ashort |= VOLPBIT_ATTR_TM;
382 if (vol->v_flags & AFPVOL_NONETIDS)
383 ashort |= VOLPBIT_ATTR_NONETIDS;
384 if (obj->afp_version >= 32) {
386 ashort |= VOLPBIT_ATTR_EXT_ATTRS;
387 if (vol->v_flags & AFPVOL_ACLS)
388 ashort |= VOLPBIT_ATTR_ACLS;
392 ashort = htons(ashort);
393 memcpy(data, &ashort, sizeof( ashort ));
394 data += sizeof( ashort );
398 ashort = htons( AFPVOLSIG_DEFAULT );
399 memcpy(data, &ashort, sizeof( ashort ));
400 data += sizeof( ashort );
405 memcpy(data, &aint, sizeof( aint ));
406 data += sizeof( aint );
410 if ( st->st_mtime > vol->v_mtime ) {
411 vol->v_mtime = st->st_mtime;
413 aint = AD_DATE_FROM_UNIX(vol->v_mtime);
414 memcpy(data, &aint, sizeof( aint ));
415 data += sizeof( aint );
419 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
420 aint = AD_DATE_START;
421 memcpy(data, &aint, sizeof( aint ));
422 data += sizeof( aint );
426 memcpy(data, &vol->v_vid, sizeof( vol->v_vid ));
427 data += sizeof( vol->v_vid );
431 bfree = htonl( bfree );
432 memcpy(data, &bfree, sizeof( bfree ));
433 data += sizeof( bfree );
436 case VOLPBIT_BTOTAL :
437 btotal = htonl( btotal );
438 memcpy(data, &btotal, sizeof( btotal ));
439 data += sizeof( btotal );
442 #ifndef NO_LARGE_VOL_SUPPORT
443 case VOLPBIT_XBFREE :
444 xbfree = hton64( xbfree );
445 memcpy(data, &xbfree, sizeof( xbfree ));
446 data += sizeof( xbfree );
449 case VOLPBIT_XBTOTAL :
450 xbtotal = hton64( xbtotal );
451 memcpy(data, &xbtotal, sizeof( xbtotal ));
452 data += sizeof( xbfree );
454 #endif /* ! NO_LARGE_VOL_SUPPORT */
458 data += sizeof( uint16_t );
461 case VOLPBIT_BSIZE: /* block size */
462 bsize = htonl(bsize);
463 memcpy(data, &bsize, sizeof(bsize));
464 data += sizeof(bsize);
469 ad_close( &ad, ADFLAGS_HF );
471 return( AFPERR_BITMAP );
477 ashort = htons( data - buf );
478 memcpy(nameoff, &ashort, sizeof( ashort ));
479 /* name is always in mac charset */
480 aint = ucs2_to_charset( vol->v_maccharset, vol->v_macname, data+1, AFPVOL_MACNAMELEN + 1);
490 ad_close(&ad, ADFLAGS_HF);
492 *buflen = data - buf;
496 /* ------------------------- */
497 static int stat_vol(const AFPObj *obj, uint16_t bitmap, struct vol *vol, char *rbuf, size_t *rbuflen)
503 if ( stat( vol->v_path, &st ) < 0 ) {
505 return( AFPERR_PARAM );
507 /* save the volume device number */
508 vol->v_dev = st.st_dev;
510 buflen = *rbuflen - sizeof( bitmap );
511 if (( ret = getvolparams(obj, bitmap, vol, &st,
512 rbuf + sizeof( bitmap ), &buflen )) != AFP_OK ) {
516 *rbuflen = buflen + sizeof( bitmap );
517 bitmap = htons( bitmap );
518 memcpy(rbuf, &bitmap, sizeof( bitmap ));
523 /* ------------------------------- */
524 int afp_getsrvrparms(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
535 load_volumes(obj, lv_none);
538 for ( vcnt = 0, volume = getvolumes(); volume && vcnt < 255; volume = volume->v_next ) {
539 if (!(volume->v_flags & AFPVOL_NOSTAT)) {
542 if ( stat( volume->v_path, &st ) < 0 ) {
543 LOG(log_info, logtype_afpd, "afp_getsrvrparms(%s): stat: %s",
544 volume->v_path, strerror(errno) );
545 continue; /* can't access directory */
547 if (!S_ISDIR(st.st_mode)) {
548 continue; /* not a dir */
550 accessmode(obj, volume, volume->v_path, &ma, NULL, &st);
551 if ((ma.ma_user & (AR_UREAD | AR_USEARCH)) != (AR_UREAD | AR_USEARCH)) {
552 continue; /* no r-x access */
556 if (utf8_encoding(obj)) {
557 len = ucs2_to_charset_allocate(CH_UTF8_MAC, &namebuf, volume->v_u8mname);
559 len = ucs2_to_charset_allocate(obj->options.maccharset, &namebuf, volume->v_macname);
562 if (len == (size_t)-1)
566 * There seems to be an undocumented limit on how big our reply can get
567 * before the client chokes and closes the connection.
568 * Testing with 10.8.4 found the limit at ~4600 bytes. Go figure.
570 if (((data + len + 3) - rbuf) > 4600)
573 /* set password bit if there's a volume password */
574 *data = (volume->v_password) ? AFPSRVR_PASSWD : 0;
576 *data++ |= 0; /* UNIX PRIVS BIT ..., OSX doesn't seem to use it, so we don't either */
578 memcpy(data, namebuf, len );
584 *rbuflen = data - rbuf;
586 if ( gettimeofday( &tv, NULL ) < 0 ) {
587 LOG(log_error, logtype_afpd, "afp_getsrvrparms: gettimeofday: %s", strerror(errno) );
592 aint = AD_DATE_FROM_UNIX(tv.tv_sec);
593 memcpy(data, &aint, sizeof( uint32_t));
594 data += sizeof( uint32_t);
599 /* ------------------------- */
600 static int volume_codepage(AFPObj *obj, struct vol *volume)
602 struct charset_functions *charset;
605 if (!volume->v_volcodepage)
606 volume->v_volcodepage = strdup("UTF8");
608 if ( (charset_t) -1 == ( volume->v_volcharset = add_charset(volume->v_volcodepage)) ) {
609 LOG (log_error, logtype_afpd, "Setting codepage %s as volume codepage failed", volume->v_volcodepage);
613 if ( NULL == (charset = find_charset_functions(volume->v_volcodepage)) || charset->flags & CHARSET_ICONV ) {
614 LOG (log_warning, logtype_afpd, "WARNING: volume encoding %s is *not* supported by netatalk, expect problems !!!!", volume->v_volcodepage);
617 if (!volume->v_maccodepage)
618 volume->v_maccodepage = strdup(obj->options.maccodepage);
620 if ( (charset_t) -1 == ( volume->v_maccharset = add_charset(volume->v_maccodepage)) ) {
621 LOG (log_error, logtype_afpd, "Setting codepage %s as mac codepage failed", volume->v_maccodepage);
625 if ( NULL == ( charset = find_charset_functions(volume->v_maccodepage)) || ! (charset->flags & CHARSET_CLIENT) ) {
626 LOG (log_error, logtype_afpd, "Fatal error: mac charset %s not supported", volume->v_maccodepage);
629 volume->v_kTextEncoding = htonl(charset->kTextEncoding);
633 /* ------------------------- */
634 static int volume_openDB(const AFPObj *obj, struct vol *volume)
638 if ((volume->v_flags & AFPVOL_NODEV)) {
639 flags |= CNID_FLAG_NODEV;
642 volume->v_cdb = cnid_open(volume, volume->v_cnidscheme, flags);
644 if ( ! volume->v_cdb && ! (flags & CNID_FLAG_MEMORY)) {
645 /* The first attempt failed and it wasn't yet an attempt to open in-memory */
646 LOG(log_error, logtype_afpd, "Can't open volume \"%s\" CNID backend \"%s\" ",
647 volume->v_path, volume->v_cnidscheme);
648 LOG(log_error, logtype_afpd, "Reopen volume %s using in memory temporary CNID DB.",
650 flags |= CNID_FLAG_MEMORY;
651 volume->v_cdb = cnid_open(volume, "tdb", flags);
653 /* kill ourself with SIGUSR2 aka msg pending */
655 setmessage("Something wrong with the volume's CNID DB, using temporary CNID DB instead."
656 "Check server messages for details!");
657 kill(getpid(), SIGUSR2);
658 /* deactivate cnid caching/storing in AppleDouble files */
663 return (!volume->v_cdb)?-1:0;
667 * Send list of open volumes to afpd master via IPC
669 static void server_ipc_volumes(AFPObj *obj)
671 struct vol *volume, *vols;
672 volume = vols = getvolumes();
673 bstring openvolnames = bfromcstr("");
674 bool firstvol = true;
677 if (volume->v_flags & AFPVOL_OPEN) {
679 bcatcstr(openvolnames, ", ");
682 bcatcstr(openvolnames, volume->v_localname);
684 volume = volume->v_next;
687 ipc_child_write(obj->ipc_fd, IPC_VOLUMES, blength(openvolnames), bdata(openvolnames));
688 bdestroy(openvolnames);
691 /* -------------------------
692 * we are the user here
694 int afp_openvol(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
705 char *vol_mname = NULL;
709 memcpy(&bitmap, ibuf, sizeof( bitmap ));
710 bitmap = ntohs( bitmap );
711 ibuf += sizeof( bitmap );
714 if (( bitmap & (1<<VOLPBIT_VID)) == 0 ) {
715 return AFPERR_BITMAP;
718 len = (unsigned char)*ibuf++;
719 volname = obj->oldtmp;
721 if ((volname_tmp = strchr(volname,'+')) != NULL)
722 volname = volname_tmp+1;
724 if (utf8_encoding(obj)) {
725 namelen = convert_string(CH_UTF8_MAC, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
727 namelen = convert_string(obj->options.maccharset, CH_UCS2, ibuf, len, volname, sizeof(obj->oldtmp));
735 if ((len + 1) & 1) /* pad to an even boundary */
738 load_volumes(obj, lv_none);
740 for ( volume = getvolumes(); volume; volume = volume->v_next ) {
741 if ( strcasecmp_w( (ucs2_t*) volname, volume->v_name ) == 0 ) {
746 if ( volume == NULL ) {
750 /* check for a volume password */
751 if (volume->v_password && strncmp(ibuf, volume->v_password, VOLPASSLEN)) {
752 return AFPERR_ACCESS;
755 if (( volume->v_flags & AFPVOL_OPEN ) ) {
756 /* the volume is already open */
757 return stat_vol(obj, bitmap, volume, rbuf, rbuflen);
760 if (volume->v_root_preexec) {
761 if ((ret = afprun(1, volume->v_root_preexec, NULL)) && volume->v_root_preexec_close) {
762 LOG(log_error, logtype_afpd, "afp_openvol(%s): root preexec : %d", volume->v_path, ret );
767 if (volume->v_preexec) {
768 if ((ret = afprun(0, volume->v_preexec, NULL)) && volume->v_preexec_close) {
769 LOG(log_error, logtype_afpd, "afp_openvol(%s): preexec : %d", volume->v_path, ret );
774 if ( stat( volume->v_path, &st ) < 0 ) {
778 if ( chdir( volume->v_path ) < 0 ) {
782 if (volume_codepage(obj, volume) < 0) {
787 /* initialize volume variables
790 if (utf8_encoding(obj)) {
791 volume->max_filename = UTF8FILELEN_EARLY;
794 volume->max_filename = MACFILELEN;
797 volume->v_flags |= AFPVOL_OPEN;
798 volume->v_cdb = NULL;
800 if (utf8_encoding(obj)) {
801 len = convert_string_allocate(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, namelen, &vol_mname);
803 len = convert_string_allocate(CH_UCS2, obj->options.maccharset, volume->v_macname, namelen, &vol_mname);
805 if ( !vol_mname || len <= 0) {
810 if ((vol_uname = strrchr(volume->v_path, '/')) == NULL)
811 vol_uname = volume->v_path;
812 else if (vol_uname[1] != '\0')
815 if ((dir = dir_new(vol_mname,
820 bfromcstr(volume->v_path),
824 LOG(log_error, logtype_afpd, "afp_openvol(%s): malloc: %s", volume->v_path, strerror(errno) );
828 volume->v_root = dir;
831 if (volume_openDB(obj, volume) < 0) {
832 LOG(log_error, logtype_afpd, "Fatal error: cannot open CNID or invalid CNID backend for %s: %s",
833 volume->v_path, volume->v_cnidscheme);
838 ret = stat_vol(obj, bitmap, volume, rbuf, rbuflen);
842 * If you mount a volume twice, the second time the trash appears on
843 * the desk-top. That's because the Mac remembers the DID for the
844 * trash (even for volumes in different zones, on different servers).
845 * Just so this works better, we prime the DID cache with the trash,
846 * fixing the trash at DID 17.
847 * FIXME (RL): should it be done inside a CNID backend ? (always returning Trash DID when asked) ?
849 if ((volume->v_cdb->cnid_db_flags & CNID_FLAG_PERSISTENT)) {
851 /* FIXME find db time stamp */
852 if (cnid_getstamp(volume->v_cdb, volume->v_stamp, sizeof(volume->v_stamp)) < 0) {
853 LOG (log_error, logtype_afpd,
854 "afp_openvol(%s): Fatal error: Unable to get stamp value from CNID backend",
862 if ((msg = atalk_iniparser_getstring(obj->iniconfig, volume->v_configname, "login message", NULL)) != NULL)
866 server_ipc_volumes(obj);
871 if (volume->v_root) {
872 dir_free( volume->v_root );
873 volume->v_root = NULL;
876 volume->v_flags &= ~AFPVOL_OPEN;
877 if (volume->v_cdb != NULL) {
878 cnid_close(volume->v_cdb);
879 volume->v_cdb = NULL;
886 void closevol(const AFPObj *obj, struct vol *vol)
891 vol->v_flags &= ~AFPVOL_OPEN;
893 of_closevol(obj, vol);
895 dir_free( vol->v_root );
897 if (vol->v_cdb != NULL) {
898 cnid_close(vol->v_cdb);
902 if (vol->v_postexec) {
903 afprun(0, vol->v_postexec, NULL);
905 if (vol->v_root_postexec) {
906 afprun(1, vol->v_root_postexec, NULL);
910 /* ------------------------- */
911 void close_all_vol(const AFPObj *obj)
915 for ( ovol = getvolumes(); ovol; ovol = ovol->v_next ) {
916 if ( (ovol->v_flags & AFPVOL_OPEN) ) {
917 ovol->v_flags &= ~AFPVOL_OPEN;
923 /* ------------------------- */
924 int afp_closevol(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
931 memcpy(&vid, ibuf, sizeof( vid ));
932 if (NULL == ( vol = getvolbyvid( vid )) ) {
933 return( AFPERR_PARAM );
939 server_ipc_volumes(obj);
944 /* --------------------------
945 poll if a volume is changed by other processes.
947 0 no attention msg sent
949 -1 error (socket closed)
951 Note: if attention return -1 no packet has been
952 sent because the buffer is full, we don't care
953 either there's no reader or there's a lot of
954 traffic and another pollvoltime will follow
956 int pollvoltime(AFPObj *obj)
963 if (!(obj->afp_version > 21 && obj->options.flags & OPTION_SERVERNOTIF))
966 if ( gettimeofday( &tv, NULL ) < 0 )
969 for ( vol = getvolumes(); vol; vol = vol->v_next ) {
970 if ( (vol->v_flags & AFPVOL_OPEN) && vol->v_mtime + 30 < tv.tv_sec) {
971 if ( !stat( vol->v_path, &st ) && vol->v_mtime != st.st_mtime ) {
972 vol->v_mtime = st.st_mtime;
973 if (!obj->attention(obj->dsi, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED))
982 /* ------------------------- */
983 void setvoltime(AFPObj *obj, struct vol *vol)
987 if ( gettimeofday( &tv, NULL ) < 0 ) {
988 LOG(log_error, logtype_afpd, "setvoltime(%s): gettimeofday: %s", vol->v_path, strerror(errno) );
991 if( utime( vol->v_path, NULL ) < 0 ) {
992 /* write of time failed ... probably a read only filesys,
993 * where no other users can interfere, so there's no issue here
997 /* a little granularity */
998 if (vol->v_mtime < tv.tv_sec) {
999 vol->v_mtime = tv.tv_sec;
1000 /* or finder doesn't update free space
1001 * AFP 3.2 and above clients seem to be ok without so many notification
1003 if (obj->afp_version < 32 && obj->options.flags & OPTION_SERVERNOTIF) {
1004 obj->attention(obj->dsi, AFPATTN_NOTIFY | AFPATTN_VOLCHANGED);
1009 /* ------------------------- */
1010 int afp_getvolparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_,char *rbuf, size_t *rbuflen)
1013 uint16_t vid, bitmap;
1016 memcpy(&vid, ibuf, sizeof( vid ));
1017 ibuf += sizeof( vid );
1018 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1019 bitmap = ntohs( bitmap );
1021 if (NULL == ( vol = getvolbyvid( vid )) ) {
1023 return( AFPERR_PARAM );
1026 return stat_vol(obj, bitmap, vol, rbuf, rbuflen);
1029 /* ------------------------- */
1030 int afp_setvolparams(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1034 uint16_t vid, bitmap;
1040 memcpy(&vid, ibuf, sizeof( vid ));
1041 ibuf += sizeof( vid );
1042 memcpy(&bitmap, ibuf, sizeof( bitmap ));
1043 bitmap = ntohs( bitmap );
1044 ibuf += sizeof(bitmap);
1046 if (( vol = getvolbyvid( vid )) == NULL ) {
1047 return( AFPERR_PARAM );
1050 if ((vol->v_flags & AFPVOL_RO))
1051 return AFPERR_VLOCK;
1053 /* we can only set the backup date. */
1054 if (bitmap != (1 << VOLPBIT_BDATE))
1055 return AFPERR_BITMAP;
1058 if ( ad_open(&ad, vol->v_path, ADFLAGS_HF | ADFLAGS_DIR | ADFLAGS_RDWR) < 0 ) {
1060 return AFPERR_VLOCK;
1062 return AFPERR_ACCESS;
1065 memcpy(&aint, ibuf, sizeof(aint));
1066 ad_setdate(&ad, AD_DATE_BACKUP, aint);
1068 ad_close(&ad, ADFLAGS_HF);