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"
28 return the list of file streams for RAW_FILEINFO_STREAM_INFORMATION
30 NTSTATUS pvfs_stream_information(struct pvfs_state *pvfs,
32 struct pvfs_filename *name, int fd,
33 struct stream_information *info)
35 struct xattr_DosStreams *streams;
39 streams = talloc(mem_ctx, struct xattr_DosStreams);
40 if (streams == NULL) {
41 return NT_STATUS_NO_MEMORY;
44 status = pvfs_streams_load(pvfs, name, fd, streams);
45 if (!NT_STATUS_IS_OK(status)) {
46 ZERO_STRUCTP(streams);
49 info->num_streams = streams->num_streams+1;
50 info->streams = talloc_array(mem_ctx, struct stream_struct, info->num_streams);
52 return NT_STATUS_NO_MEMORY;
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");
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,
64 streams->streams[i].name);
72 fill in the stream information for a name
74 NTSTATUS pvfs_stream_info(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd)
76 struct xattr_DosStreams *streams;
80 /* the NULL stream always exists */
81 if (name->stream_name == NULL) {
82 name->stream_exists = true;
86 streams = talloc(name, struct xattr_DosStreams);
87 if (streams == NULL) {
88 return NT_STATUS_NO_MEMORY;
91 status = pvfs_streams_load(pvfs, name, fd, streams);
92 if (!NT_STATUS_IS_OK(status)) {
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);
108 talloc_free(streams);
110 name->dos.alloc_size = 0;
111 name->st.st_size = 0;
112 name->stream_exists = false;
119 update size information for a stream
121 static NTSTATUS pvfs_stream_update_size(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd,
124 struct xattr_DosStreams *streams;
128 streams = talloc(name, struct xattr_DosStreams);
129 if (streams == NULL) {
130 return NT_STATUS_NO_MEMORY;
133 status = pvfs_streams_load(pvfs, name, fd, streams);
134 if (!NT_STATUS_IS_OK(status)) {
135 ZERO_STRUCTP(streams);
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) {
142 s->alloc_size = pvfs_round_alloc_size(pvfs, size);
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;
156 streams->num_streams++;
157 s = &streams->streams[i];
159 s->flags = XATTR_STREAM_FLAG_INTERNAL;
161 s->alloc_size = pvfs_round_alloc_size(pvfs, size);
162 s->name = name->stream_name;
165 status = pvfs_streams_save(pvfs, name, fd, streams);
166 talloc_free(streams);
173 create the xattr for a alternate data stream
175 NTSTATUS pvfs_stream_create(struct pvfs_state *pvfs,
176 struct pvfs_filename *name,
180 status = pvfs_xattr_create(pvfs, name->full_name, fd,
181 XATTR_DOSSTREAM_PREFIX, name->stream_name);
182 if (!NT_STATUS_IS_OK(status)) {
185 return pvfs_stream_update_size(pvfs, name, fd, 0);
189 delete the xattr for a alternate data stream
191 NTSTATUS pvfs_stream_delete(struct pvfs_state *pvfs,
192 struct pvfs_filename *name,
196 struct xattr_DosStreams *streams;
199 status = pvfs_xattr_delete(pvfs, name->full_name, fd,
200 XATTR_DOSSTREAM_PREFIX, name->stream_name);
201 if (!NT_STATUS_IS_OK(status)) {
205 streams = talloc(name, struct xattr_DosStreams);
206 if (streams == NULL) {
207 return NT_STATUS_NO_MEMORY;
210 status = pvfs_streams_load(pvfs, name, fd, streams);
211 if (!NT_STATUS_IS_OK(status)) {
212 talloc_free(streams);
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--;
225 status = pvfs_streams_save(pvfs, name, fd, streams);
226 talloc_free(streams);
232 the equvalent of pread() on a stream
234 ssize_t pvfs_stream_read(struct pvfs_state *pvfs,
235 struct pvfs_file_handle *h, void *data, size_t count, off_t offset)
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)) {
248 if (offset >= blob.length) {
249 data_blob_free(&blob);
252 if (count > blob.length - offset) {
253 count = blob.length - offset;
255 memcpy(data, blob.data + offset, count);
256 data_blob_free(&blob);
262 the equvalent of pwrite() on a stream
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)
272 if (offset > XATTR_MAX_STREAM_SIZE) {
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);
283 if (count+offset > blob.length) {
284 blob.data = talloc_realloc(blob.data, blob.data, uint8_t, count+offset);
285 if (blob.data == NULL) {
289 if (offset > blob.length) {
290 memset(blob.data+blob.length, 0, offset - blob.length);
292 blob.length = count+offset;
294 memcpy(blob.data + offset, data, count);
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
306 status = pvfs_stream_update_size(pvfs, h->name, h->fd, blob.length);
308 data_blob_free(&blob);
310 if (!NT_STATUS_IS_OK(status)) {
319 the equvalent of truncate() on a stream
321 NTSTATUS pvfs_stream_truncate(struct pvfs_state *pvfs,
322 struct pvfs_filename *name, int fd, off_t length)
327 if (length > XATTR_MAX_STREAM_SIZE) {
328 return NT_STATUS_DISK_FULL;
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)) {
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;
344 memset(blob.data+blob.length, 0, length - blob.length);
345 blob.length = length;
348 status = pvfs_xattr_save(pvfs, name->full_name, fd, XATTR_DOSSTREAM_PREFIX,
349 name->stream_name, &blob);
350 data_blob_free(&blob);
352 if (NT_STATUS_IS_OK(status)) {
353 status = pvfs_stream_update_size(pvfs, name, fd, blob.length);