]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_write.c
db542cb126f8bba07904c415e1f99478c5877ca9
[netatalk.git] / libatalk / adouble / ad_write.c
1 /*
2  * $Id: ad_write.c,v 1.10 2009-10-13 22:55:37 didg Exp $
3  *
4  * Copyright (c) 1990,1995 Regents of The University of Michigan.
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 <atalk/adouble.h>
13
14 #include <string.h>
15 #include <sys/param.h>
16 #include <errno.h>
17
18
19 #ifndef MIN
20 #define MIN(a,b)        ((a)<(b)?(a):(b))
21 #endif /* ! MIN */
22
23 /* XXX: locking has to be checked before each stream of consecutive
24  *      ad_writes to prevent a lock in the middle from causing problems. 
25  */
26
27 ssize_t adf_pwrite(struct ad_fd *ad_fd, const void *buf, size_t count, off_t offset)
28 {
29     ssize_t             cc;
30
31 #ifndef  HAVE_PWRITE
32     if ( ad_fd->adf_off != offset ) {
33         if ( lseek( ad_fd->adf_fd, offset, SEEK_SET ) < 0 ) {
34             return -1;
35         }
36         ad_fd->adf_off = offset;
37     }
38     cc = write( ad_fd->adf_fd, buf, count );
39     if ( cc < 0 ) {
40         return -1;
41     }
42     ad_fd->adf_off += cc;
43 #else
44    cc = pwrite(ad_fd->adf_fd, buf, count, offset );
45 #endif
46     return cc;
47 }
48
49 /* end is always 0 */
50 ssize_t ad_write(struct adouble *ad, const u_int32_t eid, off_t off, const int end, const char *buf, const size_t buflen)
51 {
52     struct stat         st;
53     ssize_t             cc;
54     
55     if ( eid == ADEID_DFORK ) {
56         if ( end ) {
57             if ( fstat( ad_data_fileno(ad), &st ) < 0 ) {
58                 return( -1 );
59             }
60             off = st.st_size - off;
61         }
62         cc = adf_pwrite(&ad->ad_data_fork, buf, buflen, off);
63     } else if ( eid == ADEID_RFORK ) {
64         off_t    r_off;
65
66         if ( end ) {
67             if ( fstat( ad_data_fileno(ad), &st ) < 0 ) {
68                 return( -1 );
69             }
70             off = st.st_size - off -ad_getentryoff(ad, eid);
71         }
72         r_off = ad_getentryoff(ad, eid) + off;
73         cc = adf_pwrite(&ad->ad_resource_fork, buf, buflen, r_off);
74
75         /* sync up our internal buffer  FIXME always false? */
76         if (r_off < ad_getentryoff(ad, ADEID_RFORK)) {
77             memcpy(ad->ad_data + r_off, buf, MIN(sizeof(ad->ad_data) -r_off, cc));
78         }
79         if ( ad->ad_rlen  < r_off + cc ) {
80              ad->ad_rlen = r_off + cc;
81         }
82     }
83     else {
84         return -1; /* we don't know how to write if it's not a ressource or data fork */
85     }
86     return( cc );
87 }
88
89 /* 
90  * the caller set the locks
91  * ftruncate is undefined when the file length is smaller than 'size'
92  */
93 int sys_ftruncate(int fd, off_t length)
94 {
95
96 #ifndef  HAVE_PWRITE
97 off_t           curpos;
98 #endif
99 int             err;
100 struct stat     st;
101 char            c = 0;
102
103     if (!ftruncate(fd, length)) {
104         return 0;
105     }
106     /* maybe ftruncate doesn't work if we try to extend the size */
107     err = errno;
108
109 #ifndef  HAVE_PWRITE
110     /* we only care about file pointer if we don't use pwrite */
111     if ((off_t)-1 == (curpos = lseek(fd, 0, SEEK_CUR)) ) {
112         errno = err;
113         return -1;
114     }
115 #endif
116
117     if ( fstat( fd, &st ) < 0 ) {
118         errno = err;
119         return -1;
120     }
121     
122     if (st.st_size > length) {
123         errno = err;
124         return -1;
125     }
126
127     if (lseek(fd, length -1, SEEK_SET) != length -1) {
128         errno = err;
129         return -1;
130     }
131
132     if (1 != write( fd, &c, 1 )) {
133         /* return the write errno */
134         return -1;
135     }
136
137 #ifndef  HAVE_PWRITE
138     if (curpos != lseek(fd, curpos,  SEEK_SET)) {
139         errno = err;
140         return -1;
141     }
142 #endif
143
144     return 0;    
145 }
146
147 /* ------------------------ */
148 int ad_rtruncate( struct adouble *ad, const off_t size)
149 {
150     if ( sys_ftruncate( ad_reso_fileno(ad),
151             size + ad->ad_eid[ ADEID_RFORK ].ade_off ) < 0 ) {
152         return -1;
153     }
154     ad->ad_rlen = size;    
155
156     return 0;
157 }
158
159 int ad_dtruncate(struct adouble *ad, const off_t size)
160 {
161     if (sys_ftruncate(ad_data_fileno(ad), size) < 0) {
162       return -1;
163     }
164     return 0;
165 }