vfs: Convert streams_xattr_unlink to cp_smb_filename
[Samba/bjacke.git] / source3 / modules / vfs_streams_xattr.c
blob87f8f000f0775de62de78aa90bd35f5a3b4147d9
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 NTSTATUS status;
198 int ret = -1;
199 struct stream_io *io = (struct stream_io *)
200 VFS_FETCH_FSP_EXTENSION(handle, fsp);
202 DEBUG(10, ("streams_xattr_fstat called for %d\n", fsp->fh->fd));
204 if (io == NULL || fsp->base_fsp == NULL) {
205 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
208 if (!streams_xattr_recheck(io)) {
209 return -1;
212 /* Create an smb_filename with stream_name == NULL. */
213 status = create_synthetic_smb_fname(talloc_tos(),
214 io->base,
215 NULL, NULL,
216 &smb_fname_base);
217 if (!NT_STATUS_IS_OK(status)) {
218 errno = map_errno_from_nt_status(status);
219 return -1;
222 if (lp_posix_pathnames()) {
223 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_base);
224 } else {
225 ret = SMB_VFS_STAT(handle->conn, smb_fname_base);
227 *sbuf = smb_fname_base->st;
228 TALLOC_FREE(smb_fname_base);
230 if (ret == -1) {
231 return -1;
234 sbuf->st_ex_size = get_xattr_size(handle->conn, fsp->base_fsp,
235 io->base, io->xattr_name);
236 if (sbuf->st_ex_size == -1) {
237 return -1;
240 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
242 sbuf->st_ex_ino = stream_inode(sbuf, io->xattr_name);
243 sbuf->st_ex_mode &= ~S_IFMT;
244 sbuf->st_ex_mode |= S_IFREG;
245 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
247 return 0;
250 static int streams_xattr_stat(vfs_handle_struct *handle,
251 struct smb_filename *smb_fname)
253 NTSTATUS status;
254 int result = -1;
255 char *xattr_name = NULL;
257 if (!is_ntfs_stream_smb_fname(smb_fname)) {
258 return SMB_VFS_NEXT_STAT(handle, smb_fname);
261 /* Note if lp_posix_paths() is true, we can never
262 * get here as is_ntfs_stream_smb_fname() is
263 * always false. So we never need worry about
264 * not following links here. */
266 /* If the default stream is requested, just stat the base file. */
267 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
268 return streams_xattr_stat_base(handle, smb_fname, true);
271 /* Populate the stat struct with info from the base file. */
272 if (streams_xattr_stat_base(handle, smb_fname, true) == -1) {
273 return -1;
276 /* Derive the xattr name to lookup. */
277 status = streams_xattr_get_name(talloc_tos(), smb_fname->stream_name,
278 &xattr_name);
279 if (!NT_STATUS_IS_OK(status)) {
280 errno = map_errno_from_nt_status(status);
281 return -1;
284 /* Augment the base file's stat information before returning. */
285 smb_fname->st.st_ex_size = get_xattr_size(handle->conn, NULL,
286 smb_fname->base_name,
287 xattr_name);
288 if (smb_fname->st.st_ex_size == -1) {
289 errno = ENOENT;
290 result = -1;
291 goto fail;
294 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
295 smb_fname->st.st_ex_mode &= ~S_IFMT;
296 smb_fname->st.st_ex_mode |= S_IFREG;
297 smb_fname->st.st_ex_blocks =
298 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
300 result = 0;
301 fail:
302 TALLOC_FREE(xattr_name);
303 return result;
306 static int streams_xattr_lstat(vfs_handle_struct *handle,
307 struct smb_filename *smb_fname)
309 NTSTATUS status;
310 int result = -1;
311 char *xattr_name = NULL;
313 if (!is_ntfs_stream_smb_fname(smb_fname)) {
314 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
317 /* If the default stream is requested, just stat the base file. */
318 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
319 return streams_xattr_stat_base(handle, smb_fname, false);
322 /* Populate the stat struct with info from the base file. */
323 if (streams_xattr_stat_base(handle, smb_fname, false) == -1) {
324 return -1;
327 /* Derive the xattr name to lookup. */
328 status = streams_xattr_get_name(talloc_tos(), smb_fname->stream_name,
329 &xattr_name);
330 if (!NT_STATUS_IS_OK(status)) {
331 errno = map_errno_from_nt_status(status);
332 return -1;
335 /* Augment the base file's stat information before returning. */
336 smb_fname->st.st_ex_size = get_xattr_size(handle->conn, NULL,
337 smb_fname->base_name,
338 xattr_name);
339 if (smb_fname->st.st_ex_size == -1) {
340 errno = ENOENT;
341 result = -1;
342 goto fail;
345 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
346 smb_fname->st.st_ex_mode &= ~S_IFMT;
347 smb_fname->st.st_ex_mode |= S_IFREG;
348 smb_fname->st.st_ex_blocks =
349 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
351 result = 0;
353 fail:
354 TALLOC_FREE(xattr_name);
355 return result;
358 static int streams_xattr_open(vfs_handle_struct *handle,
359 struct smb_filename *smb_fname,
360 files_struct *fsp, int flags, mode_t mode)
362 NTSTATUS status;
363 struct smb_filename *smb_fname_base = NULL;
364 struct stream_io *sio;
365 struct ea_struct ea;
366 char *xattr_name = NULL;
367 int baseflags;
368 int hostfd = -1;
370 DEBUG(10, ("streams_xattr_open called for %s\n",
371 smb_fname_str_dbg(smb_fname)));
373 if (!is_ntfs_stream_smb_fname(smb_fname)) {
374 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
377 /* If the default stream is requested, just open the base file. */
378 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
379 char *tmp_stream_name;
380 int ret;
382 tmp_stream_name = smb_fname->stream_name;
383 smb_fname->stream_name = NULL;
385 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
387 smb_fname->stream_name = tmp_stream_name;
389 return ret;
392 status = streams_xattr_get_name(talloc_tos(), smb_fname->stream_name,
393 &xattr_name);
394 if (!NT_STATUS_IS_OK(status)) {
395 errno = map_errno_from_nt_status(status);
396 goto fail;
399 /* Create an smb_filename with stream_name == NULL. */
400 status = create_synthetic_smb_fname(talloc_tos(),
401 smb_fname->base_name,
402 NULL, NULL,
403 &smb_fname_base);
404 if (!NT_STATUS_IS_OK(status)) {
405 errno = map_errno_from_nt_status(status);
406 goto fail;
410 * We use baseflags to turn off nasty side-effects when opening the
411 * underlying file.
413 baseflags = flags;
414 baseflags &= ~O_TRUNC;
415 baseflags &= ~O_EXCL;
416 baseflags &= ~O_CREAT;
418 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
419 baseflags, mode);
421 TALLOC_FREE(smb_fname_base);
423 /* It is legit to open a stream on a directory, but the base
424 * fd has to be read-only.
426 if ((hostfd == -1) && (errno == EISDIR)) {
427 baseflags &= ~O_ACCMODE;
428 baseflags |= O_RDONLY;
429 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname, fsp, baseflags,
430 mode);
433 if (hostfd == -1) {
434 goto fail;
437 status = get_ea_value(talloc_tos(), handle->conn, NULL,
438 smb_fname->base_name, xattr_name, &ea);
440 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
442 if (!NT_STATUS_IS_OK(status)
443 && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
445 * The base file is not there. This is an error even if we got
446 * O_CREAT, the higher levels should have created the base
447 * file for us.
449 DEBUG(10, ("streams_xattr_open: base file %s not around, "
450 "returning ENOENT\n", smb_fname->base_name));
451 errno = ENOENT;
452 goto fail;
455 if (!NT_STATUS_IS_OK(status)) {
457 * The attribute does not exist
460 if (flags & O_CREAT) {
462 * Darn, xattrs need at least 1 byte
464 char null = '\0';
466 DEBUG(10, ("creating attribute %s on file %s\n",
467 xattr_name, smb_fname->base_name));
469 if (fsp->base_fsp->fh->fd != -1) {
470 if (SMB_VFS_FSETXATTR(
471 fsp->base_fsp, xattr_name,
472 &null, sizeof(null),
473 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
474 goto fail;
476 } else {
477 if (SMB_VFS_SETXATTR(
478 handle->conn, smb_fname->base_name,
479 xattr_name, &null, sizeof(null),
480 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
481 goto fail;
487 if (flags & O_TRUNC) {
488 char null = '\0';
489 if (fsp->base_fsp->fh->fd != -1) {
490 if (SMB_VFS_FSETXATTR(
491 fsp->base_fsp, xattr_name,
492 &null, sizeof(null),
493 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
494 goto fail;
496 } else {
497 if (SMB_VFS_SETXATTR(
498 handle->conn, smb_fname->base_name,
499 xattr_name, &null, sizeof(null),
500 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
501 goto fail;
506 sio = (struct stream_io *)VFS_ADD_FSP_EXTENSION(handle, fsp,
507 struct stream_io,
508 NULL);
509 if (sio == NULL) {
510 errno = ENOMEM;
511 goto fail;
514 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
515 xattr_name);
516 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
517 smb_fname->base_name);
518 sio->fsp_name_ptr = fsp->fsp_name;
519 sio->handle = handle;
520 sio->fsp = fsp;
522 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
523 errno = ENOMEM;
524 goto fail;
527 return hostfd;
529 fail:
530 if (hostfd >= 0) {
532 * BUGBUGBUG -- we would need to call fd_close_posix here, but
533 * we don't have a full fsp yet
535 SMB_VFS_CLOSE(fsp);
538 return -1;
541 static int streams_xattr_unlink(vfs_handle_struct *handle,
542 const struct smb_filename *smb_fname)
544 NTSTATUS status;
545 int ret = -1;
546 char *xattr_name;
548 if (!is_ntfs_stream_smb_fname(smb_fname)) {
549 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
552 /* If the default stream is requested, just open the base file. */
553 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
554 struct smb_filename *smb_fname_base = NULL;
556 smb_fname_base = cp_smb_filename(talloc_tos(), smb_fname);
557 if (smb_fname_base == NULL) {
558 errno = ENOMEM;
559 return -1;
562 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_base);
564 TALLOC_FREE(smb_fname_base);
565 return ret;
568 status = streams_xattr_get_name(talloc_tos(), smb_fname->stream_name,
569 &xattr_name);
570 if (!NT_STATUS_IS_OK(status)) {
571 errno = map_errno_from_nt_status(status);
572 goto fail;
575 ret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname->base_name, xattr_name);
577 if ((ret == -1) && (errno == ENOATTR)) {
578 errno = ENOENT;
579 goto fail;
582 ret = 0;
584 fail:
585 TALLOC_FREE(xattr_name);
586 return ret;
589 static int streams_xattr_rename(vfs_handle_struct *handle,
590 const struct smb_filename *smb_fname_src,
591 const struct smb_filename *smb_fname_dst)
593 NTSTATUS status;
594 int ret = -1;
595 char *src_xattr_name = NULL;
596 char *dst_xattr_name = NULL;
597 bool src_is_stream, dst_is_stream;
598 ssize_t oret;
599 ssize_t nret;
600 struct ea_struct ea;
602 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
603 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
605 if (!src_is_stream && !dst_is_stream) {
606 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
607 smb_fname_dst);
610 /* For now don't allow renames from or to the default stream. */
611 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
612 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
613 errno = ENOSYS;
614 goto done;
617 /* Don't rename if the streams are identical. */
618 if (strcasecmp_m(smb_fname_src->stream_name,
619 smb_fname_dst->stream_name) == 0) {
620 goto done;
623 /* Get the xattr names. */
624 status = streams_xattr_get_name(talloc_tos(),
625 smb_fname_src->stream_name,
626 &src_xattr_name);
627 if (!NT_STATUS_IS_OK(status)) {
628 errno = map_errno_from_nt_status(status);
629 goto fail;
631 status = streams_xattr_get_name(talloc_tos(),
632 smb_fname_dst->stream_name,
633 &dst_xattr_name);
634 if (!NT_STATUS_IS_OK(status)) {
635 errno = map_errno_from_nt_status(status);
636 goto fail;
639 /* read the old stream */
640 status = get_ea_value(talloc_tos(), handle->conn, NULL,
641 smb_fname_src->base_name, src_xattr_name, &ea);
642 if (!NT_STATUS_IS_OK(status)) {
643 errno = ENOENT;
644 goto fail;
647 /* (over)write the new stream */
648 nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src->base_name,
649 dst_xattr_name, ea.value.data, ea.value.length,
651 if (nret < 0) {
652 if (errno == ENOATTR) {
653 errno = ENOENT;
655 goto fail;
658 /* remove the old stream */
659 oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src->base_name,
660 src_xattr_name);
661 if (oret < 0) {
662 if (errno == ENOATTR) {
663 errno = ENOENT;
665 goto fail;
668 done:
669 errno = 0;
670 ret = 0;
671 fail:
672 TALLOC_FREE(src_xattr_name);
673 TALLOC_FREE(dst_xattr_name);
674 return ret;
677 static NTSTATUS walk_xattr_streams(connection_struct *conn, files_struct *fsp,
678 const char *fname,
679 bool (*fn)(struct ea_struct *ea,
680 void *private_data),
681 void *private_data)
683 NTSTATUS status;
684 char **names;
685 size_t i, num_names;
686 size_t prefix_len = strlen(SAMBA_XATTR_DOSSTREAM_PREFIX);
688 status = get_ea_names_from_file(talloc_tos(), conn, fsp, fname,
689 &names, &num_names);
690 if (!NT_STATUS_IS_OK(status)) {
691 return status;
694 for (i=0; i<num_names; i++) {
695 struct ea_struct ea;
697 if (strncmp(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
698 prefix_len) != 0) {
699 continue;
702 status = get_ea_value(names, conn, fsp, fname, names[i], &ea);
703 if (!NT_STATUS_IS_OK(status)) {
704 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
705 names[i], fname, nt_errstr(status)));
706 continue;
709 ea.name = talloc_asprintf(ea.value.data, ":%s",
710 names[i] + prefix_len);
711 if (ea.name == NULL) {
712 DEBUG(0, ("talloc failed\n"));
713 continue;
716 if (!fn(&ea, private_data)) {
717 TALLOC_FREE(ea.value.data);
718 return NT_STATUS_OK;
721 TALLOC_FREE(ea.value.data);
724 TALLOC_FREE(names);
725 return NT_STATUS_OK;
728 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
729 struct stream_struct **streams,
730 const char *name, off_t size,
731 off_t alloc_size)
733 struct stream_struct *tmp;
735 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
736 (*num_streams)+1);
737 if (tmp == NULL) {
738 return false;
741 tmp[*num_streams].name = talloc_strdup(tmp, name);
742 if (tmp[*num_streams].name == NULL) {
743 return false;
746 tmp[*num_streams].size = size;
747 tmp[*num_streams].alloc_size = alloc_size;
749 *streams = tmp;
750 *num_streams += 1;
751 return true;
754 struct streaminfo_state {
755 TALLOC_CTX *mem_ctx;
756 vfs_handle_struct *handle;
757 unsigned int num_streams;
758 struct stream_struct *streams;
759 NTSTATUS status;
762 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
764 struct streaminfo_state *state =
765 (struct streaminfo_state *)private_data;
767 if (!add_one_stream(state->mem_ctx,
768 &state->num_streams, &state->streams,
769 ea->name, ea->value.length-1,
770 smb_roundup(state->handle->conn,
771 ea->value.length-1))) {
772 state->status = NT_STATUS_NO_MEMORY;
773 return false;
776 return true;
779 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
780 struct files_struct *fsp,
781 const char *fname,
782 TALLOC_CTX *mem_ctx,
783 unsigned int *pnum_streams,
784 struct stream_struct **pstreams)
786 SMB_STRUCT_STAT sbuf;
787 int ret;
788 NTSTATUS status;
789 struct streaminfo_state state;
791 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
792 ret = SMB_VFS_FSTAT(fsp, &sbuf);
794 else {
795 struct smb_filename *smb_fname = NULL;
796 status = create_synthetic_smb_fname(talloc_tos(), fname, NULL,
797 NULL, &smb_fname);
798 if (!NT_STATUS_IS_OK(status)) {
799 return status;
801 if (lp_posix_pathnames()) {
802 ret = SMB_VFS_LSTAT(handle->conn, smb_fname);
803 } else {
804 ret = SMB_VFS_STAT(handle->conn, smb_fname);
806 sbuf = smb_fname->st;
807 TALLOC_FREE(smb_fname);
810 if (ret == -1) {
811 return map_nt_error_from_unix(errno);
814 state.streams = *pstreams;
815 state.num_streams = *pnum_streams;
816 state.mem_ctx = mem_ctx;
817 state.handle = handle;
818 state.status = NT_STATUS_OK;
820 status = walk_xattr_streams(handle->conn, fsp, fname,
821 collect_one_stream, &state);
823 if (!NT_STATUS_IS_OK(status)) {
824 TALLOC_FREE(state.streams);
825 return status;
828 if (!NT_STATUS_IS_OK(state.status)) {
829 TALLOC_FREE(state.streams);
830 return state.status;
833 *pnum_streams = state.num_streams;
834 *pstreams = state.streams;
836 return SMB_VFS_NEXT_STREAMINFO(handle, fsp, fname, mem_ctx, pnum_streams, pstreams);
839 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
840 enum timestamp_set_resolution *p_ts_res)
842 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
845 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
846 files_struct *fsp, const void *data,
847 size_t n, off_t offset)
849 struct stream_io *sio =
850 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
851 struct ea_struct ea;
852 NTSTATUS status;
853 int ret;
855 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
857 if (sio == NULL) {
858 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
861 if (!streams_xattr_recheck(sio)) {
862 return -1;
865 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
866 sio->base, sio->xattr_name, &ea);
867 if (!NT_STATUS_IS_OK(status)) {
868 return -1;
871 if ((offset + n) > ea.value.length-1) {
872 uint8 *tmp;
874 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8,
875 offset + n + 1);
877 if (tmp == NULL) {
878 TALLOC_FREE(ea.value.data);
879 errno = ENOMEM;
880 return -1;
882 ea.value.data = tmp;
883 ea.value.length = offset + n + 1;
884 ea.value.data[offset+n] = 0;
887 memcpy(ea.value.data + offset, data, n);
889 if (fsp->base_fsp->fh->fd != -1) {
890 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
891 sio->xattr_name,
892 ea.value.data, ea.value.length, 0);
893 } else {
894 ret = SMB_VFS_SETXATTR(fsp->conn,
895 fsp->base_fsp->fsp_name->base_name,
896 sio->xattr_name,
897 ea.value.data, ea.value.length, 0);
899 TALLOC_FREE(ea.value.data);
901 if (ret == -1) {
902 return -1;
905 return n;
908 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
909 files_struct *fsp, void *data,
910 size_t n, off_t offset)
912 struct stream_io *sio =
913 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
914 struct ea_struct ea;
915 NTSTATUS status;
916 size_t length, overlap;
918 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
919 (int)offset, (int)n));
921 if (sio == NULL) {
922 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
925 if (!streams_xattr_recheck(sio)) {
926 return -1;
929 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
930 sio->base, sio->xattr_name, &ea);
931 if (!NT_STATUS_IS_OK(status)) {
932 return -1;
935 length = ea.value.length-1;
937 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
938 (int)length));
940 /* Attempt to read past EOF. */
941 if (length <= offset) {
942 return 0;
945 overlap = (offset + n) > length ? (length - offset) : n;
946 memcpy(data, ea.value.data + offset, overlap);
948 TALLOC_FREE(ea.value.data);
949 return overlap;
952 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
953 struct files_struct *fsp,
954 off_t offset)
956 int ret;
957 uint8 *tmp;
958 struct ea_struct ea;
959 NTSTATUS status;
960 struct stream_io *sio =
961 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
963 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
964 fsp_str_dbg(fsp), (double)offset));
966 if (sio == NULL) {
967 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
970 if (!streams_xattr_recheck(sio)) {
971 return -1;
974 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
975 sio->base, sio->xattr_name, &ea);
976 if (!NT_STATUS_IS_OK(status)) {
977 return -1;
980 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8,
981 offset + 1);
983 if (tmp == NULL) {
984 TALLOC_FREE(ea.value.data);
985 errno = ENOMEM;
986 return -1;
989 /* Did we expand ? */
990 if (ea.value.length < offset + 1) {
991 memset(&tmp[ea.value.length], '\0',
992 offset + 1 - ea.value.length);
995 ea.value.data = tmp;
996 ea.value.length = offset + 1;
997 ea.value.data[offset] = 0;
999 if (fsp->base_fsp->fh->fd != -1) {
1000 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
1001 sio->xattr_name,
1002 ea.value.data, ea.value.length, 0);
1003 } else {
1004 ret = SMB_VFS_SETXATTR(fsp->conn,
1005 fsp->base_fsp->fsp_name->base_name,
1006 sio->xattr_name,
1007 ea.value.data, ea.value.length, 0);
1010 TALLOC_FREE(ea.value.data);
1012 if (ret == -1) {
1013 return -1;
1016 return 0;
1019 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1020 struct files_struct *fsp,
1021 enum vfs_fallocate_mode mode,
1022 off_t offset,
1023 off_t len)
1025 struct stream_io *sio =
1026 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1028 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1029 "len = %.0f\n",
1030 fsp_str_dbg(fsp), (double)offset, (double)len));
1032 if (sio == NULL) {
1033 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1036 if (!streams_xattr_recheck(sio)) {
1037 return errno;
1040 /* Let the pwrite code path handle it. */
1041 return ENOSYS;
1045 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1046 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1047 .open_fn = streams_xattr_open,
1048 .stat_fn = streams_xattr_stat,
1049 .fstat_fn = streams_xattr_fstat,
1050 .lstat_fn = streams_xattr_lstat,
1051 .pread_fn = streams_xattr_pread,
1052 .pwrite_fn = streams_xattr_pwrite,
1053 .unlink_fn = streams_xattr_unlink,
1054 .rename_fn = streams_xattr_rename,
1055 .ftruncate_fn = streams_xattr_ftruncate,
1056 .fallocate_fn = streams_xattr_fallocate,
1057 .streaminfo_fn = streams_xattr_streaminfo,
1060 NTSTATUS vfs_streams_xattr_init(void);
1061 NTSTATUS vfs_streams_xattr_init(void)
1063 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1064 &vfs_streams_xattr_fns);