Merge commit 'release-4-0-0alpha1' into v4-0-test
[ira/wip.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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "vfs_posix.h"
24 #include "librpc/gen_ndr/xattr.h"
25
26
27 /*
28   return the list of file streams for RAW_FILEINFO_STREAM_INFORMATION
29 */
30 NTSTATUS pvfs_stream_information(struct pvfs_state *pvfs, 
31                                  TALLOC_CTX *mem_ctx,
32                                  struct pvfs_filename *name, int fd, 
33                                  struct stream_information *info)
34 {
35         struct xattr_DosStreams *streams;
36         int i;
37         NTSTATUS status;
38
39         streams = talloc(mem_ctx, struct xattr_DosStreams);
40         if (streams == NULL) {
41                 return NT_STATUS_NO_MEMORY;
42         }
43
44         status = pvfs_streams_load(pvfs, name, fd, streams);
45         if (!NT_STATUS_IS_OK(status)) {
46                 ZERO_STRUCTP(streams);
47         }
48
49         info->num_streams = streams->num_streams+1;
50         info->streams = talloc_array(mem_ctx, struct stream_struct, info->num_streams);
51         if (!info->streams) {
52                 return NT_STATUS_NO_MEMORY;
53         }
54
55         info->streams[0].size          = name->st.st_size;
56         info->streams[0].alloc_size    = name->dos.alloc_size;
57         info->streams[0].stream_name.s = talloc_strdup(info->streams, "::$DATA");
58
59         for (i=0;i<streams->num_streams;i++) {
60                 info->streams[i+1].size          = streams->streams[i].size;
61                 info->streams[i+1].alloc_size    = streams->streams[i].alloc_size;
62                 info->streams[i+1].stream_name.s = talloc_asprintf(streams->streams, 
63                                                                    ":%s:$DATA",
64                                                                    streams->streams[i].name);
65         }
66
67         return NT_STATUS_OK;
68 }
69
70
71 /*
72   fill in the stream information for a name
73 */
74 NTSTATUS pvfs_stream_info(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd)
75 {
76         struct xattr_DosStreams *streams;
77         int i;
78         NTSTATUS status;
79
80         /* the NULL stream always exists */
81         if (name->stream_name == NULL) {
82                 name->stream_exists = true;
83                 return NT_STATUS_OK;
84         }
85
86         streams = talloc(name, struct xattr_DosStreams);
87         if (streams == NULL) {
88                 return NT_STATUS_NO_MEMORY;
89         }
90
91         status = pvfs_streams_load(pvfs, name, fd, streams);
92         if (!NT_STATUS_IS_OK(status)) {
93                 talloc_free(streams);
94                 return status;
95         }
96
97         for (i=0;i<streams->num_streams;i++) {
98                 struct xattr_DosStream *s = &streams->streams[i];
99                 if (strcasecmp_m(s->name, name->stream_name) == 0) {
100                         name->dos.alloc_size = pvfs_round_alloc_size(pvfs, s->alloc_size);
101                         name->st.st_size     = s->size;
102                         name->stream_exists = true;
103                         talloc_free(streams);
104                         return NT_STATUS_OK;
105                 }
106         }
107
108         talloc_free(streams);
109
110         name->dos.alloc_size = 0;
111         name->st.st_size     = 0;
112         name->stream_exists = false;
113
114         return NT_STATUS_OK;
115 }
116
117
118 /*
119   update size information for a stream
120 */
121 static NTSTATUS pvfs_stream_update_size(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
122                                         off_t size)
123 {
124         struct xattr_DosStreams *streams;
125         int i;
126         NTSTATUS status;
127
128         streams = talloc(name, struct xattr_DosStreams);
129         if (streams == NULL) {
130                 return NT_STATUS_NO_MEMORY;
131         }
132
133         status = pvfs_streams_load(pvfs, name, fd, streams);
134         if (!NT_STATUS_IS_OK(status)) {
135                 ZERO_STRUCTP(streams);
136         }
137
138         for (i=0;i<streams->num_streams;i++) {
139                 struct xattr_DosStream *s = &streams->streams[i];
140                 if (strcasecmp_m(s->name, name->stream_name) == 0) {
141                         s->size       = size;
142                         s->alloc_size = pvfs_round_alloc_size(pvfs, size);
143                         break;
144                 }
145         }
146
147         if (i == streams->num_streams) {
148                 struct xattr_DosStream *s;
149                 streams->streams = talloc_realloc(streams, streams->streams, 
150                                                     struct xattr_DosStream,
151                                                     streams->num_streams+1);
152                 if (streams->streams == NULL) {
153                         talloc_free(streams);
154                         return NT_STATUS_NO_MEMORY;
155                 }
156                 streams->num_streams++;
157                 s = &streams->streams[i];
158                 
159                 s->flags      = XATTR_STREAM_FLAG_INTERNAL;
160                 s->size       = size;
161                 s->alloc_size = pvfs_round_alloc_size(pvfs, size);
162                 s->name       = name->stream_name;
163         }
164
165         status = pvfs_streams_save(pvfs, name, fd, streams);
166         talloc_free(streams);
167
168         return status;
169 }
170
171
172 /*
173   create the xattr for a alternate data stream
174 */
175 NTSTATUS pvfs_stream_create(struct pvfs_state *pvfs, 
176                             struct pvfs_filename *name, 
177                             int fd)
178 {
179         NTSTATUS status;
180         status = pvfs_xattr_create(pvfs, name->full_name, fd, 
181                                    XATTR_DOSSTREAM_PREFIX, name->stream_name);
182         if (!NT_STATUS_IS_OK(status)) {
183                 return status;
184         }
185         return pvfs_stream_update_size(pvfs, name, fd, 0);
186 }
187
188 /*
189   delete the xattr for a alternate data stream
190 */
191 NTSTATUS pvfs_stream_delete(struct pvfs_state *pvfs, 
192                             struct pvfs_filename *name, 
193                             int fd)
194 {
195         NTSTATUS status;
196         struct xattr_DosStreams *streams;
197         int i;
198
199         status = pvfs_xattr_delete(pvfs, name->full_name, fd, 
200                                    XATTR_DOSSTREAM_PREFIX, name->stream_name);
201         if (!NT_STATUS_IS_OK(status)) {
202                 return status;
203         }
204
205         streams = talloc(name, struct xattr_DosStreams);
206         if (streams == NULL) {
207                 return NT_STATUS_NO_MEMORY;
208         }
209
210         status = pvfs_streams_load(pvfs, name, fd, streams);
211         if (!NT_STATUS_IS_OK(status)) {
212                 talloc_free(streams);
213                 return status;
214         }
215
216         for (i=0;i<streams->num_streams;i++) {
217                 struct xattr_DosStream *s = &streams->streams[i];
218                 if (strcasecmp_m(s->name, name->stream_name) == 0) {
219                         memmove(s, s+1, (streams->num_streams - (i+1)) * sizeof(*s));
220                         streams->num_streams--;
221                         break;
222                 }
223         }
224
225         status = pvfs_streams_save(pvfs, name, fd, streams);
226         talloc_free(streams);
227
228         return status;
229 }
230
231 /*
232   the equvalent of pread() on a stream
233 */
234 ssize_t pvfs_stream_read(struct pvfs_state *pvfs,
235                          struct pvfs_file_handle *h, void *data, size_t count, off_t offset)
236 {
237         NTSTATUS status;
238         DATA_BLOB blob;
239         if (count == 0) {
240                 return 0;
241         }
242         status = pvfs_xattr_load(pvfs, h, h->name->full_name, h->fd, XATTR_DOSSTREAM_PREFIX,
243                                  h->name->stream_name, offset+count, &blob);
244         if (!NT_STATUS_IS_OK(status)) {
245                 errno = EIO;
246                 return -1;
247         }
248         if (offset >= blob.length) {
249                 data_blob_free(&blob);
250                 return 0;
251         }
252         if (count > blob.length - offset) {
253                 count = blob.length - offset;
254         }
255         memcpy(data, blob.data + offset, count);
256         data_blob_free(&blob);
257         return count;
258 }
259
260
261 /*
262   the equvalent of pwrite() on a stream
263 */
264 ssize_t pvfs_stream_write(struct pvfs_state *pvfs,
265                           struct pvfs_file_handle *h, const void *data, size_t count, off_t offset)
266 {
267         NTSTATUS status;
268         DATA_BLOB blob;
269         if (count == 0) {
270                 return 0;
271         }
272         if (offset > XATTR_MAX_STREAM_SIZE) {
273                 errno = ENOSPC;
274                 return -1;
275         }
276
277         /* we have to load the existing stream, then modify, then save */
278         status = pvfs_xattr_load(pvfs, h, h->name->full_name, h->fd, XATTR_DOSSTREAM_PREFIX,
279                                  h->name->stream_name, offset+count, &blob);
280         if (!NT_STATUS_IS_OK(status)) {
281                 blob = data_blob(NULL, 0);
282         }
283         if (count+offset > blob.length) {
284                 blob.data = talloc_realloc(blob.data, blob.data, uint8_t, count+offset);
285                 if (blob.data == NULL) {
286                         errno = ENOMEM;
287                         return -1;
288                 }
289                 if (offset > blob.length) {
290                         memset(blob.data+blob.length, 0, offset - blob.length);
291                 }
292                 blob.length = count+offset;
293         }
294         memcpy(blob.data + offset, data, count);
295
296         status = pvfs_xattr_save(pvfs, h->name->full_name, h->fd, XATTR_DOSSTREAM_PREFIX,
297                                  h->name->stream_name, &blob);
298         if (!NT_STATUS_IS_OK(status)) {
299                 data_blob_free(&blob);
300                 /* getting this error mapping right is probably
301                    not worth it */
302                 errno = ENOSPC;
303                 return -1;
304         }
305
306         status = pvfs_stream_update_size(pvfs, h->name, h->fd, blob.length);
307
308         data_blob_free(&blob);
309
310         if (!NT_STATUS_IS_OK(status)) {
311                 errno = EIO;
312                 return -1;
313         }
314
315         return count;
316 }
317
318 /*
319   the equvalent of truncate() on a stream
320 */
321 NTSTATUS pvfs_stream_truncate(struct pvfs_state *pvfs,
322                               struct pvfs_filename *name, int fd, off_t length)
323 {
324         NTSTATUS status;
325         DATA_BLOB blob;
326
327         if (length > XATTR_MAX_STREAM_SIZE) {
328                 return NT_STATUS_DISK_FULL;
329         }
330
331         /* we have to load the existing stream, then modify, then save */
332         status = pvfs_xattr_load(pvfs, name, name->full_name, fd, XATTR_DOSSTREAM_PREFIX,
333                                  name->stream_name, length, &blob);
334         if (!NT_STATUS_IS_OK(status)) {
335                 return status;
336         }
337         if (length <= blob.length) {
338                 blob.length = length;
339         } else if (length > blob.length) {
340                 blob.data = talloc_realloc(blob.data, blob.data, uint8_t, length);
341                 if (blob.data == NULL) {
342                         return NT_STATUS_NO_MEMORY;
343                 }
344                 memset(blob.data+blob.length, 0, length - blob.length);
345                 blob.length = length;
346         }
347
348         status = pvfs_xattr_save(pvfs, name->full_name, fd, XATTR_DOSSTREAM_PREFIX,
349                                  name->stream_name, &blob);
350         data_blob_free(&blob);
351
352         if (NT_STATUS_IS_OK(status)) {
353                 status = pvfs_stream_update_size(pvfs, name, fd, blob.length);
354         }
355
356         return status;
357 }