2 * Copyright (c) 1990,1995 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
12 #include <sys/param.h>
15 #include <atalk/adouble.h>
17 #include <atalk/bstrlib.h>
18 #include <atalk/bstradd.h>
19 #include <atalk/logger.h>
22 #define MIN(a,b) ((a)<(b)?(a):(b))
25 static int fsetrsrcea(struct adouble *ad, int fd, const char *eaname, const void *value, size_t size, int flags)
27 if (ad->ad_maxeafssize >= size) {
28 if (sys_fsetxattr(fd, eaname, value, size, 0) == -1)
33 /* rsrcfork is larger then maximum EA support by fs so we have to split it */
35 int eas = (size / ad->ad_maxeafssize);
36 size_t remain = size - (eas * ad->ad_maxeafssize);
39 LOG(log_debug, logtype_default, "fsetrsrcea(\"%s\"): size: %zu, maxea: %zu, eas: %d, remain: %zu",
40 eaname, size, ad->ad_maxeafssize, eas, remain);
42 for (i = 0; i < eas; i++) {
43 if ((eachunk = bformat("%s.%d", eaname, i + 1)) == NULL)
45 if (sys_fsetxattr(fd, bdata(eachunk), value + (i * ad->ad_maxeafssize), ad->ad_maxeafssize, 0) == -1) {
46 LOG(log_error, logtype_default, "fsetrsrcea(\"%s\"): %s", bdata(eachunk), strerror(errno));
53 if ((eachunk = bformat("%s.%d", eaname, i + 1)) == NULL)
55 if (sys_fsetxattr(fd, bdata(eachunk), value + (i * ad->ad_maxeafssize), remain, 0) == -1) {
56 LOG(log_error, logtype_default, "fsetrsrcea(\"%s\"): %s", bdata(eachunk), strerror(errno));
66 /* XXX: locking has to be checked before each stream of consecutive
67 * ad_writes to prevent a lock in the middle from causing problems.
70 ssize_t adf_pwrite(struct ad_fd *ad_fd, const void *buf, size_t count, off_t offset)
75 if ( ad_fd->adf_off != offset ) {
76 if ( lseek( ad_fd->adf_fd, offset, SEEK_SET ) < 0 ) {
79 ad_fd->adf_off = offset;
81 cc = write( ad_fd->adf_fd, buf, count );
87 cc = pwrite(ad_fd->adf_fd, buf, count, offset );
93 ssize_t ad_write(struct adouble *ad, uint32_t eid, off_t off, int end, const char *buf, size_t buflen)
100 if (ad_data_fileno(ad) == -2) {
106 if ( eid == ADEID_DFORK ) {
108 if ( fstat( ad_data_fileno(ad), &st ) < 0 ) {
111 off = st.st_size - off;
113 cc = adf_pwrite(&ad->ad_data_fork, buf, buflen, off);
114 } else if ( eid == ADEID_RFORK ) {
115 switch (ad->ad_vers) {
119 if ( fstat( ad_data_fileno(ad), &st ) < 0 )
121 off = st.st_size - off -ad_getentryoff(ad, eid);
123 r_off = ad_getentryoff(ad, eid) + off;
124 cc = adf_pwrite(&ad->ad_resource_fork, buf, buflen, r_off);
126 /* sync up our internal buffer FIXME always false? */
127 if (r_off < ad_getentryoff(ad, ADEID_RFORK))
128 memcpy(ad->ad_data + r_off, buf, MIN(sizeof(ad->ad_data) -r_off, cc));
129 if ( ad->ad_rlen < off + cc )
130 ad->ad_rlen = off + cc;
134 if (ad->ad_resforkbuf == NULL) {
135 ad->ad_rlen = off + buflen;
136 if ((ad->ad_resforkbuf = malloc(ad->ad_rlen)) == NULL) {
141 if ((off + buflen) > ad->ad_rlen) {
142 if ((ad->ad_resforkbuf = realloc(ad->ad_resforkbuf, off + buflen)) == NULL)
144 ad->ad_rlen = off + buflen;
146 memcpy(ad->ad_resforkbuf + off, buf, buflen);
148 if (fsetrsrcea(ad, ad_data_fileno(ad), AD_EA_RESO, ad->ad_resforkbuf, ad->ad_rlen, 0) == -1)
157 return -1; /* we don't know how to write if it's not a ressource or data fork */
164 * the caller set the locks
165 * ftruncate is undefined when the file length is smaller than 'size'
167 int sys_ftruncate(int fd, off_t length)
177 if (!ftruncate(fd, length)) {
180 /* maybe ftruncate doesn't work if we try to extend the size */
184 /* we only care about file pointer if we don't use pwrite */
185 if ((off_t)-1 == (curpos = lseek(fd, 0, SEEK_CUR)) ) {
191 if ( fstat( fd, &st ) < 0 ) {
196 if (st.st_size > length) {
201 if (lseek(fd, length -1, SEEK_SET) != length -1) {
206 if (1 != write( fd, &c, 1 )) {
207 /* return the write errno */
212 if (curpos != lseek(fd, curpos, SEEK_SET)) {
221 /* ------------------------ */
222 int ad_rtruncate( struct adouble *ad, const off_t size)
224 if (ad->ad_vers != AD_VERSION_EA)
225 if (sys_ftruncate(ad_reso_fileno(ad), size + ad->ad_eid[ ADEID_RFORK ].ade_off ) < 0 )
233 int ad_dtruncate(struct adouble *ad, const off_t size)
235 if (sys_ftruncate(ad_data_fileno(ad), size) < 0)