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 "vfs_posix.h"
25 #include "librpc/gen_ndr/ndr_xattr.h"
29 return the list of file streams for RAW_FILEINFO_STREAM_INFORMATION
31 NTSTATUS pvfs_stream_information(struct pvfs_state *pvfs,
33 struct pvfs_filename *name, int fd,
34 struct stream_information *info)
36 struct xattr_DosStreams *streams;
40 streams = talloc(mem_ctx, struct xattr_DosStreams);
41 if (streams == NULL) {
42 return NT_STATUS_NO_MEMORY;
45 status = pvfs_streams_load(pvfs, name, fd, streams);
46 if (!NT_STATUS_IS_OK(status)) {
47 ZERO_STRUCTP(streams);
50 info->num_streams = streams->num_streams+1;
51 info->streams = talloc_array(mem_ctx, struct stream_struct, info->num_streams);
53 return NT_STATUS_NO_MEMORY;
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");
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,
65 streams->streams[i].name);
73 fill in the stream information for a name
75 NTSTATUS pvfs_stream_info(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd)
77 struct xattr_DosStreams *streams;
81 /* the NULL stream always exists */
82 if (name->stream_name == NULL) {
83 name->stream_exists = True;
87 streams = talloc(name, struct xattr_DosStreams);
88 if (streams == NULL) {
89 return NT_STATUS_NO_MEMORY;
92 status = pvfs_streams_load(pvfs, name, fd, streams);
93 if (!NT_STATUS_IS_OK(status)) {
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);
109 talloc_free(streams);
111 name->dos.alloc_size = 0;
112 name->st.st_size = 0;
113 name->stream_exists = False;
120 update size information for a stream
122 static NTSTATUS pvfs_stream_update_size(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
125 struct xattr_DosStreams *streams;
129 streams = talloc(name, struct xattr_DosStreams);
130 if (streams == NULL) {
131 return NT_STATUS_NO_MEMORY;
134 status = pvfs_streams_load(pvfs, name, fd, streams);
135 if (!NT_STATUS_IS_OK(status)) {
136 ZERO_STRUCTP(streams);
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) {
143 s->alloc_size = pvfs_round_alloc_size(pvfs, size);
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;
157 streams->num_streams++;
158 s = &streams->streams[i];
160 s->flags = XATTR_STREAM_FLAG_INTERNAL;
162 s->alloc_size = pvfs_round_alloc_size(pvfs, size);
163 s->name = name->stream_name;
166 status = pvfs_streams_save(pvfs, name, fd, streams);
167 talloc_free(streams);
174 create the xattr for a alternate data stream
176 NTSTATUS pvfs_stream_create(struct pvfs_state *pvfs,
177 struct pvfs_filename *name,
181 status = pvfs_xattr_create(pvfs, name->full_name, fd,
182 XATTR_DOSSTREAM_PREFIX, name->stream_name);
183 if (!NT_STATUS_IS_OK(status)) {
186 return pvfs_stream_update_size(pvfs, name, fd, 0);
190 delete the xattr for a alternate data stream
192 NTSTATUS pvfs_stream_delete(struct pvfs_state *pvfs,
193 struct pvfs_filename *name,
197 struct xattr_DosStreams *streams;
200 status = pvfs_xattr_delete(pvfs, name->full_name, fd,
201 XATTR_DOSSTREAM_PREFIX, name->stream_name);
202 if (!NT_STATUS_IS_OK(status)) {
206 streams = talloc(name, struct xattr_DosStreams);
207 if (streams == NULL) {
208 return NT_STATUS_NO_MEMORY;
211 status = pvfs_streams_load(pvfs, name, fd, streams);
212 if (!NT_STATUS_IS_OK(status)) {
213 talloc_free(streams);
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--;
226 status = pvfs_streams_save(pvfs, name, fd, streams);
227 talloc_free(streams);
233 the equvalent of pread() on a stream
235 ssize_t pvfs_stream_read(struct pvfs_state *pvfs,
236 struct pvfs_file_handle *h, void *data, size_t count, off_t offset)
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)) {
249 if (offset >= blob.length) {
250 data_blob_free(&blob);
253 if (count > blob.length - offset) {
254 count = blob.length - offset;
256 memcpy(data, blob.data + offset, count);
257 data_blob_free(&blob);
263 the equvalent of pwrite() on a stream
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)
273 if (offset > XATTR_MAX_STREAM_SIZE) {
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);
284 if (count+offset > blob.length) {
285 blob.data = talloc_realloc(blob.data, blob.data, uint8_t, count+offset);
286 if (blob.data == NULL) {
290 if (offset > blob.length) {
291 memset(blob.data+blob.length, 0, offset - blob.length);
293 blob.length = count+offset;
295 memcpy(blob.data + offset, data, count);
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
307 status = pvfs_stream_update_size(pvfs, h->name, h->fd, blob.length);
309 data_blob_free(&blob);
311 if (!NT_STATUS_IS_OK(status)) {
320 the equvalent of truncate() on a stream
322 NTSTATUS pvfs_stream_truncate(struct pvfs_state *pvfs,
323 struct pvfs_filename *name, int fd, off_t length)
328 if (length > XATTR_MAX_STREAM_SIZE) {
329 return NT_STATUS_DISK_FULL;
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)) {
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;
345 memset(blob.data+blob.length, 0, length - blob.length);
346 blob.length = length;
349 status = pvfs_xattr_save(pvfs, name->full_name, fd, XATTR_DOSSTREAM_PREFIX,
350 name->stream_name, &blob);
351 data_blob_free(&blob);
353 if (NT_STATUS_IS_OK(status)) {
354 status = pvfs_stream_update_size(pvfs, name, fd, blob.length);