Initialize the file descriptor in the files_struct before trying to close it. Otherwi...
[Samba/gebeck_regimport.git] / source3 / modules / vfs_streams_xattr.c
blob82e2dd8d1efb2ab9e65cc4fe3f62e52aac3f85fe
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"
25 #include "smbd/smbd.h"
26 #include "system/filesys.h"
27 #include "../lib/crypto/md5.h"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_VFS
32 struct stream_io {
33 char *base;
34 char *xattr_name;
35 void *fsp_name_ptr;
36 files_struct *fsp;
37 vfs_handle_struct *handle;
40 static SMB_INO_T stream_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
42 struct MD5Context ctx;
43 unsigned char hash[16];
44 SMB_INO_T result;
45 char *upper_sname;
47 DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
48 (unsigned long)sbuf->st_ex_dev,
49 (unsigned long)sbuf->st_ex_ino, sname));
51 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
52 SMB_ASSERT(upper_sname != NULL);
54 MD5Init(&ctx);
55 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
56 sizeof(sbuf->st_ex_dev));
57 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
58 sizeof(sbuf->st_ex_ino));
59 MD5Update(&ctx, (unsigned char *)upper_sname,
60 talloc_get_size(upper_sname)-1);
61 MD5Final(hash, &ctx);
63 TALLOC_FREE(upper_sname);
65 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
66 memcpy(&result, hash, sizeof(result));
68 DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result));
70 return result;
73 static ssize_t get_xattr_size(connection_struct *conn,
74 files_struct *fsp,
75 const char *fname,
76 const char *xattr_name)
78 NTSTATUS status;
79 struct ea_struct ea;
80 ssize_t result;
82 status = get_ea_value(talloc_tos(), conn, fsp, fname,
83 xattr_name, &ea);
85 if (!NT_STATUS_IS_OK(status)) {
86 return -1;
89 result = ea.value.length-1;
90 TALLOC_FREE(ea.value.data);
91 return result;
94 /**
95 * Given a stream name, populate xattr_name with the xattr name to use for
96 * accessing the stream.
98 static NTSTATUS streams_xattr_get_name(TALLOC_CTX *ctx,
99 const char *stream_name,
100 char **xattr_name)
102 char *stype;
104 stype = strchr_m(stream_name + 1, ':');
106 *xattr_name = talloc_asprintf(ctx, "%s%s",
107 SAMBA_XATTR_DOSSTREAM_PREFIX,
108 stream_name + 1);
109 if (*xattr_name == NULL) {
110 return NT_STATUS_NO_MEMORY;
113 if (stype == NULL) {
114 /* Append an explicit stream type if one wasn't specified. */
115 *xattr_name = talloc_asprintf(ctx, "%s:$DATA",
116 *xattr_name);
117 if (*xattr_name == NULL) {
118 return NT_STATUS_NO_MEMORY;
120 } else {
121 /* Normalize the stream type to upercase. */
122 if (!strupper_m(strrchr_m(*xattr_name, ':') + 1)) {
123 return NT_STATUS_INVALID_PARAMETER;
127 DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name,
128 stream_name));
130 return NT_STATUS_OK;
133 static bool streams_xattr_recheck(struct stream_io *sio)
135 NTSTATUS status;
136 char *xattr_name = NULL;
138 if (sio->fsp->fsp_name == sio->fsp_name_ptr) {
139 return true;
142 if (sio->fsp->fsp_name->stream_name == NULL) {
143 /* how can this happen */
144 errno = EINVAL;
145 return false;
148 status = streams_xattr_get_name(talloc_tos(),
149 sio->fsp->fsp_name->stream_name,
150 &xattr_name);
151 if (!NT_STATUS_IS_OK(status)) {
152 return false;
155 TALLOC_FREE(sio->xattr_name);
156 TALLOC_FREE(sio->base);
157 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
158 xattr_name);
159 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
160 sio->fsp->fsp_name->base_name);
161 sio->fsp_name_ptr = sio->fsp->fsp_name;
163 TALLOC_FREE(xattr_name);
165 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
166 return false;
169 return true;
173 * Helper to stat/lstat the base file of an smb_fname.
175 static int streams_xattr_stat_base(vfs_handle_struct *handle,
176 struct smb_filename *smb_fname,
177 bool follow_links)
179 char *tmp_stream_name;
180 int result;
182 tmp_stream_name = smb_fname->stream_name;
183 smb_fname->stream_name = NULL;
184 if (follow_links) {
185 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
186 } else {
187 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
189 smb_fname->stream_name = tmp_stream_name;
190 return result;
193 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
194 SMB_STRUCT_STAT *sbuf)
196 struct smb_filename *smb_fname_base = NULL;
197 int ret = -1;
198 struct stream_io *io = (struct stream_io *)
199 VFS_FETCH_FSP_EXTENSION(handle, fsp);
201 DEBUG(10, ("streams_xattr_fstat called for %d\n", fsp->fh->fd));
203 if (io == NULL || fsp->base_fsp == NULL) {
204 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
207 if (!streams_xattr_recheck(io)) {
208 return -1;
211 /* Create an smb_filename with stream_name == NULL. */
212 smb_fname_base = synthetic_smb_fname(talloc_tos(), io->base,
213 NULL, NULL);
214 if (smb_fname_base == NULL) {
215 errno = ENOMEM;
216 return -1;
219 if (lp_posix_pathnames()) {
220 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_base);
221 } else {
222 ret = SMB_VFS_STAT(handle->conn, smb_fname_base);
224 *sbuf = smb_fname_base->st;
225 TALLOC_FREE(smb_fname_base);
227 if (ret == -1) {
228 return -1;
231 sbuf->st_ex_size = get_xattr_size(handle->conn, fsp->base_fsp,
232 io->base, io->xattr_name);
233 if (sbuf->st_ex_size == -1) {
234 return -1;
237 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
239 sbuf->st_ex_ino = stream_inode(sbuf, io->xattr_name);
240 sbuf->st_ex_mode &= ~S_IFMT;
241 sbuf->st_ex_mode |= S_IFREG;
242 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
244 return 0;
247 static int streams_xattr_stat(vfs_handle_struct *handle,
248 struct smb_filename *smb_fname)
250 NTSTATUS status;
251 int result = -1;
252 char *xattr_name = NULL;
254 if (!is_ntfs_stream_smb_fname(smb_fname)) {
255 return SMB_VFS_NEXT_STAT(handle, smb_fname);
258 /* Note if lp_posix_paths() is true, we can never
259 * get here as is_ntfs_stream_smb_fname() is
260 * always false. So we never need worry about
261 * not following links here. */
263 /* If the default stream is requested, just stat the base file. */
264 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
265 return streams_xattr_stat_base(handle, smb_fname, true);
268 /* Populate the stat struct with info from the base file. */
269 if (streams_xattr_stat_base(handle, smb_fname, true) == -1) {
270 return -1;
273 /* Derive the xattr name to lookup. */
274 status = streams_xattr_get_name(talloc_tos(), smb_fname->stream_name,
275 &xattr_name);
276 if (!NT_STATUS_IS_OK(status)) {
277 errno = map_errno_from_nt_status(status);
278 return -1;
281 /* Augment the base file's stat information before returning. */
282 smb_fname->st.st_ex_size = get_xattr_size(handle->conn, NULL,
283 smb_fname->base_name,
284 xattr_name);
285 if (smb_fname->st.st_ex_size == -1) {
286 errno = ENOENT;
287 result = -1;
288 goto fail;
291 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
292 smb_fname->st.st_ex_mode &= ~S_IFMT;
293 smb_fname->st.st_ex_mode |= S_IFREG;
294 smb_fname->st.st_ex_blocks =
295 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
297 result = 0;
298 fail:
299 TALLOC_FREE(xattr_name);
300 return result;
303 static int streams_xattr_lstat(vfs_handle_struct *handle,
304 struct smb_filename *smb_fname)
306 NTSTATUS status;
307 int result = -1;
308 char *xattr_name = NULL;
310 if (!is_ntfs_stream_smb_fname(smb_fname)) {
311 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
314 /* If the default stream is requested, just stat the base file. */
315 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
316 return streams_xattr_stat_base(handle, smb_fname, false);
319 /* Populate the stat struct with info from the base file. */
320 if (streams_xattr_stat_base(handle, smb_fname, false) == -1) {
321 return -1;
324 /* Derive the xattr name to lookup. */
325 status = streams_xattr_get_name(talloc_tos(), smb_fname->stream_name,
326 &xattr_name);
327 if (!NT_STATUS_IS_OK(status)) {
328 errno = map_errno_from_nt_status(status);
329 return -1;
332 /* Augment the base file's stat information before returning. */
333 smb_fname->st.st_ex_size = get_xattr_size(handle->conn, NULL,
334 smb_fname->base_name,
335 xattr_name);
336 if (smb_fname->st.st_ex_size == -1) {
337 errno = ENOENT;
338 result = -1;
339 goto fail;
342 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
343 smb_fname->st.st_ex_mode &= ~S_IFMT;
344 smb_fname->st.st_ex_mode |= S_IFREG;
345 smb_fname->st.st_ex_blocks =
346 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
348 result = 0;
350 fail:
351 TALLOC_FREE(xattr_name);
352 return result;
355 static int streams_xattr_open(vfs_handle_struct *handle,
356 struct smb_filename *smb_fname,
357 files_struct *fsp, int flags, mode_t mode)
359 NTSTATUS status;
360 struct smb_filename *smb_fname_base = NULL;
361 struct stream_io *sio;
362 struct ea_struct ea;
363 char *xattr_name = NULL;
364 int baseflags;
365 int hostfd = -1;
367 DEBUG(10, ("streams_xattr_open called for %s\n",
368 smb_fname_str_dbg(smb_fname)));
370 if (!is_ntfs_stream_smb_fname(smb_fname)) {
371 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
374 /* If the default stream is requested, just open the base file. */
375 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
376 char *tmp_stream_name;
377 int ret;
379 tmp_stream_name = smb_fname->stream_name;
380 smb_fname->stream_name = NULL;
382 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
384 smb_fname->stream_name = tmp_stream_name;
386 return ret;
389 status = streams_xattr_get_name(talloc_tos(), smb_fname->stream_name,
390 &xattr_name);
391 if (!NT_STATUS_IS_OK(status)) {
392 errno = map_errno_from_nt_status(status);
393 goto fail;
396 /* Create an smb_filename with stream_name == NULL. */
397 smb_fname_base = synthetic_smb_fname(
398 talloc_tos(), smb_fname->base_name, NULL, NULL);
399 if (smb_fname_base == NULL) {
400 errno = ENOMEM;
401 goto fail;
405 * We use baseflags to turn off nasty side-effects when opening the
406 * underlying file.
408 baseflags = flags;
409 baseflags &= ~O_TRUNC;
410 baseflags &= ~O_EXCL;
411 baseflags &= ~O_CREAT;
413 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
414 baseflags, mode);
416 TALLOC_FREE(smb_fname_base);
418 /* It is legit to open a stream on a directory, but the base
419 * fd has to be read-only.
421 if ((hostfd == -1) && (errno == EISDIR)) {
422 baseflags &= ~O_ACCMODE;
423 baseflags |= O_RDONLY;
424 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname, fsp, baseflags,
425 mode);
428 if (hostfd == -1) {
429 goto fail;
432 status = get_ea_value(talloc_tos(), handle->conn, NULL,
433 smb_fname->base_name, xattr_name, &ea);
435 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
437 if (!NT_STATUS_IS_OK(status)
438 && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
440 * The base file is not there. This is an error even if we got
441 * O_CREAT, the higher levels should have created the base
442 * file for us.
444 DEBUG(10, ("streams_xattr_open: base file %s not around, "
445 "returning ENOENT\n", smb_fname->base_name));
446 errno = ENOENT;
447 goto fail;
450 if (!NT_STATUS_IS_OK(status)) {
452 * The attribute does not exist
455 if (flags & O_CREAT) {
457 * Darn, xattrs need at least 1 byte
459 char null = '\0';
461 DEBUG(10, ("creating attribute %s on file %s\n",
462 xattr_name, smb_fname->base_name));
464 if (fsp->base_fsp->fh->fd != -1) {
465 if (SMB_VFS_FSETXATTR(
466 fsp->base_fsp, xattr_name,
467 &null, sizeof(null),
468 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
469 goto fail;
471 } else {
472 if (SMB_VFS_SETXATTR(
473 handle->conn, smb_fname->base_name,
474 xattr_name, &null, sizeof(null),
475 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
476 goto fail;
482 if (flags & O_TRUNC) {
483 char null = '\0';
484 if (fsp->base_fsp->fh->fd != -1) {
485 if (SMB_VFS_FSETXATTR(
486 fsp->base_fsp, xattr_name,
487 &null, sizeof(null),
488 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
489 goto fail;
491 } else {
492 if (SMB_VFS_SETXATTR(
493 handle->conn, smb_fname->base_name,
494 xattr_name, &null, sizeof(null),
495 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
496 goto fail;
501 sio = (struct stream_io *)VFS_ADD_FSP_EXTENSION(handle, fsp,
502 struct stream_io,
503 NULL);
504 if (sio == NULL) {
505 errno = ENOMEM;
506 goto fail;
509 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
510 xattr_name);
511 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
512 smb_fname->base_name);
513 sio->fsp_name_ptr = fsp->fsp_name;
514 sio->handle = handle;
515 sio->fsp = fsp;
517 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
518 errno = ENOMEM;
519 goto fail;
522 return hostfd;
524 fail:
525 if (hostfd >= 0) {
527 * BUGBUGBUG -- we would need to call fd_close_posix here, but
528 * we don't have a full fsp yet
530 fsp->fh->fd = hostfd;
531 SMB_VFS_CLOSE(fsp);
534 return -1;
537 static int streams_xattr_unlink(vfs_handle_struct *handle,
538 const struct smb_filename *smb_fname)
540 NTSTATUS status;
541 int ret = -1;
542 char *xattr_name;
544 if (!is_ntfs_stream_smb_fname(smb_fname)) {
545 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
548 /* If the default stream is requested, just open the base file. */
549 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
550 struct smb_filename *smb_fname_base = NULL;
552 smb_fname_base = cp_smb_filename(talloc_tos(), smb_fname);
553 if (smb_fname_base == NULL) {
554 errno = ENOMEM;
555 return -1;
558 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_base);
560 TALLOC_FREE(smb_fname_base);
561 return ret;
564 status = streams_xattr_get_name(talloc_tos(), smb_fname->stream_name,
565 &xattr_name);
566 if (!NT_STATUS_IS_OK(status)) {
567 errno = map_errno_from_nt_status(status);
568 goto fail;
571 ret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname->base_name, xattr_name);
573 if ((ret == -1) && (errno == ENOATTR)) {
574 errno = ENOENT;
575 goto fail;
578 ret = 0;
580 fail:
581 TALLOC_FREE(xattr_name);
582 return ret;
585 static int streams_xattr_rename(vfs_handle_struct *handle,
586 const struct smb_filename *smb_fname_src,
587 const struct smb_filename *smb_fname_dst)
589 NTSTATUS status;
590 int ret = -1;
591 char *src_xattr_name = NULL;
592 char *dst_xattr_name = NULL;
593 bool src_is_stream, dst_is_stream;
594 ssize_t oret;
595 ssize_t nret;
596 struct ea_struct ea;
598 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
599 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
601 if (!src_is_stream && !dst_is_stream) {
602 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
603 smb_fname_dst);
606 /* For now don't allow renames from or to the default stream. */
607 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
608 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
609 errno = ENOSYS;
610 goto done;
613 /* Don't rename if the streams are identical. */
614 if (strcasecmp_m(smb_fname_src->stream_name,
615 smb_fname_dst->stream_name) == 0) {
616 goto done;
619 /* Get the xattr names. */
620 status = streams_xattr_get_name(talloc_tos(),
621 smb_fname_src->stream_name,
622 &src_xattr_name);
623 if (!NT_STATUS_IS_OK(status)) {
624 errno = map_errno_from_nt_status(status);
625 goto fail;
627 status = streams_xattr_get_name(talloc_tos(),
628 smb_fname_dst->stream_name,
629 &dst_xattr_name);
630 if (!NT_STATUS_IS_OK(status)) {
631 errno = map_errno_from_nt_status(status);
632 goto fail;
635 /* read the old stream */
636 status = get_ea_value(talloc_tos(), handle->conn, NULL,
637 smb_fname_src->base_name, src_xattr_name, &ea);
638 if (!NT_STATUS_IS_OK(status)) {
639 errno = ENOENT;
640 goto fail;
643 /* (over)write the new stream */
644 nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src->base_name,
645 dst_xattr_name, ea.value.data, ea.value.length,
647 if (nret < 0) {
648 if (errno == ENOATTR) {
649 errno = ENOENT;
651 goto fail;
654 /* remove the old stream */
655 oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src->base_name,
656 src_xattr_name);
657 if (oret < 0) {
658 if (errno == ENOATTR) {
659 errno = ENOENT;
661 goto fail;
664 done:
665 errno = 0;
666 ret = 0;
667 fail:
668 TALLOC_FREE(src_xattr_name);
669 TALLOC_FREE(dst_xattr_name);
670 return ret;
673 static NTSTATUS walk_xattr_streams(connection_struct *conn, files_struct *fsp,
674 const char *fname,
675 bool (*fn)(struct ea_struct *ea,
676 void *private_data),
677 void *private_data)
679 NTSTATUS status;
680 char **names;
681 size_t i, num_names;
682 size_t prefix_len = strlen(SAMBA_XATTR_DOSSTREAM_PREFIX);
684 status = get_ea_names_from_file(talloc_tos(), conn, fsp, fname,
685 &names, &num_names);
686 if (!NT_STATUS_IS_OK(status)) {
687 return status;
690 for (i=0; i<num_names; i++) {
691 struct ea_struct ea;
693 if (strncmp(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
694 prefix_len) != 0) {
695 continue;
698 status = get_ea_value(names, conn, fsp, fname, names[i], &ea);
699 if (!NT_STATUS_IS_OK(status)) {
700 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
701 names[i], fname, nt_errstr(status)));
702 continue;
705 ea.name = talloc_asprintf(ea.value.data, ":%s",
706 names[i] + prefix_len);
707 if (ea.name == NULL) {
708 DEBUG(0, ("talloc failed\n"));
709 continue;
712 if (!fn(&ea, private_data)) {
713 TALLOC_FREE(ea.value.data);
714 return NT_STATUS_OK;
717 TALLOC_FREE(ea.value.data);
720 TALLOC_FREE(names);
721 return NT_STATUS_OK;
724 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
725 struct stream_struct **streams,
726 const char *name, off_t size,
727 off_t alloc_size)
729 struct stream_struct *tmp;
731 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
732 (*num_streams)+1);
733 if (tmp == NULL) {
734 return false;
737 tmp[*num_streams].name = talloc_strdup(tmp, name);
738 if (tmp[*num_streams].name == NULL) {
739 return false;
742 tmp[*num_streams].size = size;
743 tmp[*num_streams].alloc_size = alloc_size;
745 *streams = tmp;
746 *num_streams += 1;
747 return true;
750 struct streaminfo_state {
751 TALLOC_CTX *mem_ctx;
752 vfs_handle_struct *handle;
753 unsigned int num_streams;
754 struct stream_struct *streams;
755 NTSTATUS status;
758 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
760 struct streaminfo_state *state =
761 (struct streaminfo_state *)private_data;
763 if (!add_one_stream(state->mem_ctx,
764 &state->num_streams, &state->streams,
765 ea->name, ea->value.length-1,
766 smb_roundup(state->handle->conn,
767 ea->value.length-1))) {
768 state->status = NT_STATUS_NO_MEMORY;
769 return false;
772 return true;
775 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
776 struct files_struct *fsp,
777 const char *fname,
778 TALLOC_CTX *mem_ctx,
779 unsigned int *pnum_streams,
780 struct stream_struct **pstreams)
782 SMB_STRUCT_STAT sbuf;
783 int ret;
784 NTSTATUS status;
785 struct streaminfo_state state;
787 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
788 ret = SMB_VFS_FSTAT(fsp, &sbuf);
790 else {
791 struct smb_filename *smb_fname = NULL;
792 smb_fname = synthetic_smb_fname(talloc_tos(), fname, NULL,
793 NULL);
794 if (smb_fname == NULL) {
795 return NT_STATUS_NO_MEMORY;
797 if (lp_posix_pathnames()) {
798 ret = SMB_VFS_LSTAT(handle->conn, smb_fname);
799 } else {
800 ret = SMB_VFS_STAT(handle->conn, smb_fname);
802 sbuf = smb_fname->st;
803 TALLOC_FREE(smb_fname);
806 if (ret == -1) {
807 return map_nt_error_from_unix(errno);
810 state.streams = *pstreams;
811 state.num_streams = *pnum_streams;
812 state.mem_ctx = mem_ctx;
813 state.handle = handle;
814 state.status = NT_STATUS_OK;
816 status = walk_xattr_streams(handle->conn, fsp, fname,
817 collect_one_stream, &state);
819 if (!NT_STATUS_IS_OK(status)) {
820 TALLOC_FREE(state.streams);
821 return status;
824 if (!NT_STATUS_IS_OK(state.status)) {
825 TALLOC_FREE(state.streams);
826 return state.status;
829 *pnum_streams = state.num_streams;
830 *pstreams = state.streams;
832 return SMB_VFS_NEXT_STREAMINFO(handle, fsp, fname, mem_ctx, pnum_streams, pstreams);
835 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
836 enum timestamp_set_resolution *p_ts_res)
838 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
841 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
842 files_struct *fsp, const void *data,
843 size_t n, off_t offset)
845 struct stream_io *sio =
846 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
847 struct ea_struct ea;
848 NTSTATUS status;
849 int ret;
851 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
853 if (sio == NULL) {
854 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
857 if (!streams_xattr_recheck(sio)) {
858 return -1;
861 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
862 sio->base, sio->xattr_name, &ea);
863 if (!NT_STATUS_IS_OK(status)) {
864 return -1;
867 if ((offset + n) > ea.value.length-1) {
868 uint8 *tmp;
870 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8,
871 offset + n + 1);
873 if (tmp == NULL) {
874 TALLOC_FREE(ea.value.data);
875 errno = ENOMEM;
876 return -1;
878 ea.value.data = tmp;
879 ea.value.length = offset + n + 1;
880 ea.value.data[offset+n] = 0;
883 memcpy(ea.value.data + offset, data, n);
885 if (fsp->base_fsp->fh->fd != -1) {
886 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
887 sio->xattr_name,
888 ea.value.data, ea.value.length, 0);
889 } else {
890 ret = SMB_VFS_SETXATTR(fsp->conn,
891 fsp->base_fsp->fsp_name->base_name,
892 sio->xattr_name,
893 ea.value.data, ea.value.length, 0);
895 TALLOC_FREE(ea.value.data);
897 if (ret == -1) {
898 return -1;
901 return n;
904 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
905 files_struct *fsp, void *data,
906 size_t n, off_t offset)
908 struct stream_io *sio =
909 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
910 struct ea_struct ea;
911 NTSTATUS status;
912 size_t length, overlap;
914 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
915 (int)offset, (int)n));
917 if (sio == NULL) {
918 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
921 if (!streams_xattr_recheck(sio)) {
922 return -1;
925 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
926 sio->base, sio->xattr_name, &ea);
927 if (!NT_STATUS_IS_OK(status)) {
928 return -1;
931 length = ea.value.length-1;
933 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
934 (int)length));
936 /* Attempt to read past EOF. */
937 if (length <= offset) {
938 return 0;
941 overlap = (offset + n) > length ? (length - offset) : n;
942 memcpy(data, ea.value.data + offset, overlap);
944 TALLOC_FREE(ea.value.data);
945 return overlap;
948 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
949 struct files_struct *fsp,
950 off_t offset)
952 int ret;
953 uint8 *tmp;
954 struct ea_struct ea;
955 NTSTATUS status;
956 struct stream_io *sio =
957 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
959 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
960 fsp_str_dbg(fsp), (double)offset));
962 if (sio == NULL) {
963 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
966 if (!streams_xattr_recheck(sio)) {
967 return -1;
970 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
971 sio->base, sio->xattr_name, &ea);
972 if (!NT_STATUS_IS_OK(status)) {
973 return -1;
976 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8,
977 offset + 1);
979 if (tmp == NULL) {
980 TALLOC_FREE(ea.value.data);
981 errno = ENOMEM;
982 return -1;
985 /* Did we expand ? */
986 if (ea.value.length < offset + 1) {
987 memset(&tmp[ea.value.length], '\0',
988 offset + 1 - ea.value.length);
991 ea.value.data = tmp;
992 ea.value.length = offset + 1;
993 ea.value.data[offset] = 0;
995 if (fsp->base_fsp->fh->fd != -1) {
996 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
997 sio->xattr_name,
998 ea.value.data, ea.value.length, 0);
999 } else {
1000 ret = SMB_VFS_SETXATTR(fsp->conn,
1001 fsp->base_fsp->fsp_name->base_name,
1002 sio->xattr_name,
1003 ea.value.data, ea.value.length, 0);
1006 TALLOC_FREE(ea.value.data);
1008 if (ret == -1) {
1009 return -1;
1012 return 0;
1015 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1016 struct files_struct *fsp,
1017 enum vfs_fallocate_mode mode,
1018 off_t offset,
1019 off_t len)
1021 struct stream_io *sio =
1022 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1024 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1025 "len = %.0f\n",
1026 fsp_str_dbg(fsp), (double)offset, (double)len));
1028 if (sio == NULL) {
1029 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1032 if (!streams_xattr_recheck(sio)) {
1033 return errno;
1036 /* Let the pwrite code path handle it. */
1037 return ENOSYS;
1041 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1042 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1043 .open_fn = streams_xattr_open,
1044 .stat_fn = streams_xattr_stat,
1045 .fstat_fn = streams_xattr_fstat,
1046 .lstat_fn = streams_xattr_lstat,
1047 .pread_fn = streams_xattr_pread,
1048 .pwrite_fn = streams_xattr_pwrite,
1049 .unlink_fn = streams_xattr_unlink,
1050 .rename_fn = streams_xattr_rename,
1051 .ftruncate_fn = streams_xattr_ftruncate,
1052 .fallocate_fn = streams_xattr_fallocate,
1053 .streaminfo_fn = streams_xattr_streaminfo,
1056 NTSTATUS vfs_streams_xattr_init(void);
1057 NTSTATUS vfs_streams_xattr_init(void)
1059 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1060 &vfs_streams_xattr_fns);