s3:streams_xattr: add support for renaming streams
[Samba/gebeck_regimport.git] / source3 / modules / vfs_streams_xattr.c
blobe96f616e313e785be062ea42765ec7ad3fa8e089
1 /*
2 * Store streams in xattrs
4 * Copyright (C) Volker Lendecke, 2008
6 * Partly based on James Peach's Darwin module, which is
8 * Copyright (C) James Peach 2006-2007
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_VFS
29 struct stream_io {
30 char *base;
31 char *xattr_name;
34 static SMB_INO_T stream_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
36 struct MD5Context ctx;
37 unsigned char hash[16];
38 SMB_INO_T result;
39 char *upper_sname;
41 DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
42 (unsigned long)sbuf->st_dev,
43 (unsigned long)sbuf->st_ino, sname));
45 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
46 SMB_ASSERT(upper_sname != NULL);
48 MD5Init(&ctx);
49 MD5Update(&ctx, (unsigned char *)&(sbuf->st_dev),
50 sizeof(sbuf->st_dev));
51 MD5Update(&ctx, (unsigned char *)&(sbuf->st_ino),
52 sizeof(sbuf->st_ino));
53 MD5Update(&ctx, (unsigned char *)upper_sname,
54 talloc_get_size(upper_sname)-1);
55 MD5Final(hash, &ctx);
57 TALLOC_FREE(upper_sname);
59 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
60 memcpy(&result, hash, sizeof(result));
62 DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result));
64 return result;
67 static ssize_t get_xattr_size(connection_struct *conn,
68 files_struct *fsp,
69 const char *fname,
70 const char *xattr_name)
72 NTSTATUS status;
73 struct ea_struct ea;
74 ssize_t result;
76 status = get_ea_value(talloc_tos(), conn, fsp, fname,
77 xattr_name, &ea);
79 if (!NT_STATUS_IS_OK(status)) {
80 return -1;
83 result = ea.value.length-1;
84 TALLOC_FREE(ea.value.data);
85 return result;
89 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
90 SMB_STRUCT_STAT *sbuf)
92 struct stream_io *io = (struct stream_io *)
93 VFS_FETCH_FSP_EXTENSION(handle, fsp);
95 DEBUG(10, ("streams_xattr_fstat called for %d\n", fsp->fh->fd));
97 if (io == NULL || fsp->base_fsp == NULL) {
98 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
101 if (SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf) == -1) {
102 return -1;
105 sbuf->st_size = get_xattr_size(handle->conn, fsp->base_fsp,
106 io->base, io->xattr_name);
107 if (sbuf->st_size == -1) {
108 return -1;
111 DEBUG(10, ("sbuf->st_size = %d\n", (int)sbuf->st_size));
113 sbuf->st_ino = stream_inode(sbuf, io->xattr_name);
114 sbuf->st_mode &= ~S_IFMT;
115 sbuf->st_mode |= S_IFREG;
116 sbuf->st_blocks = sbuf->st_size % STAT_ST_BLOCKSIZE + 1;
118 return 0;
121 static int streams_xattr_stat(vfs_handle_struct *handle, const char *fname,
122 SMB_STRUCT_STAT *sbuf)
124 NTSTATUS status;
125 char *base = NULL, *sname = NULL;
126 int result = -1;
127 char *xattr_name;
129 if (!is_ntfs_stream_name(fname)) {
130 return SMB_VFS_NEXT_STAT(handle, fname, sbuf);
133 status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
134 if (!NT_STATUS_IS_OK(status)) {
135 errno = EINVAL;
136 return -1;
139 if (sname == NULL){
140 return SMB_VFS_NEXT_STAT(handle, base, sbuf);
143 if (SMB_VFS_STAT(handle->conn, base, sbuf) == -1) {
144 goto fail;
147 xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
148 SAMBA_XATTR_DOSSTREAM_PREFIX, sname);
149 if (xattr_name == NULL) {
150 errno = ENOMEM;
151 goto fail;
154 sbuf->st_size = get_xattr_size(handle->conn, NULL, base, xattr_name);
155 if (sbuf->st_size == -1) {
156 errno = ENOENT;
157 goto fail;
160 sbuf->st_ino = stream_inode(sbuf, xattr_name);
161 sbuf->st_mode &= ~S_IFMT;
162 sbuf->st_mode |= S_IFREG;
163 sbuf->st_blocks = sbuf->st_size % STAT_ST_BLOCKSIZE + 1;
165 result = 0;
166 fail:
167 TALLOC_FREE(base);
168 TALLOC_FREE(sname);
169 return result;
172 static int streams_xattr_lstat(vfs_handle_struct *handle, const char *fname,
173 SMB_STRUCT_STAT *sbuf)
175 NTSTATUS status;
176 char *base, *sname;
177 int result = -1;
178 char *xattr_name;
180 if (!is_ntfs_stream_name(fname)) {
181 return SMB_VFS_NEXT_LSTAT(handle, fname, sbuf);
184 status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
185 if (!NT_STATUS_IS_OK(status)) {
186 errno = EINVAL;
187 goto fail;
190 if (sname == NULL){
191 return SMB_VFS_NEXT_LSTAT(handle, base, sbuf);
194 if (SMB_VFS_LSTAT(handle->conn, base, sbuf) == -1) {
195 goto fail;
198 xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
199 SAMBA_XATTR_DOSSTREAM_PREFIX, sname);
200 if (xattr_name == NULL) {
201 errno = ENOMEM;
202 goto fail;
205 sbuf->st_size = get_xattr_size(handle->conn, NULL, base, xattr_name);
206 if (sbuf->st_size == -1) {
207 errno = ENOENT;
208 goto fail;
211 sbuf->st_ino = stream_inode(sbuf, xattr_name);
212 sbuf->st_mode &= ~S_IFMT;
213 sbuf->st_mode |= S_IFREG;
214 sbuf->st_blocks = sbuf->st_size % STAT_ST_BLOCKSIZE + 1;
216 result = 0;
217 fail:
218 TALLOC_FREE(base);
219 TALLOC_FREE(sname);
220 return result;
223 static int streams_xattr_open(vfs_handle_struct *handle, const char *fname,
224 files_struct *fsp, int flags, mode_t mode)
226 TALLOC_CTX *frame;
227 NTSTATUS status;
228 struct stream_io *sio;
229 char *base, *sname;
230 struct ea_struct ea;
231 char *xattr_name;
232 int baseflags;
233 int hostfd = -1;
235 DEBUG(10, ("streams_xattr_open called for %s\n", fname));
237 if (!is_ntfs_stream_name(fname)) {
238 return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
241 frame = talloc_stackframe();
243 status = split_ntfs_stream_name(talloc_tos(), fname,
244 &base, &sname);
245 if (!NT_STATUS_IS_OK(status)) {
246 errno = EINVAL;
247 goto fail;
250 if (sname == NULL) {
251 hostfd = SMB_VFS_NEXT_OPEN(handle, base, fsp, flags, mode);
252 talloc_free(frame);
253 return hostfd;
256 xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
257 SAMBA_XATTR_DOSSTREAM_PREFIX, sname);
258 if (xattr_name == NULL) {
259 errno = ENOMEM;
260 goto fail;
264 * We use baseflags to turn off nasty side-effects when opening the
265 * underlying file.
267 baseflags = flags;
268 baseflags &= ~O_TRUNC;
269 baseflags &= ~O_EXCL;
270 baseflags &= ~O_CREAT;
272 hostfd = SMB_VFS_OPEN(handle->conn, base, fsp, baseflags, mode);
274 /* It is legit to open a stream on a directory, but the base
275 * fd has to be read-only.
277 if ((hostfd == -1) && (errno == EISDIR)) {
278 baseflags &= ~O_ACCMODE;
279 baseflags |= O_RDONLY;
280 hostfd = SMB_VFS_OPEN(handle->conn, fname, fsp, baseflags,
281 mode);
284 if (hostfd == -1) {
285 goto fail;
288 status = get_ea_value(talloc_tos(), handle->conn, NULL, base,
289 xattr_name, &ea);
291 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
293 if (!NT_STATUS_IS_OK(status)
294 && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
296 * The base file is not there. This is an error even if we got
297 * O_CREAT, the higher levels should have created the base
298 * file for us.
300 DEBUG(10, ("streams_xattr_open: base file %s not around, "
301 "returning ENOENT\n", base));
302 errno = ENOENT;
303 goto fail;
306 if (!NT_STATUS_IS_OK(status)) {
308 * The attribute does not exist
311 if (flags & O_CREAT) {
313 * Darn, xattrs need at least 1 byte
315 char null = '\0';
317 DEBUG(10, ("creating attribute %s on file %s\n",
318 xattr_name, base));
320 if (fsp->base_fsp->fh->fd != -1) {
321 if (SMB_VFS_FSETXATTR(
322 fsp->base_fsp, xattr_name,
323 &null, sizeof(null),
324 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
325 goto fail;
327 } else {
328 if (SMB_VFS_SETXATTR(
329 handle->conn, base, xattr_name,
330 &null, sizeof(null),
331 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
332 goto fail;
338 if (flags & O_TRUNC) {
339 char null = '\0';
340 if (fsp->base_fsp->fh->fd != -1) {
341 if (SMB_VFS_FSETXATTR(
342 fsp->base_fsp, xattr_name,
343 &null, sizeof(null),
344 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
345 goto fail;
347 } else {
348 if (SMB_VFS_SETXATTR(
349 handle->conn, base, xattr_name,
350 &null, sizeof(null),
351 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
352 goto fail;
357 sio = (struct stream_io *)VFS_ADD_FSP_EXTENSION(handle, fsp,
358 struct stream_io);
359 if (sio == NULL) {
360 errno = ENOMEM;
361 goto fail;
364 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
365 xattr_name);
366 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
367 base);
369 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
370 errno = ENOMEM;
371 goto fail;
374 TALLOC_FREE(frame);
375 return hostfd;
377 fail:
378 if (hostfd >= 0) {
380 * BUGBUGBUG -- we would need to call fd_close_posix here, but
381 * we don't have a full fsp yet
383 SMB_VFS_CLOSE(fsp);
386 TALLOC_FREE(frame);
387 return -1;
390 static int streams_xattr_unlink(vfs_handle_struct *handle, const char *fname)
392 NTSTATUS status;
393 char *base = NULL;
394 char *sname = NULL;
395 int ret = -1;
396 char *xattr_name;
398 if (!is_ntfs_stream_name(fname)) {
399 return SMB_VFS_NEXT_UNLINK(handle, fname);
402 status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
403 if (!NT_STATUS_IS_OK(status)) {
404 errno = EINVAL;
405 goto fail;
408 if (sname == NULL){
409 return SMB_VFS_NEXT_UNLINK(handle, base);
412 xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
413 SAMBA_XATTR_DOSSTREAM_PREFIX, sname);
414 if (xattr_name == NULL) {
415 errno = ENOMEM;
416 goto fail;
419 ret = SMB_VFS_REMOVEXATTR(handle->conn, base, xattr_name);
421 if ((ret == -1) && (errno == ENOATTR)) {
422 errno = ENOENT;
423 goto fail;
426 ret = 0;
428 fail:
429 TALLOC_FREE(base);
430 TALLOC_FREE(sname);
431 return ret;
434 static int streams_xattr_rename(vfs_handle_struct *handle,
435 const char *oldname,
436 const char *newname)
438 NTSTATUS status;
439 TALLOC_CTX *frame = NULL;
440 char *obase;
441 char *ostream;
442 char *nbase;
443 char *nstream;
444 const char *base;
445 int ret = -1;
446 char *oxattr_name;
447 char *nxattr_name;
448 bool o_is_stream;
449 bool n_is_stream;
450 ssize_t oret;
451 ssize_t nret;
452 struct ea_struct ea;
454 o_is_stream = is_ntfs_stream_name(oldname);
455 n_is_stream = is_ntfs_stream_name(newname);
457 if (!o_is_stream && !n_is_stream) {
458 return SMB_VFS_NEXT_RENAME(handle, oldname, newname);
461 if (!(o_is_stream && n_is_stream)) {
462 errno = ENOSYS;
463 goto fail;
466 frame = talloc_stackframe();
467 if (!frame) {
468 goto fail;
471 status = split_ntfs_stream_name(talloc_tos(), oldname, &obase, &ostream);
472 if (!NT_STATUS_IS_OK(status)) {
473 errno = EINVAL;
474 goto fail;
477 status = split_ntfs_stream_name(talloc_tos(), newname, &nbase, &nstream);
478 if (!NT_STATUS_IS_OK(status)) {
479 errno = EINVAL;
480 goto fail;
483 /*TODO: maybe call SMB_VFS_NEXT_RENAME() both streams are NULL (::$DATA) */
484 if (ostream == NULL) {
485 errno = ENOSYS;
486 goto fail;
489 if (nstream == NULL) {
490 errno = ENOSYS;
491 goto fail;
494 /* the new base should be empty */
495 if (StrCaseCmp(obase, nbase) != 0) {
496 errno = ENOSYS;
497 goto fail;
500 if (StrCaseCmp(ostream, nstream) == 0) {
501 goto done;
504 base = obase;
506 oxattr_name = talloc_asprintf(talloc_tos(), "%s%s",
507 SAMBA_XATTR_DOSSTREAM_PREFIX, ostream);
508 if (oxattr_name == NULL) {
509 errno = ENOMEM;
510 goto fail;
513 nxattr_name = talloc_asprintf(talloc_tos(), "%s%s",
514 SAMBA_XATTR_DOSSTREAM_PREFIX, nstream);
515 if (nxattr_name == NULL) {
516 errno = ENOMEM;
517 goto fail;
520 /* read the old stream */
521 status = get_ea_value(talloc_tos(), handle->conn, NULL,
522 base, oxattr_name, &ea);
523 if (!NT_STATUS_IS_OK(status)) {
524 errno = ENOENT;
525 goto fail;
528 /* (over)write the new stream */
529 nret = SMB_VFS_SETXATTR(handle->conn, base, nxattr_name,
530 ea.value.data, ea.value.length, 0);
531 if (nret < 0) {
532 if (errno == ENOATTR) {
533 errno = ENOENT;
535 goto fail;
538 /* remove the old stream */
539 oret = SMB_VFS_REMOVEXATTR(handle->conn, base, oxattr_name);
540 if (oret < 0) {
541 if (errno == ENOATTR) {
542 errno = ENOENT;
544 goto fail;
547 done:
548 errno = 0;
549 ret = 0;
550 fail:
551 TALLOC_FREE(frame);
552 return ret;
555 static NTSTATUS walk_xattr_streams(connection_struct *conn, files_struct *fsp,
556 const char *fname,
557 bool (*fn)(struct ea_struct *ea,
558 void *private_data),
559 void *private_data)
561 NTSTATUS status;
562 char **names;
563 size_t i, num_names;
564 size_t prefix_len = strlen(SAMBA_XATTR_DOSSTREAM_PREFIX);
566 status = get_ea_names_from_file(talloc_tos(), conn, fsp, fname,
567 &names, &num_names);
568 if (!NT_STATUS_IS_OK(status)) {
569 return status;
572 for (i=0; i<num_names; i++) {
573 struct ea_struct ea;
575 if (strncmp(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
576 prefix_len) != 0) {
577 continue;
580 status = get_ea_value(names, conn, fsp, fname, names[i], &ea);
581 if (!NT_STATUS_IS_OK(status)) {
582 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
583 names[i], fname, nt_errstr(status)));
584 continue;
587 ea.name = talloc_asprintf(ea.value.data, ":%s",
588 names[i] + prefix_len);
589 if (ea.name == NULL) {
590 DEBUG(0, ("talloc failed\n"));
591 continue;
594 if (!fn(&ea, private_data)) {
595 TALLOC_FREE(ea.value.data);
596 return NT_STATUS_OK;
599 TALLOC_FREE(ea.value.data);
602 TALLOC_FREE(names);
603 return NT_STATUS_OK;
606 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
607 struct stream_struct **streams,
608 const char *name, SMB_OFF_T size,
609 SMB_OFF_T alloc_size)
611 struct stream_struct *tmp;
613 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
614 (*num_streams)+1);
615 if (tmp == NULL) {
616 return false;
619 tmp[*num_streams].name = talloc_strdup(tmp, name);
620 if (tmp[*num_streams].name == NULL) {
621 return false;
624 tmp[*num_streams].size = size;
625 tmp[*num_streams].alloc_size = alloc_size;
627 *streams = tmp;
628 *num_streams += 1;
629 return true;
632 struct streaminfo_state {
633 TALLOC_CTX *mem_ctx;
634 vfs_handle_struct *handle;
635 unsigned int num_streams;
636 struct stream_struct *streams;
637 NTSTATUS status;
640 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
642 struct streaminfo_state *state =
643 (struct streaminfo_state *)private_data;
645 if (!add_one_stream(state->mem_ctx,
646 &state->num_streams, &state->streams,
647 ea->name, ea->value.length-1,
648 smb_roundup(state->handle->conn,
649 ea->value.length-1))) {
650 state->status = NT_STATUS_NO_MEMORY;
651 return false;
654 return true;
657 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
658 struct files_struct *fsp,
659 const char *fname,
660 TALLOC_CTX *mem_ctx,
661 unsigned int *pnum_streams,
662 struct stream_struct **pstreams)
664 SMB_STRUCT_STAT sbuf;
665 int ret;
666 NTSTATUS status;
667 struct streaminfo_state state;
669 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
670 if (is_ntfs_stream_name(fsp->fsp_name)) {
671 return NT_STATUS_INVALID_PARAMETER;
673 ret = SMB_VFS_FSTAT(fsp, &sbuf);
675 else {
676 if (is_ntfs_stream_name(fname)) {
677 return NT_STATUS_INVALID_PARAMETER;
679 ret = SMB_VFS_STAT(handle->conn, fname, &sbuf);
682 if (ret == -1) {
683 return map_nt_error_from_unix(errno);
686 state.streams = NULL;
687 state.num_streams = 0;
689 if (!S_ISDIR(sbuf.st_mode)) {
690 if (!add_one_stream(mem_ctx,
691 &state.num_streams, &state.streams,
692 "::$DATA", sbuf.st_size,
693 get_allocation_size(handle->conn, fsp,
694 &sbuf))) {
695 return NT_STATUS_NO_MEMORY;
699 state.mem_ctx = mem_ctx;
700 state.handle = handle;
701 state.status = NT_STATUS_OK;
703 status = walk_xattr_streams(handle->conn, fsp, fname,
704 collect_one_stream, &state);
706 if (!NT_STATUS_IS_OK(status)) {
707 TALLOC_FREE(state.streams);
708 return status;
711 if (!NT_STATUS_IS_OK(state.status)) {
712 TALLOC_FREE(state.streams);
713 return state.status;
716 *pnum_streams = state.num_streams;
717 *pstreams = state.streams;
718 return NT_STATUS_OK;
721 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle)
723 return SMB_VFS_NEXT_FS_CAPABILITIES(handle) | FILE_NAMED_STREAMS;
726 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
727 files_struct *fsp, const void *data,
728 size_t n, SMB_OFF_T offset)
730 struct stream_io *sio =
731 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
732 struct ea_struct ea;
733 NTSTATUS status;
734 int ret;
736 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
738 if (sio == NULL) {
739 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
742 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
743 sio->base, sio->xattr_name, &ea);
744 if (!NT_STATUS_IS_OK(status)) {
745 return -1;
748 if ((offset + n) > ea.value.length-1) {
749 uint8 *tmp;
751 tmp = TALLOC_REALLOC_ARRAY(talloc_tos(), ea.value.data, uint8,
752 offset + n + 1);
754 if (tmp == NULL) {
755 TALLOC_FREE(ea.value.data);
756 errno = ENOMEM;
757 return -1;
759 ea.value.data = tmp;
760 ea.value.length = offset + n + 1;
761 ea.value.data[offset+n] = 0;
764 memcpy(ea.value.data + offset, data, n);
766 if (fsp->base_fsp->fh->fd != -1) {
767 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
768 sio->xattr_name,
769 ea.value.data, ea.value.length, 0);
770 } else {
771 ret = SMB_VFS_SETXATTR(fsp->conn, fsp->base_fsp->fsp_name,
772 sio->xattr_name,
773 ea.value.data, ea.value.length, 0);
775 TALLOC_FREE(ea.value.data);
777 if (ret == -1) {
778 return -1;
781 return n;
784 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
785 files_struct *fsp, void *data,
786 size_t n, SMB_OFF_T offset)
788 struct stream_io *sio =
789 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
790 struct ea_struct ea;
791 NTSTATUS status;
792 size_t length, overlap;
794 if (sio == NULL) {
795 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
798 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
799 sio->base, sio->xattr_name, &ea);
800 if (!NT_STATUS_IS_OK(status)) {
801 return -1;
804 length = ea.value.length-1;
806 /* Attempt to read past EOF. */
807 if (length <= offset) {
808 errno = EINVAL;
809 return -1;
812 overlap = (offset + n) > length ? (length - offset) : n;
813 memcpy(data, ea.value.data + offset, overlap);
815 TALLOC_FREE(ea.value.data);
816 return overlap;
819 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
820 struct files_struct *fsp,
821 SMB_OFF_T offset)
823 int ret;
824 uint8 *tmp;
825 struct ea_struct ea;
826 NTSTATUS status;
827 struct stream_io *sio =
828 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
830 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
831 fsp->fsp_name,
832 (double)offset ));
834 if (sio == NULL) {
835 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
838 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
839 sio->base, sio->xattr_name, &ea);
840 if (!NT_STATUS_IS_OK(status)) {
841 return -1;
844 tmp = TALLOC_REALLOC_ARRAY(talloc_tos(), ea.value.data, uint8,
845 offset + 1);
847 if (tmp == NULL) {
848 TALLOC_FREE(ea.value.data);
849 errno = ENOMEM;
850 return -1;
853 /* Did we expand ? */
854 if (ea.value.length < offset + 1) {
855 memset(&tmp[ea.value.length], '\0',
856 offset + 1 - ea.value.length);
859 ea.value.data = tmp;
860 ea.value.length = offset + 1;
861 ea.value.data[offset] = 0;
863 if (fsp->base_fsp->fh->fd != -1) {
864 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
865 sio->xattr_name,
866 ea.value.data, ea.value.length, 0);
867 } else {
868 ret = SMB_VFS_SETXATTR(fsp->conn, fsp->base_fsp->fsp_name,
869 sio->xattr_name,
870 ea.value.data, ea.value.length, 0);
873 TALLOC_FREE(ea.value.data);
875 if (ret == -1) {
876 return -1;
879 return 0;
882 /* VFS operations structure */
884 static vfs_op_tuple streams_xattr_ops[] = {
885 {SMB_VFS_OP(streams_xattr_fs_capabilities), SMB_VFS_OP_FS_CAPABILITIES,
886 SMB_VFS_LAYER_TRANSPARENT},
887 {SMB_VFS_OP(streams_xattr_open), SMB_VFS_OP_OPEN,
888 SMB_VFS_LAYER_TRANSPARENT},
889 {SMB_VFS_OP(streams_xattr_stat), SMB_VFS_OP_STAT,
890 SMB_VFS_LAYER_TRANSPARENT},
891 {SMB_VFS_OP(streams_xattr_fstat), SMB_VFS_OP_FSTAT,
892 SMB_VFS_LAYER_TRANSPARENT},
893 {SMB_VFS_OP(streams_xattr_lstat), SMB_VFS_OP_LSTAT,
894 SMB_VFS_LAYER_TRANSPARENT},
895 {SMB_VFS_OP(streams_xattr_pread), SMB_VFS_OP_PREAD,
896 SMB_VFS_LAYER_TRANSPARENT},
897 {SMB_VFS_OP(streams_xattr_pwrite), SMB_VFS_OP_PWRITE,
898 SMB_VFS_LAYER_TRANSPARENT},
899 {SMB_VFS_OP(streams_xattr_lstat), SMB_VFS_OP_LSTAT,
900 SMB_VFS_LAYER_TRANSPARENT},
901 {SMB_VFS_OP(streams_xattr_unlink), SMB_VFS_OP_UNLINK,
902 SMB_VFS_LAYER_TRANSPARENT},
903 {SMB_VFS_OP(streams_xattr_rename), SMB_VFS_OP_RENAME,
904 SMB_VFS_LAYER_TRANSPARENT},
905 {SMB_VFS_OP(streams_xattr_ftruncate), SMB_VFS_OP_FTRUNCATE,
906 SMB_VFS_LAYER_TRANSPARENT},
907 {SMB_VFS_OP(streams_xattr_streaminfo), SMB_VFS_OP_STREAMINFO,
908 SMB_VFS_LAYER_OPAQUE},
909 {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
912 NTSTATUS vfs_streams_xattr_init(void);
913 NTSTATUS vfs_streams_xattr_init(void)
915 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
916 streams_xattr_ops);