dsdb-acl: remove unused variable
[Samba/gbeck.git] / source3 / modules / vfs_streams_depot.c
blob620a58075383cba8aade1362ac3fa550415cc0d4
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->cwd);
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;
201 bool delete_lost;
203 if (!S_ISDIR(smb_fname_hash->st.st_ex_mode)) {
204 errno = EINVAL;
205 goto fail;
208 if (!check_valid ||
209 file_is_valid(handle, smb_fname->base_name)) {
210 return result;
214 * Someone has recreated a file under an existing inode
215 * without deleting the streams directory.
216 * Move it away or remove if streams_depot:delete_lost is set.
219 again:
220 delete_lost = lp_parm_bool(SNUM(handle->conn), "streams_depot",
221 "delete_lost", false);
223 if (delete_lost) {
224 DEBUG(3, ("Someone has recreated a file under an "
225 "existing inode. Removing: %s\n",
226 smb_fname_hash->base_name));
227 recursive_rmdir(talloc_tos(), handle->conn,
228 smb_fname_hash);
229 SMB_VFS_NEXT_RMDIR(handle, smb_fname_hash->base_name);
230 } else {
231 newname = talloc_asprintf(talloc_tos(), "lost-%lu",
232 random());
233 DEBUG(3, ("Someone has recreated a file under an "
234 "existing inode. Renaming: %s to: %s\n",
235 smb_fname_hash->base_name,
236 newname));
237 if (newname == NULL) {
238 errno = ENOMEM;
239 goto fail;
242 status = create_synthetic_smb_fname(talloc_tos(),
243 newname,
244 NULL, NULL,
245 &smb_fname_new);
246 TALLOC_FREE(newname);
247 if (!NT_STATUS_IS_OK(status)) {
248 errno = map_errno_from_nt_status(status);
249 goto fail;
252 if (SMB_VFS_NEXT_RENAME(handle, smb_fname_hash,
253 smb_fname_new) == -1) {
254 TALLOC_FREE(smb_fname_new);
255 if ((errno == EEXIST) || (errno == ENOTEMPTY)) {
256 goto again;
258 goto fail;
261 TALLOC_FREE(smb_fname_new);
265 if (!create_it) {
266 errno = ENOENT;
267 goto fail;
270 if ((SMB_VFS_NEXT_MKDIR(handle, rootdir, 0755) != 0)
271 && (errno != EEXIST)) {
272 goto fail;
275 tmp = talloc_asprintf(result, "%s/%2.2X", rootdir, first);
276 if (tmp == NULL) {
277 errno = ENOMEM;
278 goto fail;
281 if ((SMB_VFS_NEXT_MKDIR(handle, tmp, 0755) != 0)
282 && (errno != EEXIST)) {
283 goto fail;
286 TALLOC_FREE(tmp);
288 tmp = talloc_asprintf(result, "%s/%2.2X/%2.2X", rootdir, first,
289 second);
290 if (tmp == NULL) {
291 errno = ENOMEM;
292 goto fail;
295 if ((SMB_VFS_NEXT_MKDIR(handle, tmp, 0755) != 0)
296 && (errno != EEXIST)) {
297 goto fail;
300 TALLOC_FREE(tmp);
302 if ((SMB_VFS_NEXT_MKDIR(handle, result, 0755) != 0)
303 && (errno != EEXIST)) {
304 goto fail;
307 if (check_valid && !mark_file_valid(handle, smb_fname->base_name)) {
308 goto fail;
311 TALLOC_FREE(smb_fname_hash);
312 return result;
314 fail:
315 TALLOC_FREE(smb_fname_hash);
316 TALLOC_FREE(result);
317 return NULL;
320 * Given a stream name, populate smb_fname_out with the actual location of the
321 * stream.
323 static NTSTATUS stream_smb_fname(vfs_handle_struct *handle,
324 const struct smb_filename *smb_fname,
325 struct smb_filename **smb_fname_out,
326 bool create_dir)
328 char *dirname, *stream_fname;
329 const char *stype;
330 NTSTATUS status;
332 *smb_fname_out = NULL;
334 stype = strchr_m(smb_fname->stream_name + 1, ':');
336 if (stype) {
337 if (strcasecmp_m(stype, ":$DATA") != 0) {
338 return NT_STATUS_INVALID_PARAMETER;
342 dirname = stream_dir(handle, smb_fname, NULL, create_dir);
344 if (dirname == NULL) {
345 status = map_nt_error_from_unix(errno);
346 goto fail;
349 stream_fname = talloc_asprintf(talloc_tos(), "%s/%s", dirname,
350 smb_fname->stream_name);
352 if (stream_fname == NULL) {
353 status = NT_STATUS_NO_MEMORY;
354 goto fail;
357 if (stype == NULL) {
358 /* Append an explicit stream type if one wasn't specified. */
359 stream_fname = talloc_asprintf(talloc_tos(), "%s:$DATA",
360 stream_fname);
361 if (stream_fname == NULL) {
362 status = NT_STATUS_NO_MEMORY;
363 goto fail;
365 } else {
366 /* Normalize the stream type to upercase. */
367 if (!strupper_m(strrchr_m(stream_fname, ':') + 1)) {
368 status = NT_STATUS_INVALID_PARAMETER;
369 goto fail;
373 DEBUG(10, ("stream filename = %s\n", stream_fname));
375 /* Create an smb_filename with stream_name == NULL. */
376 status = create_synthetic_smb_fname(talloc_tos(), stream_fname, NULL,
377 NULL, smb_fname_out);
378 if (!NT_STATUS_IS_OK(status)) {
379 return status;
382 return NT_STATUS_OK;
384 fail:
385 DEBUG(5, ("stream_name failed: %s\n", strerror(errno)));
386 TALLOC_FREE(*smb_fname_out);
387 return status;
390 static NTSTATUS walk_streams(vfs_handle_struct *handle,
391 struct smb_filename *smb_fname_base,
392 char **pdirname,
393 bool (*fn)(const char *dirname,
394 const char *dirent,
395 void *private_data),
396 void *private_data)
398 char *dirname;
399 DIR *dirhandle = NULL;
400 const char *dirent = NULL;
401 char *talloced = NULL;
403 dirname = stream_dir(handle, smb_fname_base, &smb_fname_base->st,
404 false);
406 if (dirname == NULL) {
407 if (errno == ENOENT) {
409 * no stream around
411 return NT_STATUS_OK;
413 return map_nt_error_from_unix(errno);
416 DEBUG(10, ("walk_streams: dirname=%s\n", dirname));
418 dirhandle = SMB_VFS_NEXT_OPENDIR(handle, dirname, NULL, 0);
420 if (dirhandle == NULL) {
421 TALLOC_FREE(dirname);
422 return map_nt_error_from_unix(errno);
425 while ((dirent = vfs_readdirname(handle->conn, dirhandle, NULL,
426 &talloced)) != NULL) {
428 if (ISDOT(dirent) || ISDOTDOT(dirent)) {
429 TALLOC_FREE(talloced);
430 continue;
433 DEBUG(10, ("walk_streams: dirent=%s\n", dirent));
435 if (!fn(dirname, dirent, private_data)) {
436 TALLOC_FREE(talloced);
437 break;
439 TALLOC_FREE(talloced);
442 SMB_VFS_NEXT_CLOSEDIR(handle, dirhandle);
444 if (pdirname != NULL) {
445 *pdirname = dirname;
447 else {
448 TALLOC_FREE(dirname);
451 return NT_STATUS_OK;
455 * Helper to stat/lstat the base file of an smb_fname. This will actually
456 * fills in the stat struct in smb_filename.
458 static int streams_depot_stat_base(vfs_handle_struct *handle,
459 struct smb_filename *smb_fname,
460 bool follow_links)
462 char *tmp_stream_name;
463 int result;
465 tmp_stream_name = smb_fname->stream_name;
466 smb_fname->stream_name = NULL;
467 if (follow_links) {
468 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
469 } else {
470 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
472 smb_fname->stream_name = tmp_stream_name;
473 return result;
476 static int streams_depot_stat(vfs_handle_struct *handle,
477 struct smb_filename *smb_fname)
479 struct smb_filename *smb_fname_stream = NULL;
480 NTSTATUS status;
481 int ret = -1;
483 DEBUG(10, ("streams_depot_stat called for [%s]\n",
484 smb_fname_str_dbg(smb_fname)));
486 if (!is_ntfs_stream_smb_fname(smb_fname)) {
487 return SMB_VFS_NEXT_STAT(handle, smb_fname);
490 /* If the default stream is requested, just stat the base file. */
491 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
492 return streams_depot_stat_base(handle, smb_fname, true);
495 /* Stat the actual stream now. */
496 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
497 false);
498 if (!NT_STATUS_IS_OK(status)) {
499 ret = -1;
500 errno = map_errno_from_nt_status(status);
501 goto done;
504 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_stream);
506 /* Update the original smb_fname with the stat info. */
507 smb_fname->st = smb_fname_stream->st;
508 done:
509 TALLOC_FREE(smb_fname_stream);
510 return ret;
515 static int streams_depot_lstat(vfs_handle_struct *handle,
516 struct smb_filename *smb_fname)
518 struct smb_filename *smb_fname_stream = NULL;
519 NTSTATUS status;
520 int ret = -1;
522 DEBUG(10, ("streams_depot_lstat called for [%s]\n",
523 smb_fname_str_dbg(smb_fname)));
525 if (!is_ntfs_stream_smb_fname(smb_fname)) {
526 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
529 /* If the default stream is requested, just stat the base file. */
530 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
531 return streams_depot_stat_base(handle, smb_fname, false);
534 /* Stat the actual stream now. */
535 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
536 false);
537 if (!NT_STATUS_IS_OK(status)) {
538 ret = -1;
539 errno = map_errno_from_nt_status(status);
540 goto done;
543 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_stream);
545 done:
546 TALLOC_FREE(smb_fname_stream);
547 return ret;
550 static int streams_depot_open(vfs_handle_struct *handle,
551 struct smb_filename *smb_fname,
552 files_struct *fsp, int flags, mode_t mode)
554 struct smb_filename *smb_fname_stream = NULL;
555 struct smb_filename *smb_fname_base = NULL;
556 NTSTATUS status;
557 int ret = -1;
559 if (!is_ntfs_stream_smb_fname(smb_fname)) {
560 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
563 /* If the default stream is requested, just open the base file. */
564 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
565 char *tmp_stream_name;
567 tmp_stream_name = smb_fname->stream_name;
568 smb_fname->stream_name = NULL;
569 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
570 smb_fname->stream_name = tmp_stream_name;
572 return ret;
575 /* Ensure the base file still exists. */
576 status = create_synthetic_smb_fname(talloc_tos(),
577 smb_fname->base_name,
578 NULL, NULL,
579 &smb_fname_base);
580 if (!NT_STATUS_IS_OK(status)) {
581 ret = -1;
582 errno = map_errno_from_nt_status(status);
583 goto done;
586 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
587 if (ret == -1) {
588 goto done;
591 /* Determine the stream name, and then open it. */
592 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream, true);
593 if (!NT_STATUS_IS_OK(status)) {
594 ret = -1;
595 errno = map_errno_from_nt_status(status);
596 goto done;
599 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname_stream, fsp, flags, mode);
601 done:
602 TALLOC_FREE(smb_fname_stream);
603 TALLOC_FREE(smb_fname_base);
604 return ret;
607 static int streams_depot_unlink(vfs_handle_struct *handle,
608 const struct smb_filename *smb_fname)
610 struct smb_filename *smb_fname_base = NULL;
611 NTSTATUS status;
612 int ret = -1;
614 DEBUG(10, ("streams_depot_unlink called for %s\n",
615 smb_fname_str_dbg(smb_fname)));
617 /* If there is a valid stream, just unlink the stream and return. */
618 if (is_ntfs_stream_smb_fname(smb_fname) &&
619 !is_ntfs_default_stream_smb_fname(smb_fname)) {
620 struct smb_filename *smb_fname_stream = NULL;
622 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
623 false);
624 if (!NT_STATUS_IS_OK(status)) {
625 errno = map_errno_from_nt_status(status);
626 return -1;
629 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_stream);
631 TALLOC_FREE(smb_fname_stream);
632 return ret;
636 * We potentially need to delete the per-inode streams directory
639 status = create_synthetic_smb_fname(talloc_tos(), smb_fname->base_name,
640 NULL, NULL, &smb_fname_base);
641 if (!NT_STATUS_IS_OK(status)) {
642 errno = map_errno_from_nt_status(status);
643 return -1;
646 if (lp_posix_pathnames()) {
647 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
648 } else {
649 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
652 if (ret == -1) {
653 TALLOC_FREE(smb_fname_base);
654 return -1;
657 if (smb_fname_base->st.st_ex_nlink == 1) {
658 char *dirname = stream_dir(handle, smb_fname_base,
659 &smb_fname_base->st, false);
661 if (dirname != NULL) {
662 SMB_VFS_NEXT_RMDIR(handle, dirname);
664 TALLOC_FREE(dirname);
667 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
669 TALLOC_FREE(smb_fname_base);
670 return ret;
673 static int streams_depot_rmdir(vfs_handle_struct *handle, const char *path)
675 struct smb_filename *smb_fname_base = NULL;
676 NTSTATUS status;
677 int ret = -1;
679 DEBUG(10, ("streams_depot_rmdir called for %s\n", path));
682 * We potentially need to delete the per-inode streams directory
685 status = create_synthetic_smb_fname(talloc_tos(), path,
686 NULL, NULL, &smb_fname_base);
687 if (!NT_STATUS_IS_OK(status)) {
688 errno = map_errno_from_nt_status(status);
689 return -1;
692 if (lp_posix_pathnames()) {
693 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
694 } else {
695 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
698 if (ret == -1) {
699 TALLOC_FREE(smb_fname_base);
700 return -1;
703 if (smb_fname_base->st.st_ex_nlink == 2) {
704 char *dirname = stream_dir(handle, smb_fname_base,
705 &smb_fname_base->st, false);
707 if (dirname != NULL) {
708 SMB_VFS_NEXT_RMDIR(handle, dirname);
710 TALLOC_FREE(dirname);
713 ret = SMB_VFS_NEXT_RMDIR(handle, path);
715 TALLOC_FREE(smb_fname_base);
716 return ret;
719 static int streams_depot_rename(vfs_handle_struct *handle,
720 const struct smb_filename *smb_fname_src,
721 const struct smb_filename *smb_fname_dst)
723 struct smb_filename *smb_fname_src_stream = NULL;
724 struct smb_filename *smb_fname_dst_stream = NULL;
725 bool src_is_stream, dst_is_stream;
726 NTSTATUS status;
727 int ret = -1;
729 DEBUG(10, ("streams_depot_rename called for %s => %s\n",
730 smb_fname_str_dbg(smb_fname_src),
731 smb_fname_str_dbg(smb_fname_dst)));
733 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
734 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
736 if (!src_is_stream && !dst_is_stream) {
737 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
738 smb_fname_dst);
741 /* for now don't allow renames from or to the default stream */
742 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
743 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
744 errno = ENOSYS;
745 goto done;
748 status = stream_smb_fname(handle, smb_fname_src, &smb_fname_src_stream,
749 false);
750 if (!NT_STATUS_IS_OK(status)) {
751 errno = map_errno_from_nt_status(status);
752 goto done;
755 status = stream_smb_fname(handle, smb_fname_dst,
756 &smb_fname_dst_stream, false);
757 if (!NT_STATUS_IS_OK(status)) {
758 errno = map_errno_from_nt_status(status);
759 goto done;
762 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_stream,
763 smb_fname_dst_stream);
765 done:
766 TALLOC_FREE(smb_fname_src_stream);
767 TALLOC_FREE(smb_fname_dst_stream);
768 return ret;
771 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
772 struct stream_struct **streams,
773 const char *name, off_t size,
774 off_t alloc_size)
776 struct stream_struct *tmp;
778 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
779 (*num_streams)+1);
780 if (tmp == NULL) {
781 return false;
784 tmp[*num_streams].name = talloc_strdup(tmp, name);
785 if (tmp[*num_streams].name == NULL) {
786 return false;
789 tmp[*num_streams].size = size;
790 tmp[*num_streams].alloc_size = alloc_size;
792 *streams = tmp;
793 *num_streams += 1;
794 return true;
797 struct streaminfo_state {
798 TALLOC_CTX *mem_ctx;
799 vfs_handle_struct *handle;
800 unsigned int num_streams;
801 struct stream_struct *streams;
802 NTSTATUS status;
805 static bool collect_one_stream(const char *dirname,
806 const char *dirent,
807 void *private_data)
809 struct streaminfo_state *state =
810 (struct streaminfo_state *)private_data;
811 struct smb_filename *smb_fname = NULL;
812 char *sname = NULL;
813 NTSTATUS status;
814 bool ret;
816 sname = talloc_asprintf(talloc_tos(), "%s/%s", dirname, dirent);
817 if (sname == NULL) {
818 state->status = NT_STATUS_NO_MEMORY;
819 ret = false;
820 goto out;
823 status = create_synthetic_smb_fname(talloc_tos(), sname, NULL,
824 NULL, &smb_fname);
825 if (!NT_STATUS_IS_OK(status)) {
826 state->status = status;
827 ret = false;
828 goto out;
831 if (SMB_VFS_NEXT_STAT(state->handle, smb_fname) == -1) {
832 DEBUG(10, ("Could not stat %s: %s\n", sname,
833 strerror(errno)));
834 ret = true;
835 goto out;
838 if (!add_one_stream(state->mem_ctx,
839 &state->num_streams, &state->streams,
840 dirent, smb_fname->st.st_ex_size,
841 SMB_VFS_GET_ALLOC_SIZE(state->handle->conn, NULL,
842 &smb_fname->st))) {
843 state->status = NT_STATUS_NO_MEMORY;
844 ret = false;
845 goto out;
848 ret = true;
849 out:
850 TALLOC_FREE(sname);
851 TALLOC_FREE(smb_fname);
852 return ret;
855 static NTSTATUS streams_depot_streaminfo(vfs_handle_struct *handle,
856 struct files_struct *fsp,
857 const char *fname,
858 TALLOC_CTX *mem_ctx,
859 unsigned int *pnum_streams,
860 struct stream_struct **pstreams)
862 struct smb_filename *smb_fname_base = NULL;
863 int ret;
864 NTSTATUS status;
865 struct streaminfo_state state;
867 status = create_synthetic_smb_fname(talloc_tos(), fname, NULL, NULL,
868 &smb_fname_base);
869 if (!NT_STATUS_IS_OK(status)) {
870 return status;
873 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
874 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, &smb_fname_base->st);
876 else {
877 if (lp_posix_pathnames()) {
878 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
879 } else {
880 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
884 if (ret == -1) {
885 status = map_nt_error_from_unix(errno);
886 goto out;
889 state.streams = *pstreams;
890 state.num_streams = *pnum_streams;
891 state.mem_ctx = mem_ctx;
892 state.handle = handle;
893 state.status = NT_STATUS_OK;
895 status = walk_streams(handle, smb_fname_base, NULL, collect_one_stream,
896 &state);
898 if (!NT_STATUS_IS_OK(status)) {
899 TALLOC_FREE(state.streams);
900 goto out;
903 if (!NT_STATUS_IS_OK(state.status)) {
904 TALLOC_FREE(state.streams);
905 status = state.status;
906 goto out;
909 *pnum_streams = state.num_streams;
910 *pstreams = state.streams;
911 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, fname, mem_ctx, pnum_streams, pstreams);
913 out:
914 TALLOC_FREE(smb_fname_base);
915 return status;
918 static uint32_t streams_depot_fs_capabilities(struct vfs_handle_struct *handle,
919 enum timestamp_set_resolution *p_ts_res)
921 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
924 static struct vfs_fn_pointers vfs_streams_depot_fns = {
925 .fs_capabilities_fn = streams_depot_fs_capabilities,
926 .open_fn = streams_depot_open,
927 .stat_fn = streams_depot_stat,
928 .lstat_fn = streams_depot_lstat,
929 .unlink_fn = streams_depot_unlink,
930 .rmdir_fn = streams_depot_rmdir,
931 .rename_fn = streams_depot_rename,
932 .streaminfo_fn = streams_depot_streaminfo,
935 NTSTATUS vfs_streams_depot_init(void);
936 NTSTATUS vfs_streams_depot_init(void)
938 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_depot",
939 &vfs_streams_depot_fns);