]> arthur.barton.de Git - netatalk.git/blob - etc/afpd/fork.c
Increase IO size when sendfile() is not used
[netatalk.git] / etc / afpd / fork.c
1 /*
2  * Copyright (c) 1990,1993 Regents of The University of Michigan.
3  * Copyright (c) 2010      Frank Lahm
4  *
5  * All Rights Reserved.  See COPYRIGHT.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <sys/param.h>
16 #include <sys/socket.h>
17 #include <inttypes.h>
18
19 #include <atalk/dsi.h>
20 #include <atalk/afp.h>
21 #include <atalk/adouble.h>
22 #include <atalk/logger.h>
23 #include <atalk/util.h>
24 #include <atalk/cnid.h>
25 #include <atalk/bstrlib.h>
26 #include <atalk/bstradd.h>
27 #include <atalk/globals.h>
28 #include <atalk/netatalk_conf.h>
29 #include <atalk/ea.h>
30
31 #include "fork.h"
32 #include "file.h"
33 #include "directory.h"
34 #include "desktop.h"
35 #include "volume.h"
36
37 #ifdef AFS
38 struct ofork *writtenfork;
39 #endif
40
41 static int getforkparams(const AFPObj *obj, struct ofork *ofork, uint16_t bitmap, char *buf, size_t *buflen)
42 {
43     struct path         path;
44     struct stat     *st;
45
46     struct adouble  *adp;
47     struct dir      *dir;
48     struct vol      *vol;
49
50
51     /* can only get the length of the opened fork */
52     if ( ( (bitmap & ((1<<FILPBIT_DFLEN) | (1<<FILPBIT_EXTDFLEN)))
53            && (ofork->of_flags & AFPFORK_RSRC))
54          ||
55          ( (bitmap & ((1<<FILPBIT_RFLEN) | (1<<FILPBIT_EXTRFLEN)))
56            && (ofork->of_flags & AFPFORK_DATA))) {
57         return( AFPERR_BITMAP );
58     }
59
60     if (! AD_META_OPEN(ofork->of_ad)) {
61         adp = NULL;
62     } else {
63         adp = ofork->of_ad;
64     }
65
66     vol = ofork->of_vol;
67     dir = dirlookup(vol, ofork->of_did);
68
69     if (NULL == (path.m_name = utompath(vol, of_name(ofork), dir->d_did, utf8_encoding(obj)))) {
70         return( AFPERR_MISC );
71     }
72     path.u_name = of_name(ofork);
73     path.id = 0;
74     st = &path.st;
75     if ( bitmap & ( (1<<FILPBIT_DFLEN) | (1<<FILPBIT_EXTDFLEN) |
76                     (1<<FILPBIT_FNUM) | (1 << FILPBIT_CDATE) |
77                     (1 << FILPBIT_MDATE) | (1 << FILPBIT_BDATE))) {
78         if ( ad_data_fileno( ofork->of_ad ) <= 0 ) {
79             /* 0 is for symlink */
80             if (movecwd(vol, dir) < 0)
81                 return( AFPERR_NOOBJ );
82             if ( lstat( path.u_name, st ) < 0 )
83                 return( AFPERR_NOOBJ );
84         } else {
85             if ( fstat( ad_data_fileno( ofork->of_ad ), st ) < 0 ) {
86                 return( AFPERR_BITMAP );
87             }
88         }
89     }
90     return getmetadata(obj, vol, bitmap, &path, dir, buf, buflen, adp );
91 }
92
93 static off_t get_off_t(char **ibuf, int is64)
94 {
95     uint32_t             temp;
96     off_t                 ret;
97
98     ret = 0;
99     memcpy(&temp, *ibuf, sizeof( temp ));
100     ret = ntohl(temp); /* ntohl is unsigned */
101     *ibuf += sizeof(temp);
102
103     if (is64) {
104         memcpy(&temp, *ibuf, sizeof( temp ));
105         *ibuf += sizeof(temp);
106         ret = ntohl(temp)| (ret << 32);
107     }
108     else {
109         ret = (int)ret; /* sign extend */
110     }
111     return ret;
112 }
113
114 static int set_off_t(off_t offset, char *rbuf, int is64)
115 {
116     uint32_t  temp;
117     int        ret;
118
119     ret = 0;
120     if (is64) {
121         temp = htonl(offset >> 32);
122         memcpy(rbuf, &temp, sizeof( temp ));
123         rbuf += sizeof(temp);
124         ret = sizeof( temp );
125         offset &= 0xffffffff;
126     }
127     temp = htonl(offset);
128     memcpy(rbuf, &temp, sizeof( temp ));
129     ret += sizeof( temp );
130
131     return ret;
132 }
133
134 static int is_neg(int is64, off_t val)
135 {
136     if (val < 0 || (sizeof(off_t) == 8 && !is64 && (val & 0x80000000U)))
137         return 1;
138     return 0;
139 }
140
141 static int sum_neg(int is64, off_t offset, off_t reqcount)
142 {
143     if (is_neg(is64, offset +reqcount) )
144         return 1;
145     return 0;
146 }
147
148 static int fork_setmode(const AFPObj *obj, struct adouble *adp, int eid, int access, int ofrefnum)
149 {
150     int ret;
151     int readset;
152     int writeset;
153     int denyreadset;
154     int denywriteset;
155
156 #ifdef HAVE_FSHARE_T
157     fshare_t shmd;
158
159     if (obj->options.flags & OPTION_SHARE_RESERV) {
160         shmd.f_access = (access & OPENACC_RD ? F_RDACC : 0) | (access & OPENACC_WR ? F_WRACC : 0);
161         if (shmd.f_access == 0)
162             /* we must give an access mode, otherwise fcntl will complain */
163             shmd.f_access = F_RDACC;
164         shmd.f_deny = (access & OPENACC_DRD ? F_RDDNY : F_NODNY) | (access & OPENACC_DWR) ? F_WRDNY : 0;
165         shmd.f_id = ofrefnum;
166
167         int fd = (eid == ADEID_DFORK) ? ad_data_fileno(adp) : ad_reso_fileno(adp);
168
169         if (fd != -1 && fd != AD_SYMLINK && fcntl(fd, F_SHARE, &shmd) != 0) {
170             LOG(log_debug, logtype_afpd, "fork_setmode: fcntl: %s", strerror(errno));
171             errno = EACCES;
172             return -1;
173         }
174     }
175 #endif
176
177     if (! (access & (OPENACC_WR | OPENACC_RD | OPENACC_DWR | OPENACC_DRD))) {
178         return ad_lock(adp, eid, ADLOCK_RD | ADLOCK_FILELOCK, AD_FILELOCK_OPEN_NONE, 1, ofrefnum);
179     }
180
181     if ((access & (OPENACC_RD | OPENACC_DRD))) {
182         if ((readset = ad_testlock(adp, eid, AD_FILELOCK_OPEN_RD)) <0)
183             return readset;
184         if ((denyreadset = ad_testlock(adp, eid, AD_FILELOCK_DENY_RD)) <0)
185             return denyreadset;
186
187         if ((access & OPENACC_RD) && denyreadset) {
188             errno = EACCES;
189             return -1;
190         }
191         if ((access & OPENACC_DRD) && readset) {
192             errno = EACCES;
193             return -1;
194         }
195         /* boolean logic is not enough, because getforkmode is not always telling the
196          * true
197          */
198         if ((access & OPENACC_RD)) {
199             ret = ad_lock(adp, eid, ADLOCK_RD | ADLOCK_FILELOCK, AD_FILELOCK_OPEN_RD, 1, ofrefnum);
200             if (ret)
201                 return ret;
202         }
203         if ((access & OPENACC_DRD)) {
204             ret = ad_lock(adp, eid, ADLOCK_RD | ADLOCK_FILELOCK, AD_FILELOCK_DENY_RD, 1, ofrefnum);
205             if (ret)
206                 return ret;
207         }
208     }
209     /* ------------same for writing -------------- */
210     if ((access & (OPENACC_WR | OPENACC_DWR))) {
211         if ((writeset = ad_testlock(adp, eid, AD_FILELOCK_OPEN_WR)) <0)
212             return writeset;
213         if ((denywriteset = ad_testlock(adp, eid, AD_FILELOCK_DENY_WR)) <0)
214             return denywriteset;
215
216         if ((access & OPENACC_WR) && denywriteset) {
217             errno = EACCES;
218             return -1;
219         }
220         if ((access & OPENACC_DWR) && writeset) {
221             errno = EACCES;
222             return -1;
223         }
224         if ((access & OPENACC_WR)) {
225             ret = ad_lock(adp, eid, ADLOCK_RD | ADLOCK_FILELOCK, AD_FILELOCK_OPEN_WR, 1, ofrefnum);
226             if (ret)
227                 return ret;
228         }
229         if ((access & OPENACC_DWR)) {
230             ret = ad_lock(adp, eid, ADLOCK_RD | ADLOCK_FILELOCK, AD_FILELOCK_DENY_WR, 1, ofrefnum);
231             if (ret)
232                 return ret;
233         }
234     }
235
236     return 0;
237 }
238
239 /* ----------------------- */
240 int afp_openfork(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
241 {
242     struct vol      *vol;
243     struct dir      *dir;
244     struct ofork    *ofork, *opened;
245     struct adouble  *adsame = NULL;
246     size_t          buflen;
247     int             ret, adflags, eid;
248     uint32_t        did;
249     uint16_t        vid, bitmap, access, ofrefnum;
250     char            fork, *path, *upath;
251     struct stat     *st;
252     uint16_t        bshort;
253     struct path     *s_path;
254
255     ibuf++;
256     fork = *ibuf++;
257     memcpy(&vid, ibuf, sizeof( vid ));
258     ibuf += sizeof(vid);
259
260     *rbuflen = 0;
261     if (NULL == ( vol = getvolbyvid( vid ))) {
262         return( AFPERR_PARAM );
263     }
264
265     memcpy(&did, ibuf, sizeof( did ));
266     ibuf += sizeof( int );
267
268     if (NULL == ( dir = dirlookup( vol, did ))) {
269         return afp_errno;
270     }
271
272     memcpy(&bitmap, ibuf, sizeof( bitmap ));
273     bitmap = ntohs( bitmap );
274     ibuf += sizeof( bitmap );
275     memcpy(&access, ibuf, sizeof( access ));
276     access = ntohs( access );
277     ibuf += sizeof( access );
278
279     if ((vol->v_flags & AFPVOL_RO) && (access & OPENACC_WR)) {
280         return AFPERR_VLOCK;
281     }
282
283     if (NULL == ( s_path = cname( vol, dir, &ibuf ))) {
284         return get_afp_errno(AFPERR_PARAM);
285     }
286
287     if (*s_path->m_name == '\0') {
288         /* it's a dir ! */
289         return  AFPERR_BADTYPE;
290     }
291
292     /* stat() data fork st is set because it's not a dir */
293     switch ( s_path->st_errno ) {
294     case 0:
295         break;
296     case ENOENT:
297         return AFPERR_NOOBJ;
298     case EACCES:
299         return (access & OPENACC_WR) ? AFPERR_LOCK : AFPERR_ACCESS;
300     default:
301         LOG(log_error, logtype_afpd, "afp_openfork(%s): %s", s_path->m_name, strerror(errno));
302         return AFPERR_PARAM;
303     }
304
305     upath = s_path->u_name;
306     path = s_path->m_name;
307     st = &s_path->st;
308
309     if (!vol_unix_priv(vol)) {
310         if (check_access(obj, vol, upath, access ) < 0) {
311             return AFPERR_ACCESS;
312         }
313     } else {
314         if (file_access(obj, vol, s_path, access ) < 0) {
315             return AFPERR_ACCESS;
316         }
317     }
318
319     if ((opened = of_findname(vol, s_path))) {
320         adsame = opened->of_ad;
321     }
322
323     adflags = ADFLAGS_SETSHRMD;
324
325     if (fork == OPENFORK_DATA) {
326         eid = ADEID_DFORK;
327         adflags |= ADFLAGS_DF;
328     } else {
329         eid = ADEID_RFORK;
330         adflags |= ADFLAGS_RF;
331     }
332
333     if (access & OPENACC_WR) {
334         adflags |= ADFLAGS_RDWR;
335         if (fork != OPENFORK_DATA)
336             /*
337              * We only try to create the resource
338              * fork if the user wants to open it for write acess.
339              */
340             adflags |= ADFLAGS_CREATE;
341     } else {
342         adflags |= ADFLAGS_RDONLY;
343     }
344
345     if ((ofork = of_alloc(vol, curdir, path, &ofrefnum, eid, adsame, st)) == NULL)
346         return AFPERR_NFILE;
347
348     LOG(log_debug, logtype_afpd, "afp_openfork(\"%s\", %s, %s)",
349         fullpathname(s_path->u_name),
350         (fork == OPENFORK_DATA) ? "data" : "reso",
351         !(access & OPENACC_WR) ? "O_RDONLY" : "O_RDWR");
352
353     ret = AFPERR_NOOBJ;
354
355     /* First ad_open(), opens data or ressource fork */
356     if (ad_open(ofork->of_ad, upath, adflags, 0666) < 0) {
357         switch (errno) {
358         case EROFS:
359             ret = AFPERR_VLOCK;
360         case EACCES:
361             goto openfork_err;
362         case ENOENT:
363             goto openfork_err;
364         case EMFILE :
365         case ENFILE :
366             ret = AFPERR_NFILE;
367             goto openfork_err;
368         case EISDIR :
369             ret = AFPERR_BADTYPE;
370             goto openfork_err;
371         default:
372             LOG(log_error, logtype_afpd, "afp_openfork(%s): ad_open: %s", s_path->m_name, strerror(errno) );
373             ret = AFPERR_PARAM;
374             goto openfork_err;
375         }
376     }
377
378     /*
379      * Create metadata if we open rw, otherwise only open existing metadata
380      */
381     if (access & OPENACC_WR) {
382         adflags = ADFLAGS_HF | ADFLAGS_RDWR | ADFLAGS_CREATE;
383     } else {
384         adflags = ADFLAGS_HF | ADFLAGS_RDONLY;
385     }
386
387     if (ad_open(ofork->of_ad, upath, adflags, 0666) == 0) {
388         ofork->of_flags |= AFPFORK_META;
389     } else {
390         switch (errno) {
391         case EACCES:
392         case ENOENT:
393             /* no metadata? We don't care! */
394             break;
395         case EROFS:
396             ret = AFPERR_VLOCK;
397         case EMFILE :
398         case ENFILE :
399             ret = AFPERR_NFILE;
400             goto openfork_err;
401         case EISDIR :
402             ret = AFPERR_BADTYPE;
403             goto openfork_err;
404         default:
405             LOG(log_error, logtype_afpd, "afp_openfork(%s): ad_open: %s", s_path->m_name, strerror(errno) );
406             ret = AFPERR_PARAM;
407             goto openfork_err;
408         }
409     }
410
411     if ((adflags & ADFLAGS_RF) && (ad_get_RF_flags( ofork->of_ad) & O_CREAT)) {
412         if (ad_setname(ofork->of_ad, path)) {
413             ad_flush( ofork->of_ad );
414         }
415     }
416
417     if ((ret = getforkparams(obj, ofork, bitmap, rbuf + 2 * sizeof(int16_t), &buflen)) != AFP_OK) {
418         ad_close( ofork->of_ad, adflags | ADFLAGS_SETSHRMD);
419         goto openfork_err;
420     }
421
422     *rbuflen = buflen + 2 * sizeof( uint16_t );
423     bitmap = htons( bitmap );
424     memcpy(rbuf, &bitmap, sizeof( uint16_t ));
425     rbuf += sizeof( uint16_t );
426
427     /* check  WriteInhibit bit if we have a ressource fork
428      * the test is done here, after some Mac trafic capture
429      */
430     if (ad_meta_fileno(ofork->of_ad) != -1) {   /* META */
431         ad_getattr(ofork->of_ad, &bshort);
432         if ((bshort & htons(ATTRBIT_NOWRITE)) && (access & OPENACC_WR)) {
433             ad_close( ofork->of_ad, adflags | ADFLAGS_SETSHRMD);
434             of_dealloc(ofork);
435             ofrefnum = 0;
436             memcpy(rbuf, &ofrefnum, sizeof(ofrefnum));
437             return(AFPERR_OLOCK);
438         }
439     }
440
441     /*
442      * synchronization locks:
443      */
444
445     /* don't try to lock non-existent rforks. */
446     if ((eid == ADEID_DFORK)
447         || (ad_reso_fileno(ofork->of_ad) != -1)
448         || (ofork->of_ad->ad_vers == AD_VERSION_EA)) {
449         ret = fork_setmode(obj, ofork->of_ad, eid, access, ofrefnum);
450         /* can we access the fork? */
451         if (ret < 0) {
452             ofork->of_flags |= AFPFORK_ERROR;
453             ret = errno;
454             ad_close( ofork->of_ad, adflags | ADFLAGS_SETSHRMD);
455             of_dealloc(ofork);
456             switch (ret) {
457             case EAGAIN: /* return data anyway */
458             case EACCES:
459             case EINVAL:
460                 ofrefnum = 0;
461                 memcpy(rbuf, &ofrefnum, sizeof(ofrefnum));
462                 return( AFPERR_DENYCONF );
463                 break;
464             default:
465                 *rbuflen = 0;
466                 LOG(log_error, logtype_afpd, "afp_openfork(%s): ad_lock: %s", s_path->m_name, strerror(ret) );
467                 return( AFPERR_PARAM );
468             }
469         }
470         if ((access & OPENACC_WR))
471             ofork->of_flags |= AFPFORK_ACCWR;
472     }
473     /* the file may be open read only without ressource fork */
474     if ((access & OPENACC_RD))
475         ofork->of_flags |= AFPFORK_ACCRD;
476
477     LOG(log_debug, logtype_afpd, "afp_openfork(\"%s\"): fork: %" PRIu16,
478         fullpathname(s_path->m_name), ofork->of_refnum);
479
480     memcpy(rbuf, &ofrefnum, sizeof(ofrefnum));
481     return( AFP_OK );
482
483 openfork_err:
484     of_dealloc(ofork);
485     if (errno == EACCES)
486         return (access & OPENACC_WR) ? AFPERR_LOCK : AFPERR_ACCESS;
487     return ret;
488 }
489
490 int afp_setforkparams(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf _U_, size_t *rbuflen)
491 {
492     struct ofork    *ofork;
493     off_t       size;
494     uint16_t       ofrefnum, bitmap;
495     int                 err;
496     int                 is64;
497     int                 eid;
498     off_t       st_size;
499
500     ibuf += 2;
501
502     memcpy(&ofrefnum, ibuf, sizeof( ofrefnum ));
503     ibuf += sizeof( ofrefnum );
504
505     memcpy(&bitmap, ibuf, sizeof(bitmap));
506     bitmap = ntohs(bitmap);
507     ibuf += sizeof( bitmap );
508
509     *rbuflen = 0;
510     if (NULL == ( ofork = of_find( ofrefnum )) ) {
511         LOG(log_error, logtype_afpd, "afp_setforkparams: of_find(%d) could not locate fork", ofrefnum );
512         return( AFPERR_PARAM );
513     }
514
515     if (ofork->of_vol->v_flags & AFPVOL_RO)
516         return AFPERR_VLOCK;
517
518     if ((ofork->of_flags & AFPFORK_ACCWR) == 0)
519         return AFPERR_ACCESS;
520
521     if ( ofork->of_flags & AFPFORK_DATA) {
522         eid = ADEID_DFORK;
523     } else if (ofork->of_flags & AFPFORK_RSRC) {
524         eid = ADEID_RFORK;
525     } else
526         return AFPERR_PARAM;
527
528     if ( ( (bitmap & ( (1<<FILPBIT_DFLEN) | (1<<FILPBIT_EXTDFLEN) ))
529            && eid == ADEID_RFORK
530              ) ||
531          ( (bitmap & ( (1<<FILPBIT_RFLEN) | (1<<FILPBIT_EXTRFLEN) ))
532            && eid == ADEID_DFORK)) {
533         return AFPERR_BITMAP;
534     }
535
536     is64 = 0;
537     if ((bitmap & ( (1<<FILPBIT_EXTDFLEN) | (1<<FILPBIT_EXTRFLEN) ))) {
538         if (obj->afp_version >= 30) {
539             is64 = 4;
540         }
541         else
542             return AFPERR_BITMAP;
543     }
544
545     if (ibuflen < 2+ sizeof(ofrefnum) + sizeof(bitmap) + is64 +4)
546         return AFPERR_PARAM ;
547
548     size = get_off_t(&ibuf, is64);
549
550     if (size < 0)
551         return AFPERR_PARAM; /* Some MacOS don't return an error they just don't change the size! */
552
553
554     if (bitmap == (1<<FILPBIT_DFLEN) || bitmap == (1<<FILPBIT_EXTDFLEN)) {
555         st_size = ad_size(ofork->of_ad, eid);
556         err = -2;
557         if (st_size > size &&
558             ad_tmplock(ofork->of_ad, eid, ADLOCK_WR, size, st_size -size, ofork->of_refnum) < 0)
559             goto afp_setfork_err;
560
561         err = ad_dtruncate( ofork->of_ad, size );
562         if (st_size > size)
563             ad_tmplock(ofork->of_ad, eid, ADLOCK_CLR, size, st_size -size, ofork->of_refnum);
564         if (err < 0)
565             goto afp_setfork_err;
566     } else if (bitmap == (1<<FILPBIT_RFLEN) || bitmap == (1<<FILPBIT_EXTRFLEN)) {
567         ad_refresh(NULL, ofork->of_ad );
568
569         st_size = ad_size(ofork->of_ad, eid);
570         err = -2;
571         if (st_size > size &&
572             ad_tmplock(ofork->of_ad, eid, ADLOCK_WR, size, st_size -size, ofork->of_refnum) < 0) {
573             goto afp_setfork_err;
574         }
575
576         err = ad_rtruncate(ofork->of_ad, size);
577         if (st_size > size)
578             ad_tmplock(ofork->of_ad, eid, ADLOCK_CLR, size, st_size -size, ofork->of_refnum);
579         if (err < 0)
580             goto afp_setfork_err;
581
582         if (ad_flush( ofork->of_ad ) < 0) {
583             LOG(log_error, logtype_afpd, "afp_setforkparams(%s): ad_flush: %s", of_name(ofork), strerror(errno) );
584             return AFPERR_PARAM;
585         }
586     } else
587         return AFPERR_BITMAP;
588
589 #ifdef AFS
590     if ( flushfork( ofork ) < 0 ) {
591         LOG(log_error, logtype_afpd, "afp_setforkparams(%s): flushfork: %s", of_name(ofork), strerror(errno) );
592     }
593 #endif /* AFS */
594
595     return( AFP_OK );
596
597 afp_setfork_err:
598     if (err == -2)
599         return AFPERR_LOCK;
600     else {
601         switch (errno) {
602         case EROFS:
603             return AFPERR_VLOCK;
604         case EPERM:
605         case EACCES:
606             return AFPERR_ACCESS;
607         case EDQUOT:
608         case EFBIG:
609         case ENOSPC:
610             LOG(log_error, logtype_afpd, "afp_setforkparams: DISK FULL");
611             return AFPERR_DFULL;
612         default:
613             return AFPERR_PARAM;
614         }
615     }
616 }
617
618 /* for this to work correctly, we need to check for locks before each
619  * read and write. that's most easily handled by always doing an
620  * appropriate check before each ad_read/ad_write. other things
621  * that can change files like truncate are handled internally to those
622  * functions.
623  */
624 #define ENDBIT(a)  ((a) & 0x80)
625 #define UNLOCKBIT(a) ((a) & 0x01)
626
627
628 /* ---------------------- */
629 static int byte_lock(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen, int is64)
630 {
631     struct ofork    *ofork;
632     off_t               offset, length;
633     int                 eid;
634     uint16_t       ofrefnum;
635     uint8_t            flags;
636     int                 lockop;
637
638     *rbuflen = 0;
639
640     /* figure out parameters */
641     ibuf++;
642     flags = *ibuf; /* first bit = endflag, lastbit = lockflag */
643     ibuf++;
644     memcpy(&ofrefnum, ibuf, sizeof(ofrefnum));
645     ibuf += sizeof(ofrefnum);
646
647     if (NULL == ( ofork = of_find( ofrefnum )) ) {
648         LOG(log_error, logtype_afpd, "byte_lock: of_find(%d) could not locate fork", ofrefnum );
649         return( AFPERR_PARAM );
650     }
651
652     if ( ofork->of_flags & AFPFORK_DATA) {
653         eid = ADEID_DFORK;
654     } else if (ofork->of_flags & AFPFORK_RSRC) {
655         eid = ADEID_RFORK;
656     } else
657         return AFPERR_PARAM;
658
659     offset = get_off_t(&ibuf, is64);
660     length = get_off_t(&ibuf, is64);
661
662     if (length == -1)
663         length = BYTELOCK_MAX;
664     else if (!length || is_neg(is64, length)) {
665         return AFPERR_PARAM;
666     } else if ((length >= AD_FILELOCK_BASE) && -1 == (ad_reso_fileno(ofork->of_ad))) { /* HF ?*/
667         return AFPERR_LOCK;
668     }
669
670     if (ENDBIT(flags)) {
671         offset += ad_size(ofork->of_ad, eid);
672         /* FIXME what do we do if file size > 2 GB and
673            it's not byte_lock_ext?
674         */
675     }
676     if (offset < 0)    /* error if we have a negative offset */
677         return AFPERR_PARAM;
678
679     /* if the file is a read-only file, we use read locks instead of
680      * write locks. that way, we can prevent anyone from initiating
681      * a write lock. */
682     lockop = UNLOCKBIT(flags) ? ADLOCK_CLR : ADLOCK_WR;
683     if (ad_lock(ofork->of_ad, eid, lockop, offset, length,
684                 ofork->of_refnum) < 0) {
685         switch (errno) {
686         case EACCES:
687         case EAGAIN:
688             return UNLOCKBIT(flags) ? AFPERR_NORANGE : AFPERR_LOCK;
689             break;
690         case ENOLCK:
691             return AFPERR_NLOCK;
692             break;
693         case EINVAL:
694             return UNLOCKBIT(flags) ? AFPERR_NORANGE : AFPERR_RANGEOVR;
695             break;
696         case EBADF:
697         default:
698             return AFPERR_PARAM;
699             break;
700         }
701     }
702     *rbuflen = set_off_t (offset, rbuf, is64);
703     return( AFP_OK );
704 }
705
706 /* --------------------------- */
707 int afp_bytelock(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
708 {
709     return byte_lock ( obj, ibuf, ibuflen, rbuf, rbuflen , 0);
710 }
711
712 /* --------------------------- */
713 int afp_bytelock_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
714 {
715     return byte_lock ( obj, ibuf, ibuflen, rbuf, rbuflen , 1);
716 }
717
718 #undef UNLOCKBIT
719
720 /*!
721  * Read *rbuflen bytes from fork at offset
722  *
723  * @param ofork    (r)  fork handle
724  * @param eid      (r)  data fork or ressource fork entry id
725  * @param offset   (r)  offset
726  * @param rbuf     (r)  data buffer
727  * @param rbuflen  (rw) in: number of bytes to read, out: bytes read
728  *
729  * @return         AFP status code
730  */
731 static int read_file(const struct ofork *ofork, int eid, off_t offset, char *rbuf, size_t *rbuflen)
732 {
733     ssize_t cc;
734
735     cc = ad_read(ofork->of_ad, eid, offset, rbuf, *rbuflen);
736     if ( cc < 0 ) {
737         LOG(log_error, logtype_afpd, "afp_read(%s): ad_read: %s", of_name(ofork), strerror(errno) );
738         *rbuflen = 0;
739         return( AFPERR_PARAM );
740     }
741     *rbuflen = cc;
742
743     if ((size_t)cc < *rbuflen)
744         return AFPERR_EOF;
745     return AFP_OK;
746 }
747
748 static int read_fork(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen, int is64)
749 {
750     DSI          *dsi = obj->dsi;
751     struct ofork *ofork;
752     off_t        offset, saveoff, reqcount, savereqcount, size;
753     ssize_t      cc, err;
754     int          eid;
755     uint16_t     ofrefnum;
756
757     /* we break the AFP spec here by not supporting nlmask and nlchar anymore */
758
759     ibuf += 2;
760     memcpy(&ofrefnum, ibuf, sizeof( ofrefnum ));
761     ibuf += sizeof( u_short );
762
763     if (NULL == ( ofork = of_find( ofrefnum )) ) {
764         LOG(log_error, logtype_afpd, "afp_read: of_find(%d) could not locate fork", ofrefnum );
765         err = AFPERR_PARAM;
766         goto afp_read_err;
767     }
768
769     if ((ofork->of_flags & AFPFORK_ACCRD) == 0) {
770         err = AFPERR_ACCESS;
771         goto afp_read_err;
772     }
773
774     if ( ofork->of_flags & AFPFORK_DATA) {
775         eid = ADEID_DFORK;
776     } else if (ofork->of_flags & AFPFORK_RSRC) {
777         eid = ADEID_RFORK;
778     } else { /* fork wasn't opened. this should never really happen. */
779         err = AFPERR_ACCESS;
780         goto afp_read_err;
781     }
782
783     offset   = get_off_t(&ibuf, is64);
784     reqcount = get_off_t(&ibuf, is64);
785
786     /* zero request count */
787     err = AFP_OK;
788     if (!reqcount) {
789         goto afp_read_err;
790     }
791
792     AFP_READ_START((long)reqcount);
793
794     /* reqcount isn't always truthful. we need to deal with that. */
795     size = ad_size(ofork->of_ad, eid);
796
797     LOG(log_debug, logtype_afpd,
798         "afp_read(fork: %" PRIu16 " [%s], off: %" PRIu64 ", len: %" PRIu64 ", size: %" PRIu64 ")",
799         ofork->of_refnum, (ofork->of_flags & AFPFORK_DATA) ? "data" : "reso", offset, reqcount, size);
800
801     if (offset >= size) {
802         err = AFPERR_EOF;
803         goto afp_read_err;
804     }
805
806     /* subtract off the offset */
807     if (reqcount + offset > size) {
808         reqcount = size - offset;
809         err = AFPERR_EOF;
810     }
811
812     savereqcount = reqcount;
813     saveoff = offset;
814
815     if (reqcount < 0 || offset < 0) {
816         err = AFPERR_PARAM;
817         goto afp_read_err;
818     }
819
820     if (obj->options.flags & OPTION_AFP_READ_LOCK) {
821         if (ad_tmplock(ofork->of_ad, eid, ADLOCK_RD, offset, reqcount, ofork->of_refnum) < 0) {
822             err = AFPERR_LOCK;
823             goto afp_read_err;
824         }
825     }
826
827 #ifdef WITH_SENDFILE
828     if (!(eid == ADEID_DFORK && ad_data_fileno(ofork->of_ad) == AD_SYMLINK) &&
829         !(obj->options.flags & OPTION_NOSENDFILE)) {
830         int fd = ad_readfile_init(ofork->of_ad, eid, &offset, 0);
831         if (dsi_stream_read_file(dsi, fd, offset, reqcount, err) < 0) {
832             LOG(log_error, logtype_afpd, "afp_read(%s): ad_readfile: %s",
833                 of_name(ofork), strerror(errno));
834             goto afp_read_exit;
835         }
836         goto afp_read_done;
837     }
838 #endif
839
840     *rbuflen = MIN(reqcount, dsi->server_quantum);
841
842     cc = read_file(ofork, eid, offset, ibuf, rbuflen);
843     if (cc < 0) {
844         err = cc;
845         goto afp_read_done;
846     }
847
848     LOG(log_debug, logtype_afpd,
849         "afp_read(name: \"%s\", offset: %jd, reqcount: %jd): got %jd bytes from file",
850         of_name(ofork), (intmax_t)offset, (intmax_t)reqcount, (intmax_t)*rbuflen);
851
852     offset += *rbuflen;
853
854     /*
855      * dsi_readinit() returns size of next read buffer. by this point,
856      * we know that we're sending some data. if we fail, something
857      * horrible happened.
858      */
859     if ((cc = dsi_readinit(dsi, ibuf, *rbuflen, reqcount, err)) < 0)
860         goto afp_read_exit;
861     *rbuflen = cc;
862
863     while (*rbuflen > 0) {
864         /*
865          * This loop isn't really entered anymore, we've already
866          * sent the whole requested block in dsi_readinit().
867          */
868         cc = read_file(ofork, eid, offset, ibuf, rbuflen);
869         if (cc < 0)
870             goto afp_read_exit;
871
872         offset += *rbuflen;
873         /* dsi_read() also returns buffer size of next allocation */
874         cc = dsi_read(dsi, ibuf, *rbuflen); /* send it off */
875         if (cc < 0)
876             goto afp_read_exit;
877         *rbuflen = cc;
878     }
879     dsi_readdone(dsi);
880     goto afp_read_done;
881
882 afp_read_exit:
883     LOG(log_error, logtype_afpd, "afp_read(%s): %s", of_name(ofork), strerror(errno));
884     dsi_readdone(dsi);
885    if (obj->options.flags & OPTION_AFP_READ_LOCK)
886        ad_tmplock(ofork->of_ad, eid, ADLOCK_CLR, saveoff, savereqcount, ofork->of_refnum);
887     obj->exit(EXITERR_CLNT);
888
889 afp_read_done:
890     if (obj->options.flags & OPTION_AFP_READ_LOCK)
891         ad_tmplock(ofork->of_ad, eid, ADLOCK_CLR, saveoff, savereqcount, ofork->of_refnum);
892
893     AFP_READ_DONE();
894     return err;
895
896 afp_read_err:
897     *rbuflen = 0;
898     return err;
899 }
900
901 /* ---------------------- */
902 int afp_read(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
903 {
904     return read_fork(obj, ibuf, ibuflen, rbuf, rbuflen, 0);
905 }
906
907 /* ---------------------- */
908 int afp_read_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
909 {
910     return read_fork(obj, ibuf, ibuflen, rbuf, rbuflen, 1);
911 }
912
913 /* ---------------------- */
914 int afp_flush(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
915 {
916     struct vol *vol;
917     uint16_t vid;
918
919     *rbuflen = 0;
920     ibuf += 2;
921
922     memcpy(&vid, ibuf, sizeof(vid));
923     if (NULL == ( vol = getvolbyvid( vid )) ) {
924         return( AFPERR_PARAM );
925     }
926
927     of_flush(vol);
928     return( AFP_OK );
929 }
930
931 int afp_flushfork(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
932 {
933     struct ofork    *ofork;
934     uint16_t       ofrefnum;
935
936     *rbuflen = 0;
937     ibuf += 2;
938     memcpy(&ofrefnum, ibuf, sizeof( ofrefnum ));
939
940     if (NULL == ( ofork = of_find( ofrefnum )) ) {
941         LOG(log_error, logtype_afpd, "afp_flushfork: of_find(%d) could not locate fork", ofrefnum );
942         return( AFPERR_PARAM );
943     }
944
945     LOG(log_debug, logtype_afpd, "afp_flushfork(fork: %s)",
946         (ofork->of_flags & AFPFORK_DATA) ? "d" : "r");
947
948     if ( flushfork( ofork ) < 0 ) {
949         LOG(log_error, logtype_afpd, "afp_flushfork(%s): %s", of_name(ofork), strerror(errno) );
950     }
951
952     return( AFP_OK );
953 }
954
955 /*
956   FIXME
957   There is a lot to tell about fsync, fdatasync, F_FULLFSYNC.
958   fsync(2) on OSX is implemented differently than on other platforms.
959   see: http://mirror.linux.org.au/pub/linux.conf.au/2007/video/talks/278.pdf.
960 */
961 int afp_syncfork(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
962 {
963     struct ofork        *ofork;
964     uint16_t           ofrefnum;
965
966     *rbuflen = 0;
967     ibuf += 2;
968
969     memcpy(&ofrefnum, ibuf, sizeof(ofrefnum));
970     ibuf += sizeof( ofrefnum );
971
972     if (NULL == ( ofork = of_find( ofrefnum )) ) {
973         LOG(log_error, logtype_afpd, "afpd_syncfork: of_find(%d) could not locate fork", ofrefnum );
974         return( AFPERR_PARAM );
975     }
976
977     LOG(log_debug, logtype_afpd, "afp_syncfork(fork: %s)",
978         (ofork->of_flags & AFPFORK_DATA) ? "d" : "r");
979
980     if ( flushfork( ofork ) < 0 ) {
981         LOG(log_error, logtype_afpd, "flushfork(%s): %s", of_name(ofork), strerror(errno) );
982         return AFPERR_MISC;
983     }
984
985     return( AFP_OK );
986 }
987
988 /* this is very similar to closefork */
989 int flushfork(struct ofork *ofork)
990 {
991     struct timeval tv;
992
993     int err = 0, doflush = 0;
994
995     if ( ad_data_fileno( ofork->of_ad ) != -1 &&
996          fsync( ad_data_fileno( ofork->of_ad )) < 0 ) {
997         LOG(log_error, logtype_afpd, "flushfork(%s): dfile(%d) %s",
998             of_name(ofork), ad_data_fileno(ofork->of_ad), strerror(errno) );
999         err = -1;
1000     }
1001
1002     if ( ad_reso_fileno( ofork->of_ad ) != -1 &&  /* HF */
1003          (ofork->of_flags & AFPFORK_RSRC)) {
1004
1005         /* read in the rfork length */
1006         ad_refresh(NULL, ofork->of_ad);
1007
1008         /* set the date if we're dirty */
1009         if ((ofork->of_flags & AFPFORK_DIRTY) && !gettimeofday(&tv, NULL)) {
1010             ad_setdate(ofork->of_ad, AD_DATE_MODIFY|AD_DATE_UNIX, tv.tv_sec);
1011             ofork->of_flags &= ~AFPFORK_DIRTY;
1012             doflush++;
1013         }
1014
1015         /* flush the header */
1016         if (doflush && ad_flush(ofork->of_ad) < 0)
1017             err = -1;
1018
1019         if (fsync( ad_reso_fileno( ofork->of_ad )) < 0)
1020             err = -1;
1021
1022         if (err < 0)
1023             LOG(log_error, logtype_afpd, "flushfork(%s): hfile(%d) %s",
1024                 of_name(ofork), ad_reso_fileno(ofork->of_ad), strerror(errno) );
1025     }
1026
1027     return( err );
1028 }
1029
1030 int afp_closefork(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1031 {
1032     struct ofork    *ofork;
1033     uint16_t       ofrefnum;
1034
1035     *rbuflen = 0;
1036     ibuf += 2;
1037     memcpy(&ofrefnum, ibuf, sizeof( ofrefnum ));
1038
1039     if (NULL == ( ofork = of_find( ofrefnum )) ) {
1040         LOG(log_error, logtype_afpd, "afp_closefork: of_find(%d) could not locate fork", ofrefnum );
1041         return( AFPERR_PARAM );
1042     }
1043
1044     LOG(log_debug, logtype_afpd, "afp_closefork(fork: %" PRIu16 " [%s])",
1045         ofork->of_refnum, (ofork->of_flags & AFPFORK_DATA) ? "data" : "rsrc");
1046
1047     if (of_closefork(obj, ofork) < 0 ) {
1048         LOG(log_error, logtype_afpd, "afp_closefork: of_closefork: %s", strerror(errno) );
1049         return( AFPERR_PARAM );
1050     }
1051
1052     return( AFP_OK );
1053 }
1054
1055
1056 static ssize_t write_file(struct ofork *ofork, int eid,
1057                           off_t offset, char *rbuf,
1058                           size_t rbuflen)
1059 {
1060     ssize_t cc;
1061
1062     LOG(log_debug, logtype_afpd, "write_file(off: %ju, size: %zu)",
1063         (uintmax_t)offset, rbuflen);
1064
1065     if (( cc = ad_write(ofork->of_ad, eid, offset, 0,
1066                         rbuf, rbuflen)) < 0 ) {
1067         switch ( errno ) {
1068         case EDQUOT :
1069         case EFBIG :
1070         case ENOSPC :
1071             LOG(log_error, logtype_afpd, "write_file: DISK FULL");
1072             return( AFPERR_DFULL );
1073         case EACCES:
1074             return AFPERR_ACCESS;
1075         default :
1076             LOG(log_error, logtype_afpd, "afp_write(%s): ad_write: %s", of_name(ofork), strerror(errno) );
1077             return( AFPERR_PARAM );
1078         }
1079     }
1080
1081     return cc;
1082 }
1083
1084
1085 /*
1086  * FPWrite. NOTE: on an error, we always use afp_write_err as
1087  * the client may have sent us a bunch of data that's not reflected
1088  * in reqcount et al.
1089  */
1090 static int write_fork(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen, int is64)
1091 {
1092     struct ofork    *ofork;
1093     off_t           offset, saveoff, reqcount, oldsize, newsize;
1094     int             endflag, eid, err = AFP_OK;
1095     uint16_t        ofrefnum;
1096     ssize_t         cc;
1097     DSI             *dsi = obj->dsi;
1098     char            *rcvbuf = (char *)dsi->commands;
1099     size_t          rcvbuflen = dsi->server_quantum;
1100
1101     /* figure out parameters */
1102     ibuf++;
1103     endflag = ENDBIT(*ibuf);
1104     ibuf++;
1105     memcpy(&ofrefnum, ibuf, sizeof( ofrefnum ));
1106     ibuf += sizeof( ofrefnum );
1107
1108     offset   = get_off_t(&ibuf, is64);
1109     reqcount = get_off_t(&ibuf, is64);
1110
1111     if (NULL == ( ofork = of_find( ofrefnum )) ) {
1112         LOG(log_error, logtype_afpd, "afp_write: of_find(%d) could not locate fork", ofrefnum );
1113         err = AFPERR_PARAM;
1114         goto afp_write_err;
1115     }
1116
1117     LOG(log_debug, logtype_afpd, "afp_write(fork: %" PRIu16 " [%s], off: %" PRIu64 ", size: %" PRIu64 ")",
1118         ofork->of_refnum, (ofork->of_flags & AFPFORK_DATA) ? "data" : "reso", offset, reqcount);
1119
1120     if ((ofork->of_flags & AFPFORK_ACCWR) == 0) {
1121         err = AFPERR_ACCESS;
1122         goto afp_write_err;
1123     }
1124
1125 #ifdef AFS
1126     writtenfork = ofork;
1127 #endif /* AFS */
1128
1129     if ( ofork->of_flags & AFPFORK_DATA) {
1130         eid = ADEID_DFORK;
1131     } else if (ofork->of_flags & AFPFORK_RSRC) {
1132         eid = ADEID_RFORK;
1133     } else {
1134         err = AFPERR_ACCESS; /* should never happen */
1135         goto afp_write_err;
1136     }
1137
1138     oldsize = ad_size(ofork->of_ad, eid);
1139     if (endflag)
1140         offset += oldsize;
1141
1142     /* handle bogus parameters */
1143     if (reqcount < 0 || offset < 0) {
1144         err = AFPERR_PARAM;
1145         goto afp_write_err;
1146     }
1147
1148     newsize = ((offset + reqcount) > oldsize) ? (offset + reqcount) : oldsize;
1149
1150     /* offset can overflow on 64-bit capable filesystems.
1151      * report disk full if that's going to happen. */
1152     if (sum_neg(is64, offset, reqcount)) {
1153         LOG(log_error, logtype_afpd, "write_fork: DISK FULL");
1154         err = AFPERR_DFULL;
1155         goto afp_write_err;
1156     }
1157
1158     if (!reqcount) { /* handle request counts of 0 */
1159         err = AFP_OK;
1160         *rbuflen = set_off_t (offset, rbuf, is64);
1161         goto afp_write_err;
1162     }
1163
1164     AFP_WRITE_START((long)reqcount);
1165
1166     saveoff = offset;
1167     if (obj->options.flags & OPTION_AFP_READ_LOCK) {
1168         if (ad_tmplock(ofork->of_ad, eid, ADLOCK_WR, saveoff, reqcount, ofork->of_refnum) < 0) {
1169             err = AFPERR_LOCK;
1170             goto afp_write_err;
1171         }
1172     }
1173
1174     /* find out what we have already */
1175     cc = dsi_writeinit(dsi, rcvbuf, rcvbuflen);
1176
1177     if (!cc || (cc = write_file(ofork, eid, offset, rcvbuf, cc)) < 0) {
1178         dsi_writeflush(dsi);
1179         *rbuflen = 0;
1180         if (obj->options.flags & OPTION_AFP_READ_LOCK)
1181             ad_tmplock(ofork->of_ad, eid, ADLOCK_CLR, saveoff, reqcount, ofork->of_refnum);
1182         return cc;
1183     }
1184
1185     offset += cc;
1186
1187 #if 0 /*def HAVE_SENDFILE_WRITE*/
1188     if ((cc = ad_writefile(ofork->of_ad, eid, dsi->socket, offset, dsi->datasize)) < 0) {
1189         switch (errno) {
1190         case EDQUOT:
1191         case EFBIG:
1192         case ENOSPC:
1193             cc = AFPERR_DFULL;
1194             break;
1195         default:
1196             LOG(log_error, logtype_afpd, "afp_write: ad_writefile: %s", strerror(errno) );
1197             goto afp_write_loop;
1198         }
1199         dsi_writeflush(dsi);
1200         *rbuflen = 0;
1201         if (obj->options.flags & OPTION_AFP_READ_LOCK)
1202             ad_tmplock(ofork->of_ad, eid, ADLOCK_CLR, saveoff, reqcount,  ofork->of_refnum);
1203         return cc;
1204     }
1205
1206     offset += cc;
1207     goto afp_write_done;
1208 #endif /* 0, was HAVE_SENDFILE_WRITE */
1209
1210     /* loop until everything gets written. currently
1211      * dsi_write handles the end case by itself. */
1212     while ((cc = dsi_write(dsi, rcvbuf, rcvbuflen))) {
1213
1214         if ((cc = write_file(ofork, eid, offset, rcvbuf, cc)) < 0) {
1215             dsi_writeflush(dsi);
1216             *rbuflen = 0;
1217             if (obj->options.flags & OPTION_AFP_READ_LOCK)
1218                 ad_tmplock(ofork->of_ad, eid, ADLOCK_CLR, saveoff, reqcount,  ofork->of_refnum);
1219             return cc;
1220         }
1221
1222         LOG(log_debug, logtype_afpd, "afp_write: wrote: %jd, offset: %jd",
1223             (intmax_t)cc, (intmax_t)offset);
1224
1225         offset += cc;
1226     }
1227
1228     if (obj->options.flags & OPTION_AFP_READ_LOCK)
1229         ad_tmplock(ofork->of_ad, eid, ADLOCK_CLR, saveoff, reqcount,  ofork->of_refnum);
1230     if ( ad_meta_fileno( ofork->of_ad ) != -1 ) /* META */
1231         ofork->of_flags |= AFPFORK_DIRTY;
1232
1233     /* we have modified any fork, remember until close_fork */
1234     ofork->of_flags |= AFPFORK_MODIFIED;
1235
1236     /* update write count */
1237     ofork->of_vol->v_appended += (newsize > oldsize) ? (newsize - oldsize) : 0;
1238
1239     *rbuflen = set_off_t (offset, rbuf, is64);
1240     AFP_WRITE_DONE();
1241     return( AFP_OK );
1242
1243 afp_write_err:
1244     dsi_writeinit(dsi, rcvbuf, rcvbuflen);
1245     dsi_writeflush(dsi);
1246
1247     if (err != AFP_OK) {
1248         *rbuflen = 0;
1249     }
1250     return err;
1251 }
1252
1253 /* ---------------------------- */
1254 int afp_write(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
1255 {
1256     return write_fork(obj, ibuf, ibuflen, rbuf, rbuflen, 0);
1257 }
1258
1259 /* ----------------------------
1260  * FIXME need to deal with SIGXFSZ signal
1261  */
1262 int afp_write_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
1263 {
1264     return write_fork(obj, ibuf, ibuflen, rbuf, rbuflen, 1);
1265 }
1266
1267 /* ---------------------------- */
1268 int afp_getforkparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1269 {
1270     struct ofork    *ofork;
1271     int             ret;
1272     uint16_t       ofrefnum, bitmap;
1273     size_t          buflen;
1274     ibuf += 2;
1275     memcpy(&ofrefnum, ibuf, sizeof( ofrefnum ));
1276     ibuf += sizeof( ofrefnum );
1277     memcpy(&bitmap, ibuf, sizeof( bitmap ));
1278     bitmap = ntohs( bitmap );
1279     ibuf += sizeof( bitmap );
1280
1281     *rbuflen = 0;
1282     if (NULL == ( ofork = of_find( ofrefnum )) ) {
1283         LOG(log_error, logtype_afpd, "afp_getforkparams: of_find(%d) could not locate fork", ofrefnum );
1284         return( AFPERR_PARAM );
1285     }
1286
1287     if (AD_META_OPEN(ofork->of_ad)) {
1288         if ( ad_refresh(NULL, ofork->of_ad ) < 0 ) {
1289             LOG(log_error, logtype_afpd, "getforkparams(%s): ad_refresh: %s", of_name(ofork), strerror(errno) );
1290             return( AFPERR_PARAM );
1291         }
1292     }
1293
1294     if (AFP_OK != (ret = getforkparams(obj, ofork, bitmap, rbuf + sizeof( u_short ), &buflen ))) {
1295         return( ret );
1296     }
1297
1298     *rbuflen = buflen + sizeof( u_short );
1299     bitmap = htons( bitmap );
1300     memcpy(rbuf, &bitmap, sizeof( bitmap ));
1301     return( AFP_OK );
1302 }
1303