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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "system/filesys.h"
25 #include "vfs_posix.h"
26 #include "librpc/gen_ndr/ndr_xattr.h"
30 return the list of file streams for RAW_FILEINFO_STREAM_INFORMATION
32 NTSTATUS pvfs_stream_information(struct pvfs_state *pvfs,
34 struct pvfs_filename *name, int fd,
35 struct stream_information *info)
37 struct xattr_DosStreams *streams;
41 streams = talloc_p(mem_ctx, struct xattr_DosStreams);
42 if (streams == NULL) {
43 return NT_STATUS_NO_MEMORY;
46 status = pvfs_streams_load(pvfs, name, fd, streams);
47 if (!NT_STATUS_IS_OK(status)) {
48 ZERO_STRUCTP(streams);
51 info->num_streams = streams->num_streams+1;
52 info->streams = talloc_array_p(mem_ctx, struct stream_struct, info->num_streams);
54 return NT_STATUS_NO_MEMORY;
57 info->streams[0].size = name->st.st_size;
58 info->streams[0].alloc_size = name->dos.alloc_size;
59 info->streams[0].stream_name.s = talloc_strdup(info->streams, "::$DATA");
61 for (i=0;i<streams->num_streams;i++) {
62 info->streams[i+1].size = streams->streams[i].size;
63 info->streams[i+1].alloc_size = streams->streams[i].alloc_size;
64 info->streams[i+1].stream_name.s = talloc_asprintf(streams->streams,
66 streams->streams[i].name);
74 fill in the stream information for a name
76 NTSTATUS pvfs_stream_info(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd)
78 struct xattr_DosStreams *streams;
82 /* the NULL stream always exists */
83 if (name->stream_name == NULL) {
84 name->stream_exists = True;
88 streams = talloc_p(name, struct xattr_DosStreams);
89 if (streams == NULL) {
90 return NT_STATUS_NO_MEMORY;
93 status = pvfs_streams_load(pvfs, name, fd, streams);
94 if (!NT_STATUS_IS_OK(status)) {
99 for (i=0;i<streams->num_streams;i++) {
100 struct xattr_DosStream *s = &streams->streams[i];
101 if (StrCaseCmp(s->name, name->stream_name) == 0) {
102 name->dos.alloc_size = pvfs_round_alloc_size(pvfs, s->alloc_size);
103 name->st.st_size = s->size;
104 name->stream_exists = True;
105 talloc_free(streams);
110 talloc_free(streams);
112 name->dos.alloc_size = 0;
113 name->st.st_size = 0;
114 name->stream_exists = False;
121 update size information for a stream
123 static NTSTATUS pvfs_stream_update_size(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
126 struct xattr_DosStreams *streams;
130 streams = talloc_p(name, struct xattr_DosStreams);
131 if (streams == NULL) {
132 return NT_STATUS_NO_MEMORY;
135 status = pvfs_streams_load(pvfs, name, fd, streams);
136 if (!NT_STATUS_IS_OK(status)) {
137 ZERO_STRUCTP(streams);
140 for (i=0;i<streams->num_streams;i++) {
141 struct xattr_DosStream *s = &streams->streams[i];
142 if (StrCaseCmp(s->name, name->stream_name) == 0) {
144 s->alloc_size = pvfs_round_alloc_size(pvfs, size);
149 if (i == streams->num_streams) {
150 struct xattr_DosStream *s;
151 streams->streams = talloc_realloc_p(streams, streams->streams,
152 struct xattr_DosStream,
153 streams->num_streams+1);
154 if (streams->streams == NULL) {
155 talloc_free(streams);
156 return NT_STATUS_NO_MEMORY;
158 streams->num_streams++;
159 s = &streams->streams[i];
161 s->flags = XATTR_STREAM_FLAG_INTERNAL;
163 s->alloc_size = pvfs_round_alloc_size(pvfs, size);
164 s->name = name->stream_name;
167 status = pvfs_streams_save(pvfs, name, fd, streams);
168 talloc_free(streams);
175 create the xattr for a alternate data stream
177 NTSTATUS pvfs_stream_create(struct pvfs_state *pvfs,
178 struct pvfs_filename *name,
182 status = pvfs_xattr_create(pvfs, name->full_name, fd,
183 XATTR_DOSSTREAM_PREFIX, name->stream_name);
184 if (!NT_STATUS_IS_OK(status)) {
187 return pvfs_stream_update_size(pvfs, name, fd, 0);
191 delete the xattr for a alternate data stream
193 NTSTATUS pvfs_stream_delete(struct pvfs_state *pvfs,
194 struct pvfs_filename *name,
198 struct xattr_DosStreams *streams;
201 status = pvfs_xattr_delete(pvfs, name->full_name, fd,
202 XATTR_DOSSTREAM_PREFIX, name->stream_name);
203 if (!NT_STATUS_IS_OK(status)) {
207 streams = talloc_p(name, struct xattr_DosStreams);
208 if (streams == NULL) {
209 return NT_STATUS_NO_MEMORY;
212 status = pvfs_streams_load(pvfs, name, fd, streams);
213 if (!NT_STATUS_IS_OK(status)) {
214 talloc_free(streams);
218 for (i=0;i<streams->num_streams;i++) {
219 struct xattr_DosStream *s = &streams->streams[i];
220 if (StrCaseCmp(s->name, name->stream_name) == 0) {
221 memmove(s, s+1, (streams->num_streams - (i+1)) * sizeof(*s));
222 streams->num_streams--;
227 status = pvfs_streams_save(pvfs, name, fd, streams);
228 talloc_free(streams);
234 the equvalent of pread() on a stream
236 ssize_t pvfs_stream_read(struct pvfs_state *pvfs,
237 struct pvfs_file_handle *h, void *data, size_t count, off_t offset)
244 status = pvfs_xattr_load(pvfs, h, h->name->full_name, h->fd, XATTR_DOSSTREAM_PREFIX,
245 h->name->stream_name, offset+count, &blob);
246 if (!NT_STATUS_IS_OK(status)) {
250 if (offset >= blob.length) {
251 data_blob_free(&blob);
254 if (count > blob.length - offset) {
255 count = blob.length - offset;
257 memcpy(data, blob.data + offset, count);
258 data_blob_free(&blob);
264 the equvalent of pwrite() on a stream
266 ssize_t pvfs_stream_write(struct pvfs_state *pvfs,
267 struct pvfs_file_handle *h, const void *data, size_t count, off_t offset)
274 if (offset > XATTR_MAX_STREAM_SIZE) {
279 /* we have to load the existing stream, then modify, then save */
280 status = pvfs_xattr_load(pvfs, h, h->name->full_name, h->fd, XATTR_DOSSTREAM_PREFIX,
281 h->name->stream_name, offset+count, &blob);
282 if (!NT_STATUS_IS_OK(status)) {
283 blob = data_blob(NULL, 0);
285 if (count+offset > blob.length) {
286 blob.data = talloc_realloc(blob.data, blob.data, uint8_t, count+offset);
287 if (blob.data == NULL) {
291 if (offset > blob.length) {
292 memset(blob.data+blob.length, 0, offset - blob.length);
294 blob.length = count+offset;
296 memcpy(blob.data + offset, data, count);
298 status = pvfs_xattr_save(pvfs, h->name->full_name, h->fd, XATTR_DOSSTREAM_PREFIX,
299 h->name->stream_name, &blob);
300 if (!NT_STATUS_IS_OK(status)) {
301 data_blob_free(&blob);
302 /* getting this error mapping right is probably
308 status = pvfs_stream_update_size(pvfs, h->name, h->fd, blob.length);
310 data_blob_free(&blob);
312 if (!NT_STATUS_IS_OK(status)) {
321 the equvalent of truncate() on a stream
323 NTSTATUS pvfs_stream_truncate(struct pvfs_state *pvfs,
324 struct pvfs_filename *name, int fd, off_t length)
329 if (length > XATTR_MAX_STREAM_SIZE) {
330 return NT_STATUS_DISK_FULL;
333 /* we have to load the existing stream, then modify, then save */
334 status = pvfs_xattr_load(pvfs, name, name->full_name, fd, XATTR_DOSSTREAM_PREFIX,
335 name->stream_name, length, &blob);
336 if (!NT_STATUS_IS_OK(status)) {
339 if (length <= blob.length) {
340 blob.length = length;
341 } else if (length > blob.length) {
342 blob.data = talloc_realloc(blob.data, blob.data, uint8_t, length);
343 if (blob.data == NULL) {
344 return NT_STATUS_NO_MEMORY;
346 memset(blob.data+blob.length, 0, length - blob.length);
347 blob.length = length;
350 status = pvfs_xattr_save(pvfs, name->full_name, fd, XATTR_DOSSTREAM_PREFIX,
351 name->stream_name, &blob);
352 data_blob_free(&blob);
354 if (NT_STATUS_IS_OK(status)) {
355 status = pvfs_stream_update_size(pvfs, name, fd, blob.length);