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/>.
25 #include "smbd/smbd.h"
26 #include "system/filesys.h"
27 #include "../lib/crypto/md5.h"
30 #define DBGC_CLASS DBGC_VFS
32 struct streams_xattr_config {
35 bool store_stream_type;
43 vfs_handle_struct *handle;
46 static SMB_INO_T stream_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
49 unsigned char hash[16];
53 DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
54 (unsigned long)sbuf->st_ex_dev,
55 (unsigned long)sbuf->st_ex_ino, sname));
57 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
58 SMB_ASSERT(upper_sname != NULL);
61 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
62 sizeof(sbuf->st_ex_dev));
63 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
64 sizeof(sbuf->st_ex_ino));
65 MD5Update(&ctx, (unsigned char *)upper_sname,
66 talloc_get_size(upper_sname)-1);
69 TALLOC_FREE(upper_sname);
71 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
72 memcpy(&result, hash, sizeof(result));
74 DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result));
79 static ssize_t get_xattr_size(connection_struct *conn,
82 const char *xattr_name)
88 status = get_ea_value(talloc_tos(), conn, fsp, fname,
91 if (!NT_STATUS_IS_OK(status)) {
95 result = ea.value.length-1;
96 TALLOC_FREE(ea.value.data);
101 * Given a stream name, populate xattr_name with the xattr name to use for
102 * accessing the stream.
104 static NTSTATUS streams_xattr_get_name(vfs_handle_struct *handle,
106 const char *stream_name,
110 struct streams_xattr_config *config;
112 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
113 return NT_STATUS_UNSUCCESSFUL);
115 stype = strchr_m(stream_name + 1, ':');
117 *xattr_name = talloc_asprintf(ctx, "%s%s",
120 if (*xattr_name == NULL) {
121 return NT_STATUS_NO_MEMORY;
125 /* Normalize the stream type to upercase. */
126 if (!strupper_m(strrchr_m(*xattr_name, ':') + 1)) {
127 return NT_STATUS_INVALID_PARAMETER;
129 } else if (config->store_stream_type) {
131 * Append an explicit stream type if one wasn't
134 *xattr_name = talloc_asprintf(ctx, "%s%s",
135 *xattr_name, ":$DATA");
136 if (*xattr_name == NULL) {
137 return NT_STATUS_NO_MEMORY;
141 DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name,
147 static bool streams_xattr_recheck(struct stream_io *sio)
150 char *xattr_name = NULL;
152 if (sio->fsp->fsp_name == sio->fsp_name_ptr) {
156 if (sio->fsp->fsp_name->stream_name == NULL) {
157 /* how can this happen */
162 status = streams_xattr_get_name(sio->handle, talloc_tos(),
163 sio->fsp->fsp_name->stream_name,
165 if (!NT_STATUS_IS_OK(status)) {
169 TALLOC_FREE(sio->xattr_name);
170 TALLOC_FREE(sio->base);
171 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
173 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
174 sio->fsp->fsp_name->base_name);
175 sio->fsp_name_ptr = sio->fsp->fsp_name;
177 TALLOC_FREE(xattr_name);
179 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
187 * Helper to stat/lstat the base file of an smb_fname.
189 static int streams_xattr_stat_base(vfs_handle_struct *handle,
190 struct smb_filename *smb_fname,
193 char *tmp_stream_name;
196 tmp_stream_name = smb_fname->stream_name;
197 smb_fname->stream_name = NULL;
199 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
201 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
203 smb_fname->stream_name = tmp_stream_name;
207 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
208 SMB_STRUCT_STAT *sbuf)
210 struct smb_filename *smb_fname_base = NULL;
212 struct stream_io *io = (struct stream_io *)
213 VFS_FETCH_FSP_EXTENSION(handle, fsp);
215 DEBUG(10, ("streams_xattr_fstat called for %d\n", fsp->fh->fd));
217 if (io == NULL || fsp->base_fsp == NULL) {
218 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
221 if (!streams_xattr_recheck(io)) {
225 /* Create an smb_filename with stream_name == NULL. */
226 smb_fname_base = synthetic_smb_fname(talloc_tos(), io->base,
228 if (smb_fname_base == NULL) {
233 if (lp_posix_pathnames()) {
234 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_base);
236 ret = SMB_VFS_STAT(handle->conn, smb_fname_base);
238 *sbuf = smb_fname_base->st;
239 TALLOC_FREE(smb_fname_base);
245 sbuf->st_ex_size = get_xattr_size(handle->conn, fsp->base_fsp,
246 io->base, io->xattr_name);
247 if (sbuf->st_ex_size == -1) {
251 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
253 sbuf->st_ex_ino = stream_inode(sbuf, io->xattr_name);
254 sbuf->st_ex_mode &= ~S_IFMT;
255 sbuf->st_ex_mode |= S_IFREG;
256 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
261 static int streams_xattr_stat(vfs_handle_struct *handle,
262 struct smb_filename *smb_fname)
266 char *xattr_name = NULL;
268 if (!is_ntfs_stream_smb_fname(smb_fname)) {
269 return SMB_VFS_NEXT_STAT(handle, smb_fname);
272 /* Note if lp_posix_paths() is true, we can never
273 * get here as is_ntfs_stream_smb_fname() is
274 * always false. So we never need worry about
275 * not following links here. */
277 /* If the default stream is requested, just stat the base file. */
278 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
279 return streams_xattr_stat_base(handle, smb_fname, true);
282 /* Populate the stat struct with info from the base file. */
283 if (streams_xattr_stat_base(handle, smb_fname, true) == -1) {
287 /* Derive the xattr name to lookup. */
288 status = streams_xattr_get_name(handle, talloc_tos(),
289 smb_fname->stream_name, &xattr_name);
290 if (!NT_STATUS_IS_OK(status)) {
291 errno = map_errno_from_nt_status(status);
295 /* Augment the base file's stat information before returning. */
296 smb_fname->st.st_ex_size = get_xattr_size(handle->conn, NULL,
297 smb_fname->base_name,
299 if (smb_fname->st.st_ex_size == -1) {
305 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
306 smb_fname->st.st_ex_mode &= ~S_IFMT;
307 smb_fname->st.st_ex_mode |= S_IFREG;
308 smb_fname->st.st_ex_blocks =
309 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
313 TALLOC_FREE(xattr_name);
317 static int streams_xattr_lstat(vfs_handle_struct *handle,
318 struct smb_filename *smb_fname)
322 char *xattr_name = NULL;
324 if (!is_ntfs_stream_smb_fname(smb_fname)) {
325 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
328 /* If the default stream is requested, just stat the base file. */
329 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
330 return streams_xattr_stat_base(handle, smb_fname, false);
333 /* Populate the stat struct with info from the base file. */
334 if (streams_xattr_stat_base(handle, smb_fname, false) == -1) {
338 /* Derive the xattr name to lookup. */
339 status = streams_xattr_get_name(handle, talloc_tos(),
340 smb_fname->stream_name, &xattr_name);
341 if (!NT_STATUS_IS_OK(status)) {
342 errno = map_errno_from_nt_status(status);
346 /* Augment the base file's stat information before returning. */
347 smb_fname->st.st_ex_size = get_xattr_size(handle->conn, NULL,
348 smb_fname->base_name,
350 if (smb_fname->st.st_ex_size == -1) {
356 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
357 smb_fname->st.st_ex_mode &= ~S_IFMT;
358 smb_fname->st.st_ex_mode |= S_IFREG;
359 smb_fname->st.st_ex_blocks =
360 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
365 TALLOC_FREE(xattr_name);
369 static int streams_xattr_open(vfs_handle_struct *handle,
370 struct smb_filename *smb_fname,
371 files_struct *fsp, int flags, mode_t mode)
374 struct smb_filename *smb_fname_base = NULL;
375 struct stream_io *sio;
377 char *xattr_name = NULL;
381 DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
382 smb_fname_str_dbg(smb_fname), flags));
384 if (!is_ntfs_stream_smb_fname(smb_fname)) {
385 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
388 /* If the default stream is requested, just open the base file. */
389 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
390 char *tmp_stream_name;
393 tmp_stream_name = smb_fname->stream_name;
394 smb_fname->stream_name = NULL;
396 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
398 smb_fname->stream_name = tmp_stream_name;
403 status = streams_xattr_get_name(handle, talloc_tos(),
404 smb_fname->stream_name, &xattr_name);
405 if (!NT_STATUS_IS_OK(status)) {
406 errno = map_errno_from_nt_status(status);
410 /* Create an smb_filename with stream_name == NULL. */
411 smb_fname_base = synthetic_smb_fname(
412 talloc_tos(), smb_fname->base_name, NULL, NULL);
413 if (smb_fname_base == NULL) {
419 * We use baseflags to turn off nasty side-effects when opening the
423 baseflags &= ~O_TRUNC;
424 baseflags &= ~O_EXCL;
425 baseflags &= ~O_CREAT;
427 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
430 TALLOC_FREE(smb_fname_base);
432 /* It is legit to open a stream on a directory, but the base
433 * fd has to be read-only.
435 if ((hostfd == -1) && (errno == EISDIR)) {
436 baseflags &= ~O_ACCMODE;
437 baseflags |= O_RDONLY;
438 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname, fsp, baseflags,
446 status = get_ea_value(talloc_tos(), handle->conn, NULL,
447 smb_fname->base_name, xattr_name, &ea);
449 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
451 if (!NT_STATUS_IS_OK(status)
452 && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
454 * The base file is not there. This is an error even if we got
455 * O_CREAT, the higher levels should have created the base
458 DEBUG(10, ("streams_xattr_open: base file %s not around, "
459 "returning ENOENT\n", smb_fname->base_name));
464 if ((!NT_STATUS_IS_OK(status) && (flags & O_CREAT)) ||
467 * The attribute does not exist or needs to be truncated
471 * Darn, xattrs need at least 1 byte
475 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
476 xattr_name, smb_fname->base_name));
478 if (fsp->base_fsp->fh->fd != -1) {
479 if (SMB_VFS_FSETXATTR(
480 fsp->base_fsp, xattr_name,
482 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
486 if (SMB_VFS_SETXATTR(
487 handle->conn, smb_fname->base_name,
488 xattr_name, &null, sizeof(null),
489 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
495 sio = (struct stream_io *)VFS_ADD_FSP_EXTENSION(handle, fsp,
503 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
505 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
506 smb_fname->base_name);
507 sio->fsp_name_ptr = fsp->fsp_name;
508 sio->handle = handle;
511 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
521 * BUGBUGBUG -- we would need to call fd_close_posix here, but
522 * we don't have a full fsp yet
524 fsp->fh->fd = hostfd;
531 static int streams_xattr_unlink(vfs_handle_struct *handle,
532 const struct smb_filename *smb_fname)
536 char *xattr_name = NULL;
538 if (!is_ntfs_stream_smb_fname(smb_fname)) {
539 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
542 /* If the default stream is requested, just open the base file. */
543 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
544 struct smb_filename *smb_fname_base = NULL;
546 smb_fname_base = cp_smb_filename(talloc_tos(), smb_fname);
547 if (smb_fname_base == NULL) {
552 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_base);
554 TALLOC_FREE(smb_fname_base);
558 status = streams_xattr_get_name(handle, talloc_tos(),
559 smb_fname->stream_name, &xattr_name);
560 if (!NT_STATUS_IS_OK(status)) {
561 errno = map_errno_from_nt_status(status);
565 ret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname->base_name, xattr_name);
567 if ((ret == -1) && (errno == ENOATTR)) {
575 TALLOC_FREE(xattr_name);
579 static int streams_xattr_rename(vfs_handle_struct *handle,
580 const struct smb_filename *smb_fname_src,
581 const struct smb_filename *smb_fname_dst)
585 char *src_xattr_name = NULL;
586 char *dst_xattr_name = NULL;
587 bool src_is_stream, dst_is_stream;
592 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
593 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
595 if (!src_is_stream && !dst_is_stream) {
596 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
600 /* For now don't allow renames from or to the default stream. */
601 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
602 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
607 /* Don't rename if the streams are identical. */
608 if (strcasecmp_m(smb_fname_src->stream_name,
609 smb_fname_dst->stream_name) == 0) {
613 /* Get the xattr names. */
614 status = streams_xattr_get_name(handle, talloc_tos(),
615 smb_fname_src->stream_name,
617 if (!NT_STATUS_IS_OK(status)) {
618 errno = map_errno_from_nt_status(status);
621 status = streams_xattr_get_name(handle, talloc_tos(),
622 smb_fname_dst->stream_name,
624 if (!NT_STATUS_IS_OK(status)) {
625 errno = map_errno_from_nt_status(status);
629 /* read the old stream */
630 status = get_ea_value(talloc_tos(), handle->conn, NULL,
631 smb_fname_src->base_name, src_xattr_name, &ea);
632 if (!NT_STATUS_IS_OK(status)) {
637 /* (over)write the new stream */
638 nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src->base_name,
639 dst_xattr_name, ea.value.data, ea.value.length,
642 if (errno == ENOATTR) {
648 /* remove the old stream */
649 oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src->base_name,
652 if (errno == ENOATTR) {
662 TALLOC_FREE(src_xattr_name);
663 TALLOC_FREE(dst_xattr_name);
667 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle, files_struct *fsp,
669 bool (*fn)(struct ea_struct *ea,
676 struct streams_xattr_config *config;
678 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
679 return NT_STATUS_UNSUCCESSFUL);
681 status = get_ea_names_from_file(talloc_tos(), handle->conn, fsp, fname,
683 if (!NT_STATUS_IS_OK(status)) {
687 for (i=0; i<num_names; i++) {
691 * We want to check with samba_private_attr_name()
692 * whether the xattr name is a private one,
693 * unfortunately it flags xattrs that begin with the
694 * default streams prefix as private.
696 * By only calling samba_private_attr_name() in case
697 * the xattr does NOT begin with the default prefix,
698 * we know that if it returns 'true' it definitely one
699 * of our internal xattr like "user.DOSATTRIB".
701 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
702 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
703 if (samba_private_attr_name(names[i])) {
708 if (strncmp(names[i], config->prefix,
709 config->prefix_len) != 0) {
713 status = get_ea_value(names, handle->conn, fsp, fname,
715 if (!NT_STATUS_IS_OK(status)) {
716 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
717 names[i], fname, nt_errstr(status)));
721 ea.name = talloc_asprintf(
722 ea.value.data, ":%s%s",
723 names[i] + config->prefix_len,
724 config->store_stream_type ? "" : ":$DATA");
725 if (ea.name == NULL) {
726 DEBUG(0, ("talloc failed\n"));
730 if (!fn(&ea, private_data)) {
731 TALLOC_FREE(ea.value.data);
735 TALLOC_FREE(ea.value.data);
742 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
743 struct stream_struct **streams,
744 const char *name, off_t size,
747 struct stream_struct *tmp;
749 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
755 tmp[*num_streams].name = talloc_strdup(tmp, name);
756 if (tmp[*num_streams].name == NULL) {
760 tmp[*num_streams].size = size;
761 tmp[*num_streams].alloc_size = alloc_size;
768 struct streaminfo_state {
770 vfs_handle_struct *handle;
771 unsigned int num_streams;
772 struct stream_struct *streams;
776 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
778 struct streaminfo_state *state =
779 (struct streaminfo_state *)private_data;
781 if (!add_one_stream(state->mem_ctx,
782 &state->num_streams, &state->streams,
783 ea->name, ea->value.length-1,
784 smb_roundup(state->handle->conn,
785 ea->value.length-1))) {
786 state->status = NT_STATUS_NO_MEMORY;
793 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
794 struct files_struct *fsp,
797 unsigned int *pnum_streams,
798 struct stream_struct **pstreams)
800 SMB_STRUCT_STAT sbuf;
803 struct streaminfo_state state;
805 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
806 ret = SMB_VFS_FSTAT(fsp, &sbuf);
809 struct smb_filename *smb_fname = NULL;
810 smb_fname = synthetic_smb_fname(talloc_tos(), fname, NULL,
812 if (smb_fname == NULL) {
813 return NT_STATUS_NO_MEMORY;
815 if (lp_posix_pathnames()) {
816 ret = SMB_VFS_LSTAT(handle->conn, smb_fname);
818 ret = SMB_VFS_STAT(handle->conn, smb_fname);
820 sbuf = smb_fname->st;
821 TALLOC_FREE(smb_fname);
825 return map_nt_error_from_unix(errno);
828 state.streams = *pstreams;
829 state.num_streams = *pnum_streams;
830 state.mem_ctx = mem_ctx;
831 state.handle = handle;
832 state.status = NT_STATUS_OK;
834 if (S_ISLNK(sbuf.st_ex_mode)) {
836 * Currently we do't have SMB_VFS_LLISTXATTR
837 * inside the VFS which means there's no way
838 * to cope with a symlink when lp_posix_pathnames().
839 * returns true. For now ignore links.
840 * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
842 status = NT_STATUS_OK;
844 status = walk_xattr_streams(handle, fsp, fname,
845 collect_one_stream, &state);
848 if (!NT_STATUS_IS_OK(status)) {
849 TALLOC_FREE(state.streams);
853 if (!NT_STATUS_IS_OK(state.status)) {
854 TALLOC_FREE(state.streams);
858 *pnum_streams = state.num_streams;
859 *pstreams = state.streams;
861 return SMB_VFS_NEXT_STREAMINFO(handle, fsp, fname, mem_ctx, pnum_streams, pstreams);
864 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
865 enum timestamp_set_resolution *p_ts_res)
867 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
870 static int streams_xattr_connect(vfs_handle_struct *handle,
871 const char *service, const char *user)
873 struct streams_xattr_config *config;
874 const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
877 config = talloc_zero(handle->conn, struct streams_xattr_config);
878 if (config == NULL) {
879 DEBUG(1, ("talloc_zero() failed\n"));
884 prefix = lp_parm_const_string(SNUM(handle->conn),
885 "streams_xattr", "prefix",
887 config->prefix = talloc_strdup(config, prefix);
888 if (config->prefix == NULL) {
889 DEBUG(1, ("talloc_strdup() failed\n"));
893 config->prefix_len = strlen(config->prefix);
894 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
896 config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
901 SMB_VFS_HANDLE_SET_DATA(handle, config,
902 NULL, struct stream_xattr_config,
908 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
909 files_struct *fsp, const void *data,
910 size_t n, off_t offset)
912 struct stream_io *sio =
913 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
918 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
921 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
924 if (!streams_xattr_recheck(sio)) {
928 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
929 sio->base, sio->xattr_name, &ea);
930 if (!NT_STATUS_IS_OK(status)) {
934 if ((offset + n) > ea.value.length-1) {
937 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8,
941 TALLOC_FREE(ea.value.data);
946 ea.value.length = offset + n + 1;
947 ea.value.data[offset+n] = 0;
950 memcpy(ea.value.data + offset, data, n);
952 if (fsp->base_fsp->fh->fd != -1) {
953 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
955 ea.value.data, ea.value.length, 0);
957 ret = SMB_VFS_SETXATTR(fsp->conn,
958 fsp->base_fsp->fsp_name->base_name,
960 ea.value.data, ea.value.length, 0);
962 TALLOC_FREE(ea.value.data);
971 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
972 files_struct *fsp, void *data,
973 size_t n, off_t offset)
975 struct stream_io *sio =
976 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
979 size_t length, overlap;
981 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
982 (int)offset, (int)n));
985 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
988 if (!streams_xattr_recheck(sio)) {
992 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
993 sio->base, sio->xattr_name, &ea);
994 if (!NT_STATUS_IS_OK(status)) {
998 length = ea.value.length-1;
1000 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1003 /* Attempt to read past EOF. */
1004 if (length <= offset) {
1008 overlap = (offset + n) > length ? (length - offset) : n;
1009 memcpy(data, ea.value.data + offset, overlap);
1011 TALLOC_FREE(ea.value.data);
1015 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1016 struct files_struct *fsp,
1021 struct ea_struct ea;
1023 struct stream_io *sio =
1024 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1026 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1027 fsp_str_dbg(fsp), (double)offset));
1030 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1033 if (!streams_xattr_recheck(sio)) {
1037 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
1038 sio->base, sio->xattr_name, &ea);
1039 if (!NT_STATUS_IS_OK(status)) {
1043 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8,
1047 TALLOC_FREE(ea.value.data);
1052 /* Did we expand ? */
1053 if (ea.value.length < offset + 1) {
1054 memset(&tmp[ea.value.length], '\0',
1055 offset + 1 - ea.value.length);
1058 ea.value.data = tmp;
1059 ea.value.length = offset + 1;
1060 ea.value.data[offset] = 0;
1062 if (fsp->base_fsp->fh->fd != -1) {
1063 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
1065 ea.value.data, ea.value.length, 0);
1067 ret = SMB_VFS_SETXATTR(fsp->conn,
1068 fsp->base_fsp->fsp_name->base_name,
1070 ea.value.data, ea.value.length, 0);
1073 TALLOC_FREE(ea.value.data);
1082 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1083 struct files_struct *fsp,
1084 enum vfs_fallocate_mode mode,
1088 struct stream_io *sio =
1089 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1091 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1093 fsp_str_dbg(fsp), (double)offset, (double)len));
1096 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1099 if (!streams_xattr_recheck(sio)) {
1103 /* Let the pwrite code path handle it. */
1108 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1109 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1110 .connect_fn = streams_xattr_connect,
1111 .open_fn = streams_xattr_open,
1112 .stat_fn = streams_xattr_stat,
1113 .fstat_fn = streams_xattr_fstat,
1114 .lstat_fn = streams_xattr_lstat,
1115 .pread_fn = streams_xattr_pread,
1116 .pwrite_fn = streams_xattr_pwrite,
1117 .unlink_fn = streams_xattr_unlink,
1118 .rename_fn = streams_xattr_rename,
1119 .ftruncate_fn = streams_xattr_ftruncate,
1120 .fallocate_fn = streams_xattr_fallocate,
1121 .streaminfo_fn = streams_xattr_streaminfo,
1124 NTSTATUS vfs_streams_xattr_init(void);
1125 NTSTATUS vfs_streams_xattr_init(void)
1127 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1128 &vfs_streams_xattr_fns);