2ee5034736fa31127d130d57c3a13ed8e3d09d4b
[kai/samba.git] / source / ntvfs / posix / pvfs_streams.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    POSIX NTVFS backend - alternate data streams
5
6    Copyright (C) Andrew Tridgell 2004
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "vfs_posix.h"
25 #include "librpc/gen_ndr/ndr_xattr.h"
26
27
28 /*
29   return the list of file streams for RAW_FILEINFO_STREAM_INFORMATION
30 */
31 NTSTATUS pvfs_stream_information(struct pvfs_state *pvfs, 
32                                  TALLOC_CTX *mem_ctx,
33                                  struct pvfs_filename *name, int fd, 
34                                  struct stream_information *info)
35 {
36         struct xattr_DosStreams *streams;
37         int i;
38         NTSTATUS status;
39
40         streams = talloc(mem_ctx, struct xattr_DosStreams);
41         if (streams == NULL) {
42                 return NT_STATUS_NO_MEMORY;
43         }
44
45         status = pvfs_streams_load(pvfs, name, fd, streams);
46         if (!NT_STATUS_IS_OK(status)) {
47                 ZERO_STRUCTP(streams);
48         }
49
50         info->num_streams = streams->num_streams+1;
51         info->streams = talloc_array(mem_ctx, struct stream_struct, info->num_streams);
52         if (!info->streams) {
53                 return NT_STATUS_NO_MEMORY;
54         }
55
56         info->streams[0].size          = name->st.st_size;
57         info->streams[0].alloc_size    = name->dos.alloc_size;
58         info->streams[0].stream_name.s = talloc_strdup(info->streams, "::$DATA");
59
60         for (i=0;i<streams->num_streams;i++) {
61                 info->streams[i+1].size          = streams->streams[i].size;
62                 info->streams[i+1].alloc_size    = streams->streams[i].alloc_size;
63                 info->streams[i+1].stream_name.s = talloc_asprintf(streams->streams, 
64                                                                    ":%s:$DATA",
65                                                                    streams->streams[i].name);
66         }
67
68         return NT_STATUS_OK;
69 }
70
71
72 /*
73   fill in the stream information for a name
74 */
75 NTSTATUS pvfs_stream_info(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd)
76 {
77         struct xattr_DosStreams *streams;
78         int i;
79         NTSTATUS status;
80
81         /* the NULL stream always exists */
82         if (name->stream_name == NULL) {
83                 name->stream_exists = True;
84                 return NT_STATUS_OK;
85         }
86
87         streams = talloc(name, struct xattr_DosStreams);
88         if (streams == NULL) {
89                 return NT_STATUS_NO_MEMORY;
90         }
91
92         status = pvfs_streams_load(pvfs, name, fd, streams);
93         if (!NT_STATUS_IS_OK(status)) {
94                 talloc_free(streams);
95                 return status;
96         }
97
98         for (i=0;i<streams->num_streams;i++) {
99                 struct xattr_DosStream *s = &streams->streams[i];
100                 if (StrCaseCmp(s->name, name->stream_name) == 0) {
101                         name->dos.alloc_size = pvfs_round_alloc_size(pvfs, s->alloc_size);
102                         name->st.st_size     = s->size;
103                         name->stream_exists = True;
104                         talloc_free(streams);
105                         return NT_STATUS_OK;
106                 }
107         }
108
109         talloc_free(streams);
110
111         name->dos.alloc_size = 0;
112         name->st.st_size     = 0;
113         name->stream_exists = False;
114
115         return NT_STATUS_OK;
116 }
117
118
119 /*
120   update size information for a stream
121 */
122 static NTSTATUS pvfs_stream_update_size(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
123                                         off_t size)
124 {
125         struct xattr_DosStreams *streams;
126         int i;
127         NTSTATUS status;
128
129         streams = talloc(name, struct xattr_DosStreams);
130         if (streams == NULL) {
131                 return NT_STATUS_NO_MEMORY;
132         }
133
134         status = pvfs_streams_load(pvfs, name, fd, streams);
135         if (!NT_STATUS_IS_OK(status)) {
136                 ZERO_STRUCTP(streams);
137         }
138
139         for (i=0;i<streams->num_streams;i++) {
140                 struct xattr_DosStream *s = &streams->streams[i];
141                 if (StrCaseCmp(s->name, name->stream_name) == 0) {
142                         s->size       = size;
143                         s->alloc_size = pvfs_round_alloc_size(pvfs, size);
144                         break;
145                 }
146         }
147
148         if (i == streams->num_streams) {
149                 struct xattr_DosStream *s;
150                 streams->streams = talloc_realloc(streams, streams->streams, 
151                                                     struct xattr_DosStream,
152                                                     streams->num_streams+1);
153                 if (streams->streams == NULL) {
154                         talloc_free(streams);
155                         return NT_STATUS_NO_MEMORY;
156                 }
157                 streams->num_streams++;
158                 s = &streams->streams[i];
159                 
160                 s->flags      = XATTR_STREAM_FLAG_INTERNAL;
161                 s->size       = size;
162                 s->alloc_size = pvfs_round_alloc_size(pvfs, size);
163                 s->name       = name->stream_name;
164         }
165
166         status = pvfs_streams_save(pvfs, name, fd, streams);
167         talloc_free(streams);
168
169         return status;
170 }
171
172
173 /*
174   create the xattr for a alternate data stream
175 */
176 NTSTATUS pvfs_stream_create(struct pvfs_state *pvfs, 
177                             struct pvfs_filename *name, 
178                             int fd)
179 {
180         NTSTATUS status;
181         status = pvfs_xattr_create(pvfs, name->full_name, fd, 
182                                    XATTR_DOSSTREAM_PREFIX, name->stream_name);
183         if (!NT_STATUS_IS_OK(status)) {
184                 return status;
185         }
186         return pvfs_stream_update_size(pvfs, name, fd, 0);
187 }
188
189 /*
190   delete the xattr for a alternate data stream
191 */
192 NTSTATUS pvfs_stream_delete(struct pvfs_state *pvfs, 
193                             struct pvfs_filename *name, 
194                             int fd)
195 {
196         NTSTATUS status;
197         struct xattr_DosStreams *streams;
198         int i;
199
200         status = pvfs_xattr_delete(pvfs, name->full_name, fd, 
201                                    XATTR_DOSSTREAM_PREFIX, name->stream_name);
202         if (!NT_STATUS_IS_OK(status)) {
203                 return status;
204         }
205
206         streams = talloc(name, struct xattr_DosStreams);
207         if (streams == NULL) {
208                 return NT_STATUS_NO_MEMORY;
209         }
210
211         status = pvfs_streams_load(pvfs, name, fd, streams);
212         if (!NT_STATUS_IS_OK(status)) {
213                 talloc_free(streams);
214                 return status;
215         }
216
217         for (i=0;i<streams->num_streams;i++) {
218                 struct xattr_DosStream *s = &streams->streams[i];
219                 if (StrCaseCmp(s->name, name->stream_name) == 0) {
220                         memmove(s, s+1, (streams->num_streams - (i+1)) * sizeof(*s));
221                         streams->num_streams--;
222                         break;
223                 }
224         }
225
226         status = pvfs_streams_save(pvfs, name, fd, streams);
227         talloc_free(streams);
228
229         return status;
230 }
231
232 /*
233   the equvalent of pread() on a stream
234 */
235 ssize_t pvfs_stream_read(struct pvfs_state *pvfs,
236                          struct pvfs_file_handle *h, void *data, size_t count, off_t offset)
237 {
238         NTSTATUS status;
239         DATA_BLOB blob;
240         if (count == 0) {
241                 return 0;
242         }
243         status = pvfs_xattr_load(pvfs, h, h->name->full_name, h->fd, XATTR_DOSSTREAM_PREFIX,
244                                  h->name->stream_name, offset+count, &blob);
245         if (!NT_STATUS_IS_OK(status)) {
246                 errno = EIO;
247                 return -1;
248         }
249         if (offset >= blob.length) {
250                 data_blob_free(&blob);
251                 return 0;
252         }
253         if (count > blob.length - offset) {
254                 count = blob.length - offset;
255         }
256         memcpy(data, blob.data + offset, count);
257         data_blob_free(&blob);
258         return count;
259 }
260
261
262 /*
263   the equvalent of pwrite() on a stream
264 */
265 ssize_t pvfs_stream_write(struct pvfs_state *pvfs,
266                           struct pvfs_file_handle *h, const void *data, size_t count, off_t offset)
267 {
268         NTSTATUS status;
269         DATA_BLOB blob;
270         if (count == 0) {
271                 return 0;
272         }
273         if (offset > XATTR_MAX_STREAM_SIZE) {
274                 errno = ENOSPC;
275                 return -1;
276         }
277
278         /* we have to load the existing stream, then modify, then save */
279         status = pvfs_xattr_load(pvfs, h, h->name->full_name, h->fd, XATTR_DOSSTREAM_PREFIX,
280                                  h->name->stream_name, offset+count, &blob);
281         if (!NT_STATUS_IS_OK(status)) {
282                 blob = data_blob(NULL, 0);
283         }
284         if (count+offset > blob.length) {
285                 blob.data = talloc_realloc(blob.data, blob.data, uint8_t, count+offset);
286                 if (blob.data == NULL) {
287                         errno = ENOMEM;
288                         return -1;
289                 }
290                 if (offset > blob.length) {
291                         memset(blob.data+blob.length, 0, offset - blob.length);
292                 }
293                 blob.length = count+offset;
294         }
295         memcpy(blob.data + offset, data, count);
296
297         status = pvfs_xattr_save(pvfs, h->name->full_name, h->fd, XATTR_DOSSTREAM_PREFIX,
298                                  h->name->stream_name, &blob);
299         if (!NT_STATUS_IS_OK(status)) {
300                 data_blob_free(&blob);
301                 /* getting this error mapping right is probably
302                    not worth it */
303                 errno = ENOSPC;
304                 return -1;
305         }
306
307         status = pvfs_stream_update_size(pvfs, h->name, h->fd, blob.length);
308
309         data_blob_free(&blob);
310
311         if (!NT_STATUS_IS_OK(status)) {
312                 errno = EIO;
313                 return -1;
314         }
315
316         return count;
317 }
318
319 /*
320   the equvalent of truncate() on a stream
321 */
322 NTSTATUS pvfs_stream_truncate(struct pvfs_state *pvfs,
323                               struct pvfs_filename *name, int fd, off_t length)
324 {
325         NTSTATUS status;
326         DATA_BLOB blob;
327
328         if (length > XATTR_MAX_STREAM_SIZE) {
329                 return NT_STATUS_DISK_FULL;
330         }
331
332         /* we have to load the existing stream, then modify, then save */
333         status = pvfs_xattr_load(pvfs, name, name->full_name, fd, XATTR_DOSSTREAM_PREFIX,
334                                  name->stream_name, length, &blob);
335         if (!NT_STATUS_IS_OK(status)) {
336                 return status;
337         }
338         if (length <= blob.length) {
339                 blob.length = length;
340         } else if (length > blob.length) {
341                 blob.data = talloc_realloc(blob.data, blob.data, uint8_t, length);
342                 if (blob.data == NULL) {
343                         return NT_STATUS_NO_MEMORY;
344                 }
345                 memset(blob.data+blob.length, 0, length - blob.length);
346                 blob.length = length;
347         }
348
349         status = pvfs_xattr_save(pvfs, name->full_name, fd, XATTR_DOSSTREAM_PREFIX,
350                                  name->stream_name, &blob);
351         data_blob_free(&blob);
352
353         if (NT_STATUS_IS_OK(status)) {
354                 status = pvfs_stream_update_size(pvfs, name, fd, blob.length);
355         }
356
357         return status;
358 }