s3:smbd: change blocking.c to use fsp_fnum_dbg() for fsp->fnum logging.
[Samba/gebeck_regimport.git] / source3 / modules / vfs_streams_depot.c
blobf984f75e9f25018bfab4626c74e9aec0576e4e3e
1 /*
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/>.
20 #include "includes.h"
21 #include "smbd/smbd.h"
22 #include "system/filesys.h"
24 #undef DBGC_CLASS
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
36 * name.
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
62 * directory.
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, const char *path)
72 char buf;
74 DEBUG(10, ("file_is_valid (%s) called\n", path));
76 if (SMB_VFS_GETXATTR(handle->conn, path, SAMBA_XATTR_MARKER,
77 &buf, sizeof(buf)) != sizeof(buf)) {
78 DEBUG(10, ("GETXATTR failed: %s\n", strerror(errno)));
79 return false;
82 if (buf != '1') {
83 DEBUG(10, ("got wrong buffer content: '%c'\n", buf));
84 return false;
87 return true;
90 static bool mark_file_valid(vfs_handle_struct *handle, const char *path)
92 char buf = '1';
93 int ret;
95 DEBUG(10, ("marking file %s as valid\n", path));
97 ret = SMB_VFS_SETXATTR(handle->conn, path, SAMBA_XATTR_MARKER,
98 &buf, sizeof(buf), 0);
100 if (ret == -1) {
101 DEBUG(10, ("SETXATTR failed: %s\n", strerror(errno)));
102 return false;
105 return true;
109 * Given an smb_filename, determine the stream directory using the file's
110 * base_name.
112 static char *stream_dir(vfs_handle_struct *handle,
113 const struct smb_filename *smb_fname,
114 const SMB_STRUCT_STAT *base_sbuf, bool create_it)
116 uint32_t hash;
117 struct smb_filename *smb_fname_hash = NULL;
118 char *result = NULL;
119 SMB_STRUCT_STAT base_sbuf_tmp;
120 uint8_t first, second;
121 char *tmp;
122 char *id_hex;
123 struct file_id id;
124 uint8 id_buf[16];
125 bool check_valid;
126 const char *rootdir;
127 NTSTATUS status;
129 check_valid = lp_parm_bool(SNUM(handle->conn),
130 "streams_depot", "check_valid", true);
132 tmp = talloc_asprintf(talloc_tos(), "%s/.streams", handle->conn->connectpath);
134 if (tmp == NULL) {
135 errno = ENOMEM;
136 goto fail;
139 rootdir = lp_parm_const_string(
140 SNUM(handle->conn), "streams_depot", "directory",
141 tmp);
143 /* Stat the base file if it hasn't already been done. */
144 if (base_sbuf == NULL) {
145 struct smb_filename *smb_fname_base = NULL;
147 status = create_synthetic_smb_fname(talloc_tos(),
148 smb_fname->base_name,
149 NULL, NULL,
150 &smb_fname_base);
151 if (!NT_STATUS_IS_OK(status)) {
152 errno = map_errno_from_nt_status(status);
153 goto fail;
155 if (SMB_VFS_NEXT_STAT(handle, smb_fname_base) == -1) {
156 TALLOC_FREE(smb_fname_base);
157 goto fail;
159 base_sbuf_tmp = smb_fname_base->st;
160 TALLOC_FREE(smb_fname_base);
161 } else {
162 base_sbuf_tmp = *base_sbuf;
165 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &base_sbuf_tmp);
167 push_file_id_16((char *)id_buf, &id);
169 hash = hash_fn(data_blob_const(id_buf, sizeof(id_buf)));
171 first = hash & 0xff;
172 second = (hash >> 8) & 0xff;
174 id_hex = hex_encode_talloc(talloc_tos(), id_buf, sizeof(id_buf));
176 if (id_hex == NULL) {
177 errno = ENOMEM;
178 goto fail;
181 result = talloc_asprintf(talloc_tos(), "%s/%2.2X/%2.2X/%s", rootdir,
182 first, second, id_hex);
184 TALLOC_FREE(id_hex);
186 if (result == NULL) {
187 errno = ENOMEM;
188 return NULL;
191 status = create_synthetic_smb_fname(talloc_tos(), result, NULL, NULL,
192 &smb_fname_hash);
193 if (!NT_STATUS_IS_OK(status)) {
194 errno = map_errno_from_nt_status(status);
195 goto fail;
198 if (SMB_VFS_NEXT_STAT(handle, smb_fname_hash) == 0) {
199 struct smb_filename *smb_fname_new = NULL;
200 char *newname;
202 if (!S_ISDIR(smb_fname_hash->st.st_ex_mode)) {
203 errno = EINVAL;
204 goto fail;
207 if (!check_valid ||
208 file_is_valid(handle, smb_fname->base_name)) {
209 return result;
213 * Someone has recreated a file under an existing inode
214 * without deleting the streams directory. For now, just move
215 * it away.
218 again:
219 newname = talloc_asprintf(talloc_tos(), "lost-%lu", random());
220 if (newname == NULL) {
221 errno = ENOMEM;
222 goto fail;
225 status = create_synthetic_smb_fname(talloc_tos(), newname,
226 NULL, NULL,
227 &smb_fname_new);
228 TALLOC_FREE(newname);
229 if (!NT_STATUS_IS_OK(status)) {
230 errno = map_errno_from_nt_status(status);
231 goto fail;
234 if (SMB_VFS_NEXT_RENAME(handle, smb_fname_hash,
235 smb_fname_new) == -1) {
236 TALLOC_FREE(smb_fname_new);
237 if ((errno == EEXIST) || (errno == ENOTEMPTY)) {
238 goto again;
240 goto fail;
243 TALLOC_FREE(smb_fname_new);
246 if (!create_it) {
247 errno = ENOENT;
248 goto fail;
251 if ((SMB_VFS_NEXT_MKDIR(handle, rootdir, 0755) != 0)
252 && (errno != EEXIST)) {
253 goto fail;
256 tmp = talloc_asprintf(result, "%s/%2.2X", rootdir, first);
257 if (tmp == NULL) {
258 errno = ENOMEM;
259 goto fail;
262 if ((SMB_VFS_NEXT_MKDIR(handle, tmp, 0755) != 0)
263 && (errno != EEXIST)) {
264 goto fail;
267 TALLOC_FREE(tmp);
269 tmp = talloc_asprintf(result, "%s/%2.2X/%2.2X", rootdir, first,
270 second);
271 if (tmp == NULL) {
272 errno = ENOMEM;
273 goto fail;
276 if ((SMB_VFS_NEXT_MKDIR(handle, tmp, 0755) != 0)
277 && (errno != EEXIST)) {
278 goto fail;
281 TALLOC_FREE(tmp);
283 if ((SMB_VFS_NEXT_MKDIR(handle, result, 0755) != 0)
284 && (errno != EEXIST)) {
285 goto fail;
288 if (check_valid && !mark_file_valid(handle, smb_fname->base_name)) {
289 goto fail;
292 TALLOC_FREE(smb_fname_hash);
293 return result;
295 fail:
296 TALLOC_FREE(smb_fname_hash);
297 TALLOC_FREE(result);
298 return NULL;
301 * Given a stream name, populate smb_fname_out with the actual location of the
302 * stream.
304 static NTSTATUS stream_smb_fname(vfs_handle_struct *handle,
305 const struct smb_filename *smb_fname,
306 struct smb_filename **smb_fname_out,
307 bool create_dir)
309 char *dirname, *stream_fname;
310 const char *stype;
311 NTSTATUS status;
313 *smb_fname_out = NULL;
315 stype = strchr_m(smb_fname->stream_name + 1, ':');
317 if (stype) {
318 if (strcasecmp_m(stype, ":$DATA") != 0) {
319 return NT_STATUS_INVALID_PARAMETER;
323 dirname = stream_dir(handle, smb_fname, NULL, create_dir);
325 if (dirname == NULL) {
326 status = map_nt_error_from_unix(errno);
327 goto fail;
330 stream_fname = talloc_asprintf(talloc_tos(), "%s/%s", dirname,
331 smb_fname->stream_name);
333 if (stream_fname == NULL) {
334 status = NT_STATUS_NO_MEMORY;
335 goto fail;
338 if (stype == NULL) {
339 /* Append an explicit stream type if one wasn't specified. */
340 stream_fname = talloc_asprintf(talloc_tos(), "%s:$DATA",
341 stream_fname);
342 if (stream_fname == NULL) {
343 status = NT_STATUS_NO_MEMORY;
344 goto fail;
346 } else {
347 /* Normalize the stream type to upercase. */
348 strupper_m(strrchr_m(stream_fname, ':') + 1);
351 DEBUG(10, ("stream filename = %s\n", stream_fname));
353 /* Create an smb_filename with stream_name == NULL. */
354 status = create_synthetic_smb_fname(talloc_tos(), stream_fname, NULL,
355 NULL, smb_fname_out);
356 if (!NT_STATUS_IS_OK(status)) {
357 return status;
360 return NT_STATUS_OK;
362 fail:
363 DEBUG(5, ("stream_name failed: %s\n", strerror(errno)));
364 TALLOC_FREE(*smb_fname_out);
365 return status;
368 static NTSTATUS walk_streams(vfs_handle_struct *handle,
369 struct smb_filename *smb_fname_base,
370 char **pdirname,
371 bool (*fn)(const char *dirname,
372 const char *dirent,
373 void *private_data),
374 void *private_data)
376 char *dirname;
377 DIR *dirhandle = NULL;
378 const char *dirent = NULL;
379 char *talloced = NULL;
381 dirname = stream_dir(handle, smb_fname_base, &smb_fname_base->st,
382 false);
384 if (dirname == NULL) {
385 if (errno == ENOENT) {
387 * no stream around
389 return NT_STATUS_OK;
391 return map_nt_error_from_unix(errno);
394 DEBUG(10, ("walk_streams: dirname=%s\n", dirname));
396 dirhandle = SMB_VFS_NEXT_OPENDIR(handle, dirname, NULL, 0);
398 if (dirhandle == NULL) {
399 TALLOC_FREE(dirname);
400 return map_nt_error_from_unix(errno);
403 while ((dirent = vfs_readdirname(handle->conn, dirhandle, NULL,
404 &talloced)) != NULL) {
406 if (ISDOT(dirent) || ISDOTDOT(dirent)) {
407 TALLOC_FREE(talloced);
408 continue;
411 DEBUG(10, ("walk_streams: dirent=%s\n", dirent));
413 if (!fn(dirname, dirent, private_data)) {
414 TALLOC_FREE(talloced);
415 break;
417 TALLOC_FREE(talloced);
420 SMB_VFS_NEXT_CLOSEDIR(handle, dirhandle);
422 if (pdirname != NULL) {
423 *pdirname = dirname;
425 else {
426 TALLOC_FREE(dirname);
429 return NT_STATUS_OK;
433 * Helper to stat/lstat the base file of an smb_fname. This will actually
434 * fills in the stat struct in smb_filename.
436 static int streams_depot_stat_base(vfs_handle_struct *handle,
437 struct smb_filename *smb_fname,
438 bool follow_links)
440 char *tmp_stream_name;
441 int result;
443 tmp_stream_name = smb_fname->stream_name;
444 smb_fname->stream_name = NULL;
445 if (follow_links) {
446 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
447 } else {
448 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
450 smb_fname->stream_name = tmp_stream_name;
451 return result;
454 static int streams_depot_stat(vfs_handle_struct *handle,
455 struct smb_filename *smb_fname)
457 struct smb_filename *smb_fname_stream = NULL;
458 NTSTATUS status;
459 int ret = -1;
461 DEBUG(10, ("streams_depot_stat called for [%s]\n",
462 smb_fname_str_dbg(smb_fname)));
464 if (!is_ntfs_stream_smb_fname(smb_fname)) {
465 return SMB_VFS_NEXT_STAT(handle, smb_fname);
468 /* If the default stream is requested, just stat the base file. */
469 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
470 return streams_depot_stat_base(handle, smb_fname, true);
473 /* Stat the actual stream now. */
474 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
475 false);
476 if (!NT_STATUS_IS_OK(status)) {
477 ret = -1;
478 errno = map_errno_from_nt_status(status);
479 goto done;
482 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_stream);
484 /* Update the original smb_fname with the stat info. */
485 smb_fname->st = smb_fname_stream->st;
486 done:
487 TALLOC_FREE(smb_fname_stream);
488 return ret;
493 static int streams_depot_lstat(vfs_handle_struct *handle,
494 struct smb_filename *smb_fname)
496 struct smb_filename *smb_fname_stream = NULL;
497 NTSTATUS status;
498 int ret = -1;
500 DEBUG(10, ("streams_depot_lstat called for [%s]\n",
501 smb_fname_str_dbg(smb_fname)));
503 if (!is_ntfs_stream_smb_fname(smb_fname)) {
504 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
507 /* If the default stream is requested, just stat the base file. */
508 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
509 return streams_depot_stat_base(handle, smb_fname, false);
512 /* Stat the actual stream now. */
513 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
514 false);
515 if (!NT_STATUS_IS_OK(status)) {
516 ret = -1;
517 errno = map_errno_from_nt_status(status);
518 goto done;
521 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_stream);
523 done:
524 TALLOC_FREE(smb_fname_stream);
525 return ret;
528 static int streams_depot_open(vfs_handle_struct *handle,
529 struct smb_filename *smb_fname,
530 files_struct *fsp, int flags, mode_t mode)
532 struct smb_filename *smb_fname_stream = NULL;
533 struct smb_filename *smb_fname_base = NULL;
534 NTSTATUS status;
535 int ret = -1;
537 if (!is_ntfs_stream_smb_fname(smb_fname)) {
538 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
541 /* If the default stream is requested, just open the base file. */
542 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
543 char *tmp_stream_name;
545 tmp_stream_name = smb_fname->stream_name;
546 smb_fname->stream_name = NULL;
547 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
548 smb_fname->stream_name = tmp_stream_name;
550 return ret;
553 /* Ensure the base file still exists. */
554 status = create_synthetic_smb_fname(talloc_tos(),
555 smb_fname->base_name,
556 NULL, NULL,
557 &smb_fname_base);
558 if (!NT_STATUS_IS_OK(status)) {
559 ret = -1;
560 errno = map_errno_from_nt_status(status);
561 goto done;
564 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
565 if (ret == -1) {
566 goto done;
569 /* Determine the stream name, and then open it. */
570 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream, true);
571 if (!NT_STATUS_IS_OK(status)) {
572 ret = -1;
573 errno = map_errno_from_nt_status(status);
574 goto done;
577 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname_stream, fsp, flags, mode);
579 done:
580 TALLOC_FREE(smb_fname_stream);
581 TALLOC_FREE(smb_fname_base);
582 return ret;
585 static int streams_depot_unlink(vfs_handle_struct *handle,
586 const struct smb_filename *smb_fname)
588 struct smb_filename *smb_fname_base = NULL;
589 NTSTATUS status;
590 int ret = -1;
592 DEBUG(10, ("streams_depot_unlink called for %s\n",
593 smb_fname_str_dbg(smb_fname)));
595 /* If there is a valid stream, just unlink the stream and return. */
596 if (is_ntfs_stream_smb_fname(smb_fname) &&
597 !is_ntfs_default_stream_smb_fname(smb_fname)) {
598 struct smb_filename *smb_fname_stream = NULL;
600 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
601 false);
602 if (!NT_STATUS_IS_OK(status)) {
603 errno = map_errno_from_nt_status(status);
604 return -1;
607 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_stream);
609 TALLOC_FREE(smb_fname_stream);
610 return ret;
614 * We potentially need to delete the per-inode streams directory
617 status = create_synthetic_smb_fname(talloc_tos(), smb_fname->base_name,
618 NULL, NULL, &smb_fname_base);
619 if (!NT_STATUS_IS_OK(status)) {
620 errno = map_errno_from_nt_status(status);
621 return -1;
624 if (lp_posix_pathnames()) {
625 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
626 } else {
627 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
630 if (ret == -1) {
631 TALLOC_FREE(smb_fname_base);
632 return -1;
635 if (smb_fname_base->st.st_ex_nlink == 1) {
636 char *dirname = stream_dir(handle, smb_fname_base,
637 &smb_fname_base->st, false);
639 if (dirname != NULL) {
640 SMB_VFS_NEXT_RMDIR(handle, dirname);
642 TALLOC_FREE(dirname);
645 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
647 TALLOC_FREE(smb_fname_base);
648 return ret;
651 static int streams_depot_rmdir(vfs_handle_struct *handle, const char *path)
653 struct smb_filename *smb_fname_base = NULL;
654 NTSTATUS status;
655 int ret = -1;
657 DEBUG(10, ("streams_depot_rmdir called for %s\n", path));
660 * We potentially need to delete the per-inode streams directory
663 status = create_synthetic_smb_fname(talloc_tos(), path,
664 NULL, NULL, &smb_fname_base);
665 if (!NT_STATUS_IS_OK(status)) {
666 errno = map_errno_from_nt_status(status);
667 return -1;
670 if (lp_posix_pathnames()) {
671 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
672 } else {
673 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
676 if (ret == -1) {
677 TALLOC_FREE(smb_fname_base);
678 return -1;
681 if (smb_fname_base->st.st_ex_nlink == 2) {
682 char *dirname = stream_dir(handle, smb_fname_base,
683 &smb_fname_base->st, false);
685 if (dirname != NULL) {
686 SMB_VFS_NEXT_RMDIR(handle, dirname);
688 TALLOC_FREE(dirname);
691 ret = SMB_VFS_NEXT_RMDIR(handle, path);
693 TALLOC_FREE(smb_fname_base);
694 return ret;
697 static int streams_depot_rename(vfs_handle_struct *handle,
698 const struct smb_filename *smb_fname_src,
699 const struct smb_filename *smb_fname_dst)
701 struct smb_filename *smb_fname_src_stream = NULL;
702 struct smb_filename *smb_fname_dst_stream = NULL;
703 bool src_is_stream, dst_is_stream;
704 NTSTATUS status;
705 int ret = -1;
707 DEBUG(10, ("streams_depot_rename called for %s => %s\n",
708 smb_fname_str_dbg(smb_fname_src),
709 smb_fname_str_dbg(smb_fname_dst)));
711 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
712 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
714 if (!src_is_stream && !dst_is_stream) {
715 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
716 smb_fname_dst);
719 /* for now don't allow renames from or to the default stream */
720 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
721 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
722 errno = ENOSYS;
723 goto done;
726 status = stream_smb_fname(handle, smb_fname_src, &smb_fname_src_stream,
727 false);
728 if (!NT_STATUS_IS_OK(status)) {
729 errno = map_errno_from_nt_status(status);
730 goto done;
733 status = stream_smb_fname(handle, smb_fname_dst,
734 &smb_fname_dst_stream, false);
735 if (!NT_STATUS_IS_OK(status)) {
736 errno = map_errno_from_nt_status(status);
737 goto done;
740 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_stream,
741 smb_fname_dst_stream);
743 done:
744 TALLOC_FREE(smb_fname_src_stream);
745 TALLOC_FREE(smb_fname_dst_stream);
746 return ret;
749 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
750 struct stream_struct **streams,
751 const char *name, off_t size,
752 off_t alloc_size)
754 struct stream_struct *tmp;
756 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
757 (*num_streams)+1);
758 if (tmp == NULL) {
759 return false;
762 tmp[*num_streams].name = talloc_strdup(tmp, name);
763 if (tmp[*num_streams].name == NULL) {
764 return false;
767 tmp[*num_streams].size = size;
768 tmp[*num_streams].alloc_size = alloc_size;
770 *streams = tmp;
771 *num_streams += 1;
772 return true;
775 struct streaminfo_state {
776 TALLOC_CTX *mem_ctx;
777 vfs_handle_struct *handle;
778 unsigned int num_streams;
779 struct stream_struct *streams;
780 NTSTATUS status;
783 static bool collect_one_stream(const char *dirname,
784 const char *dirent,
785 void *private_data)
787 struct streaminfo_state *state =
788 (struct streaminfo_state *)private_data;
789 struct smb_filename *smb_fname = NULL;
790 char *sname = NULL;
791 NTSTATUS status;
792 bool ret;
794 sname = talloc_asprintf(talloc_tos(), "%s/%s", dirname, dirent);
795 if (sname == NULL) {
796 state->status = NT_STATUS_NO_MEMORY;
797 ret = false;
798 goto out;
801 status = create_synthetic_smb_fname(talloc_tos(), sname, NULL,
802 NULL, &smb_fname);
803 if (!NT_STATUS_IS_OK(status)) {
804 state->status = status;
805 ret = false;
806 goto out;
809 if (SMB_VFS_NEXT_STAT(state->handle, smb_fname) == -1) {
810 DEBUG(10, ("Could not stat %s: %s\n", sname,
811 strerror(errno)));
812 ret = true;
813 goto out;
816 if (!add_one_stream(state->mem_ctx,
817 &state->num_streams, &state->streams,
818 dirent, smb_fname->st.st_ex_size,
819 SMB_VFS_GET_ALLOC_SIZE(state->handle->conn, NULL,
820 &smb_fname->st))) {
821 state->status = NT_STATUS_NO_MEMORY;
822 ret = false;
823 goto out;
826 ret = true;
827 out:
828 TALLOC_FREE(sname);
829 TALLOC_FREE(smb_fname);
830 return ret;
833 static NTSTATUS streams_depot_streaminfo(vfs_handle_struct *handle,
834 struct files_struct *fsp,
835 const char *fname,
836 TALLOC_CTX *mem_ctx,
837 unsigned int *pnum_streams,
838 struct stream_struct **pstreams)
840 struct smb_filename *smb_fname_base = NULL;
841 int ret;
842 NTSTATUS status;
843 struct streaminfo_state state;
845 status = create_synthetic_smb_fname(talloc_tos(), fname, NULL, NULL,
846 &smb_fname_base);
847 if (!NT_STATUS_IS_OK(status)) {
848 return status;
851 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
852 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, &smb_fname_base->st);
854 else {
855 if (lp_posix_pathnames()) {
856 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
857 } else {
858 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
862 if (ret == -1) {
863 status = map_nt_error_from_unix(errno);
864 goto out;
867 state.streams = *pstreams;
868 state.num_streams = *pnum_streams;
869 state.mem_ctx = mem_ctx;
870 state.handle = handle;
871 state.status = NT_STATUS_OK;
873 status = walk_streams(handle, smb_fname_base, NULL, collect_one_stream,
874 &state);
876 if (!NT_STATUS_IS_OK(status)) {
877 TALLOC_FREE(state.streams);
878 goto out;
881 if (!NT_STATUS_IS_OK(state.status)) {
882 TALLOC_FREE(state.streams);
883 status = state.status;
884 goto out;
887 *pnum_streams = state.num_streams;
888 *pstreams = state.streams;
889 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, fname, mem_ctx, pnum_streams, pstreams);
891 out:
892 TALLOC_FREE(smb_fname_base);
893 return status;
896 static uint32_t streams_depot_fs_capabilities(struct vfs_handle_struct *handle,
897 enum timestamp_set_resolution *p_ts_res)
899 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
902 static struct vfs_fn_pointers vfs_streams_depot_fns = {
903 .fs_capabilities_fn = streams_depot_fs_capabilities,
904 .open_fn = streams_depot_open,
905 .stat_fn = streams_depot_stat,
906 .lstat_fn = streams_depot_lstat,
907 .unlink_fn = streams_depot_unlink,
908 .rmdir_fn = streams_depot_rmdir,
909 .rename_fn = streams_depot_rename,
910 .streaminfo_fn = streams_depot_streaminfo,
913 NTSTATUS vfs_streams_depot_init(void);
914 NTSTATUS vfs_streams_depot_init(void)
916 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_depot",
917 &vfs_streams_depot_fns);