]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_write.c
Allow opening symlinks r/w, but don't actually allow writing. Fixes test426
[netatalk.git] / libatalk / adouble / ad_write.c
1 /*
2  * $Id: ad_write.c,v 1.11 2010-03-30 12:55:26 franklahm 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 (ad_data_fileno(ad) == -2) {
56         /* It's a symlink */
57         errno = EACCES;
58         return -1;
59     }
60     
61     if ( eid == ADEID_DFORK ) {
62         if ( end ) {
63             if ( fstat( ad_data_fileno(ad), &st ) < 0 ) {
64                 return( -1 );
65             }
66             off = st.st_size - off;
67         }
68         cc = adf_pwrite(&ad->ad_data_fork, buf, buflen, off);
69     } else if ( eid == ADEID_RFORK ) {
70         off_t    r_off;
71
72         if ( end ) {
73             if ( fstat( ad_data_fileno(ad), &st ) < 0 ) {
74                 return( -1 );
75             }
76             off = st.st_size - off -ad_getentryoff(ad, eid);
77         }
78         r_off = ad_getentryoff(ad, eid) + off;
79         cc = adf_pwrite(&ad->ad_resource_fork, buf, buflen, r_off);
80
81         /* sync up our internal buffer  FIXME always false? */
82         if (r_off < ad_getentryoff(ad, ADEID_RFORK)) {
83             memcpy(ad->ad_data + r_off, buf, MIN(sizeof(ad->ad_data) -r_off, cc));
84         }
85         if ( ad->ad_rlen  < r_off + cc ) {
86              ad->ad_rlen = r_off + cc;
87         }
88     }
89     else {
90         return -1; /* we don't know how to write if it's not a ressource or data fork */
91     }
92     return( cc );
93 }
94
95 /* 
96  * the caller set the locks
97  * ftruncate is undefined when the file length is smaller than 'size'
98  */
99 int sys_ftruncate(int fd, off_t length)
100 {
101
102 #ifndef  HAVE_PWRITE
103 off_t           curpos;
104 #endif
105 int             err;
106 struct stat     st;
107 char            c = 0;
108
109     if (!ftruncate(fd, length)) {
110         return 0;
111     }
112     /* maybe ftruncate doesn't work if we try to extend the size */
113     err = errno;
114
115 #ifndef  HAVE_PWRITE
116     /* we only care about file pointer if we don't use pwrite */
117     if ((off_t)-1 == (curpos = lseek(fd, 0, SEEK_CUR)) ) {
118         errno = err;
119         return -1;
120     }
121 #endif
122
123     if ( fstat( fd, &st ) < 0 ) {
124         errno = err;
125         return -1;
126     }
127     
128     if (st.st_size > length) {
129         errno = err;
130         return -1;
131     }
132
133     if (lseek(fd, length -1, SEEK_SET) != length -1) {
134         errno = err;
135         return -1;
136     }
137
138     if (1 != write( fd, &c, 1 )) {
139         /* return the write errno */
140         return -1;
141     }
142
143 #ifndef  HAVE_PWRITE
144     if (curpos != lseek(fd, curpos,  SEEK_SET)) {
145         errno = err;
146         return -1;
147     }
148 #endif
149
150     return 0;    
151 }
152
153 /* ------------------------ */
154 int ad_rtruncate( struct adouble *ad, const off_t size)
155 {
156     if ( sys_ftruncate( ad_reso_fileno(ad),
157             size + ad->ad_eid[ ADEID_RFORK ].ade_off ) < 0 ) {
158         return -1;
159     }
160     ad->ad_rlen = size;    
161
162     return 0;
163 }
164
165 int ad_dtruncate(struct adouble *ad, const off_t size)
166 {
167     if (sys_ftruncate(ad_data_fileno(ad), size) < 0) {
168       return -1;
169     }
170     return 0;
171 }