2 Unix SMB/CIFS implementation.
4 POSIX NTVFS backend - alternate data streams
6 Copyright (C) Andrew Tridgell 2004
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.
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.
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/>.
23 #include "vfs_posix.h"
24 #include "librpc/gen_ndr/xattr.h"
27 normalise a stream name, removing a :$DATA suffix if there is one
28 Note: this returns the existing pointer to the name if the name does
31 static const char *stream_name_normalise(TALLOC_CTX *ctx, const char *name)
33 const char *c = strchr_m(name, ':');
34 if (c == NULL || strcasecmp_m(c, ":$DATA") != 0) {
37 return talloc_strndup(ctx, name, c-name);
41 compare two stream names, taking account of the default $DATA extension
43 static int stream_name_cmp(const char *name1, const char *name2)
47 c1 = strchr_m(name1, ':');
48 c2 = strchr_m(name2, ':');
50 /* check the first part is the same */
51 l1 = c1?(c1 - name1):strlen(name1);
52 l2 = c2?(c2 - name2):strlen(name2);
56 ret = strncasecmp_m(name1, name2, l1);
61 /* the first parts are the same, check the suffix */
63 return strcasecmp_m(c1, c2);
67 return strcasecmp_m(c1, ":$DATA");
70 return strcasecmp_m(c2, ":$DATA");
73 /* neither names have a suffix */
79 return the list of file streams for RAW_FILEINFO_STREAM_INFORMATION
81 NTSTATUS pvfs_stream_information(struct pvfs_state *pvfs,
83 struct pvfs_filename *name, int fd,
84 struct stream_information *info)
86 struct xattr_DosStreams *streams;
90 /* directories don't have streams */
91 if (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) {
92 info->num_streams = 0;
97 streams = talloc(mem_ctx, struct xattr_DosStreams);
98 if (streams == NULL) {
99 return NT_STATUS_NO_MEMORY;
102 status = pvfs_streams_load(pvfs, name, fd, streams);
103 if (!NT_STATUS_IS_OK(status)) {
104 ZERO_STRUCTP(streams);
107 info->num_streams = streams->num_streams+1;
108 info->streams = talloc_array(mem_ctx, struct stream_struct, info->num_streams);
109 if (!info->streams) {
110 return NT_STATUS_NO_MEMORY;
113 info->streams[0].size = name->st.st_size;
114 info->streams[0].alloc_size = name->dos.alloc_size;
115 info->streams[0].stream_name.s = talloc_strdup(info->streams, "::$DATA");
117 for (i=0;i<streams->num_streams;i++) {
118 info->streams[i+1].size = streams->streams[i].size;
119 info->streams[i+1].alloc_size = streams->streams[i].alloc_size;
120 if (strchr(streams->streams[i].name, ':') == NULL) {
121 info->streams[i+1].stream_name.s = talloc_asprintf(streams->streams,
123 streams->streams[i].name);
125 info->streams[i+1].stream_name.s = talloc_strdup(streams->streams,
126 streams->streams[i].name);
135 fill in the stream information for a name
137 NTSTATUS pvfs_stream_info(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd)
139 struct xattr_DosStreams *streams;
143 /* the NULL stream always exists */
144 if (name->stream_name == NULL) {
145 name->stream_exists = true;
149 streams = talloc(name, struct xattr_DosStreams);
150 if (streams == NULL) {
151 return NT_STATUS_NO_MEMORY;
154 status = pvfs_streams_load(pvfs, name, fd, streams);
155 if (!NT_STATUS_IS_OK(status)) {
156 talloc_free(streams);
160 for (i=0;i<streams->num_streams;i++) {
161 struct xattr_DosStream *s = &streams->streams[i];
162 if (stream_name_cmp(s->name, name->stream_name) == 0) {
163 name->dos.alloc_size = pvfs_round_alloc_size(pvfs, s->alloc_size);
164 name->st.st_size = s->size;
165 name->stream_exists = true;
166 talloc_free(streams);
171 talloc_free(streams);
173 name->dos.alloc_size = 0;
174 name->st.st_size = 0;
175 name->stream_exists = false;
182 update size information for a stream
184 static NTSTATUS pvfs_stream_update_size(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
187 struct xattr_DosStreams *streams;
191 streams = talloc(name, struct xattr_DosStreams);
192 if (streams == NULL) {
193 return NT_STATUS_NO_MEMORY;
196 status = pvfs_streams_load(pvfs, name, fd, streams);
197 if (!NT_STATUS_IS_OK(status)) {
198 ZERO_STRUCTP(streams);
201 for (i=0;i<streams->num_streams;i++) {
202 struct xattr_DosStream *s = &streams->streams[i];
203 if (stream_name_cmp(s->name, name->stream_name) == 0) {
205 s->alloc_size = pvfs_round_alloc_size(pvfs, size);
210 if (i == streams->num_streams) {
211 struct xattr_DosStream *s;
212 streams->streams = talloc_realloc(streams, streams->streams,
213 struct xattr_DosStream,
214 streams->num_streams+1);
215 if (streams->streams == NULL) {
216 talloc_free(streams);
217 return NT_STATUS_NO_MEMORY;
219 streams->num_streams++;
220 s = &streams->streams[i];
222 s->flags = XATTR_STREAM_FLAG_INTERNAL;
224 s->alloc_size = pvfs_round_alloc_size(pvfs, size);
225 s->name = stream_name_normalise(streams, name->stream_name);
226 if (s->name == NULL) {
227 talloc_free(streams);
228 return NT_STATUS_NO_MEMORY;
232 status = pvfs_streams_save(pvfs, name, fd, streams);
233 talloc_free(streams);
242 NTSTATUS pvfs_stream_rename(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
243 const char *new_name)
245 struct xattr_DosStreams *streams;
246 int i, found_old, found_new;
249 streams = talloc(name, struct xattr_DosStreams);
250 if (streams == NULL) {
251 return NT_STATUS_NO_MEMORY;
254 new_name = stream_name_normalise(streams, new_name);
255 if (new_name == NULL) {
256 return NT_STATUS_NO_MEMORY;
259 status = pvfs_streams_load(pvfs, name, fd, streams);
260 if (!NT_STATUS_IS_OK(status)) {
261 ZERO_STRUCTP(streams);
264 /* the default stream always exists */
265 if (strcmp(new_name, "") == 0 ||
266 strcasecmp_m(new_name, ":$DATA") == 0) {
267 return NT_STATUS_OBJECT_NAME_COLLISION;
270 /* try to find the old/new names in the list */
271 found_old = found_new = -1;
272 for (i=0;i<streams->num_streams;i++) {
273 struct xattr_DosStream *s = &streams->streams[i];
274 if (stream_name_cmp(s->name, new_name) == 0) {
277 if (stream_name_cmp(s->name, name->stream_name) == 0) {
282 if (found_old == -1) {
283 talloc_free(streams);
284 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
287 if (found_new == -1) {
288 /* a simple rename */
289 struct xattr_DosStream *s = &streams->streams[found_old];
292 /* remove the old one and replace with the new one */
293 streams->streams[found_old].name = new_name;
294 memmove(&streams->streams[found_new],
295 &streams->streams[found_new+1],
296 sizeof(streams->streams[0]) *
297 (streams->num_streams - (found_new+1)));
300 status = pvfs_streams_save(pvfs, name, fd, streams);
301 talloc_free(streams);
308 create the xattr for a alternate data stream
310 NTSTATUS pvfs_stream_create(struct pvfs_state *pvfs,
311 struct pvfs_filename *name,
315 status = pvfs_xattr_create(pvfs, name->full_name, fd,
316 XATTR_DOSSTREAM_PREFIX, name->stream_name);
317 if (!NT_STATUS_IS_OK(status)) {
320 return pvfs_stream_update_size(pvfs, name, fd, 0);
324 delete the xattr for a alternate data stream
326 NTSTATUS pvfs_stream_delete(struct pvfs_state *pvfs,
327 struct pvfs_filename *name,
331 struct xattr_DosStreams *streams;
334 status = pvfs_xattr_delete(pvfs, name->full_name, fd,
335 XATTR_DOSSTREAM_PREFIX, name->stream_name);
336 if (!NT_STATUS_IS_OK(status)) {
340 streams = talloc(name, struct xattr_DosStreams);
341 if (streams == NULL) {
342 return NT_STATUS_NO_MEMORY;
345 status = pvfs_streams_load(pvfs, name, fd, streams);
346 if (!NT_STATUS_IS_OK(status)) {
347 talloc_free(streams);
351 for (i=0;i<streams->num_streams;i++) {
352 struct xattr_DosStream *s = &streams->streams[i];
353 if (stream_name_cmp(s->name, name->stream_name) == 0) {
354 memmove(s, s+1, (streams->num_streams - (i+1)) * sizeof(*s));
355 streams->num_streams--;
360 status = pvfs_streams_save(pvfs, name, fd, streams);
361 talloc_free(streams);
367 load a stream into a blob
369 static NTSTATUS pvfs_stream_load(struct pvfs_state *pvfs,
371 struct pvfs_filename *name,
373 size_t estimated_size,
378 status = pvfs_xattr_load(pvfs, mem_ctx, name->full_name, fd,
379 XATTR_DOSSTREAM_PREFIX,
380 name->stream_name, estimated_size, blob);
382 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
383 /* try with a case insensitive match */
384 struct xattr_DosStreams *streams;
387 streams = talloc(mem_ctx, struct xattr_DosStreams);
388 if (streams == NULL) {
389 return NT_STATUS_NO_MEMORY;
392 status = pvfs_streams_load(pvfs, name, fd, streams);
393 if (!NT_STATUS_IS_OK(status)) {
394 talloc_free(streams);
395 return NT_STATUS_NOT_FOUND;
397 for (i=0;i<streams->num_streams;i++) {
398 struct xattr_DosStream *s = &streams->streams[i];
399 if (stream_name_cmp(s->name, name->stream_name) == 0) {
400 status = pvfs_xattr_load(pvfs, mem_ctx, name->full_name, fd,
401 XATTR_DOSSTREAM_PREFIX,
402 s->name, estimated_size, blob);
403 talloc_free(streams);
407 talloc_free(streams);
408 return NT_STATUS_NOT_FOUND;
415 the equvalent of pread() on a stream
417 ssize_t pvfs_stream_read(struct pvfs_state *pvfs,
418 struct pvfs_file_handle *h, void *data, size_t count, off_t offset)
425 status = pvfs_stream_load(pvfs, h, h->name, h->fd, offset+count, &blob);
426 if (!NT_STATUS_IS_OK(status)) {
430 if (offset >= blob.length) {
431 data_blob_free(&blob);
434 if (count > blob.length - offset) {
435 count = blob.length - offset;
437 memcpy(data, blob.data + offset, count);
438 data_blob_free(&blob);
444 the equvalent of pwrite() on a stream
446 ssize_t pvfs_stream_write(struct pvfs_state *pvfs,
447 struct pvfs_file_handle *h, const void *data, size_t count, off_t offset)
455 if (count+offset > XATTR_MAX_STREAM_SIZE) {
456 if (!pvfs->ea_db || count+offset > XATTR_MAX_STREAM_SIZE_TDB) {
462 /* we have to load the existing stream, then modify, then save */
463 status = pvfs_stream_load(pvfs, h, h->name, h->fd, offset+count, &blob);
464 if (!NT_STATUS_IS_OK(status)) {
465 blob = data_blob(NULL, 0);
467 if (count+offset > blob.length) {
468 blob.data = talloc_realloc(blob.data, blob.data, uint8_t, count+offset);
469 if (blob.data == NULL) {
473 if (offset > blob.length) {
474 memset(blob.data+blob.length, 0, offset - blob.length);
476 blob.length = count+offset;
478 memcpy(blob.data + offset, data, count);
480 status = pvfs_xattr_save(pvfs, h->name->full_name, h->fd, XATTR_DOSSTREAM_PREFIX,
481 h->name->stream_name, &blob);
482 if (!NT_STATUS_IS_OK(status)) {
483 data_blob_free(&blob);
484 /* getting this error mapping right is probably
490 status = pvfs_stream_update_size(pvfs, h->name, h->fd, blob.length);
492 data_blob_free(&blob);
494 if (!NT_STATUS_IS_OK(status)) {
503 the equvalent of truncate() on a stream
505 NTSTATUS pvfs_stream_truncate(struct pvfs_state *pvfs,
506 struct pvfs_filename *name, int fd, off_t length)
511 if (length > XATTR_MAX_STREAM_SIZE) {
512 if (!pvfs->ea_db || length > XATTR_MAX_STREAM_SIZE_TDB) {
513 return NT_STATUS_DISK_FULL;
517 /* we have to load the existing stream, then modify, then save */
518 status = pvfs_stream_load(pvfs, name, name, fd, length, &blob);
519 if (!NT_STATUS_IS_OK(status)) {
522 if (length <= blob.length) {
523 blob.length = length;
524 } else if (length > blob.length) {
525 blob.data = talloc_realloc(blob.data, blob.data, uint8_t, length);
526 if (blob.data == NULL) {
527 return NT_STATUS_NO_MEMORY;
529 memset(blob.data+blob.length, 0, length - blob.length);
530 blob.length = length;
533 status = pvfs_xattr_save(pvfs, name->full_name, fd, XATTR_DOSSTREAM_PREFIX,
534 name->stream_name, &blob);
536 if (NT_STATUS_IS_OK(status)) {
537 status = pvfs_stream_update_size(pvfs, name, fd, blob.length);
539 data_blob_free(&blob);