VERSION: Bump version up to 3.6.0rc1.
[Samba.git] / source3 / modules / vfs_streams_xattr.c
blob34e01b0b5f2a4cc322add09863070f3825e28fa0
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, (unsigned char *)&(sbuf->st_ex_dev),
56 sizeof(sbuf->st_ex_dev));
57 MD5Update(&ctx, (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 strupper_m(strrchr_m(*xattr_name, ':') + 1);
125 DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name,
126 stream_name));
128 return NT_STATUS_OK;
131 static bool streams_xattr_recheck(struct stream_io *sio)
133 NTSTATUS status;
134 char *xattr_name = NULL;
136 if (sio->fsp->fsp_name == sio->fsp_name_ptr) {
137 return true;
140 if (sio->fsp->fsp_name->stream_name == NULL) {
141 /* how can this happen */
142 errno = EINVAL;
143 return false;
146 status = streams_xattr_get_name(talloc_tos(),
147 sio->fsp->fsp_name->stream_name,
148 &xattr_name);
149 if (!NT_STATUS_IS_OK(status)) {
150 return false;
153 TALLOC_FREE(sio->xattr_name);
154 TALLOC_FREE(sio->base);
155 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
156 xattr_name);
157 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
158 sio->fsp->fsp_name->base_name);
159 sio->fsp_name_ptr = sio->fsp->fsp_name;
161 TALLOC_FREE(xattr_name);
163 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
164 return false;
167 return true;
171 * Helper to stat/lstat the base file of an smb_fname.
173 static int streams_xattr_stat_base(vfs_handle_struct *handle,
174 struct smb_filename *smb_fname,
175 bool follow_links)
177 char *tmp_stream_name;
178 int result;
180 tmp_stream_name = smb_fname->stream_name;
181 smb_fname->stream_name = NULL;
182 if (follow_links) {
183 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
184 } else {
185 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
187 smb_fname->stream_name = tmp_stream_name;
188 return result;
191 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
192 SMB_STRUCT_STAT *sbuf)
194 struct smb_filename *smb_fname_base = NULL;
195 NTSTATUS status;
196 int ret = -1;
197 struct stream_io *io = (struct stream_io *)
198 VFS_FETCH_FSP_EXTENSION(handle, fsp);
200 DEBUG(10, ("streams_xattr_fstat called for %d\n", fsp->fh->fd));
202 if (io == NULL || fsp->base_fsp == NULL) {
203 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
206 if (!streams_xattr_recheck(io)) {
207 return -1;
210 /* Create an smb_filename with stream_name == NULL. */
211 status = create_synthetic_smb_fname(talloc_tos(),
212 io->base,
213 NULL, NULL,
214 &smb_fname_base);
215 if (!NT_STATUS_IS_OK(status)) {
216 errno = map_errno_from_nt_status(status);
217 return -1;
220 if (lp_posix_pathnames()) {
221 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_base);
222 } else {
223 ret = SMB_VFS_STAT(handle->conn, smb_fname_base);
225 *sbuf = smb_fname_base->st;
226 TALLOC_FREE(smb_fname_base);
228 if (ret == -1) {
229 return -1;
232 sbuf->st_ex_size = get_xattr_size(handle->conn, fsp->base_fsp,
233 io->base, io->xattr_name);
234 if (sbuf->st_ex_size == -1) {
235 return -1;
238 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
240 sbuf->st_ex_ino = stream_inode(sbuf, io->xattr_name);
241 sbuf->st_ex_mode &= ~S_IFMT;
242 sbuf->st_ex_mode |= S_IFREG;
243 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
245 return 0;
248 static int streams_xattr_stat(vfs_handle_struct *handle,
249 struct smb_filename *smb_fname)
251 NTSTATUS status;
252 int result = -1;
253 char *xattr_name = NULL;
255 if (!is_ntfs_stream_smb_fname(smb_fname)) {
256 return SMB_VFS_NEXT_STAT(handle, smb_fname);
259 /* Note if lp_posix_paths() is true, we can never
260 * get here as is_ntfs_stream_smb_fname() is
261 * always false. So we never need worry about
262 * not following links here. */
264 /* If the default stream is requested, just stat the base file. */
265 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
266 return streams_xattr_stat_base(handle, smb_fname, true);
269 /* Populate the stat struct with info from the base file. */
270 if (streams_xattr_stat_base(handle, smb_fname, true) == -1) {
271 return -1;
274 /* Derive the xattr name to lookup. */
275 status = streams_xattr_get_name(talloc_tos(), smb_fname->stream_name,
276 &xattr_name);
277 if (!NT_STATUS_IS_OK(status)) {
278 errno = map_errno_from_nt_status(status);
279 return -1;
282 /* Augment the base file's stat information before returning. */
283 smb_fname->st.st_ex_size = get_xattr_size(handle->conn, NULL,
284 smb_fname->base_name,
285 xattr_name);
286 if (smb_fname->st.st_ex_size == -1) {
287 errno = ENOENT;
288 result = -1;
289 goto fail;
292 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
293 smb_fname->st.st_ex_mode &= ~S_IFMT;
294 smb_fname->st.st_ex_mode |= S_IFREG;
295 smb_fname->st.st_ex_blocks =
296 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
298 result = 0;
299 fail:
300 TALLOC_FREE(xattr_name);
301 return result;
304 static int streams_xattr_lstat(vfs_handle_struct *handle,
305 struct smb_filename *smb_fname)
307 NTSTATUS status;
308 int result = -1;
309 char *xattr_name = NULL;
311 if (!is_ntfs_stream_smb_fname(smb_fname)) {
312 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
315 /* If the default stream is requested, just stat the base file. */
316 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
317 return streams_xattr_stat_base(handle, smb_fname, false);
320 /* Populate the stat struct with info from the base file. */
321 if (streams_xattr_stat_base(handle, smb_fname, false) == -1) {
322 return -1;
325 /* Derive the xattr name to lookup. */
326 status = streams_xattr_get_name(talloc_tos(), smb_fname->stream_name,
327 &xattr_name);
328 if (!NT_STATUS_IS_OK(status)) {
329 errno = map_errno_from_nt_status(status);
330 return -1;
333 /* Augment the base file's stat information before returning. */
334 smb_fname->st.st_ex_size = get_xattr_size(handle->conn, NULL,
335 smb_fname->base_name,
336 xattr_name);
337 if (smb_fname->st.st_ex_size == -1) {
338 errno = ENOENT;
339 result = -1;
340 goto fail;
343 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
344 smb_fname->st.st_ex_mode &= ~S_IFMT;
345 smb_fname->st.st_ex_mode |= S_IFREG;
346 smb_fname->st.st_ex_blocks =
347 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
349 result = 0;
351 fail:
352 TALLOC_FREE(xattr_name);
353 return result;
356 static int streams_xattr_open(vfs_handle_struct *handle,
357 struct smb_filename *smb_fname,
358 files_struct *fsp, int flags, mode_t mode)
360 NTSTATUS status;
361 struct smb_filename *smb_fname_base = NULL;
362 struct stream_io *sio;
363 struct ea_struct ea;
364 char *xattr_name = NULL;
365 int baseflags;
366 int hostfd = -1;
368 DEBUG(10, ("streams_xattr_open called for %s\n",
369 smb_fname_str_dbg(smb_fname)));
371 if (!is_ntfs_stream_smb_fname(smb_fname)) {
372 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
375 /* If the default stream is requested, just open the base file. */
376 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
377 char *tmp_stream_name;
378 int ret;
380 tmp_stream_name = smb_fname->stream_name;
381 smb_fname->stream_name = NULL;
383 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
385 smb_fname->stream_name = tmp_stream_name;
387 return ret;
390 status = streams_xattr_get_name(talloc_tos(), smb_fname->stream_name,
391 &xattr_name);
392 if (!NT_STATUS_IS_OK(status)) {
393 errno = map_errno_from_nt_status(status);
394 goto fail;
397 /* Create an smb_filename with stream_name == NULL. */
398 status = create_synthetic_smb_fname(talloc_tos(),
399 smb_fname->base_name,
400 NULL, NULL,
401 &smb_fname_base);
402 if (!NT_STATUS_IS_OK(status)) {
403 errno = map_errno_from_nt_status(status);
404 goto fail;
408 * We use baseflags to turn off nasty side-effects when opening the
409 * underlying file.
411 baseflags = flags;
412 baseflags &= ~O_TRUNC;
413 baseflags &= ~O_EXCL;
414 baseflags &= ~O_CREAT;
416 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
417 baseflags, mode);
419 TALLOC_FREE(smb_fname_base);
421 /* It is legit to open a stream on a directory, but the base
422 * fd has to be read-only.
424 if ((hostfd == -1) && (errno == EISDIR)) {
425 baseflags &= ~O_ACCMODE;
426 baseflags |= O_RDONLY;
427 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname, fsp, baseflags,
428 mode);
431 if (hostfd == -1) {
432 goto fail;
435 status = get_ea_value(talloc_tos(), handle->conn, NULL,
436 smb_fname->base_name, xattr_name, &ea);
438 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
440 if (!NT_STATUS_IS_OK(status)
441 && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
443 * The base file is not there. This is an error even if we got
444 * O_CREAT, the higher levels should have created the base
445 * file for us.
447 DEBUG(10, ("streams_xattr_open: base file %s not around, "
448 "returning ENOENT\n", smb_fname->base_name));
449 errno = ENOENT;
450 goto fail;
453 if (!NT_STATUS_IS_OK(status)) {
455 * The attribute does not exist
458 if (flags & O_CREAT) {
460 * Darn, xattrs need at least 1 byte
462 char null = '\0';
464 DEBUG(10, ("creating attribute %s on file %s\n",
465 xattr_name, smb_fname->base_name));
467 if (fsp->base_fsp->fh->fd != -1) {
468 if (SMB_VFS_FSETXATTR(
469 fsp->base_fsp, xattr_name,
470 &null, sizeof(null),
471 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
472 goto fail;
474 } else {
475 if (SMB_VFS_SETXATTR(
476 handle->conn, smb_fname->base_name,
477 xattr_name, &null, sizeof(null),
478 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
479 goto fail;
485 if (flags & O_TRUNC) {
486 char null = '\0';
487 if (fsp->base_fsp->fh->fd != -1) {
488 if (SMB_VFS_FSETXATTR(
489 fsp->base_fsp, xattr_name,
490 &null, sizeof(null),
491 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
492 goto fail;
494 } else {
495 if (SMB_VFS_SETXATTR(
496 handle->conn, smb_fname->base_name,
497 xattr_name, &null, sizeof(null),
498 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
499 goto fail;
504 sio = (struct stream_io *)VFS_ADD_FSP_EXTENSION(handle, fsp,
505 struct stream_io,
506 NULL);
507 if (sio == NULL) {
508 errno = ENOMEM;
509 goto fail;
512 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
513 xattr_name);
514 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
515 smb_fname->base_name);
516 sio->fsp_name_ptr = fsp->fsp_name;
517 sio->handle = handle;
518 sio->fsp = fsp;
520 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
521 errno = ENOMEM;
522 goto fail;
525 return hostfd;
527 fail:
528 if (hostfd >= 0) {
530 * BUGBUGBUG -- we would need to call fd_close_posix here, but
531 * we don't have a full fsp yet
533 SMB_VFS_CLOSE(fsp);
536 return -1;
539 static int streams_xattr_unlink(vfs_handle_struct *handle,
540 const struct smb_filename *smb_fname)
542 NTSTATUS status;
543 int ret = -1;
544 char *xattr_name;
546 if (!is_ntfs_stream_smb_fname(smb_fname)) {
547 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
550 /* If the default stream is requested, just open the base file. */
551 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
552 struct smb_filename *smb_fname_base = NULL;
554 status = copy_smb_filename(talloc_tos(), smb_fname,
555 &smb_fname_base);
556 if (!NT_STATUS_IS_OK(status)) {
557 errno = map_errno_from_nt_status(status);
558 return -1;
561 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_base);
563 TALLOC_FREE(smb_fname_base);
564 return ret;
567 status = streams_xattr_get_name(talloc_tos(), smb_fname->stream_name,
568 &xattr_name);
569 if (!NT_STATUS_IS_OK(status)) {
570 errno = map_errno_from_nt_status(status);
571 goto fail;
574 ret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname->base_name, xattr_name);
576 if ((ret == -1) && (errno == ENOATTR)) {
577 errno = ENOENT;
578 goto fail;
581 ret = 0;
583 fail:
584 TALLOC_FREE(xattr_name);
585 return ret;
588 static int streams_xattr_rename(vfs_handle_struct *handle,
589 const struct smb_filename *smb_fname_src,
590 const struct smb_filename *smb_fname_dst)
592 NTSTATUS status;
593 int ret = -1;
594 char *src_xattr_name = NULL;
595 char *dst_xattr_name = NULL;
596 bool src_is_stream, dst_is_stream;
597 ssize_t oret;
598 ssize_t nret;
599 struct ea_struct ea;
601 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
602 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
604 if (!src_is_stream && !dst_is_stream) {
605 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
606 smb_fname_dst);
609 /* For now don't allow renames from or to the default stream. */
610 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
611 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
612 errno = ENOSYS;
613 goto done;
616 /* Don't rename if the streams are identical. */
617 if (StrCaseCmp(smb_fname_src->stream_name,
618 smb_fname_dst->stream_name) == 0) {
619 goto done;
622 /* Get the xattr names. */
623 status = streams_xattr_get_name(talloc_tos(),
624 smb_fname_src->stream_name,
625 &src_xattr_name);
626 if (!NT_STATUS_IS_OK(status)) {
627 errno = map_errno_from_nt_status(status);
628 goto fail;
630 status = streams_xattr_get_name(talloc_tos(),
631 smb_fname_dst->stream_name,
632 &dst_xattr_name);
633 if (!NT_STATUS_IS_OK(status)) {
634 errno = map_errno_from_nt_status(status);
635 goto fail;
638 /* read the old stream */
639 status = get_ea_value(talloc_tos(), handle->conn, NULL,
640 smb_fname_src->base_name, src_xattr_name, &ea);
641 if (!NT_STATUS_IS_OK(status)) {
642 errno = ENOENT;
643 goto fail;
646 /* (over)write the new stream */
647 nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src->base_name,
648 dst_xattr_name, ea.value.data, ea.value.length,
650 if (nret < 0) {
651 if (errno == ENOATTR) {
652 errno = ENOENT;
654 goto fail;
657 /* remove the old stream */
658 oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src->base_name,
659 src_xattr_name);
660 if (oret < 0) {
661 if (errno == ENOATTR) {
662 errno = ENOENT;
664 goto fail;
667 done:
668 errno = 0;
669 ret = 0;
670 fail:
671 TALLOC_FREE(src_xattr_name);
672 TALLOC_FREE(dst_xattr_name);
673 return ret;
676 static NTSTATUS walk_xattr_streams(connection_struct *conn, files_struct *fsp,
677 const char *fname,
678 bool (*fn)(struct ea_struct *ea,
679 void *private_data),
680 void *private_data)
682 NTSTATUS status;
683 char **names;
684 size_t i, num_names;
685 size_t prefix_len = strlen(SAMBA_XATTR_DOSSTREAM_PREFIX);
687 status = get_ea_names_from_file(talloc_tos(), conn, fsp, fname,
688 &names, &num_names);
689 if (!NT_STATUS_IS_OK(status)) {
690 return status;
693 for (i=0; i<num_names; i++) {
694 struct ea_struct ea;
696 if (strncmp(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
697 prefix_len) != 0) {
698 continue;
701 status = get_ea_value(names, conn, fsp, fname, names[i], &ea);
702 if (!NT_STATUS_IS_OK(status)) {
703 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
704 names[i], fname, nt_errstr(status)));
705 continue;
708 ea.name = talloc_asprintf(ea.value.data, ":%s",
709 names[i] + prefix_len);
710 if (ea.name == NULL) {
711 DEBUG(0, ("talloc failed\n"));
712 continue;
715 if (!fn(&ea, private_data)) {
716 TALLOC_FREE(ea.value.data);
717 return NT_STATUS_OK;
720 TALLOC_FREE(ea.value.data);
723 TALLOC_FREE(names);
724 return NT_STATUS_OK;
727 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
728 struct stream_struct **streams,
729 const char *name, SMB_OFF_T size,
730 SMB_OFF_T alloc_size)
732 struct stream_struct *tmp;
734 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
735 (*num_streams)+1);
736 if (tmp == NULL) {
737 return false;
740 tmp[*num_streams].name = talloc_strdup(tmp, name);
741 if (tmp[*num_streams].name == NULL) {
742 return false;
745 tmp[*num_streams].size = size;
746 tmp[*num_streams].alloc_size = alloc_size;
748 *streams = tmp;
749 *num_streams += 1;
750 return true;
753 struct streaminfo_state {
754 TALLOC_CTX *mem_ctx;
755 vfs_handle_struct *handle;
756 unsigned int num_streams;
757 struct stream_struct *streams;
758 NTSTATUS status;
761 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
763 struct streaminfo_state *state =
764 (struct streaminfo_state *)private_data;
766 if (!add_one_stream(state->mem_ctx,
767 &state->num_streams, &state->streams,
768 ea->name, ea->value.length-1,
769 smb_roundup(state->handle->conn,
770 ea->value.length-1))) {
771 state->status = NT_STATUS_NO_MEMORY;
772 return false;
775 return true;
778 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
779 struct files_struct *fsp,
780 const char *fname,
781 TALLOC_CTX *mem_ctx,
782 unsigned int *pnum_streams,
783 struct stream_struct **pstreams)
785 SMB_STRUCT_STAT sbuf;
786 int ret;
787 NTSTATUS status;
788 struct streaminfo_state state;
790 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
791 ret = SMB_VFS_FSTAT(fsp, &sbuf);
793 else {
794 struct smb_filename *smb_fname = NULL;
795 status = create_synthetic_smb_fname(talloc_tos(), fname, NULL,
796 NULL, &smb_fname);
797 if (!NT_STATUS_IS_OK(status)) {
798 return status;
800 if (lp_posix_pathnames()) {
801 ret = SMB_VFS_LSTAT(handle->conn, smb_fname);
802 } else {
803 ret = SMB_VFS_STAT(handle->conn, smb_fname);
805 sbuf = smb_fname->st;
806 TALLOC_FREE(smb_fname);
809 if (ret == -1) {
810 return map_nt_error_from_unix(errno);
813 state.streams = NULL;
814 state.num_streams = 0;
816 if (!S_ISDIR(sbuf.st_ex_mode)) {
817 if (!add_one_stream(mem_ctx,
818 &state.num_streams, &state.streams,
819 "::$DATA", sbuf.st_ex_size,
820 SMB_VFS_GET_ALLOC_SIZE(handle->conn, fsp,
821 &sbuf))) {
822 return NT_STATUS_NO_MEMORY;
826 state.mem_ctx = mem_ctx;
827 state.handle = handle;
828 state.status = NT_STATUS_OK;
830 status = walk_xattr_streams(handle->conn, fsp, fname,
831 collect_one_stream, &state);
833 if (!NT_STATUS_IS_OK(status)) {
834 TALLOC_FREE(state.streams);
835 return status;
838 if (!NT_STATUS_IS_OK(state.status)) {
839 TALLOC_FREE(state.streams);
840 return state.status;
843 *pnum_streams = state.num_streams;
844 *pstreams = state.streams;
845 return NT_STATUS_OK;
848 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
849 enum timestamp_set_resolution *p_ts_res)
851 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
854 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
855 files_struct *fsp, const void *data,
856 size_t n, SMB_OFF_T offset)
858 struct stream_io *sio =
859 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
860 struct ea_struct ea;
861 NTSTATUS status;
862 int ret;
864 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
866 if (sio == NULL) {
867 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
870 if (!streams_xattr_recheck(sio)) {
871 return -1;
874 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
875 sio->base, sio->xattr_name, &ea);
876 if (!NT_STATUS_IS_OK(status)) {
877 return -1;
880 if ((offset + n) > ea.value.length-1) {
881 uint8 *tmp;
883 tmp = TALLOC_REALLOC_ARRAY(talloc_tos(), ea.value.data, uint8,
884 offset + n + 1);
886 if (tmp == NULL) {
887 TALLOC_FREE(ea.value.data);
888 errno = ENOMEM;
889 return -1;
891 ea.value.data = tmp;
892 ea.value.length = offset + n + 1;
893 ea.value.data[offset+n] = 0;
896 memcpy(ea.value.data + offset, data, n);
898 if (fsp->base_fsp->fh->fd != -1) {
899 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
900 sio->xattr_name,
901 ea.value.data, ea.value.length, 0);
902 } else {
903 ret = SMB_VFS_SETXATTR(fsp->conn,
904 fsp->base_fsp->fsp_name->base_name,
905 sio->xattr_name,
906 ea.value.data, ea.value.length, 0);
908 TALLOC_FREE(ea.value.data);
910 if (ret == -1) {
911 return -1;
914 return n;
917 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
918 files_struct *fsp, void *data,
919 size_t n, SMB_OFF_T offset)
921 struct stream_io *sio =
922 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
923 struct ea_struct ea;
924 NTSTATUS status;
925 size_t length, overlap;
927 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
928 (int)offset, (int)n));
930 if (sio == NULL) {
931 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
934 if (!streams_xattr_recheck(sio)) {
935 return -1;
938 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
939 sio->base, sio->xattr_name, &ea);
940 if (!NT_STATUS_IS_OK(status)) {
941 return -1;
944 length = ea.value.length-1;
946 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
947 (int)length));
949 /* Attempt to read past EOF. */
950 if (length <= offset) {
951 return 0;
954 overlap = (offset + n) > length ? (length - offset) : n;
955 memcpy(data, ea.value.data + offset, overlap);
957 TALLOC_FREE(ea.value.data);
958 return overlap;
961 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
962 struct files_struct *fsp,
963 SMB_OFF_T offset)
965 int ret;
966 uint8 *tmp;
967 struct ea_struct ea;
968 NTSTATUS status;
969 struct stream_io *sio =
970 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
972 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
973 fsp_str_dbg(fsp), (double)offset));
975 if (sio == NULL) {
976 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
979 if (!streams_xattr_recheck(sio)) {
980 return -1;
983 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
984 sio->base, sio->xattr_name, &ea);
985 if (!NT_STATUS_IS_OK(status)) {
986 return -1;
989 tmp = TALLOC_REALLOC_ARRAY(talloc_tos(), ea.value.data, uint8,
990 offset + 1);
992 if (tmp == NULL) {
993 TALLOC_FREE(ea.value.data);
994 errno = ENOMEM;
995 return -1;
998 /* Did we expand ? */
999 if (ea.value.length < offset + 1) {
1000 memset(&tmp[ea.value.length], '\0',
1001 offset + 1 - ea.value.length);
1004 ea.value.data = tmp;
1005 ea.value.length = offset + 1;
1006 ea.value.data[offset] = 0;
1008 if (fsp->base_fsp->fh->fd != -1) {
1009 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
1010 sio->xattr_name,
1011 ea.value.data, ea.value.length, 0);
1012 } else {
1013 ret = SMB_VFS_SETXATTR(fsp->conn,
1014 fsp->base_fsp->fsp_name->base_name,
1015 sio->xattr_name,
1016 ea.value.data, ea.value.length, 0);
1019 TALLOC_FREE(ea.value.data);
1021 if (ret == -1) {
1022 return -1;
1025 return 0;
1028 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1029 struct files_struct *fsp,
1030 enum vfs_fallocate_mode mode,
1031 SMB_OFF_T offset,
1032 SMB_OFF_T len)
1034 struct stream_io *sio =
1035 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1037 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1038 "len = %.0f\n",
1039 fsp_str_dbg(fsp), (double)offset, (double)len));
1041 if (sio == NULL) {
1042 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1045 if (!streams_xattr_recheck(sio)) {
1046 return errno;
1049 /* Let the pwrite code path handle it. */
1050 return ENOSYS;
1054 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1055 .fs_capabilities = streams_xattr_fs_capabilities,
1056 .open_fn = streams_xattr_open,
1057 .stat = streams_xattr_stat,
1058 .fstat = streams_xattr_fstat,
1059 .lstat = streams_xattr_lstat,
1060 .pread = streams_xattr_pread,
1061 .pwrite = streams_xattr_pwrite,
1062 .unlink = streams_xattr_unlink,
1063 .rename = streams_xattr_rename,
1064 .ftruncate = streams_xattr_ftruncate,
1065 .fallocate = streams_xattr_fallocate,
1066 .streaminfo = streams_xattr_streaminfo,
1069 NTSTATUS vfs_streams_xattr_init(void);
1070 NTSTATUS vfs_streams_xattr_init(void)
1072 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1073 &vfs_streams_xattr_fns);