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
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];
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
);
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);
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
));
73 static ssize_t
get_xattr_size(connection_struct
*conn
,
76 const char *xattr_name
)
82 status
= get_ea_value(talloc_tos(), conn
, fsp
, fname
,
85 if (!NT_STATUS_IS_OK(status
)) {
89 result
= ea
.value
.length
-1;
90 TALLOC_FREE(ea
.value
.data
);
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
,
104 stype
= strchr_m(stream_name
+ 1, ':');
106 *xattr_name
= talloc_asprintf(ctx
, "%s%s",
107 SAMBA_XATTR_DOSSTREAM_PREFIX
,
109 if (*xattr_name
== NULL
) {
110 return NT_STATUS_NO_MEMORY
;
114 /* Append an explicit stream type if one wasn't specified. */
115 *xattr_name
= talloc_asprintf(ctx
, "%s:$DATA",
117 if (*xattr_name
== NULL
) {
118 return NT_STATUS_NO_MEMORY
;
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
,
133 static bool streams_xattr_recheck(struct stream_io
*sio
)
136 char *xattr_name
= NULL
;
138 if (sio
->fsp
->fsp_name
== sio
->fsp_name_ptr
) {
142 if (sio
->fsp
->fsp_name
->stream_name
== NULL
) {
143 /* how can this happen */
148 status
= streams_xattr_get_name(talloc_tos(),
149 sio
->fsp
->fsp_name
->stream_name
,
151 if (!NT_STATUS_IS_OK(status
)) {
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
),
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
)) {
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
,
179 char *tmp_stream_name
;
182 tmp_stream_name
= smb_fname
->stream_name
;
183 smb_fname
->stream_name
= NULL
;
185 result
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
187 result
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
189 smb_fname
->stream_name
= tmp_stream_name
;
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
;
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
)) {
212 /* Create an smb_filename with stream_name == NULL. */
213 status
= create_synthetic_smb_fname(talloc_tos(),
217 if (!NT_STATUS_IS_OK(status
)) {
218 errno
= map_errno_from_nt_status(status
);
222 if (lp_posix_pathnames()) {
223 ret
= SMB_VFS_LSTAT(handle
->conn
, smb_fname_base
);
225 ret
= SMB_VFS_STAT(handle
->conn
, smb_fname_base
);
227 *sbuf
= smb_fname_base
->st
;
228 TALLOC_FREE(smb_fname_base
);
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) {
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;
250 static int streams_xattr_stat(vfs_handle_struct
*handle
,
251 struct smb_filename
*smb_fname
)
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) {
276 /* Derive the xattr name to lookup. */
277 status
= streams_xattr_get_name(talloc_tos(), smb_fname
->stream_name
,
279 if (!NT_STATUS_IS_OK(status
)) {
280 errno
= map_errno_from_nt_status(status
);
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
,
288 if (smb_fname
->st
.st_ex_size
== -1) {
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;
302 TALLOC_FREE(xattr_name
);
306 static int streams_xattr_lstat(vfs_handle_struct
*handle
,
307 struct smb_filename
*smb_fname
)
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) {
327 /* Derive the xattr name to lookup. */
328 status
= streams_xattr_get_name(talloc_tos(), smb_fname
->stream_name
,
330 if (!NT_STATUS_IS_OK(status
)) {
331 errno
= map_errno_from_nt_status(status
);
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
,
339 if (smb_fname
->st
.st_ex_size
== -1) {
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;
354 TALLOC_FREE(xattr_name
);
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
)
363 struct smb_filename
*smb_fname_base
= NULL
;
364 struct stream_io
*sio
;
366 char *xattr_name
= NULL
;
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
;
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
;
392 status
= streams_xattr_get_name(talloc_tos(), smb_fname
->stream_name
,
394 if (!NT_STATUS_IS_OK(status
)) {
395 errno
= map_errno_from_nt_status(status
);
399 /* Create an smb_filename with stream_name == NULL. */
400 status
= create_synthetic_smb_fname(talloc_tos(),
401 smb_fname
->base_name
,
404 if (!NT_STATUS_IS_OK(status
)) {
405 errno
= map_errno_from_nt_status(status
);
410 * We use baseflags to turn off nasty side-effects when opening the
414 baseflags
&= ~O_TRUNC
;
415 baseflags
&= ~O_EXCL
;
416 baseflags
&= ~O_CREAT
;
418 hostfd
= SMB_VFS_OPEN(handle
->conn
, smb_fname_base
, fsp
,
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
,
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
449 DEBUG(10, ("streams_xattr_open: base file %s not around, "
450 "returning ENOENT\n", smb_fname
->base_name
));
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
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
,
473 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
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) {
487 if (flags
& O_TRUNC
) {
489 if (fsp
->base_fsp
->fh
->fd
!= -1) {
490 if (SMB_VFS_FSETXATTR(
491 fsp
->base_fsp
, xattr_name
,
493 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
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) {
506 sio
= (struct stream_io
*)VFS_ADD_FSP_EXTENSION(handle
, fsp
,
514 sio
->xattr_name
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
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
;
522 if ((sio
->xattr_name
== NULL
) || (sio
->base
== NULL
)) {
532 * BUGBUGBUG -- we would need to call fd_close_posix here, but
533 * we don't have a full fsp yet
541 static int streams_xattr_unlink(vfs_handle_struct
*handle
,
542 const struct smb_filename
*smb_fname
)
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
,
558 if (!NT_STATUS_IS_OK(status
)) {
559 errno
= map_errno_from_nt_status(status
);
563 ret
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname_base
);
565 TALLOC_FREE(smb_fname_base
);
569 status
= streams_xattr_get_name(talloc_tos(), smb_fname
->stream_name
,
571 if (!NT_STATUS_IS_OK(status
)) {
572 errno
= map_errno_from_nt_status(status
);
576 ret
= SMB_VFS_REMOVEXATTR(handle
->conn
, smb_fname
->base_name
, xattr_name
);
578 if ((ret
== -1) && (errno
== ENOATTR
)) {
586 TALLOC_FREE(xattr_name
);
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
)
596 char *src_xattr_name
= NULL
;
597 char *dst_xattr_name
= NULL
;
598 bool src_is_stream
, dst_is_stream
;
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
,
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
)) {
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) {
624 /* Get the xattr names. */
625 status
= streams_xattr_get_name(talloc_tos(),
626 smb_fname_src
->stream_name
,
628 if (!NT_STATUS_IS_OK(status
)) {
629 errno
= map_errno_from_nt_status(status
);
632 status
= streams_xattr_get_name(talloc_tos(),
633 smb_fname_dst
->stream_name
,
635 if (!NT_STATUS_IS_OK(status
)) {
636 errno
= map_errno_from_nt_status(status
);
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
)) {
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
,
653 if (errno
== ENOATTR
) {
659 /* remove the old stream */
660 oret
= SMB_VFS_REMOVEXATTR(handle
->conn
, smb_fname_src
->base_name
,
663 if (errno
== ENOATTR
) {
673 TALLOC_FREE(src_xattr_name
);
674 TALLOC_FREE(dst_xattr_name
);
678 static NTSTATUS
walk_xattr_streams(connection_struct
*conn
, files_struct
*fsp
,
680 bool (*fn
)(struct ea_struct
*ea
,
687 size_t prefix_len
= strlen(SAMBA_XATTR_DOSSTREAM_PREFIX
);
689 status
= get_ea_names_from_file(talloc_tos(), conn
, fsp
, fname
,
691 if (!NT_STATUS_IS_OK(status
)) {
695 for (i
=0; i
<num_names
; i
++) {
698 if (strncmp(names
[i
], SAMBA_XATTR_DOSSTREAM_PREFIX
,
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
)));
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"));
717 if (!fn(&ea
, private_data
)) {
718 TALLOC_FREE(ea
.value
.data
);
722 TALLOC_FREE(ea
.value
.data
);
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
,
734 struct stream_struct
*tmp
;
736 tmp
= talloc_realloc(mem_ctx
, *streams
, struct stream_struct
,
742 tmp
[*num_streams
].name
= talloc_strdup(tmp
, name
);
743 if (tmp
[*num_streams
].name
== NULL
) {
747 tmp
[*num_streams
].size
= size
;
748 tmp
[*num_streams
].alloc_size
= alloc_size
;
755 struct streaminfo_state
{
757 vfs_handle_struct
*handle
;
758 unsigned int num_streams
;
759 struct stream_struct
*streams
;
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
;
780 static NTSTATUS
streams_xattr_streaminfo(vfs_handle_struct
*handle
,
781 struct files_struct
*fsp
,
784 unsigned int *pnum_streams
,
785 struct stream_struct
**pstreams
)
787 SMB_STRUCT_STAT sbuf
;
790 struct streaminfo_state state
;
792 if ((fsp
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
793 ret
= SMB_VFS_FSTAT(fsp
, &sbuf
);
796 struct smb_filename
*smb_fname
= NULL
;
797 status
= create_synthetic_smb_fname(talloc_tos(), fname
, NULL
,
799 if (!NT_STATUS_IS_OK(status
)) {
802 if (lp_posix_pathnames()) {
803 ret
= SMB_VFS_LSTAT(handle
->conn
, smb_fname
);
805 ret
= SMB_VFS_STAT(handle
->conn
, smb_fname
);
807 sbuf
= smb_fname
->st
;
808 TALLOC_FREE(smb_fname
);
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
);
829 if (!NT_STATUS_IS_OK(state
.status
)) {
830 TALLOC_FREE(state
.streams
);
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
);
856 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n
));
859 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
862 if (!streams_xattr_recheck(sio
)) {
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
)) {
872 if ((offset
+ n
) > ea
.value
.length
-1) {
875 tmp
= talloc_realloc(talloc_tos(), ea
.value
.data
, uint8
,
879 TALLOC_FREE(ea
.value
.data
);
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
,
893 ea
.value
.data
, ea
.value
.length
, 0);
895 ret
= SMB_VFS_SETXATTR(fsp
->conn
,
896 fsp
->base_fsp
->fsp_name
->base_name
,
898 ea
.value
.data
, ea
.value
.length
, 0);
900 TALLOC_FREE(ea
.value
.data
);
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
);
917 size_t length
, overlap
;
919 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
920 (int)offset
, (int)n
));
923 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
926 if (!streams_xattr_recheck(sio
)) {
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
)) {
936 length
= ea
.value
.length
-1;
938 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
941 /* Attempt to read past EOF. */
942 if (length
<= offset
) {
946 overlap
= (offset
+ n
) > length
? (length
- offset
) : n
;
947 memcpy(data
, ea
.value
.data
+ offset
, overlap
);
949 TALLOC_FREE(ea
.value
.data
);
953 static int streams_xattr_ftruncate(struct vfs_handle_struct
*handle
,
954 struct files_struct
*fsp
,
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
));
968 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
971 if (!streams_xattr_recheck(sio
)) {
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
)) {
981 tmp
= talloc_realloc(talloc_tos(), ea
.value
.data
, uint8
,
985 TALLOC_FREE(ea
.value
.data
);
990 /* Did we expand ? */
991 if (ea
.value
.length
< offset
+ 1) {
992 memset(&tmp
[ea
.value
.length
], '\0',
993 offset
+ 1 - ea
.value
.length
);
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
,
1003 ea
.value
.data
, ea
.value
.length
, 0);
1005 ret
= SMB_VFS_SETXATTR(fsp
->conn
,
1006 fsp
->base_fsp
->fsp_name
->base_name
,
1008 ea
.value
.data
, ea
.value
.length
, 0);
1011 TALLOC_FREE(ea
.value
.data
);
1020 static int streams_xattr_fallocate(struct vfs_handle_struct
*handle
,
1021 struct files_struct
*fsp
,
1022 enum vfs_fallocate_mode mode
,
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"
1031 fsp_str_dbg(fsp
), (double)offset
, (double)len
));
1034 return SMB_VFS_NEXT_FALLOCATE(handle
, fsp
, mode
, offset
, len
);
1037 if (!streams_xattr_recheck(sio
)) {
1041 /* Let the pwrite code path handle it. */
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
);