Second attempt at fix for bug 6529 - Offline files conflict with Vista and Office...
[Samba/fernandojvsilva.git] / source3 / modules / vfs_streams_depot.c
blobd09255a4a23d8e040b2b99ba5ff096bf9bfbfb60
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"
22 #undef DBGC_CLASS
23 #define DBGC_CLASS DBGC_VFS
26 * Excerpt from a mail from tridge:
28 * Volker, what I'm thinking of is this:
29 * /mount-point/.streams/XX/YY/aaaa.bbbb/namedstream1
30 * /mount-point/.streams/XX/YY/aaaa.bbbb/namedstream2
32 * where XX/YY is a 2 level hash based on the fsid/inode. "aaaa.bbbb"
33 * is the fsid/inode. "namedstreamX" is a file named after the stream
34 * name.
37 static uint32_t hash_fn(DATA_BLOB key)
39 uint32_t value; /* Used to compute the hash value. */
40 uint32_t i; /* Used to cycle through random values. */
42 /* Set the initial value from the key size. */
43 for (value = 0x238F13AF * key.length, i=0; i < key.length; i++)
44 value = (value + (key.data[i] << (i*5 % 24)));
46 return (1103515243 * value + 12345);
50 * With the hashing scheme based on the inode we need to protect against
51 * streams showing up on files with re-used inodes. This can happen if we
52 * create a stream directory from within Samba, and a local process or NFS
53 * client deletes the file without deleting the streams directory. When the
54 * inode is re-used and the stream directory is still around, the streams in
55 * there would be show up as belonging to the new file.
57 * There are several workarounds for this, probably the easiest one is on
58 * systems which have a true birthtime stat element: When the file has a later
59 * birthtime than the streams directory, then we have to recreate the
60 * directory.
62 * The other workaround is to somehow mark the file as generated by Samba with
63 * something that a NFS client would not do. The closest one is a special
64 * xattr value being set. On systems which do not support xattrs, it might be
65 * an option to put in a special ACL entry for a non-existing group.
68 #define SAMBA_XATTR_MARKER "user.SAMBA_STREAMS"
70 static bool file_is_valid(vfs_handle_struct *handle, const char *path,
71 bool check_valid)
73 char buf;
75 if (!check_valid) {
76 return true;
79 DEBUG(10, ("file_is_valid (%s) called\n", path));
81 if (SMB_VFS_GETXATTR(handle->conn, path, SAMBA_XATTR_MARKER,
82 &buf, sizeof(buf)) != sizeof(buf)) {
83 DEBUG(10, ("GETXATTR failed: %s\n", strerror(errno)));
84 return false;
87 if (buf != '1') {
88 DEBUG(10, ("got wrong buffer content: '%c'\n", buf));
89 return false;
92 return true;
95 static bool mark_file_valid(vfs_handle_struct *handle, const char *path,
96 bool check_valid)
98 char buf = '1';
99 int ret;
101 if (!check_valid) {
102 return true;
105 DEBUG(10, ("marking file %s as valid\n", path));
107 ret = SMB_VFS_SETXATTR(handle->conn, path, SAMBA_XATTR_MARKER,
108 &buf, sizeof(buf), 0);
110 if (ret == -1) {
111 DEBUG(10, ("SETXATTR failed: %s\n", strerror(errno)));
112 return false;
115 return true;
119 * Given an smb_filename, determine the stream directory using the file's
120 * base_name.
122 static char *stream_dir(vfs_handle_struct *handle,
123 const struct smb_filename *smb_fname,
124 const SMB_STRUCT_STAT *base_sbuf, bool create_it)
126 uint32_t hash;
127 struct smb_filename *smb_fname_hash = NULL;
128 char *result = NULL;
129 SMB_STRUCT_STAT base_sbuf_tmp;
130 uint8_t first, second;
131 char *tmp;
132 char *id_hex;
133 struct file_id id;
134 uint8 id_buf[16];
135 bool check_valid;
136 const char *rootdir;
137 NTSTATUS status;
139 check_valid = lp_parm_bool(SNUM(handle->conn),
140 "streams_depot", "check_valid", true);
142 tmp = talloc_asprintf(talloc_tos(), "%s/.streams", handle->conn->connectpath);
144 if (tmp == NULL) {
145 errno = ENOMEM;
146 goto fail;
149 rootdir = lp_parm_const_string(
150 SNUM(handle->conn), "streams_depot", "directory",
151 tmp);
153 /* Stat the base file if it hasn't already been done. */
154 if (base_sbuf == NULL) {
155 struct smb_filename *smb_fname_base = NULL;
157 status = create_synthetic_smb_fname(talloc_tos(),
158 smb_fname->base_name,
159 NULL, NULL,
160 &smb_fname_base);
161 if (!NT_STATUS_IS_OK(status)) {
162 errno = map_errno_from_nt_status(status);
163 goto fail;
165 if (SMB_VFS_NEXT_STAT(handle, smb_fname_base) == -1) {
166 TALLOC_FREE(smb_fname_base);
167 goto fail;
169 base_sbuf_tmp = smb_fname_base->st;
170 TALLOC_FREE(smb_fname_base);
171 } else {
172 base_sbuf_tmp = *base_sbuf;
175 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &base_sbuf_tmp);
177 push_file_id_16((char *)id_buf, &id);
179 hash = hash_fn(data_blob_const(id_buf, sizeof(id_buf)));
181 first = hash & 0xff;
182 second = (hash >> 8) & 0xff;
184 id_hex = hex_encode_talloc(talloc_tos(), id_buf, sizeof(id_buf));
186 if (id_hex == NULL) {
187 errno = ENOMEM;
188 goto fail;
191 result = talloc_asprintf(talloc_tos(), "%s/%2.2X/%2.2X/%s", rootdir,
192 first, second, id_hex);
194 TALLOC_FREE(id_hex);
196 if (result == NULL) {
197 errno = ENOMEM;
198 return NULL;
201 status = create_synthetic_smb_fname(talloc_tos(), result, NULL, NULL,
202 &smb_fname_hash);
203 if (!NT_STATUS_IS_OK(status)) {
204 errno = map_errno_from_nt_status(status);
205 goto fail;
208 if (SMB_VFS_NEXT_STAT(handle, smb_fname_hash) == 0) {
209 struct smb_filename *smb_fname_new = NULL;
210 char *newname;
212 if (!S_ISDIR(smb_fname_hash->st.st_ex_mode)) {
213 errno = EINVAL;
214 goto fail;
217 if (file_is_valid(handle, smb_fname->base_name, check_valid)) {
218 return result;
222 * Someone has recreated a file under an existing inode
223 * without deleting the streams directory. For now, just move
224 * it away.
227 again:
228 newname = talloc_asprintf(talloc_tos(), "lost-%lu", random());
229 if (newname == NULL) {
230 errno = ENOMEM;
231 goto fail;
234 status = create_synthetic_smb_fname(talloc_tos(), newname,
235 NULL, NULL,
236 &smb_fname_new);
237 TALLOC_FREE(newname);
238 if (!NT_STATUS_IS_OK(status)) {
239 errno = map_errno_from_nt_status(status);
240 goto fail;
243 if (SMB_VFS_NEXT_RENAME(handle, smb_fname_hash,
244 smb_fname_new) == -1) {
245 TALLOC_FREE(smb_fname_new);
246 if ((errno == EEXIST) || (errno == ENOTEMPTY)) {
247 goto again;
249 goto fail;
252 TALLOC_FREE(smb_fname_new);
255 if (!create_it) {
256 errno = ENOENT;
257 goto fail;
260 if ((SMB_VFS_NEXT_MKDIR(handle, rootdir, 0755) != 0)
261 && (errno != EEXIST)) {
262 goto fail;
265 tmp = talloc_asprintf(result, "%s/%2.2X", rootdir, first);
266 if (tmp == NULL) {
267 errno = ENOMEM;
268 goto fail;
271 if ((SMB_VFS_NEXT_MKDIR(handle, tmp, 0755) != 0)
272 && (errno != EEXIST)) {
273 goto fail;
276 TALLOC_FREE(tmp);
278 tmp = talloc_asprintf(result, "%s/%2.2X/%2.2X", rootdir, first,
279 second);
280 if (tmp == NULL) {
281 errno = ENOMEM;
282 goto fail;
285 if ((SMB_VFS_NEXT_MKDIR(handle, tmp, 0755) != 0)
286 && (errno != EEXIST)) {
287 goto fail;
290 TALLOC_FREE(tmp);
292 if ((SMB_VFS_NEXT_MKDIR(handle, result, 0755) != 0)
293 && (errno != EEXIST)) {
294 goto fail;
297 if (!mark_file_valid(handle, smb_fname->base_name, check_valid)) {
298 goto fail;
301 TALLOC_FREE(smb_fname_hash);
302 return result;
304 fail:
305 TALLOC_FREE(smb_fname_hash);
306 TALLOC_FREE(result);
307 return NULL;
310 * Given a stream name, populate smb_fname_out with the actual location of the
311 * stream.
313 static NTSTATUS stream_smb_fname(vfs_handle_struct *handle,
314 const struct smb_filename *smb_fname,
315 struct smb_filename **smb_fname_out,
316 bool create_dir)
318 char *dirname, *stream_fname;
319 const char *stype;
320 NTSTATUS status;
322 *smb_fname_out = NULL;
324 dirname = stream_dir(handle, smb_fname, NULL, create_dir);
326 if (dirname == NULL) {
327 status = map_nt_error_from_unix(errno);
328 goto fail;
331 stype = strchr_m(smb_fname->stream_name + 1, ':');
333 stream_fname = talloc_asprintf(talloc_tos(), "%s/%s", dirname,
334 smb_fname->stream_name);
336 if (stream_fname == NULL) {
337 status = NT_STATUS_NO_MEMORY;
338 goto fail;
341 if (stype == NULL) {
342 /* Append an explicit stream type if one wasn't specified. */
343 stream_fname = talloc_asprintf(talloc_tos(), "%s:$DATA",
344 stream_fname);
345 if (stream_fname == NULL) {
346 status = NT_STATUS_NO_MEMORY;
347 goto fail;
349 } else {
350 /* Normalize the stream type to upercase. */
351 strupper_m(strrchr_m(stream_fname, ':') + 1);
354 DEBUG(10, ("stream filename = %s\n", stream_fname));
356 /* Create an smb_filename with stream_name == NULL. */
357 status = create_synthetic_smb_fname(talloc_tos(), stream_fname, NULL,
358 NULL, smb_fname_out);
359 if (!NT_STATUS_IS_OK(status)) {
360 return status;
363 return NT_STATUS_OK;
365 fail:
366 DEBUG(5, ("stream_name failed: %s\n", strerror(errno)));
367 TALLOC_FREE(*smb_fname_out);
368 return status;
371 static NTSTATUS walk_streams(vfs_handle_struct *handle,
372 struct smb_filename *smb_fname_base,
373 char **pdirname,
374 bool (*fn)(const char *dirname,
375 const char *dirent,
376 void *private_data),
377 void *private_data)
379 char *dirname;
380 SMB_STRUCT_DIR *dirhandle = NULL;
381 char *dirent;
383 dirname = stream_dir(handle, smb_fname_base, &smb_fname_base->st,
384 false);
386 if (dirname == NULL) {
387 if (errno == ENOENT) {
389 * no stream around
391 return NT_STATUS_OK;
393 return map_nt_error_from_unix(errno);
396 DEBUG(10, ("walk_streams: dirname=%s\n", dirname));
398 dirhandle = SMB_VFS_NEXT_OPENDIR(handle, dirname, NULL, 0);
400 if (dirhandle == NULL) {
401 TALLOC_FREE(dirname);
402 return map_nt_error_from_unix(errno);
405 while ((dirent = vfs_readdirname(handle->conn, dirhandle, NULL)) != NULL) {
407 if (ISDOT(dirent) || ISDOTDOT(dirent)) {
408 continue;
411 DEBUG(10, ("walk_streams: dirent=%s\n", dirent));
413 if (!fn(dirname, dirent, private_data)) {
414 break;
418 SMB_VFS_NEXT_CLOSEDIR(handle, dirhandle);
420 if (pdirname != NULL) {
421 *pdirname = dirname;
423 else {
424 TALLOC_FREE(dirname);
427 return NT_STATUS_OK;
431 * Helper to stat/lstat the base file of an smb_fname. This will actually
432 * fills in the stat struct in smb_filename.
434 static int streams_depot_stat_base(vfs_handle_struct *handle,
435 struct smb_filename *smb_fname,
436 bool follow_links)
438 char *tmp_stream_name;
439 int result;
441 tmp_stream_name = smb_fname->stream_name;
442 smb_fname->stream_name = NULL;
443 if (follow_links) {
444 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
445 } else {
446 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
448 smb_fname->stream_name = tmp_stream_name;
449 return result;
452 static int streams_depot_stat(vfs_handle_struct *handle,
453 struct smb_filename *smb_fname)
455 struct smb_filename *smb_fname_stream = NULL;
456 NTSTATUS status;
457 int ret = -1;
459 DEBUG(10, ("streams_depot_stat called for [%s]\n",
460 smb_fname_str_dbg(smb_fname)));
462 if (!is_ntfs_stream_smb_fname(smb_fname)) {
463 return SMB_VFS_NEXT_STAT(handle, smb_fname);
466 /* If the default stream is requested, just stat the base file. */
467 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
468 return streams_depot_stat_base(handle, smb_fname, true);
471 /* Stat the actual stream now. */
472 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
473 false);
474 if (!NT_STATUS_IS_OK(status)) {
475 ret = -1;
476 errno = map_errno_from_nt_status(status);
477 goto done;
480 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_stream);
482 /* Update the original smb_fname with the stat info. */
483 smb_fname->st = smb_fname_stream->st;
484 done:
485 TALLOC_FREE(smb_fname_stream);
486 return ret;
491 static int streams_depot_lstat(vfs_handle_struct *handle,
492 struct smb_filename *smb_fname)
494 struct smb_filename *smb_fname_stream = NULL;
495 NTSTATUS status;
496 int ret = -1;
498 DEBUG(10, ("streams_depot_lstat called for [%s]\n",
499 smb_fname_str_dbg(smb_fname)));
501 if (!is_ntfs_stream_smb_fname(smb_fname)) {
502 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
505 /* If the default stream is requested, just stat the base file. */
506 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
507 return streams_depot_stat_base(handle, smb_fname, false);
510 /* Stat the actual stream now. */
511 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
512 false);
513 if (!NT_STATUS_IS_OK(status)) {
514 ret = -1;
515 errno = map_errno_from_nt_status(status);
516 goto done;
519 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_stream);
521 done:
522 TALLOC_FREE(smb_fname_stream);
523 return ret;
526 static int streams_depot_open(vfs_handle_struct *handle,
527 struct smb_filename *smb_fname,
528 files_struct *fsp, int flags, mode_t mode)
530 struct smb_filename *smb_fname_stream = NULL;
531 struct smb_filename *smb_fname_base = NULL;
532 NTSTATUS status;
533 int ret = -1;
535 if (!is_ntfs_stream_smb_fname(smb_fname)) {
536 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
539 /* If the default stream is requested, just open the base file. */
540 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
541 char *tmp_stream_name;
543 tmp_stream_name = smb_fname->stream_name;
544 smb_fname->stream_name = NULL;
545 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
546 smb_fname->stream_name = tmp_stream_name;
548 return ret;
551 /* Ensure the base file still exists. */
552 status = create_synthetic_smb_fname(talloc_tos(),
553 smb_fname->base_name,
554 NULL, NULL,
555 &smb_fname_base);
556 if (!NT_STATUS_IS_OK(status)) {
557 ret = -1;
558 errno = map_errno_from_nt_status(status);
559 goto done;
562 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
563 if (ret == -1) {
564 goto done;
567 /* Determine the stream name, and then open it. */
568 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream, true);
569 if (!NT_STATUS_IS_OK(status)) {
570 ret = -1;
571 errno = map_errno_from_nt_status(status);
572 goto done;
575 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname_stream, fsp, flags, mode);
577 done:
578 TALLOC_FREE(smb_fname_stream);
579 TALLOC_FREE(smb_fname_base);
580 return ret;
583 static int streams_depot_unlink(vfs_handle_struct *handle,
584 const struct smb_filename *smb_fname)
586 struct smb_filename *smb_fname_base = NULL;
587 NTSTATUS status;
588 int ret = -1;
590 DEBUG(10, ("streams_depot_unlink called for %s\n",
591 smb_fname_str_dbg(smb_fname)));
593 /* If there is a valid stream, just unlink the stream and return. */
594 if (is_ntfs_stream_smb_fname(smb_fname) &&
595 !is_ntfs_default_stream_smb_fname(smb_fname)) {
596 struct smb_filename *smb_fname_stream = NULL;
598 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
599 false);
600 if (!NT_STATUS_IS_OK(status)) {
601 errno = map_errno_from_nt_status(status);
602 return -1;
605 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_stream);
607 TALLOC_FREE(smb_fname_stream);
608 return ret;
612 * We potentially need to delete the per-inode streams directory
615 status = create_synthetic_smb_fname(talloc_tos(), smb_fname->base_name,
616 NULL, NULL, &smb_fname_base);
617 if (!NT_STATUS_IS_OK(status)) {
618 errno = map_errno_from_nt_status(status);
619 return -1;
622 if (lp_posix_pathnames()) {
623 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
624 } else {
625 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
628 if (ret == -1) {
629 TALLOC_FREE(smb_fname_base);
630 return -1;
633 if (smb_fname_base->st.st_ex_nlink == 1) {
634 char *dirname = stream_dir(handle, smb_fname_base,
635 &smb_fname_base->st, false);
637 if (dirname != NULL) {
638 SMB_VFS_NEXT_RMDIR(handle, dirname);
640 TALLOC_FREE(dirname);
643 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
645 TALLOC_FREE(smb_fname_base);
646 return ret;
649 static int streams_depot_rename(vfs_handle_struct *handle,
650 const struct smb_filename *smb_fname_src,
651 const struct smb_filename *smb_fname_dst)
653 struct smb_filename *smb_fname_src_stream = NULL;
654 struct smb_filename *smb_fname_dst_stream = NULL;
655 struct smb_filename *smb_fname_dst_mod = NULL;
656 bool src_is_stream, dst_is_stream;
657 NTSTATUS status;
658 int ret = -1;
660 DEBUG(10, ("streams_depot_rename called for %s => %s\n",
661 smb_fname_str_dbg(smb_fname_src),
662 smb_fname_str_dbg(smb_fname_dst)));
664 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
665 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
667 if (!src_is_stream && !dst_is_stream) {
668 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
669 smb_fname_dst);
672 /* for now don't allow renames from or to the default stream */
673 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
674 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
675 errno = ENOSYS;
676 goto done;
679 status = stream_smb_fname(handle, smb_fname_src, &smb_fname_src_stream,
680 false);
681 if (!NT_STATUS_IS_OK(status)) {
682 errno = map_errno_from_nt_status(status);
683 goto done;
687 * Handle passing in a stream name without the base file. This is
688 * exercised by the NTRENAME streams rename path.
690 if (StrCaseCmp(smb_fname_dst->base_name, "./") == 0) {
691 status = create_synthetic_smb_fname(talloc_tos(),
692 smb_fname_src->base_name,
693 smb_fname_dst->stream_name,
694 NULL, &smb_fname_dst_mod);
695 if (!NT_STATUS_IS_OK(status)) {
696 errno = map_errno_from_nt_status(status);
697 goto done;
701 status = stream_smb_fname(handle, (smb_fname_dst_mod ?
702 smb_fname_dst_mod : smb_fname_dst),
703 &smb_fname_dst_stream, false);
704 if (!NT_STATUS_IS_OK(status)) {
705 errno = map_errno_from_nt_status(status);
706 goto done;
709 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_stream,
710 smb_fname_dst_stream);
712 done:
713 TALLOC_FREE(smb_fname_src_stream);
714 TALLOC_FREE(smb_fname_dst_stream);
715 TALLOC_FREE(smb_fname_dst_mod);
716 return ret;
719 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
720 struct stream_struct **streams,
721 const char *name, SMB_OFF_T size,
722 SMB_OFF_T alloc_size)
724 struct stream_struct *tmp;
726 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
727 (*num_streams)+1);
728 if (tmp == NULL) {
729 return false;
732 tmp[*num_streams].name = talloc_strdup(tmp, name);
733 if (tmp[*num_streams].name == NULL) {
734 return false;
737 tmp[*num_streams].size = size;
738 tmp[*num_streams].alloc_size = alloc_size;
740 *streams = tmp;
741 *num_streams += 1;
742 return true;
745 struct streaminfo_state {
746 TALLOC_CTX *mem_ctx;
747 vfs_handle_struct *handle;
748 unsigned int num_streams;
749 struct stream_struct *streams;
750 NTSTATUS status;
753 static bool collect_one_stream(const char *dirname,
754 const char *dirent,
755 void *private_data)
757 struct streaminfo_state *state =
758 (struct streaminfo_state *)private_data;
759 struct smb_filename *smb_fname = NULL;
760 char *sname = NULL;
761 NTSTATUS status;
762 bool ret;
764 sname = talloc_asprintf(talloc_tos(), "%s/%s", dirname, dirent);
765 if (sname == NULL) {
766 state->status = NT_STATUS_NO_MEMORY;
767 ret = false;
768 goto out;
771 status = create_synthetic_smb_fname(talloc_tos(), sname, NULL,
772 NULL, &smb_fname);
773 if (!NT_STATUS_IS_OK(status)) {
774 state->status = status;
775 ret = false;
776 goto out;
779 if (SMB_VFS_NEXT_STAT(state->handle, smb_fname) == -1) {
780 DEBUG(10, ("Could not stat %s: %s\n", sname,
781 strerror(errno)));
782 ret = true;
783 goto out;
786 if (!add_one_stream(state->mem_ctx,
787 &state->num_streams, &state->streams,
788 dirent, smb_fname->st.st_ex_size,
789 SMB_VFS_GET_ALLOC_SIZE(state->handle->conn, NULL,
790 &smb_fname->st))) {
791 state->status = NT_STATUS_NO_MEMORY;
792 ret = false;
793 goto out;
796 ret = true;
797 out:
798 TALLOC_FREE(sname);
799 TALLOC_FREE(smb_fname);
800 return ret;
803 static NTSTATUS streams_depot_streaminfo(vfs_handle_struct *handle,
804 struct files_struct *fsp,
805 const char *fname,
806 TALLOC_CTX *mem_ctx,
807 unsigned int *pnum_streams,
808 struct stream_struct **pstreams)
810 struct smb_filename *smb_fname_base = NULL;
811 int ret;
812 NTSTATUS status;
813 struct streaminfo_state state;
815 status = create_synthetic_smb_fname(talloc_tos(), fname, NULL, NULL,
816 &smb_fname_base);
817 if (!NT_STATUS_IS_OK(status)) {
818 return status;
821 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
822 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, &smb_fname_base->st);
824 else {
825 if (lp_posix_pathnames()) {
826 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
827 } else {
828 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
832 if (ret == -1) {
833 status = map_nt_error_from_unix(errno);
834 goto out;
837 state.streams = NULL;
838 state.num_streams = 0;
840 if (!S_ISDIR(smb_fname_base->st.st_ex_mode)) {
841 if (!add_one_stream(mem_ctx,
842 &state.num_streams, &state.streams,
843 "::$DATA", smb_fname_base->st.st_ex_size,
844 SMB_VFS_GET_ALLOC_SIZE(handle->conn, fsp,
845 &smb_fname_base->st))) {
846 status = NT_STATUS_NO_MEMORY;
847 goto out;
851 state.mem_ctx = mem_ctx;
852 state.handle = handle;
853 state.status = NT_STATUS_OK;
855 status = walk_streams(handle, smb_fname_base, NULL, collect_one_stream,
856 &state);
858 if (!NT_STATUS_IS_OK(status)) {
859 TALLOC_FREE(state.streams);
860 goto out;
863 if (!NT_STATUS_IS_OK(state.status)) {
864 TALLOC_FREE(state.streams);
865 status = state.status;
866 goto out;
869 *pnum_streams = state.num_streams;
870 *pstreams = state.streams;
871 status = NT_STATUS_OK;
873 out:
874 TALLOC_FREE(smb_fname_base);
875 return status;
878 static uint32_t streams_depot_fs_capabilities(struct vfs_handle_struct *handle,
879 enum timestamp_set_resolution *p_ts_res)
881 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
884 static struct vfs_fn_pointers vfs_streams_depot_fns = {
885 .fs_capabilities = streams_depot_fs_capabilities,
886 .open = streams_depot_open,
887 .stat = streams_depot_stat,
888 .lstat = streams_depot_lstat,
889 .unlink = streams_depot_unlink,
890 .rename = streams_depot_rename,
891 .streaminfo = streams_depot_streaminfo,
894 NTSTATUS vfs_streams_depot_init(void);
895 NTSTATUS vfs_streams_depot_init(void)
897 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_depot",
898 &vfs_streams_depot_fns);