2 * $Id: ad_read.c,v 1.10 2010-02-10 14:05:37 franklahm Exp $
4 * Copyright (c) 1990,1991 Regents of The University of Michigan.
7 * Permission to use, copy, modify, and distribute this software and
8 * its documentation for any purpose and without fee is hereby granted,
9 * provided that the above copyright notice appears in all copies and
10 * that both that copyright notice and this permission notice appear
11 * in supporting documentation, and that the name of The University
12 * of Michigan not be used in advertising or publicity pertaining to
13 * distribution of the software without specific, written prior
14 * permission. This software is supplied as is without expressed or
15 * implied warranties of any kind.
17 * Research Systems Unix Group
18 * The University of Michigan
20 * 535 W. William Street
23 * netatalk@itd.umich.edu
28 #endif /* HAVE_CONFIG_H */
30 #include <atalk/adouble.h>
32 #include <sys/param.h>
35 #define MIN(a,b) ((a)<(b)?(a):(b))
38 ssize_t adf_pread(struct ad_fd *ad_fd, void *buf, size_t count, off_t offset)
43 if ( ad_fd->adf_off != offset ) {
44 if ( lseek( ad_fd->adf_fd, offset, SEEK_SET ) < 0 ) {
47 ad_fd->adf_off = offset;
49 if (( cc = read( ad_fd->adf_fd, buf, count )) < 0 ) {
54 cc = pread(ad_fd->adf_fd, buf, count, offset );
59 /* XXX: locks have to be checked before each stream of consecutive
60 * ad_reads to prevent a denial in the middle from causing
62 ssize_t ad_read( struct adouble *ad, const u_int32_t eid, off_t off, char *buf, const size_t buflen)
66 /* We're either reading the data fork (and thus the data file)
67 * or we're reading anything else (and thus the header file). */
68 if ( eid == ADEID_DFORK ) {
69 if (ad->ad_data_fork.adf_syml !=0 ) {
70 /* It's a symlink, we already have the target in adf_syml */
71 cc = strlen(ad->ad_data_fork.adf_syml);
73 /* Request buffersize is too small, force AFPERR_PARAM */
75 memcpy(buf, ad->ad_data_fork.adf_syml, cc);
77 cc = adf_pread(&ad->ad_data_fork, buf, buflen, off);
82 if ( ad_reso_fileno( ad ) == -1 ) {
83 /* resource fork is not open ( cf etc/afp/fork.c) */
86 r_off = ad_getentryoff(ad, eid) + off;
88 if (( cc = adf_pread( &ad->ad_resource_fork, buf, buflen, r_off )) < 0 ) {
92 * We've just read in bytes from the disk that we read earlier
93 * into ad_data. If we're going to write this buffer out later,
94 * we need to update ad_data.
95 * FIXME : always false?
97 if (r_off < ad_getentryoff(ad, ADEID_RFORK)) {
98 if ( ad->ad_resource_fork.adf_flags & O_RDWR ) {
99 memcpy(buf, ad->ad_data + r_off,
100 MIN(sizeof( ad->ad_data ) - r_off, cc));
102 memcpy(ad->ad_data + r_off, buf,
103 MIN(sizeof( ad->ad_data ) - r_off, cc));