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 smb_fname_base
= cp_smb_filename(talloc_tos(), smb_fname
);
557 if (smb_fname_base
== NULL
) {
562 ret
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname_base
);
564 TALLOC_FREE(smb_fname_base
);
568 status
= streams_xattr_get_name(talloc_tos(), smb_fname
->stream_name
,
570 if (!NT_STATUS_IS_OK(status
)) {
571 errno
= map_errno_from_nt_status(status
);
575 ret
= SMB_VFS_REMOVEXATTR(handle
->conn
, smb_fname
->base_name
, xattr_name
);
577 if ((ret
== -1) && (errno
== ENOATTR
)) {
585 TALLOC_FREE(xattr_name
);
589 static int streams_xattr_rename(vfs_handle_struct
*handle
,
590 const struct smb_filename
*smb_fname_src
,
591 const struct smb_filename
*smb_fname_dst
)
595 char *src_xattr_name
= NULL
;
596 char *dst_xattr_name
= NULL
;
597 bool src_is_stream
, dst_is_stream
;
602 src_is_stream
= is_ntfs_stream_smb_fname(smb_fname_src
);
603 dst_is_stream
= is_ntfs_stream_smb_fname(smb_fname_dst
);
605 if (!src_is_stream
&& !dst_is_stream
) {
606 return SMB_VFS_NEXT_RENAME(handle
, smb_fname_src
,
610 /* For now don't allow renames from or to the default stream. */
611 if (is_ntfs_default_stream_smb_fname(smb_fname_src
) ||
612 is_ntfs_default_stream_smb_fname(smb_fname_dst
)) {
617 /* Don't rename if the streams are identical. */
618 if (strcasecmp_m(smb_fname_src
->stream_name
,
619 smb_fname_dst
->stream_name
) == 0) {
623 /* Get the xattr names. */
624 status
= streams_xattr_get_name(talloc_tos(),
625 smb_fname_src
->stream_name
,
627 if (!NT_STATUS_IS_OK(status
)) {
628 errno
= map_errno_from_nt_status(status
);
631 status
= streams_xattr_get_name(talloc_tos(),
632 smb_fname_dst
->stream_name
,
634 if (!NT_STATUS_IS_OK(status
)) {
635 errno
= map_errno_from_nt_status(status
);
639 /* read the old stream */
640 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
,
641 smb_fname_src
->base_name
, src_xattr_name
, &ea
);
642 if (!NT_STATUS_IS_OK(status
)) {
647 /* (over)write the new stream */
648 nret
= SMB_VFS_SETXATTR(handle
->conn
, smb_fname_src
->base_name
,
649 dst_xattr_name
, ea
.value
.data
, ea
.value
.length
,
652 if (errno
== ENOATTR
) {
658 /* remove the old stream */
659 oret
= SMB_VFS_REMOVEXATTR(handle
->conn
, smb_fname_src
->base_name
,
662 if (errno
== ENOATTR
) {
672 TALLOC_FREE(src_xattr_name
);
673 TALLOC_FREE(dst_xattr_name
);
677 static NTSTATUS
walk_xattr_streams(connection_struct
*conn
, files_struct
*fsp
,
679 bool (*fn
)(struct ea_struct
*ea
,
686 size_t prefix_len
= strlen(SAMBA_XATTR_DOSSTREAM_PREFIX
);
688 status
= get_ea_names_from_file(talloc_tos(), conn
, fsp
, fname
,
690 if (!NT_STATUS_IS_OK(status
)) {
694 for (i
=0; i
<num_names
; i
++) {
697 if (strncmp(names
[i
], SAMBA_XATTR_DOSSTREAM_PREFIX
,
702 status
= get_ea_value(names
, conn
, fsp
, fname
, names
[i
], &ea
);
703 if (!NT_STATUS_IS_OK(status
)) {
704 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
705 names
[i
], fname
, nt_errstr(status
)));
709 ea
.name
= talloc_asprintf(ea
.value
.data
, ":%s",
710 names
[i
] + prefix_len
);
711 if (ea
.name
== NULL
) {
712 DEBUG(0, ("talloc failed\n"));
716 if (!fn(&ea
, private_data
)) {
717 TALLOC_FREE(ea
.value
.data
);
721 TALLOC_FREE(ea
.value
.data
);
728 static bool add_one_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
729 struct stream_struct
**streams
,
730 const char *name
, off_t size
,
733 struct stream_struct
*tmp
;
735 tmp
= talloc_realloc(mem_ctx
, *streams
, struct stream_struct
,
741 tmp
[*num_streams
].name
= talloc_strdup(tmp
, name
);
742 if (tmp
[*num_streams
].name
== NULL
) {
746 tmp
[*num_streams
].size
= size
;
747 tmp
[*num_streams
].alloc_size
= alloc_size
;
754 struct streaminfo_state
{
756 vfs_handle_struct
*handle
;
757 unsigned int num_streams
;
758 struct stream_struct
*streams
;
762 static bool collect_one_stream(struct ea_struct
*ea
, void *private_data
)
764 struct streaminfo_state
*state
=
765 (struct streaminfo_state
*)private_data
;
767 if (!add_one_stream(state
->mem_ctx
,
768 &state
->num_streams
, &state
->streams
,
769 ea
->name
, ea
->value
.length
-1,
770 smb_roundup(state
->handle
->conn
,
771 ea
->value
.length
-1))) {
772 state
->status
= NT_STATUS_NO_MEMORY
;
779 static NTSTATUS
streams_xattr_streaminfo(vfs_handle_struct
*handle
,
780 struct files_struct
*fsp
,
783 unsigned int *pnum_streams
,
784 struct stream_struct
**pstreams
)
786 SMB_STRUCT_STAT sbuf
;
789 struct streaminfo_state state
;
791 if ((fsp
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
792 ret
= SMB_VFS_FSTAT(fsp
, &sbuf
);
795 struct smb_filename
*smb_fname
= NULL
;
796 status
= create_synthetic_smb_fname(talloc_tos(), fname
, NULL
,
798 if (!NT_STATUS_IS_OK(status
)) {
801 if (lp_posix_pathnames()) {
802 ret
= SMB_VFS_LSTAT(handle
->conn
, smb_fname
);
804 ret
= SMB_VFS_STAT(handle
->conn
, smb_fname
);
806 sbuf
= smb_fname
->st
;
807 TALLOC_FREE(smb_fname
);
811 return map_nt_error_from_unix(errno
);
814 state
.streams
= *pstreams
;
815 state
.num_streams
= *pnum_streams
;
816 state
.mem_ctx
= mem_ctx
;
817 state
.handle
= handle
;
818 state
.status
= NT_STATUS_OK
;
820 status
= walk_xattr_streams(handle
->conn
, fsp
, fname
,
821 collect_one_stream
, &state
);
823 if (!NT_STATUS_IS_OK(status
)) {
824 TALLOC_FREE(state
.streams
);
828 if (!NT_STATUS_IS_OK(state
.status
)) {
829 TALLOC_FREE(state
.streams
);
833 *pnum_streams
= state
.num_streams
;
834 *pstreams
= state
.streams
;
836 return SMB_VFS_NEXT_STREAMINFO(handle
, fsp
, fname
, mem_ctx
, pnum_streams
, pstreams
);
839 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct
*handle
,
840 enum timestamp_set_resolution
*p_ts_res
)
842 return SMB_VFS_NEXT_FS_CAPABILITIES(handle
, p_ts_res
) | FILE_NAMED_STREAMS
;
845 static ssize_t
streams_xattr_pwrite(vfs_handle_struct
*handle
,
846 files_struct
*fsp
, const void *data
,
847 size_t n
, off_t offset
)
849 struct stream_io
*sio
=
850 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
855 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n
));
858 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
861 if (!streams_xattr_recheck(sio
)) {
865 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
866 sio
->base
, sio
->xattr_name
, &ea
);
867 if (!NT_STATUS_IS_OK(status
)) {
871 if ((offset
+ n
) > ea
.value
.length
-1) {
874 tmp
= talloc_realloc(talloc_tos(), ea
.value
.data
, uint8
,
878 TALLOC_FREE(ea
.value
.data
);
883 ea
.value
.length
= offset
+ n
+ 1;
884 ea
.value
.data
[offset
+n
] = 0;
887 memcpy(ea
.value
.data
+ offset
, data
, n
);
889 if (fsp
->base_fsp
->fh
->fd
!= -1) {
890 ret
= SMB_VFS_FSETXATTR(fsp
->base_fsp
,
892 ea
.value
.data
, ea
.value
.length
, 0);
894 ret
= SMB_VFS_SETXATTR(fsp
->conn
,
895 fsp
->base_fsp
->fsp_name
->base_name
,
897 ea
.value
.data
, ea
.value
.length
, 0);
899 TALLOC_FREE(ea
.value
.data
);
908 static ssize_t
streams_xattr_pread(vfs_handle_struct
*handle
,
909 files_struct
*fsp
, void *data
,
910 size_t n
, off_t offset
)
912 struct stream_io
*sio
=
913 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
916 size_t length
, overlap
;
918 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
919 (int)offset
, (int)n
));
922 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
925 if (!streams_xattr_recheck(sio
)) {
929 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
930 sio
->base
, sio
->xattr_name
, &ea
);
931 if (!NT_STATUS_IS_OK(status
)) {
935 length
= ea
.value
.length
-1;
937 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
940 /* Attempt to read past EOF. */
941 if (length
<= offset
) {
945 overlap
= (offset
+ n
) > length
? (length
- offset
) : n
;
946 memcpy(data
, ea
.value
.data
+ offset
, overlap
);
948 TALLOC_FREE(ea
.value
.data
);
952 static int streams_xattr_ftruncate(struct vfs_handle_struct
*handle
,
953 struct files_struct
*fsp
,
960 struct stream_io
*sio
=
961 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
963 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
964 fsp_str_dbg(fsp
), (double)offset
));
967 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
970 if (!streams_xattr_recheck(sio
)) {
974 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
975 sio
->base
, sio
->xattr_name
, &ea
);
976 if (!NT_STATUS_IS_OK(status
)) {
980 tmp
= talloc_realloc(talloc_tos(), ea
.value
.data
, uint8
,
984 TALLOC_FREE(ea
.value
.data
);
989 /* Did we expand ? */
990 if (ea
.value
.length
< offset
+ 1) {
991 memset(&tmp
[ea
.value
.length
], '\0',
992 offset
+ 1 - ea
.value
.length
);
996 ea
.value
.length
= offset
+ 1;
997 ea
.value
.data
[offset
] = 0;
999 if (fsp
->base_fsp
->fh
->fd
!= -1) {
1000 ret
= SMB_VFS_FSETXATTR(fsp
->base_fsp
,
1002 ea
.value
.data
, ea
.value
.length
, 0);
1004 ret
= SMB_VFS_SETXATTR(fsp
->conn
,
1005 fsp
->base_fsp
->fsp_name
->base_name
,
1007 ea
.value
.data
, ea
.value
.length
, 0);
1010 TALLOC_FREE(ea
.value
.data
);
1019 static int streams_xattr_fallocate(struct vfs_handle_struct
*handle
,
1020 struct files_struct
*fsp
,
1021 enum vfs_fallocate_mode mode
,
1025 struct stream_io
*sio
=
1026 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1028 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1030 fsp_str_dbg(fsp
), (double)offset
, (double)len
));
1033 return SMB_VFS_NEXT_FALLOCATE(handle
, fsp
, mode
, offset
, len
);
1036 if (!streams_xattr_recheck(sio
)) {
1040 /* Let the pwrite code path handle it. */
1045 static struct vfs_fn_pointers vfs_streams_xattr_fns
= {
1046 .fs_capabilities_fn
= streams_xattr_fs_capabilities
,
1047 .open_fn
= streams_xattr_open
,
1048 .stat_fn
= streams_xattr_stat
,
1049 .fstat_fn
= streams_xattr_fstat
,
1050 .lstat_fn
= streams_xattr_lstat
,
1051 .pread_fn
= streams_xattr_pread
,
1052 .pwrite_fn
= streams_xattr_pwrite
,
1053 .unlink_fn
= streams_xattr_unlink
,
1054 .rename_fn
= streams_xattr_rename
,
1055 .ftruncate_fn
= streams_xattr_ftruncate
,
1056 .fallocate_fn
= streams_xattr_fallocate
,
1057 .streaminfo_fn
= streams_xattr_streaminfo
,
1060 NTSTATUS
vfs_streams_xattr_init(void);
1061 NTSTATUS
vfs_streams_xattr_init(void)
1063 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "streams_xattr",
1064 &vfs_streams_xattr_fns
);