]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_write.c
f5a5c9355b5d57603351f1e3ff64c074c3a8264a
[netatalk.git] / libatalk / adouble / ad_write.c
1 /*
2  * Copyright (c) 1990,1995 Regents of The University of Michigan.
3  * All Rights Reserved.  See COPYRIGHT.
4  */
5
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif /* HAVE_CONFIG_H */
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/param.h>
13 #include <errno.h>
14
15 #include <atalk/adouble.h>
16 #include <atalk/ea.h>
17 #include <atalk/bstrlib.h>
18 #include <atalk/bstradd.h>
19 #include <atalk/logger.h>
20 #include <atalk/util.h>
21 #include <atalk/errchk.h>
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, uint32_t eid, off_t off, int end, const char *buf, size_t buflen)
51 {
52     EC_INIT;
53     struct stat         st;
54     ssize_t             cc;
55     size_t roundup;
56     off_t    r_off;
57
58     if (ad_data_fileno(ad) == -2) {
59         /* It's a symlink */
60         errno = EACCES;
61         return -1;
62     }
63
64     LOG(log_debug, logtype_default, "ad_write: off: %ju, size: %zu, eabuflen: %zu",
65         (uintmax_t)off, buflen, ad->ad_rlen);
66     
67     if ( eid == ADEID_DFORK ) {
68         if ( end ) {
69             if ( fstat( ad_data_fileno(ad), &st ) < 0 ) {
70                 return( -1 );
71             }
72             off = st.st_size - off;
73         }
74         cc = adf_pwrite(&ad->ad_data_fork, buf, buflen, off);
75     } else if ( eid == ADEID_RFORK ) {
76         if (end) {
77             if (fstat( ad_reso_fileno(ad), &st ) < 0)
78                 return(-1);
79             off = st.st_size - off - ad_getentryoff(ad, eid);
80         }
81 #ifdef HAVE_EAFD
82         if (ad->ad_vers == AD_VERSION_EA) {
83             r_off = 0;
84         } else
85 #endif
86             r_off = ad_getentryoff(ad, eid) + off;
87
88         cc = adf_pwrite(&ad->ad_resource_fork, buf, buflen, r_off);
89
90         if ( ad->ad_rlen < off + cc )
91             ad->ad_rlen = off + cc;
92     } else {
93         return -1; /* we don't know how to write if it's not a ressource or data fork */
94     }
95
96 EC_CLEANUP:
97     if (ret != 0)
98         return ret;
99     return( cc );
100 }
101
102 /* 
103  * the caller set the locks
104  * ftruncate is undefined when the file length is smaller than 'size'
105  */
106 int sys_ftruncate(int fd, off_t length)
107 {
108
109 #ifndef  HAVE_PWRITE
110 off_t           curpos;
111 #endif
112 int             err;
113 struct stat     st;
114 char            c = 0;
115
116     if (!ftruncate(fd, length)) {
117         return 0;
118     }
119     /* maybe ftruncate doesn't work if we try to extend the size */
120     err = errno;
121
122 #ifndef  HAVE_PWRITE
123     /* we only care about file pointer if we don't use pwrite */
124     if ((off_t)-1 == (curpos = lseek(fd, 0, SEEK_CUR)) ) {
125         errno = err;
126         return -1;
127     }
128 #endif
129
130     if ( fstat( fd, &st ) < 0 ) {
131         errno = err;
132         return -1;
133     }
134     
135     if (st.st_size > length) {
136         errno = err;
137         return -1;
138     }
139
140     if (lseek(fd, length -1, SEEK_SET) != length -1) {
141         errno = err;
142         return -1;
143     }
144
145     if (1 != write( fd, &c, 1 )) {
146         /* return the write errno */
147         return -1;
148     }
149
150 #ifndef  HAVE_PWRITE
151     if (curpos != lseek(fd, curpos,  SEEK_SET)) {
152         errno = err;
153         return -1;
154     }
155 #endif
156
157     return 0;    
158 }
159
160 /* ------------------------ */
161 int ad_rtruncate( struct adouble *ad, const off_t size)
162 {
163     if (ad->ad_vers != AD_VERSION_EA)
164         if (sys_ftruncate(ad_reso_fileno(ad), size + ad->ad_eid[ ADEID_RFORK ].ade_off ) < 0 )
165             return -1;
166
167     ad->ad_rlen = size;    
168
169     return 0;
170 }
171
172 int ad_dtruncate(struct adouble *ad, const off_t size)
173 {
174     if (sys_ftruncate(ad_data_fileno(ad), size) < 0)
175         return -1;
176
177     return 0;
178 }