2 * Store streams in a separate subdirectory
4 * Copyright (C) Volker Lendecke, 2007
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "smbd/smbd.h"
22 #include "system/filesys.h"
25 #define DBGC_CLASS DBGC_VFS
28 * Excerpt from a mail from tridge:
30 * Volker, what I'm thinking of is this:
31 * /mount-point/.streams/XX/YY/aaaa.bbbb/namedstream1
32 * /mount-point/.streams/XX/YY/aaaa.bbbb/namedstream2
34 * where XX/YY is a 2 level hash based on the fsid/inode. "aaaa.bbbb"
35 * is the fsid/inode. "namedstreamX" is a file named after the stream
39 static uint32_t hash_fn(DATA_BLOB key)
41 uint32_t value; /* Used to compute the hash value. */
42 uint32_t i; /* Used to cycle through random values. */
44 /* Set the initial value from the key size. */
45 for (value = 0x238F13AF * key.length, i=0; i < key.length; i++)
46 value = (value + (key.data[i] << (i*5 % 24)));
48 return (1103515243 * value + 12345);
52 * With the hashing scheme based on the inode we need to protect against
53 * streams showing up on files with re-used inodes. This can happen if we
54 * create a stream directory from within Samba, and a local process or NFS
55 * client deletes the file without deleting the streams directory. When the
56 * inode is re-used and the stream directory is still around, the streams in
57 * there would be show up as belonging to the new file.
59 * There are several workarounds for this, probably the easiest one is on
60 * systems which have a true birthtime stat element: When the file has a later
61 * birthtime than the streams directory, then we have to recreate the
64 * The other workaround is to somehow mark the file as generated by Samba with
65 * something that a NFS client would not do. The closest one is a special
66 * xattr value being set. On systems which do not support xattrs, it might be
67 * an option to put in a special ACL entry for a non-existing group.
70 static bool file_is_valid(vfs_handle_struct *handle,
71 const struct smb_filename *smb_fname)
75 struct smb_filename *pathref = NULL;
78 DEBUG(10, ("file_is_valid (%s) called\n", smb_fname->base_name));
80 status = synthetic_pathref(talloc_tos(),
81 handle->conn->cwd_fsp,
88 if (!NT_STATUS_IS_OK(status)) {
91 ret = SMB_VFS_FGETXATTR(pathref->fsp,
95 if (ret != sizeof(buf)) {
96 int saved_errno = errno;
97 DBG_DEBUG("FGETXATTR failed: %s\n", strerror(saved_errno));
103 TALLOC_FREE(pathref);
106 DEBUG(10, ("got wrong buffer content: '%c'\n", buf));
113 static bool mark_file_valid(vfs_handle_struct *handle,
114 const struct smb_filename *smb_fname)
119 DEBUG(10, ("marking file %s as valid\n", smb_fname->base_name));
121 ret = SMB_VFS_FSETXATTR(smb_fname->fsp, SAMBA_XATTR_MARKER,
122 &buf, sizeof(buf), 0);
125 DEBUG(10, ("SETXATTR failed: %s\n", strerror(errno)));
133 * Return the root of the stream directory. Can be
134 * external to the share definition but by default
135 * is "handle->conn->connectpath/.streams".
137 * Note that this is an *absolute* path, starting
138 * with '/', so the dirfsp being used in the
139 * calls below isn't looked at.
142 static char *stream_rootdir(vfs_handle_struct *handle,
145 const struct loadparm_substitution *lp_sub =
146 loadparm_s3_global_substitution();
149 tmp = talloc_asprintf(ctx,
151 handle->conn->connectpath);
157 return lp_parm_substituted_string(ctx,
166 * Given an smb_filename, determine the stream directory using the file's
169 static char *stream_dir(vfs_handle_struct *handle,
170 const struct smb_filename *smb_fname,
171 const SMB_STRUCT_STAT *base_sbuf, bool create_it)
174 struct smb_filename *smb_fname_hash = NULL;
176 SMB_STRUCT_STAT base_sbuf_tmp;
178 uint8_t first, second;
183 char *rootdir = NULL;
184 struct smb_filename *rootdir_fname = NULL;
185 struct smb_filename *tmp_fname = NULL;
186 struct smb_filename *tmpref = NULL;
187 const struct smb_filename *pathref = NULL;
190 check_valid = lp_parm_bool(SNUM(handle->conn),
191 "streams_depot", "check_valid", true);
193 rootdir = stream_rootdir(handle,
195 if (rootdir == NULL) {
200 rootdir_fname = synthetic_smb_fname(talloc_tos(),
206 if (rootdir_fname == NULL) {
211 /* Stat the base file if it hasn't already been done. */
212 if (base_sbuf == NULL) {
213 struct smb_filename *smb_fname_base;
215 smb_fname_base = synthetic_smb_fname(
217 smb_fname->base_name,
222 if (smb_fname_base == NULL) {
226 if (SMB_VFS_NEXT_STAT(handle, smb_fname_base) == -1) {
227 TALLOC_FREE(smb_fname_base);
230 base_sbuf_tmp = smb_fname_base->st;
231 TALLOC_FREE(smb_fname_base);
233 base_sbuf_tmp = *base_sbuf;
236 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &base_sbuf_tmp);
238 push_file_id_16((char *)id_buf, &id);
240 hash = hash_fn(data_blob_const(id_buf, sizeof(id_buf)));
243 second = (hash >> 8) & 0xff;
245 id_hex = hex_encode_talloc(talloc_tos(), id_buf, sizeof(id_buf));
247 if (id_hex == NULL) {
252 result = talloc_asprintf(talloc_tos(), "%s/%2.2X/%2.2X/%s", rootdir,
253 first, second, id_hex);
257 if (result == NULL) {
262 smb_fname_hash = synthetic_smb_fname(talloc_tos(),
268 if (smb_fname_hash == NULL) {
273 if (SMB_VFS_NEXT_STAT(handle, smb_fname_hash) == 0) {
274 struct smb_filename *smb_fname_new = NULL;
278 if (!S_ISDIR(smb_fname_hash->st.st_ex_mode)) {
284 file_is_valid(handle, smb_fname)) {
289 * Someone has recreated a file under an existing inode
290 * without deleting the streams directory.
291 * Move it away or remove if streams_depot:delete_lost is set.
295 delete_lost = lp_parm_bool(SNUM(handle->conn), "streams_depot",
296 "delete_lost", false);
299 DEBUG(3, ("Someone has recreated a file under an "
300 "existing inode. Removing: %s\n",
301 smb_fname_hash->base_name));
302 recursive_rmdir(talloc_tos(), handle->conn,
304 SMB_VFS_NEXT_UNLINKAT(handle,
305 handle->conn->cwd_fsp,
309 newname = talloc_asprintf(talloc_tos(), "lost-%lu",
311 DEBUG(3, ("Someone has recreated a file under an "
312 "existing inode. Renaming: %s to: %s\n",
313 smb_fname_hash->base_name,
315 if (newname == NULL) {
320 smb_fname_new = synthetic_smb_fname(
327 TALLOC_FREE(newname);
328 if (smb_fname_new == NULL) {
333 if (SMB_VFS_NEXT_RENAMEAT(handle,
334 handle->conn->cwd_fsp,
336 handle->conn->cwd_fsp,
337 smb_fname_new) == -1) {
338 TALLOC_FREE(smb_fname_new);
339 if ((errno == EEXIST) || (errno == ENOTEMPTY)) {
345 TALLOC_FREE(smb_fname_new);
354 ret = SMB_VFS_NEXT_MKDIRAT(handle,
355 handle->conn->cwd_fsp,
358 if ((ret != 0) && (errno != EEXIST)) {
362 tmp = talloc_asprintf(result, "%s/%2.2X", rootdir, first);
368 tmp_fname = synthetic_smb_fname(talloc_tos(),
374 if (tmp_fname == NULL) {
379 ret = SMB_VFS_NEXT_MKDIRAT(handle,
380 handle->conn->cwd_fsp,
383 if ((ret != 0) && (errno != EEXIST)) {
388 TALLOC_FREE(tmp_fname);
390 tmp = talloc_asprintf(result, "%s/%2.2X/%2.2X", rootdir, first,
397 tmp_fname = synthetic_smb_fname(talloc_tos(),
403 if (tmp_fname == NULL) {
408 ret = SMB_VFS_NEXT_MKDIRAT(handle,
409 handle->conn->cwd_fsp,
412 if ((ret != 0) && (errno != EEXIST)) {
417 TALLOC_FREE(tmp_fname);
419 /* smb_fname_hash is the struct smb_filename version of 'result' */
420 ret = SMB_VFS_NEXT_MKDIRAT(handle,
421 handle->conn->cwd_fsp,
424 if ((ret != 0) && (errno != EEXIST)) {
428 if (smb_fname->fsp == NULL) {
430 status = synthetic_pathref(talloc_tos(),
431 handle->conn->cwd_fsp,
432 smb_fname->base_name,
438 if (!NT_STATUS_IS_OK(status)) {
443 if (check_valid && !mark_file_valid(handle, pathref)) {
448 TALLOC_FREE(rootdir_fname);
449 TALLOC_FREE(rootdir);
450 TALLOC_FREE(tmp_fname);
451 TALLOC_FREE(smb_fname_hash);
456 TALLOC_FREE(rootdir_fname);
457 TALLOC_FREE(rootdir);
458 TALLOC_FREE(tmp_fname);
459 TALLOC_FREE(smb_fname_hash);
464 * Given a stream name, populate smb_fname_out with the actual location of the
467 static NTSTATUS stream_smb_fname(vfs_handle_struct *handle,
468 const struct smb_filename *smb_fname,
469 struct smb_filename **smb_fname_out,
472 char *dirname, *stream_fname;
476 *smb_fname_out = NULL;
478 stype = strchr_m(smb_fname->stream_name + 1, ':');
481 if (strcasecmp_m(stype, ":$DATA") != 0) {
482 return NT_STATUS_INVALID_PARAMETER;
486 dirname = stream_dir(handle, smb_fname, NULL, create_dir);
488 if (dirname == NULL) {
489 status = map_nt_error_from_unix(errno);
493 stream_fname = talloc_asprintf(talloc_tos(), "%s/%s", dirname,
494 smb_fname->stream_name);
496 if (stream_fname == NULL) {
497 status = NT_STATUS_NO_MEMORY;
502 /* Append an explicit stream type if one wasn't specified. */
503 stream_fname = talloc_asprintf(talloc_tos(), "%s:$DATA",
505 if (stream_fname == NULL) {
506 status = NT_STATUS_NO_MEMORY;
510 /* Normalize the stream type to upercase. */
511 if (!strupper_m(strrchr_m(stream_fname, ':') + 1)) {
512 status = NT_STATUS_INVALID_PARAMETER;
517 DEBUG(10, ("stream filename = %s\n", stream_fname));
519 /* Create an smb_filename with stream_name == NULL. */
520 *smb_fname_out = synthetic_smb_fname(talloc_tos(),
526 if (*smb_fname_out == NULL) {
527 return NT_STATUS_NO_MEMORY;
533 DEBUG(5, ("stream_name failed: %s\n", strerror(errno)));
534 TALLOC_FREE(*smb_fname_out);
538 static NTSTATUS walk_streams(vfs_handle_struct *handle,
539 struct smb_filename *smb_fname_base,
541 bool (*fn)(const struct smb_filename *dirname,
547 char *rootdir = NULL;
548 char *orig_connectpath = NULL;
549 struct smb_filename *dir_smb_fname = NULL;
550 struct smb_Dir *dir_hnd = NULL;
551 const char *dname = NULL;
553 char *talloced = NULL;
555 dirname = stream_dir(handle, smb_fname_base, &smb_fname_base->st,
558 if (dirname == NULL) {
559 if (errno == ENOENT) {
565 return map_nt_error_from_unix(errno);
568 DEBUG(10, ("walk_streams: dirname=%s\n", dirname));
570 dir_smb_fname = synthetic_smb_fname(talloc_tos(),
574 smb_fname_base->twrp,
575 smb_fname_base->flags);
576 if (dir_smb_fname == NULL) {
577 TALLOC_FREE(dirname);
578 return NT_STATUS_NO_MEMORY;
582 * For OpenDir to succeed if the stream rootdir is outside
583 * the share path, we must temporarily swap out the connect
584 * path for this share. We're dealing with absolute paths
585 * here so we don't care about chdir calls.
587 rootdir = stream_rootdir(handle, talloc_tos());
588 if (rootdir == NULL) {
589 TALLOC_FREE(dir_smb_fname);
590 TALLOC_FREE(dirname);
591 return NT_STATUS_NO_MEMORY;
594 orig_connectpath = handle->conn->connectpath;
595 handle->conn->connectpath = rootdir;
597 dir_hnd = OpenDir(talloc_tos(), handle->conn, dir_smb_fname, NULL, 0);
598 if (dir_hnd == NULL) {
599 handle->conn->connectpath = orig_connectpath;
600 TALLOC_FREE(rootdir);
601 TALLOC_FREE(dir_smb_fname);
602 TALLOC_FREE(dirname);
603 return map_nt_error_from_unix(errno);
606 while ((dname = ReadDirName(dir_hnd, &offset, NULL, &talloced))
609 if (ISDOT(dname) || ISDOTDOT(dname)) {
610 TALLOC_FREE(talloced);
614 DBG_DEBUG("dirent=%s\n", dname);
616 if (!fn(dir_smb_fname, dname, private_data)) {
617 TALLOC_FREE(talloced);
620 TALLOC_FREE(talloced);
623 /* Restore the original connectpath. */
624 handle->conn->connectpath = orig_connectpath;
625 TALLOC_FREE(rootdir);
626 TALLOC_FREE(dir_smb_fname);
627 TALLOC_FREE(dir_hnd);
629 if (pdirname != NULL) {
633 TALLOC_FREE(dirname);
639 static int streams_depot_stat(vfs_handle_struct *handle,
640 struct smb_filename *smb_fname)
642 struct smb_filename *smb_fname_stream = NULL;
646 DEBUG(10, ("streams_depot_stat called for [%s]\n",
647 smb_fname_str_dbg(smb_fname)));
649 if (!is_named_stream(smb_fname)) {
650 return SMB_VFS_NEXT_STAT(handle, smb_fname);
653 /* Stat the actual stream now. */
654 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
656 if (!NT_STATUS_IS_OK(status)) {
658 errno = map_errno_from_nt_status(status);
662 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_stream);
664 /* Update the original smb_fname with the stat info. */
665 smb_fname->st = smb_fname_stream->st;
667 TALLOC_FREE(smb_fname_stream);
673 static int streams_depot_lstat(vfs_handle_struct *handle,
674 struct smb_filename *smb_fname)
676 struct smb_filename *smb_fname_stream = NULL;
680 DEBUG(10, ("streams_depot_lstat called for [%s]\n",
681 smb_fname_str_dbg(smb_fname)));
683 if (!is_named_stream(smb_fname)) {
684 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
687 /* Stat the actual stream now. */
688 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
690 if (!NT_STATUS_IS_OK(status)) {
692 errno = map_errno_from_nt_status(status);
696 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_stream);
699 TALLOC_FREE(smb_fname_stream);
703 static int streams_depot_openat(struct vfs_handle_struct *handle,
704 const struct files_struct *dirfsp,
705 const struct smb_filename *smb_fname,
706 struct files_struct *fsp,
710 struct smb_filename *smb_fname_stream = NULL;
711 struct smb_filename *smb_fname_base = NULL;
712 struct files_struct *fspcwd = NULL;
716 if (!is_named_stream(smb_fname)) {
717 return SMB_VFS_NEXT_OPENAT(handle,
726 * For now assert this so the below SMB_VFS_STAT() is ok.
728 SMB_ASSERT(fsp_get_pathref_fd(dirfsp) == AT_FDCWD);
730 /* Ensure the base file still exists. */
731 smb_fname_base = synthetic_smb_fname(talloc_tos(),
732 smb_fname->base_name,
737 if (smb_fname_base == NULL) {
743 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
748 /* Determine the stream name, and then open it. */
749 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream, true);
750 if (!NT_STATUS_IS_OK(status)) {
752 errno = map_errno_from_nt_status(status);
756 status = vfs_at_fspcwd(talloc_tos(), handle->conn, &fspcwd);
757 if (!NT_STATUS_IS_OK(status)) {
759 errno = map_errno_from_nt_status(status);
763 ret = SMB_VFS_NEXT_OPENAT(handle,
771 TALLOC_FREE(smb_fname_stream);
772 TALLOC_FREE(smb_fname_base);
777 static int streams_depot_unlink_internal(vfs_handle_struct *handle,
778 struct files_struct *dirfsp,
779 const struct smb_filename *smb_fname,
782 struct smb_filename *full_fname = NULL;
783 char *dirname = NULL;
786 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
789 if (full_fname == NULL) {
793 DEBUG(10, ("streams_depot_unlink called for %s\n",
794 smb_fname_str_dbg(full_fname)));
796 /* If there is a valid stream, just unlink the stream and return. */
797 if (is_named_stream(full_fname)) {
798 struct smb_filename *smb_fname_stream = NULL;
801 status = stream_smb_fname(handle, full_fname, &smb_fname_stream,
803 TALLOC_FREE(full_fname);
804 if (!NT_STATUS_IS_OK(status)) {
805 errno = map_errno_from_nt_status(status);
809 ret = SMB_VFS_NEXT_UNLINKAT(handle,
810 dirfsp->conn->cwd_fsp,
814 TALLOC_FREE(smb_fname_stream);
819 * We potentially need to delete the per-inode streams directory
822 if (full_fname->flags & SMB_FILENAME_POSIX_PATH) {
823 ret = SMB_VFS_NEXT_LSTAT(handle, full_fname);
825 ret = SMB_VFS_NEXT_STAT(handle, full_fname);
828 TALLOC_FREE(full_fname);
833 * We know the unlink should succeed as the ACL
834 * check is already done in the caller. Remove the
835 * file *after* the streams.
837 dirname = stream_dir(handle,
841 TALLOC_FREE(full_fname);
842 if (dirname != NULL) {
843 struct smb_filename *smb_fname_dir = NULL;
845 smb_fname_dir = synthetic_smb_fname(talloc_tos(),
851 if (smb_fname_dir == NULL) {
852 TALLOC_FREE(dirname);
857 SMB_VFS_NEXT_UNLINKAT(handle,
858 dirfsp->conn->cwd_fsp,
861 TALLOC_FREE(smb_fname_dir);
862 TALLOC_FREE(dirname);
865 ret = SMB_VFS_NEXT_UNLINKAT(handle,
872 static int streams_depot_rmdir_internal(vfs_handle_struct *handle,
873 struct files_struct *dirfsp,
874 const struct smb_filename *smb_fname)
876 struct smb_filename *full_fname = NULL;
877 struct smb_filename *smb_fname_base = NULL;
880 full_fname = full_path_from_dirfsp_atname(talloc_tos(),
883 if (full_fname == NULL) {
887 DBG_DEBUG("called for %s\n", full_fname->base_name);
890 * We potentially need to delete the per-inode streams directory
893 smb_fname_base = synthetic_smb_fname(talloc_tos(),
894 full_fname->base_name,
899 TALLOC_FREE(full_fname);
900 if (smb_fname_base == NULL) {
905 if (smb_fname_base->flags & SMB_FILENAME_POSIX_PATH) {
906 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
908 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
912 TALLOC_FREE(smb_fname_base);
917 * We know the rmdir should succeed as the ACL
918 * check is already done in the caller. Remove the
919 * directory *after* the streams.
922 char *dirname = stream_dir(handle, smb_fname_base,
923 &smb_fname_base->st, false);
925 if (dirname != NULL) {
926 struct smb_filename *smb_fname_dir =
927 synthetic_smb_fname(talloc_tos(),
933 if (smb_fname_dir == NULL) {
934 TALLOC_FREE(smb_fname_base);
935 TALLOC_FREE(dirname);
939 SMB_VFS_NEXT_UNLINKAT(handle,
940 dirfsp->conn->cwd_fsp,
943 TALLOC_FREE(smb_fname_dir);
945 TALLOC_FREE(dirname);
948 ret = SMB_VFS_NEXT_UNLINKAT(handle,
952 TALLOC_FREE(smb_fname_base);
956 static int streams_depot_unlinkat(vfs_handle_struct *handle,
957 struct files_struct *dirfsp,
958 const struct smb_filename *smb_fname,
962 if (flags & AT_REMOVEDIR) {
963 ret = streams_depot_rmdir_internal(handle,
967 ret = streams_depot_unlink_internal(handle,
975 static int streams_depot_renameat(vfs_handle_struct *handle,
976 files_struct *srcfsp,
977 const struct smb_filename *smb_fname_src,
978 files_struct *dstfsp,
979 const struct smb_filename *smb_fname_dst)
981 struct smb_filename *smb_fname_src_stream = NULL;
982 struct smb_filename *smb_fname_dst_stream = NULL;
983 struct smb_filename *full_src = NULL;
984 struct smb_filename *full_dst = NULL;
985 bool src_is_stream, dst_is_stream;
989 DEBUG(10, ("streams_depot_renameat called for %s => %s\n",
990 smb_fname_str_dbg(smb_fname_src),
991 smb_fname_str_dbg(smb_fname_dst)));
993 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
994 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
996 if (!src_is_stream && !dst_is_stream) {
997 return SMB_VFS_NEXT_RENAMEAT(handle,
1004 /* for now don't allow renames from or to the default stream */
1005 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
1006 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
1011 full_src = full_path_from_dirfsp_atname(talloc_tos(),
1014 if (full_src == NULL) {
1019 full_dst = full_path_from_dirfsp_atname(talloc_tos(),
1022 if (full_dst == NULL) {
1027 status = stream_smb_fname(handle, full_src, &smb_fname_src_stream,
1029 if (!NT_STATUS_IS_OK(status)) {
1030 errno = map_errno_from_nt_status(status);
1034 status = stream_smb_fname(handle, full_dst,
1035 &smb_fname_dst_stream, false);
1036 if (!NT_STATUS_IS_OK(status)) {
1037 errno = map_errno_from_nt_status(status);
1042 * We must use handle->conn->cwd_fsp as
1043 * srcfsp and dstfsp directory handles here
1044 * as we used the full pathname from the cwd dir
1045 * to calculate the streams directory and filename
1048 ret = SMB_VFS_NEXT_RENAMEAT(handle,
1049 handle->conn->cwd_fsp,
1050 smb_fname_src_stream,
1051 handle->conn->cwd_fsp,
1052 smb_fname_dst_stream);
1055 TALLOC_FREE(smb_fname_src_stream);
1056 TALLOC_FREE(smb_fname_dst_stream);
1060 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1061 struct stream_struct **streams,
1062 const char *name, off_t size,
1065 struct stream_struct *tmp;
1067 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
1073 tmp[*num_streams].name = talloc_strdup(tmp, name);
1074 if (tmp[*num_streams].name == NULL) {
1078 tmp[*num_streams].size = size;
1079 tmp[*num_streams].alloc_size = alloc_size;
1086 struct streaminfo_state {
1087 TALLOC_CTX *mem_ctx;
1088 vfs_handle_struct *handle;
1089 unsigned int num_streams;
1090 struct stream_struct *streams;
1094 static bool collect_one_stream(const struct smb_filename *dirfname,
1098 const char *dirname = dirfname->base_name;
1099 struct streaminfo_state *state =
1100 (struct streaminfo_state *)private_data;
1101 struct smb_filename *smb_fname = NULL;
1105 sname = talloc_asprintf(talloc_tos(), "%s/%s", dirname, dirent);
1106 if (sname == NULL) {
1107 state->status = NT_STATUS_NO_MEMORY;
1112 smb_fname = synthetic_smb_fname(talloc_tos(),
1118 if (smb_fname == NULL) {
1119 state->status = NT_STATUS_NO_MEMORY;
1124 if (SMB_VFS_NEXT_STAT(state->handle, smb_fname) == -1) {
1125 DEBUG(10, ("Could not stat %s: %s\n", sname,
1131 if (!add_one_stream(state->mem_ctx,
1132 &state->num_streams, &state->streams,
1133 dirent, smb_fname->st.st_ex_size,
1134 SMB_VFS_GET_ALLOC_SIZE(state->handle->conn, NULL,
1136 state->status = NT_STATUS_NO_MEMORY;
1144 TALLOC_FREE(smb_fname);
1148 static NTSTATUS streams_depot_fstreaminfo(vfs_handle_struct *handle,
1149 struct files_struct *fsp,
1150 TALLOC_CTX *mem_ctx,
1151 unsigned int *pnum_streams,
1152 struct stream_struct **pstreams)
1154 struct smb_filename *smb_fname_base = NULL;
1157 struct streaminfo_state state;
1159 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1160 fsp->fsp_name->base_name,
1163 fsp->fsp_name->twrp,
1164 fsp->fsp_name->flags);
1165 if (smb_fname_base == NULL) {
1166 return NT_STATUS_NO_MEMORY;
1169 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, &smb_fname_base->st);
1171 status = map_nt_error_from_unix(errno);
1175 state.streams = *pstreams;
1176 state.num_streams = *pnum_streams;
1177 state.mem_ctx = mem_ctx;
1178 state.handle = handle;
1179 state.status = NT_STATUS_OK;
1181 status = walk_streams(handle,
1187 if (!NT_STATUS_IS_OK(status)) {
1188 TALLOC_FREE(state.streams);
1192 if (!NT_STATUS_IS_OK(state.status)) {
1193 TALLOC_FREE(state.streams);
1194 status = state.status;
1198 *pnum_streams = state.num_streams;
1199 *pstreams = state.streams;
1200 status = SMB_VFS_NEXT_FSTREAMINFO(handle,
1201 fsp->base_fsp ? fsp->base_fsp : fsp,
1207 TALLOC_FREE(smb_fname_base);
1211 static uint32_t streams_depot_fs_capabilities(struct vfs_handle_struct *handle,
1212 enum timestamp_set_resolution *p_ts_res)
1214 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
1217 static struct vfs_fn_pointers vfs_streams_depot_fns = {
1218 .fs_capabilities_fn = streams_depot_fs_capabilities,
1219 .openat_fn = streams_depot_openat,
1220 .stat_fn = streams_depot_stat,
1221 .lstat_fn = streams_depot_lstat,
1222 .unlinkat_fn = streams_depot_unlinkat,
1223 .renameat_fn = streams_depot_renameat,
1224 .fstreaminfo_fn = streams_depot_fstreaminfo,
1228 NTSTATUS vfs_streams_depot_init(TALLOC_CTX *ctx)
1230 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_depot",
1231 &vfs_streams_depot_fns);