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 "../lib/crypto/md5.h"
28 #define DBGC_CLASS DBGC_VFS
35 vfs_handle_struct
*handle
;
38 static SMB_INO_T
stream_inode(const SMB_STRUCT_STAT
*sbuf
, const char *sname
)
40 struct MD5Context ctx
;
41 unsigned char hash
[16];
45 DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
46 (unsigned long)sbuf
->st_ex_dev
,
47 (unsigned long)sbuf
->st_ex_ino
, sname
));
49 upper_sname
= talloc_strdup_upper(talloc_tos(), sname
);
50 SMB_ASSERT(upper_sname
!= NULL
);
53 MD5Update(&ctx
, (unsigned char *)&(sbuf
->st_ex_dev
),
54 sizeof(sbuf
->st_ex_dev
));
55 MD5Update(&ctx
, (unsigned char *)&(sbuf
->st_ex_ino
),
56 sizeof(sbuf
->st_ex_ino
));
57 MD5Update(&ctx
, (unsigned char *)upper_sname
,
58 talloc_get_size(upper_sname
)-1);
61 TALLOC_FREE(upper_sname
);
63 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
64 memcpy(&result
, hash
, sizeof(result
));
66 DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result
));
71 static ssize_t
get_xattr_size(connection_struct
*conn
,
74 const char *xattr_name
)
80 status
= get_ea_value(talloc_tos(), conn
, fsp
, fname
,
83 if (!NT_STATUS_IS_OK(status
)) {
87 result
= ea
.value
.length
-1;
88 TALLOC_FREE(ea
.value
.data
);
93 * Given a stream name, populate xattr_name with the xattr name to use for
94 * accessing the stream.
96 static NTSTATUS
streams_xattr_get_name(TALLOC_CTX
*ctx
,
97 const char *stream_name
,
102 stype
= strchr_m(stream_name
+ 1, ':');
104 *xattr_name
= talloc_asprintf(ctx
, "%s%s",
105 SAMBA_XATTR_DOSSTREAM_PREFIX
,
107 if (*xattr_name
== NULL
) {
108 return NT_STATUS_NO_MEMORY
;
112 /* Append an explicit stream type if one wasn't specified. */
113 *xattr_name
= talloc_asprintf(ctx
, "%s:$DATA",
115 if (*xattr_name
== NULL
) {
116 return NT_STATUS_NO_MEMORY
;
119 /* Normalize the stream type to upercase. */
120 strupper_m(strrchr_m(*xattr_name
, ':') + 1);
123 DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name
,
129 static bool streams_xattr_recheck(struct stream_io
*sio
)
132 char *xattr_name
= NULL
;
134 if (sio
->fsp
->fsp_name
== sio
->fsp_name_ptr
) {
138 if (sio
->fsp
->fsp_name
->stream_name
== NULL
) {
139 /* how can this happen */
144 status
= streams_xattr_get_name(talloc_tos(),
145 sio
->fsp
->fsp_name
->stream_name
,
147 if (!NT_STATUS_IS_OK(status
)) {
151 TALLOC_FREE(sio
->xattr_name
);
152 TALLOC_FREE(sio
->base
);
153 sio
->xattr_name
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio
->handle
, sio
->fsp
),
155 sio
->base
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio
->handle
, sio
->fsp
),
156 sio
->fsp
->fsp_name
->base_name
);
157 sio
->fsp_name_ptr
= sio
->fsp
->fsp_name
;
159 TALLOC_FREE(xattr_name
);
161 if ((sio
->xattr_name
== NULL
) || (sio
->base
== NULL
)) {
169 * Helper to stat/lstat the base file of an smb_fname.
171 static int streams_xattr_stat_base(vfs_handle_struct
*handle
,
172 struct smb_filename
*smb_fname
,
175 char *tmp_stream_name
;
178 tmp_stream_name
= smb_fname
->stream_name
;
179 smb_fname
->stream_name
= NULL
;
181 result
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
183 result
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
185 smb_fname
->stream_name
= tmp_stream_name
;
189 static int streams_xattr_fstat(vfs_handle_struct
*handle
, files_struct
*fsp
,
190 SMB_STRUCT_STAT
*sbuf
)
192 struct smb_filename
*smb_fname_base
= NULL
;
195 struct stream_io
*io
= (struct stream_io
*)
196 VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
198 DEBUG(10, ("streams_xattr_fstat called for %d\n", fsp
->fh
->fd
));
200 if (io
== NULL
|| fsp
->base_fsp
== NULL
) {
201 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
204 if (!streams_xattr_recheck(io
)) {
208 /* Create an smb_filename with stream_name == NULL. */
209 status
= create_synthetic_smb_fname(talloc_tos(),
213 if (!NT_STATUS_IS_OK(status
)) {
214 errno
= map_errno_from_nt_status(status
);
218 if (lp_posix_pathnames()) {
219 ret
= SMB_VFS_LSTAT(handle
->conn
, smb_fname_base
);
221 ret
= SMB_VFS_STAT(handle
->conn
, smb_fname_base
);
223 *sbuf
= smb_fname_base
->st
;
224 TALLOC_FREE(smb_fname_base
);
230 sbuf
->st_ex_size
= get_xattr_size(handle
->conn
, fsp
->base_fsp
,
231 io
->base
, io
->xattr_name
);
232 if (sbuf
->st_ex_size
== -1) {
236 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf
->st_ex_size
));
238 sbuf
->st_ex_ino
= stream_inode(sbuf
, io
->xattr_name
);
239 sbuf
->st_ex_mode
&= ~S_IFMT
;
240 sbuf
->st_ex_mode
|= S_IFREG
;
241 sbuf
->st_ex_blocks
= sbuf
->st_ex_size
% STAT_ST_BLOCKSIZE
+ 1;
246 static int streams_xattr_stat(vfs_handle_struct
*handle
,
247 struct smb_filename
*smb_fname
)
251 char *xattr_name
= NULL
;
253 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
254 return SMB_VFS_NEXT_STAT(handle
, smb_fname
);
257 /* Note if lp_posix_paths() is true, we can never
258 * get here as is_ntfs_stream_smb_fname() is
259 * always false. So we never need worry about
260 * not following links here. */
262 /* If the default stream is requested, just stat the base file. */
263 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
264 return streams_xattr_stat_base(handle
, smb_fname
, true);
267 /* Populate the stat struct with info from the base file. */
268 if (streams_xattr_stat_base(handle
, smb_fname
, true) == -1) {
272 /* Derive the xattr name to lookup. */
273 status
= streams_xattr_get_name(talloc_tos(), smb_fname
->stream_name
,
275 if (!NT_STATUS_IS_OK(status
)) {
276 errno
= map_errno_from_nt_status(status
);
280 /* Augment the base file's stat information before returning. */
281 smb_fname
->st
.st_ex_size
= get_xattr_size(handle
->conn
, NULL
,
282 smb_fname
->base_name
,
284 if (smb_fname
->st
.st_ex_size
== -1) {
290 smb_fname
->st
.st_ex_ino
= stream_inode(&smb_fname
->st
, xattr_name
);
291 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
292 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
293 smb_fname
->st
.st_ex_blocks
=
294 smb_fname
->st
.st_ex_size
% STAT_ST_BLOCKSIZE
+ 1;
298 TALLOC_FREE(xattr_name
);
302 static int streams_xattr_lstat(vfs_handle_struct
*handle
,
303 struct smb_filename
*smb_fname
)
307 char *xattr_name
= NULL
;
309 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
310 return SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
313 /* If the default stream is requested, just stat the base file. */
314 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
315 return streams_xattr_stat_base(handle
, smb_fname
, false);
318 /* Populate the stat struct with info from the base file. */
319 if (streams_xattr_stat_base(handle
, smb_fname
, false) == -1) {
323 /* Derive the xattr name to lookup. */
324 status
= streams_xattr_get_name(talloc_tos(), smb_fname
->stream_name
,
326 if (!NT_STATUS_IS_OK(status
)) {
327 errno
= map_errno_from_nt_status(status
);
331 /* Augment the base file's stat information before returning. */
332 smb_fname
->st
.st_ex_size
= get_xattr_size(handle
->conn
, NULL
,
333 smb_fname
->base_name
,
335 if (smb_fname
->st
.st_ex_size
== -1) {
341 smb_fname
->st
.st_ex_ino
= stream_inode(&smb_fname
->st
, xattr_name
);
342 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
343 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
344 smb_fname
->st
.st_ex_blocks
=
345 smb_fname
->st
.st_ex_size
% STAT_ST_BLOCKSIZE
+ 1;
350 TALLOC_FREE(xattr_name
);
354 static int streams_xattr_open(vfs_handle_struct
*handle
,
355 struct smb_filename
*smb_fname
,
356 files_struct
*fsp
, int flags
, mode_t mode
)
359 struct smb_filename
*smb_fname_base
= NULL
;
360 struct stream_io
*sio
;
362 char *xattr_name
= NULL
;
366 DEBUG(10, ("streams_xattr_open called for %s\n",
367 smb_fname_str_dbg(smb_fname
)));
369 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
370 return SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
373 /* If the default stream is requested, just open the base file. */
374 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
375 char *tmp_stream_name
;
378 tmp_stream_name
= smb_fname
->stream_name
;
379 smb_fname
->stream_name
= NULL
;
381 ret
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
383 smb_fname
->stream_name
= tmp_stream_name
;
388 status
= streams_xattr_get_name(talloc_tos(), smb_fname
->stream_name
,
390 if (!NT_STATUS_IS_OK(status
)) {
391 errno
= map_errno_from_nt_status(status
);
395 /* Create an smb_filename with stream_name == NULL. */
396 status
= create_synthetic_smb_fname(talloc_tos(),
397 smb_fname
->base_name
,
400 if (!NT_STATUS_IS_OK(status
)) {
401 errno
= map_errno_from_nt_status(status
);
406 * We use baseflags to turn off nasty side-effects when opening the
410 baseflags
&= ~O_TRUNC
;
411 baseflags
&= ~O_EXCL
;
412 baseflags
&= ~O_CREAT
;
414 hostfd
= SMB_VFS_OPEN(handle
->conn
, smb_fname_base
, fsp
,
417 TALLOC_FREE(smb_fname_base
);
419 /* It is legit to open a stream on a directory, but the base
420 * fd has to be read-only.
422 if ((hostfd
== -1) && (errno
== EISDIR
)) {
423 baseflags
&= ~O_ACCMODE
;
424 baseflags
|= O_RDONLY
;
425 hostfd
= SMB_VFS_OPEN(handle
->conn
, smb_fname
, fsp
, baseflags
,
433 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
,
434 smb_fname
->base_name
, xattr_name
, &ea
);
436 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status
)));
438 if (!NT_STATUS_IS_OK(status
)
439 && !NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
441 * The base file is not there. This is an error even if we got
442 * O_CREAT, the higher levels should have created the base
445 DEBUG(10, ("streams_xattr_open: base file %s not around, "
446 "returning ENOENT\n", smb_fname
->base_name
));
451 if (!NT_STATUS_IS_OK(status
)) {
453 * The attribute does not exist
456 if (flags
& O_CREAT
) {
458 * Darn, xattrs need at least 1 byte
462 DEBUG(10, ("creating attribute %s on file %s\n",
463 xattr_name
, smb_fname
->base_name
));
465 if (fsp
->base_fsp
->fh
->fd
!= -1) {
466 if (SMB_VFS_FSETXATTR(
467 fsp
->base_fsp
, xattr_name
,
469 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
473 if (SMB_VFS_SETXATTR(
474 handle
->conn
, smb_fname
->base_name
,
475 xattr_name
, &null
, sizeof(null
),
476 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
483 if (flags
& O_TRUNC
) {
485 if (fsp
->base_fsp
->fh
->fd
!= -1) {
486 if (SMB_VFS_FSETXATTR(
487 fsp
->base_fsp
, xattr_name
,
489 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
493 if (SMB_VFS_SETXATTR(
494 handle
->conn
, smb_fname
->base_name
,
495 xattr_name
, &null
, sizeof(null
),
496 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
502 sio
= (struct stream_io
*)VFS_ADD_FSP_EXTENSION(handle
, fsp
,
510 sio
->xattr_name
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
512 sio
->base
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
513 smb_fname
->base_name
);
514 sio
->fsp_name_ptr
= fsp
->fsp_name
;
515 sio
->handle
= handle
;
518 if ((sio
->xattr_name
== NULL
) || (sio
->base
== NULL
)) {
528 * BUGBUGBUG -- we would need to call fd_close_posix here, but
529 * we don't have a full fsp yet
537 static int streams_xattr_unlink(vfs_handle_struct
*handle
,
538 const struct smb_filename
*smb_fname
)
544 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
545 return SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
548 /* If the default stream is requested, just open the base file. */
549 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
550 struct smb_filename
*smb_fname_base
= NULL
;
552 status
= copy_smb_filename(talloc_tos(), smb_fname
,
554 if (!NT_STATUS_IS_OK(status
)) {
555 errno
= map_errno_from_nt_status(status
);
559 ret
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname_base
);
561 TALLOC_FREE(smb_fname_base
);
565 status
= streams_xattr_get_name(talloc_tos(), smb_fname
->stream_name
,
567 if (!NT_STATUS_IS_OK(status
)) {
568 errno
= map_errno_from_nt_status(status
);
572 ret
= SMB_VFS_REMOVEXATTR(handle
->conn
, smb_fname
->base_name
, xattr_name
);
574 if ((ret
== -1) && (errno
== ENOATTR
)) {
582 TALLOC_FREE(xattr_name
);
586 static int streams_xattr_rename(vfs_handle_struct
*handle
,
587 const struct smb_filename
*smb_fname_src
,
588 const struct smb_filename
*smb_fname_dst
)
592 char *src_xattr_name
= NULL
;
593 char *dst_xattr_name
= NULL
;
594 bool src_is_stream
, dst_is_stream
;
599 src_is_stream
= is_ntfs_stream_smb_fname(smb_fname_src
);
600 dst_is_stream
= is_ntfs_stream_smb_fname(smb_fname_dst
);
602 if (!src_is_stream
&& !dst_is_stream
) {
603 return SMB_VFS_NEXT_RENAME(handle
, smb_fname_src
,
607 /* For now don't allow renames from or to the default stream. */
608 if (is_ntfs_default_stream_smb_fname(smb_fname_src
) ||
609 is_ntfs_default_stream_smb_fname(smb_fname_dst
)) {
614 /* Don't rename if the streams are identical. */
615 if (StrCaseCmp(smb_fname_src
->stream_name
,
616 smb_fname_dst
->stream_name
) == 0) {
620 /* Get the xattr names. */
621 status
= streams_xattr_get_name(talloc_tos(),
622 smb_fname_src
->stream_name
,
624 if (!NT_STATUS_IS_OK(status
)) {
625 errno
= map_errno_from_nt_status(status
);
628 status
= streams_xattr_get_name(talloc_tos(),
629 smb_fname_dst
->stream_name
,
631 if (!NT_STATUS_IS_OK(status
)) {
632 errno
= map_errno_from_nt_status(status
);
636 /* read the old stream */
637 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
,
638 smb_fname_src
->base_name
, src_xattr_name
, &ea
);
639 if (!NT_STATUS_IS_OK(status
)) {
644 /* (over)write the new stream */
645 nret
= SMB_VFS_SETXATTR(handle
->conn
, smb_fname_src
->base_name
,
646 dst_xattr_name
, ea
.value
.data
, ea
.value
.length
,
649 if (errno
== ENOATTR
) {
655 /* remove the old stream */
656 oret
= SMB_VFS_REMOVEXATTR(handle
->conn
, smb_fname_src
->base_name
,
659 if (errno
== ENOATTR
) {
669 TALLOC_FREE(src_xattr_name
);
670 TALLOC_FREE(dst_xattr_name
);
674 static NTSTATUS
walk_xattr_streams(connection_struct
*conn
, files_struct
*fsp
,
676 bool (*fn
)(struct ea_struct
*ea
,
683 size_t prefix_len
= strlen(SAMBA_XATTR_DOSSTREAM_PREFIX
);
685 status
= get_ea_names_from_file(talloc_tos(), conn
, fsp
, fname
,
687 if (!NT_STATUS_IS_OK(status
)) {
691 for (i
=0; i
<num_names
; i
++) {
694 if (strncmp(names
[i
], SAMBA_XATTR_DOSSTREAM_PREFIX
,
699 status
= get_ea_value(names
, conn
, fsp
, fname
, names
[i
], &ea
);
700 if (!NT_STATUS_IS_OK(status
)) {
701 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
702 names
[i
], fname
, nt_errstr(status
)));
706 ea
.name
= talloc_asprintf(ea
.value
.data
, ":%s",
707 names
[i
] + prefix_len
);
708 if (ea
.name
== NULL
) {
709 DEBUG(0, ("talloc failed\n"));
713 if (!fn(&ea
, private_data
)) {
714 TALLOC_FREE(ea
.value
.data
);
718 TALLOC_FREE(ea
.value
.data
);
725 static bool add_one_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
726 struct stream_struct
**streams
,
727 const char *name
, SMB_OFF_T size
,
728 SMB_OFF_T alloc_size
)
730 struct stream_struct
*tmp
;
732 tmp
= TALLOC_REALLOC_ARRAY(mem_ctx
, *streams
, struct stream_struct
,
738 tmp
[*num_streams
].name
= talloc_strdup(tmp
, name
);
739 if (tmp
[*num_streams
].name
== NULL
) {
743 tmp
[*num_streams
].size
= size
;
744 tmp
[*num_streams
].alloc_size
= alloc_size
;
751 struct streaminfo_state
{
753 vfs_handle_struct
*handle
;
754 unsigned int num_streams
;
755 struct stream_struct
*streams
;
759 static bool collect_one_stream(struct ea_struct
*ea
, void *private_data
)
761 struct streaminfo_state
*state
=
762 (struct streaminfo_state
*)private_data
;
764 if (!add_one_stream(state
->mem_ctx
,
765 &state
->num_streams
, &state
->streams
,
766 ea
->name
, ea
->value
.length
-1,
767 smb_roundup(state
->handle
->conn
,
768 ea
->value
.length
-1))) {
769 state
->status
= NT_STATUS_NO_MEMORY
;
776 static NTSTATUS
streams_xattr_streaminfo(vfs_handle_struct
*handle
,
777 struct files_struct
*fsp
,
780 unsigned int *pnum_streams
,
781 struct stream_struct
**pstreams
)
783 SMB_STRUCT_STAT sbuf
;
786 struct streaminfo_state state
;
788 if ((fsp
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
789 ret
= SMB_VFS_FSTAT(fsp
, &sbuf
);
792 struct smb_filename
*smb_fname
= NULL
;
793 status
= create_synthetic_smb_fname(talloc_tos(), fname
, NULL
,
795 if (!NT_STATUS_IS_OK(status
)) {
798 if (lp_posix_pathnames()) {
799 ret
= SMB_VFS_LSTAT(handle
->conn
, smb_fname
);
801 ret
= SMB_VFS_STAT(handle
->conn
, smb_fname
);
803 sbuf
= smb_fname
->st
;
804 TALLOC_FREE(smb_fname
);
808 return map_nt_error_from_unix(errno
);
811 state
.streams
= NULL
;
812 state
.num_streams
= 0;
814 if (!S_ISDIR(sbuf
.st_ex_mode
)) {
815 if (!add_one_stream(mem_ctx
,
816 &state
.num_streams
, &state
.streams
,
817 "::$DATA", sbuf
.st_ex_size
,
818 SMB_VFS_GET_ALLOC_SIZE(handle
->conn
, fsp
,
820 return NT_STATUS_NO_MEMORY
;
824 state
.mem_ctx
= mem_ctx
;
825 state
.handle
= handle
;
826 state
.status
= NT_STATUS_OK
;
828 status
= walk_xattr_streams(handle
->conn
, fsp
, fname
,
829 collect_one_stream
, &state
);
831 if (!NT_STATUS_IS_OK(status
)) {
832 TALLOC_FREE(state
.streams
);
836 if (!NT_STATUS_IS_OK(state
.status
)) {
837 TALLOC_FREE(state
.streams
);
841 *pnum_streams
= state
.num_streams
;
842 *pstreams
= state
.streams
;
846 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct
*handle
,
847 enum timestamp_set_resolution
*p_ts_res
)
849 return SMB_VFS_NEXT_FS_CAPABILITIES(handle
, p_ts_res
) | FILE_NAMED_STREAMS
;
852 static ssize_t
streams_xattr_pwrite(vfs_handle_struct
*handle
,
853 files_struct
*fsp
, const void *data
,
854 size_t n
, SMB_OFF_T offset
)
856 struct stream_io
*sio
=
857 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
862 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n
));
865 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
868 if (!streams_xattr_recheck(sio
)) {
872 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
873 sio
->base
, sio
->xattr_name
, &ea
);
874 if (!NT_STATUS_IS_OK(status
)) {
878 if ((offset
+ n
) > ea
.value
.length
-1) {
881 tmp
= TALLOC_REALLOC_ARRAY(talloc_tos(), ea
.value
.data
, uint8
,
885 TALLOC_FREE(ea
.value
.data
);
890 ea
.value
.length
= offset
+ n
+ 1;
891 ea
.value
.data
[offset
+n
] = 0;
894 memcpy(ea
.value
.data
+ offset
, data
, n
);
896 if (fsp
->base_fsp
->fh
->fd
!= -1) {
897 ret
= SMB_VFS_FSETXATTR(fsp
->base_fsp
,
899 ea
.value
.data
, ea
.value
.length
, 0);
901 ret
= SMB_VFS_SETXATTR(fsp
->conn
,
902 fsp
->base_fsp
->fsp_name
->base_name
,
904 ea
.value
.data
, ea
.value
.length
, 0);
906 TALLOC_FREE(ea
.value
.data
);
915 static ssize_t
streams_xattr_pread(vfs_handle_struct
*handle
,
916 files_struct
*fsp
, void *data
,
917 size_t n
, SMB_OFF_T offset
)
919 struct stream_io
*sio
=
920 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
923 size_t length
, overlap
;
925 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
926 (int)offset
, (int)n
));
929 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
932 if (!streams_xattr_recheck(sio
)) {
936 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
937 sio
->base
, sio
->xattr_name
, &ea
);
938 if (!NT_STATUS_IS_OK(status
)) {
942 length
= ea
.value
.length
-1;
944 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
947 /* Attempt to read past EOF. */
948 if (length
<= offset
) {
952 overlap
= (offset
+ n
) > length
? (length
- offset
) : n
;
953 memcpy(data
, ea
.value
.data
+ offset
, overlap
);
955 TALLOC_FREE(ea
.value
.data
);
959 static int streams_xattr_ftruncate(struct vfs_handle_struct
*handle
,
960 struct files_struct
*fsp
,
967 struct stream_io
*sio
=
968 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
970 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
971 fsp_str_dbg(fsp
), (double)offset
));
974 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
977 if (!streams_xattr_recheck(sio
)) {
981 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
982 sio
->base
, sio
->xattr_name
, &ea
);
983 if (!NT_STATUS_IS_OK(status
)) {
987 tmp
= TALLOC_REALLOC_ARRAY(talloc_tos(), ea
.value
.data
, uint8
,
991 TALLOC_FREE(ea
.value
.data
);
996 /* Did we expand ? */
997 if (ea
.value
.length
< offset
+ 1) {
998 memset(&tmp
[ea
.value
.length
], '\0',
999 offset
+ 1 - ea
.value
.length
);
1002 ea
.value
.data
= tmp
;
1003 ea
.value
.length
= offset
+ 1;
1004 ea
.value
.data
[offset
] = 0;
1006 if (fsp
->base_fsp
->fh
->fd
!= -1) {
1007 ret
= SMB_VFS_FSETXATTR(fsp
->base_fsp
,
1009 ea
.value
.data
, ea
.value
.length
, 0);
1011 ret
= SMB_VFS_SETXATTR(fsp
->conn
,
1012 fsp
->base_fsp
->fsp_name
->base_name
,
1014 ea
.value
.data
, ea
.value
.length
, 0);
1017 TALLOC_FREE(ea
.value
.data
);
1026 static struct vfs_fn_pointers vfs_streams_xattr_fns
= {
1027 .fs_capabilities
= streams_xattr_fs_capabilities
,
1028 .open
= streams_xattr_open
,
1029 .stat
= streams_xattr_stat
,
1030 .fstat
= streams_xattr_fstat
,
1031 .lstat
= streams_xattr_lstat
,
1032 .pread
= streams_xattr_pread
,
1033 .pwrite
= streams_xattr_pwrite
,
1034 .unlink
= streams_xattr_unlink
,
1035 .rename
= streams_xattr_rename
,
1036 .ftruncate
= streams_xattr_ftruncate
,
1037 .streaminfo
= streams_xattr_streaminfo
,
1040 NTSTATUS
vfs_streams_xattr_init(void);
1041 NTSTATUS
vfs_streams_xattr_init(void)
1043 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "streams_xattr",
1044 &vfs_streams_xattr_fns
);