tdb: Make tdb_new_database() follow a more conventional style
[Samba/bb.git] / source3 / modules / vfs_streams_xattr.c
blobdd1135d77ccbb82a4f23207710fbd455523431a6
1 /*
2 * Store streams in xattrs
4 * Copyright (C) Volker Lendecke, 2008
6 * Partly based on James Peach's Darwin module, which is
8 * Copyright (C) James Peach 2006-2007
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "smbd/smbd.h"
26 #include "system/filesys.h"
27 #include "../lib/crypto/md5.h"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_VFS
32 struct stream_io {
33 char *base;
34 char *xattr_name;
35 void *fsp_name_ptr;
36 files_struct *fsp;
37 vfs_handle_struct *handle;
40 static SMB_INO_T stream_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
42 struct MD5Context ctx;
43 unsigned char hash[16];
44 SMB_INO_T result;
45 char *upper_sname;
47 DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
48 (unsigned long)sbuf->st_ex_dev,
49 (unsigned long)sbuf->st_ex_ino, sname));
51 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
52 SMB_ASSERT(upper_sname != NULL);
54 MD5Init(&ctx);
55 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
56 sizeof(sbuf->st_ex_dev));
57 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
58 sizeof(sbuf->st_ex_ino));
59 MD5Update(&ctx, (unsigned char *)upper_sname,
60 talloc_get_size(upper_sname)-1);
61 MD5Final(hash, &ctx);
63 TALLOC_FREE(upper_sname);
65 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
66 memcpy(&result, hash, sizeof(result));
68 DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result));
70 return result;
73 static ssize_t get_xattr_size(connection_struct *conn,
74 files_struct *fsp,
75 const char *fname,
76 const char *xattr_name)
78 NTSTATUS status;
79 struct ea_struct ea;
80 ssize_t result;
82 status = get_ea_value(talloc_tos(), conn, fsp, fname,
83 xattr_name, &ea);
85 if (!NT_STATUS_IS_OK(status)) {
86 return -1;
89 result = ea.value.length-1;
90 TALLOC_FREE(ea.value.data);
91 return result;
94 /**
95 * Given a stream name, populate xattr_name with the xattr name to use for
96 * accessing the stream.
98 static NTSTATUS streams_xattr_get_name(TALLOC_CTX *ctx,
99 const char *stream_name,
100 char **xattr_name)
102 char *stype;
104 stype = strchr_m(stream_name + 1, ':');
106 *xattr_name = talloc_asprintf(ctx, "%s%s",
107 SAMBA_XATTR_DOSSTREAM_PREFIX,
108 stream_name + 1);
109 if (*xattr_name == NULL) {
110 return NT_STATUS_NO_MEMORY;
113 if (stype == NULL) {
114 /* Append an explicit stream type if one wasn't specified. */
115 *xattr_name = talloc_asprintf(ctx, "%s:$DATA",
116 *xattr_name);
117 if (*xattr_name == NULL) {
118 return NT_STATUS_NO_MEMORY;
120 } else {
121 /* Normalize the stream type to upercase. */
122 if (!strupper_m(strrchr_m(*xattr_name, ':') + 1)) {
123 return NT_STATUS_INVALID_PARAMETER;
127 DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name,
128 stream_name));
130 return NT_STATUS_OK;
133 static bool streams_xattr_recheck(struct stream_io *sio)
135 NTSTATUS status;
136 char *xattr_name = NULL;
138 if (sio->fsp->fsp_name == sio->fsp_name_ptr) {
139 return true;
142 if (sio->fsp->fsp_name->stream_name == NULL) {
143 /* how can this happen */
144 errno = EINVAL;
145 return false;
148 status = streams_xattr_get_name(talloc_tos(),
149 sio->fsp->fsp_name->stream_name,
150 &xattr_name);
151 if (!NT_STATUS_IS_OK(status)) {
152 return false;
155 TALLOC_FREE(sio->xattr_name);
156 TALLOC_FREE(sio->base);
157 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
158 xattr_name);
159 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
160 sio->fsp->fsp_name->base_name);
161 sio->fsp_name_ptr = sio->fsp->fsp_name;
163 TALLOC_FREE(xattr_name);
165 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
166 return false;
169 return true;
173 * Helper to stat/lstat the base file of an smb_fname.
175 static int streams_xattr_stat_base(vfs_handle_struct *handle,
176 struct smb_filename *smb_fname,
177 bool follow_links)
179 char *tmp_stream_name;
180 int result;
182 tmp_stream_name = smb_fname->stream_name;
183 smb_fname->stream_name = NULL;
184 if (follow_links) {
185 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
186 } else {
187 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
189 smb_fname->stream_name = tmp_stream_name;
190 return result;
193 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
194 SMB_STRUCT_STAT *sbuf)
196 struct smb_filename *smb_fname_base = NULL;
197 NTSTATUS status;
198 int ret = -1;
199 struct stream_io *io = (struct stream_io *)
200 VFS_FETCH_FSP_EXTENSION(handle, fsp);
202 DEBUG(10, ("streams_xattr_fstat called for %d\n", fsp->fh->fd));
204 if (io == NULL || fsp->base_fsp == NULL) {
205 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
208 if (!streams_xattr_recheck(io)) {
209 return -1;
212 /* Create an smb_filename with stream_name == NULL. */
213 status = create_synthetic_smb_fname(talloc_tos(),
214 io->base,
215 NULL, NULL,
216 &smb_fname_base);
217 if (!NT_STATUS_IS_OK(status)) {
218 errno = map_errno_from_nt_status(status);
219 return -1;
222 if (lp_posix_pathnames()) {
223 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_base);
224 } else {
225 ret = SMB_VFS_STAT(handle->conn, smb_fname_base);
227 *sbuf = smb_fname_base->st;
228 TALLOC_FREE(smb_fname_base);
230 if (ret == -1) {
231 return -1;
234 sbuf->st_ex_size = get_xattr_size(handle->conn, fsp->base_fsp,
235 io->base, io->xattr_name);
236 if (sbuf->st_ex_size == -1) {
237 return -1;
240 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
242 sbuf->st_ex_ino = stream_inode(sbuf, io->xattr_name);
243 sbuf->st_ex_mode &= ~S_IFMT;
244 sbuf->st_ex_mode |= S_IFREG;
245 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
247 return 0;
250 static int streams_xattr_stat(vfs_handle_struct *handle,
251 struct smb_filename *smb_fname)
253 NTSTATUS status;
254 int result = -1;
255 char *xattr_name = NULL;
257 if (!is_ntfs_stream_smb_fname(smb_fname)) {
258 return SMB_VFS_NEXT_STAT(handle, smb_fname);
261 /* Note if lp_posix_paths() is true, we can never
262 * get here as is_ntfs_stream_smb_fname() is
263 * always false. So we never need worry about
264 * not following links here. */
266 /* If the default stream is requested, just stat the base file. */
267 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
268 return streams_xattr_stat_base(handle, smb_fname, true);
271 /* Populate the stat struct with info from the base file. */
272 if (streams_xattr_stat_base(handle, smb_fname, true) == -1) {
273 return -1;
276 /* Derive the xattr name to lookup. */
277 status = streams_xattr_get_name(talloc_tos(), smb_fname->stream_name,
278 &xattr_name);
279 if (!NT_STATUS_IS_OK(status)) {
280 errno = map_errno_from_nt_status(status);
281 return -1;
284 /* Augment the base file's stat information before returning. */
285 smb_fname->st.st_ex_size = get_xattr_size(handle->conn, NULL,
286 smb_fname->base_name,
287 xattr_name);
288 if (smb_fname->st.st_ex_size == -1) {
289 errno = ENOENT;
290 result = -1;
291 goto fail;
294 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
295 smb_fname->st.st_ex_mode &= ~S_IFMT;
296 smb_fname->st.st_ex_mode |= S_IFREG;
297 smb_fname->st.st_ex_blocks =
298 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
300 result = 0;
301 fail:
302 TALLOC_FREE(xattr_name);
303 return result;
306 static int streams_xattr_lstat(vfs_handle_struct *handle,
307 struct smb_filename *smb_fname)
309 NTSTATUS status;
310 int result = -1;
311 char *xattr_name = NULL;
313 if (!is_ntfs_stream_smb_fname(smb_fname)) {
314 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
317 /* If the default stream is requested, just stat the base file. */
318 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
319 return streams_xattr_stat_base(handle, smb_fname, false);
322 /* Populate the stat struct with info from the base file. */
323 if (streams_xattr_stat_base(handle, smb_fname, false) == -1) {
324 return -1;
327 /* Derive the xattr name to lookup. */
328 status = streams_xattr_get_name(talloc_tos(), smb_fname->stream_name,
329 &xattr_name);
330 if (!NT_STATUS_IS_OK(status)) {
331 errno = map_errno_from_nt_status(status);
332 return -1;
335 /* Augment the base file's stat information before returning. */
336 smb_fname->st.st_ex_size = get_xattr_size(handle->conn, NULL,
337 smb_fname->base_name,
338 xattr_name);
339 if (smb_fname->st.st_ex_size == -1) {
340 errno = ENOENT;
341 result = -1;
342 goto fail;
345 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
346 smb_fname->st.st_ex_mode &= ~S_IFMT;
347 smb_fname->st.st_ex_mode |= S_IFREG;
348 smb_fname->st.st_ex_blocks =
349 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
351 result = 0;
353 fail:
354 TALLOC_FREE(xattr_name);
355 return result;
358 static int streams_xattr_open(vfs_handle_struct *handle,
359 struct smb_filename *smb_fname,
360 files_struct *fsp, int flags, mode_t mode)
362 NTSTATUS status;
363 struct smb_filename *smb_fname_base = NULL;
364 struct stream_io *sio;
365 struct ea_struct ea;
366 char *xattr_name = NULL;
367 int baseflags;
368 int hostfd = -1;
370 DEBUG(10, ("streams_xattr_open called for %s\n",
371 smb_fname_str_dbg(smb_fname)));
373 if (!is_ntfs_stream_smb_fname(smb_fname)) {
374 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
377 /* If the default stream is requested, just open the base file. */
378 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
379 char *tmp_stream_name;
380 int ret;
382 tmp_stream_name = smb_fname->stream_name;
383 smb_fname->stream_name = NULL;
385 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
387 smb_fname->stream_name = tmp_stream_name;
389 return ret;
392 status = streams_xattr_get_name(talloc_tos(), smb_fname->stream_name,
393 &xattr_name);
394 if (!NT_STATUS_IS_OK(status)) {
395 errno = map_errno_from_nt_status(status);
396 goto fail;
399 /* Create an smb_filename with stream_name == NULL. */
400 status = create_synthetic_smb_fname(talloc_tos(),
401 smb_fname->base_name,
402 NULL, NULL,
403 &smb_fname_base);
404 if (!NT_STATUS_IS_OK(status)) {
405 errno = map_errno_from_nt_status(status);
406 goto fail;
410 * We use baseflags to turn off nasty side-effects when opening the
411 * underlying file.
413 baseflags = flags;
414 baseflags &= ~O_TRUNC;
415 baseflags &= ~O_EXCL;
416 baseflags &= ~O_CREAT;
418 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
419 baseflags, mode);
421 TALLOC_FREE(smb_fname_base);
423 /* It is legit to open a stream on a directory, but the base
424 * fd has to be read-only.
426 if ((hostfd == -1) && (errno == EISDIR)) {
427 baseflags &= ~O_ACCMODE;
428 baseflags |= O_RDONLY;
429 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname, fsp, baseflags,
430 mode);
433 if (hostfd == -1) {
434 goto fail;
437 status = get_ea_value(talloc_tos(), handle->conn, NULL,
438 smb_fname->base_name, xattr_name, &ea);
440 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
442 if (!NT_STATUS_IS_OK(status)
443 && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
445 * The base file is not there. This is an error even if we got
446 * O_CREAT, the higher levels should have created the base
447 * file for us.
449 DEBUG(10, ("streams_xattr_open: base file %s not around, "
450 "returning ENOENT\n", smb_fname->base_name));
451 errno = ENOENT;
452 goto fail;
455 if (!NT_STATUS_IS_OK(status)) {
457 * The attribute does not exist
460 if (flags & O_CREAT) {
462 * Darn, xattrs need at least 1 byte
464 char null = '\0';
466 DEBUG(10, ("creating attribute %s on file %s\n",
467 xattr_name, smb_fname->base_name));
469 if (fsp->base_fsp->fh->fd != -1) {
470 if (SMB_VFS_FSETXATTR(
471 fsp->base_fsp, xattr_name,
472 &null, sizeof(null),
473 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
474 goto fail;
476 } else {
477 if (SMB_VFS_SETXATTR(
478 handle->conn, smb_fname->base_name,
479 xattr_name, &null, sizeof(null),
480 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
481 goto fail;
487 if (flags & O_TRUNC) {
488 char null = '\0';
489 if (fsp->base_fsp->fh->fd != -1) {
490 if (SMB_VFS_FSETXATTR(
491 fsp->base_fsp, xattr_name,
492 &null, sizeof(null),
493 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
494 goto fail;
496 } else {
497 if (SMB_VFS_SETXATTR(
498 handle->conn, smb_fname->base_name,
499 xattr_name, &null, sizeof(null),
500 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
501 goto fail;
506 sio = (struct stream_io *)VFS_ADD_FSP_EXTENSION(handle, fsp,
507 struct stream_io,
508 NULL);
509 if (sio == NULL) {
510 errno = ENOMEM;
511 goto fail;
514 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
515 xattr_name);
516 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
517 smb_fname->base_name);
518 sio->fsp_name_ptr = fsp->fsp_name;
519 sio->handle = handle;
520 sio->fsp = fsp;
522 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
523 errno = ENOMEM;
524 goto fail;
527 return hostfd;
529 fail:
530 if (hostfd >= 0) {
532 * BUGBUGBUG -- we would need to call fd_close_posix here, but
533 * we don't have a full fsp yet
535 SMB_VFS_CLOSE(fsp);
538 return -1;
541 static int streams_xattr_unlink(vfs_handle_struct *handle,
542 const struct smb_filename *smb_fname)
544 NTSTATUS status;
545 int ret = -1;
546 char *xattr_name;
548 if (!is_ntfs_stream_smb_fname(smb_fname)) {
549 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
552 /* If the default stream is requested, just open the base file. */
553 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
554 struct smb_filename *smb_fname_base = NULL;
556 status = copy_smb_filename(talloc_tos(), smb_fname,
557 &smb_fname_base);
558 if (!NT_STATUS_IS_OK(status)) {
559 errno = map_errno_from_nt_status(status);
560 return -1;
563 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_base);
565 TALLOC_FREE(smb_fname_base);
566 return ret;
569 status = streams_xattr_get_name(talloc_tos(), smb_fname->stream_name,
570 &xattr_name);
571 if (!NT_STATUS_IS_OK(status)) {
572 errno = map_errno_from_nt_status(status);
573 goto fail;
576 ret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname->base_name, xattr_name);
578 if ((ret == -1) && (errno == ENOATTR)) {
579 errno = ENOENT;
580 goto fail;
583 ret = 0;
585 fail:
586 TALLOC_FREE(xattr_name);
587 return ret;
590 static int streams_xattr_rename(vfs_handle_struct *handle,
591 const struct smb_filename *smb_fname_src,
592 const struct smb_filename *smb_fname_dst)
594 NTSTATUS status;
595 int ret = -1;
596 char *src_xattr_name = NULL;
597 char *dst_xattr_name = NULL;
598 bool src_is_stream, dst_is_stream;
599 ssize_t oret;
600 ssize_t nret;
601 struct ea_struct ea;
603 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
604 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
606 if (!src_is_stream && !dst_is_stream) {
607 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
608 smb_fname_dst);
611 /* For now don't allow renames from or to the default stream. */
612 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
613 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
614 errno = ENOSYS;
615 goto done;
618 /* Don't rename if the streams are identical. */
619 if (strcasecmp_m(smb_fname_src->stream_name,
620 smb_fname_dst->stream_name) == 0) {
621 goto done;
624 /* Get the xattr names. */
625 status = streams_xattr_get_name(talloc_tos(),
626 smb_fname_src->stream_name,
627 &src_xattr_name);
628 if (!NT_STATUS_IS_OK(status)) {
629 errno = map_errno_from_nt_status(status);
630 goto fail;
632 status = streams_xattr_get_name(talloc_tos(),
633 smb_fname_dst->stream_name,
634 &dst_xattr_name);
635 if (!NT_STATUS_IS_OK(status)) {
636 errno = map_errno_from_nt_status(status);
637 goto fail;
640 /* read the old stream */
641 status = get_ea_value(talloc_tos(), handle->conn, NULL,
642 smb_fname_src->base_name, src_xattr_name, &ea);
643 if (!NT_STATUS_IS_OK(status)) {
644 errno = ENOENT;
645 goto fail;
648 /* (over)write the new stream */
649 nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src->base_name,
650 dst_xattr_name, ea.value.data, ea.value.length,
652 if (nret < 0) {
653 if (errno == ENOATTR) {
654 errno = ENOENT;
656 goto fail;
659 /* remove the old stream */
660 oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src->base_name,
661 src_xattr_name);
662 if (oret < 0) {
663 if (errno == ENOATTR) {
664 errno = ENOENT;
666 goto fail;
669 done:
670 errno = 0;
671 ret = 0;
672 fail:
673 TALLOC_FREE(src_xattr_name);
674 TALLOC_FREE(dst_xattr_name);
675 return ret;
678 static NTSTATUS walk_xattr_streams(connection_struct *conn, files_struct *fsp,
679 const char *fname,
680 bool (*fn)(struct ea_struct *ea,
681 void *private_data),
682 void *private_data)
684 NTSTATUS status;
685 char **names;
686 size_t i, num_names;
687 size_t prefix_len = strlen(SAMBA_XATTR_DOSSTREAM_PREFIX);
689 status = get_ea_names_from_file(talloc_tos(), conn, fsp, fname,
690 &names, &num_names);
691 if (!NT_STATUS_IS_OK(status)) {
692 return status;
695 for (i=0; i<num_names; i++) {
696 struct ea_struct ea;
698 if (strncmp(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
699 prefix_len) != 0) {
700 continue;
703 status = get_ea_value(names, conn, fsp, fname, names[i], &ea);
704 if (!NT_STATUS_IS_OK(status)) {
705 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
706 names[i], fname, nt_errstr(status)));
707 continue;
710 ea.name = talloc_asprintf(ea.value.data, ":%s",
711 names[i] + prefix_len);
712 if (ea.name == NULL) {
713 DEBUG(0, ("talloc failed\n"));
714 continue;
717 if (!fn(&ea, private_data)) {
718 TALLOC_FREE(ea.value.data);
719 return NT_STATUS_OK;
722 TALLOC_FREE(ea.value.data);
725 TALLOC_FREE(names);
726 return NT_STATUS_OK;
729 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
730 struct stream_struct **streams,
731 const char *name, off_t size,
732 off_t alloc_size)
734 struct stream_struct *tmp;
736 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
737 (*num_streams)+1);
738 if (tmp == NULL) {
739 return false;
742 tmp[*num_streams].name = talloc_strdup(tmp, name);
743 if (tmp[*num_streams].name == NULL) {
744 return false;
747 tmp[*num_streams].size = size;
748 tmp[*num_streams].alloc_size = alloc_size;
750 *streams = tmp;
751 *num_streams += 1;
752 return true;
755 struct streaminfo_state {
756 TALLOC_CTX *mem_ctx;
757 vfs_handle_struct *handle;
758 unsigned int num_streams;
759 struct stream_struct *streams;
760 NTSTATUS status;
763 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
765 struct streaminfo_state *state =
766 (struct streaminfo_state *)private_data;
768 if (!add_one_stream(state->mem_ctx,
769 &state->num_streams, &state->streams,
770 ea->name, ea->value.length-1,
771 smb_roundup(state->handle->conn,
772 ea->value.length-1))) {
773 state->status = NT_STATUS_NO_MEMORY;
774 return false;
777 return true;
780 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
781 struct files_struct *fsp,
782 const char *fname,
783 TALLOC_CTX *mem_ctx,
784 unsigned int *pnum_streams,
785 struct stream_struct **pstreams)
787 SMB_STRUCT_STAT sbuf;
788 int ret;
789 NTSTATUS status;
790 struct streaminfo_state state;
792 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
793 ret = SMB_VFS_FSTAT(fsp, &sbuf);
795 else {
796 struct smb_filename *smb_fname = NULL;
797 status = create_synthetic_smb_fname(talloc_tos(), fname, NULL,
798 NULL, &smb_fname);
799 if (!NT_STATUS_IS_OK(status)) {
800 return status;
802 if (lp_posix_pathnames()) {
803 ret = SMB_VFS_LSTAT(handle->conn, smb_fname);
804 } else {
805 ret = SMB_VFS_STAT(handle->conn, smb_fname);
807 sbuf = smb_fname->st;
808 TALLOC_FREE(smb_fname);
811 if (ret == -1) {
812 return map_nt_error_from_unix(errno);
815 state.streams = *pstreams;
816 state.num_streams = *pnum_streams;
817 state.mem_ctx = mem_ctx;
818 state.handle = handle;
819 state.status = NT_STATUS_OK;
821 status = walk_xattr_streams(handle->conn, fsp, fname,
822 collect_one_stream, &state);
824 if (!NT_STATUS_IS_OK(status)) {
825 TALLOC_FREE(state.streams);
826 return status;
829 if (!NT_STATUS_IS_OK(state.status)) {
830 TALLOC_FREE(state.streams);
831 return state.status;
834 *pnum_streams = state.num_streams;
835 *pstreams = state.streams;
837 return SMB_VFS_NEXT_STREAMINFO(handle, fsp, fname, mem_ctx, pnum_streams, pstreams);
840 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
841 enum timestamp_set_resolution *p_ts_res)
843 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
846 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
847 files_struct *fsp, const void *data,
848 size_t n, off_t offset)
850 struct stream_io *sio =
851 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
852 struct ea_struct ea;
853 NTSTATUS status;
854 int ret;
856 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
858 if (sio == NULL) {
859 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
862 if (!streams_xattr_recheck(sio)) {
863 return -1;
866 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
867 sio->base, sio->xattr_name, &ea);
868 if (!NT_STATUS_IS_OK(status)) {
869 return -1;
872 if ((offset + n) > ea.value.length-1) {
873 uint8 *tmp;
875 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8,
876 offset + n + 1);
878 if (tmp == NULL) {
879 TALLOC_FREE(ea.value.data);
880 errno = ENOMEM;
881 return -1;
883 ea.value.data = tmp;
884 ea.value.length = offset + n + 1;
885 ea.value.data[offset+n] = 0;
888 memcpy(ea.value.data + offset, data, n);
890 if (fsp->base_fsp->fh->fd != -1) {
891 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
892 sio->xattr_name,
893 ea.value.data, ea.value.length, 0);
894 } else {
895 ret = SMB_VFS_SETXATTR(fsp->conn,
896 fsp->base_fsp->fsp_name->base_name,
897 sio->xattr_name,
898 ea.value.data, ea.value.length, 0);
900 TALLOC_FREE(ea.value.data);
902 if (ret == -1) {
903 return -1;
906 return n;
909 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
910 files_struct *fsp, void *data,
911 size_t n, off_t offset)
913 struct stream_io *sio =
914 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
915 struct ea_struct ea;
916 NTSTATUS status;
917 size_t length, overlap;
919 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
920 (int)offset, (int)n));
922 if (sio == NULL) {
923 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
926 if (!streams_xattr_recheck(sio)) {
927 return -1;
930 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
931 sio->base, sio->xattr_name, &ea);
932 if (!NT_STATUS_IS_OK(status)) {
933 return -1;
936 length = ea.value.length-1;
938 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
939 (int)length));
941 /* Attempt to read past EOF. */
942 if (length <= offset) {
943 return 0;
946 overlap = (offset + n) > length ? (length - offset) : n;
947 memcpy(data, ea.value.data + offset, overlap);
949 TALLOC_FREE(ea.value.data);
950 return overlap;
953 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
954 struct files_struct *fsp,
955 off_t offset)
957 int ret;
958 uint8 *tmp;
959 struct ea_struct ea;
960 NTSTATUS status;
961 struct stream_io *sio =
962 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
964 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
965 fsp_str_dbg(fsp), (double)offset));
967 if (sio == NULL) {
968 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
971 if (!streams_xattr_recheck(sio)) {
972 return -1;
975 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
976 sio->base, sio->xattr_name, &ea);
977 if (!NT_STATUS_IS_OK(status)) {
978 return -1;
981 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8,
982 offset + 1);
984 if (tmp == NULL) {
985 TALLOC_FREE(ea.value.data);
986 errno = ENOMEM;
987 return -1;
990 /* Did we expand ? */
991 if (ea.value.length < offset + 1) {
992 memset(&tmp[ea.value.length], '\0',
993 offset + 1 - ea.value.length);
996 ea.value.data = tmp;
997 ea.value.length = offset + 1;
998 ea.value.data[offset] = 0;
1000 if (fsp->base_fsp->fh->fd != -1) {
1001 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
1002 sio->xattr_name,
1003 ea.value.data, ea.value.length, 0);
1004 } else {
1005 ret = SMB_VFS_SETXATTR(fsp->conn,
1006 fsp->base_fsp->fsp_name->base_name,
1007 sio->xattr_name,
1008 ea.value.data, ea.value.length, 0);
1011 TALLOC_FREE(ea.value.data);
1013 if (ret == -1) {
1014 return -1;
1017 return 0;
1020 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1021 struct files_struct *fsp,
1022 enum vfs_fallocate_mode mode,
1023 off_t offset,
1024 off_t len)
1026 struct stream_io *sio =
1027 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1029 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1030 "len = %.0f\n",
1031 fsp_str_dbg(fsp), (double)offset, (double)len));
1033 if (sio == NULL) {
1034 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1037 if (!streams_xattr_recheck(sio)) {
1038 return errno;
1041 /* Let the pwrite code path handle it. */
1042 return ENOSYS;
1046 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1047 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1048 .open_fn = streams_xattr_open,
1049 .stat_fn = streams_xattr_stat,
1050 .fstat_fn = streams_xattr_fstat,
1051 .lstat_fn = streams_xattr_lstat,
1052 .pread_fn = streams_xattr_pread,
1053 .pwrite_fn = streams_xattr_pwrite,
1054 .unlink_fn = streams_xattr_unlink,
1055 .rename_fn = streams_xattr_rename,
1056 .ftruncate_fn = streams_xattr_ftruncate,
1057 .fallocate_fn = streams_xattr_fallocate,
1058 .streaminfo_fn = streams_xattr_streaminfo,
1061 NTSTATUS vfs_streams_xattr_init(void);
1062 NTSTATUS vfs_streams_xattr_init(void)
1064 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1065 &vfs_streams_xattr_fns);