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
)
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
;
198 struct stream_io
*io
= (struct stream_io
*)
199 VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
201 DEBUG(10, ("streams_xattr_fstat called for %d\n", fsp
->fh
->fd
));
203 if (io
== NULL
|| fsp
->base_fsp
== NULL
) {
204 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
207 if (!streams_xattr_recheck(io
)) {
211 /* Create an smb_filename with stream_name == NULL. */
212 smb_fname_base
= synthetic_smb_fname(talloc_tos(), io
->base
,
214 if (smb_fname_base
== NULL
) {
219 if (lp_posix_pathnames()) {
220 ret
= SMB_VFS_LSTAT(handle
->conn
, smb_fname_base
);
222 ret
= SMB_VFS_STAT(handle
->conn
, smb_fname_base
);
224 *sbuf
= smb_fname_base
->st
;
225 TALLOC_FREE(smb_fname_base
);
231 sbuf
->st_ex_size
= get_xattr_size(handle
->conn
, fsp
->base_fsp
,
232 io
->base
, io
->xattr_name
);
233 if (sbuf
->st_ex_size
== -1) {
237 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf
->st_ex_size
));
239 sbuf
->st_ex_ino
= stream_inode(sbuf
, io
->xattr_name
);
240 sbuf
->st_ex_mode
&= ~S_IFMT
;
241 sbuf
->st_ex_mode
|= S_IFREG
;
242 sbuf
->st_ex_blocks
= sbuf
->st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
247 static int streams_xattr_stat(vfs_handle_struct
*handle
,
248 struct smb_filename
*smb_fname
)
252 char *xattr_name
= NULL
;
254 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
255 return SMB_VFS_NEXT_STAT(handle
, smb_fname
);
258 /* Note if lp_posix_paths() is true, we can never
259 * get here as is_ntfs_stream_smb_fname() is
260 * always false. So we never need worry about
261 * not following links here. */
263 /* If the default stream is requested, just stat the base file. */
264 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
265 return streams_xattr_stat_base(handle
, smb_fname
, true);
268 /* Populate the stat struct with info from the base file. */
269 if (streams_xattr_stat_base(handle
, smb_fname
, true) == -1) {
273 /* Derive the xattr name to lookup. */
274 status
= streams_xattr_get_name(talloc_tos(), smb_fname
->stream_name
,
276 if (!NT_STATUS_IS_OK(status
)) {
277 errno
= map_errno_from_nt_status(status
);
281 /* Augment the base file's stat information before returning. */
282 smb_fname
->st
.st_ex_size
= get_xattr_size(handle
->conn
, NULL
,
283 smb_fname
->base_name
,
285 if (smb_fname
->st
.st_ex_size
== -1) {
291 smb_fname
->st
.st_ex_ino
= stream_inode(&smb_fname
->st
, xattr_name
);
292 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
293 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
294 smb_fname
->st
.st_ex_blocks
=
295 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
299 TALLOC_FREE(xattr_name
);
303 static int streams_xattr_lstat(vfs_handle_struct
*handle
,
304 struct smb_filename
*smb_fname
)
308 char *xattr_name
= NULL
;
310 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
311 return SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
314 /* If the default stream is requested, just stat the base file. */
315 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
316 return streams_xattr_stat_base(handle
, smb_fname
, false);
319 /* Populate the stat struct with info from the base file. */
320 if (streams_xattr_stat_base(handle
, smb_fname
, false) == -1) {
324 /* Derive the xattr name to lookup. */
325 status
= streams_xattr_get_name(talloc_tos(), smb_fname
->stream_name
,
327 if (!NT_STATUS_IS_OK(status
)) {
328 errno
= map_errno_from_nt_status(status
);
332 /* Augment the base file's stat information before returning. */
333 smb_fname
->st
.st_ex_size
= get_xattr_size(handle
->conn
, NULL
,
334 smb_fname
->base_name
,
336 if (smb_fname
->st
.st_ex_size
== -1) {
342 smb_fname
->st
.st_ex_ino
= stream_inode(&smb_fname
->st
, xattr_name
);
343 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
344 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
345 smb_fname
->st
.st_ex_blocks
=
346 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
351 TALLOC_FREE(xattr_name
);
355 static int streams_xattr_open(vfs_handle_struct
*handle
,
356 struct smb_filename
*smb_fname
,
357 files_struct
*fsp
, int flags
, mode_t mode
)
360 struct smb_filename
*smb_fname_base
= NULL
;
361 struct stream_io
*sio
;
363 char *xattr_name
= NULL
;
367 DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
368 smb_fname_str_dbg(smb_fname
), flags
));
370 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
371 return SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
374 /* If the default stream is requested, just open the base file. */
375 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
376 char *tmp_stream_name
;
379 tmp_stream_name
= smb_fname
->stream_name
;
380 smb_fname
->stream_name
= NULL
;
382 ret
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
384 smb_fname
->stream_name
= tmp_stream_name
;
389 status
= streams_xattr_get_name(talloc_tos(), smb_fname
->stream_name
,
391 if (!NT_STATUS_IS_OK(status
)) {
392 errno
= map_errno_from_nt_status(status
);
396 /* Create an smb_filename with stream_name == NULL. */
397 smb_fname_base
= synthetic_smb_fname(
398 talloc_tos(), smb_fname
->base_name
, NULL
, NULL
);
399 if (smb_fname_base
== NULL
) {
405 * We use baseflags to turn off nasty side-effects when opening the
409 baseflags
&= ~O_TRUNC
;
410 baseflags
&= ~O_EXCL
;
411 baseflags
&= ~O_CREAT
;
413 hostfd
= SMB_VFS_OPEN(handle
->conn
, smb_fname_base
, fsp
,
416 TALLOC_FREE(smb_fname_base
);
418 /* It is legit to open a stream on a directory, but the base
419 * fd has to be read-only.
421 if ((hostfd
== -1) && (errno
== EISDIR
)) {
422 baseflags
&= ~O_ACCMODE
;
423 baseflags
|= O_RDONLY
;
424 hostfd
= SMB_VFS_OPEN(handle
->conn
, smb_fname
, fsp
, baseflags
,
432 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
,
433 smb_fname
->base_name
, xattr_name
, &ea
);
435 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status
)));
437 if (!NT_STATUS_IS_OK(status
)
438 && !NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
440 * The base file is not there. This is an error even if we got
441 * O_CREAT, the higher levels should have created the base
444 DEBUG(10, ("streams_xattr_open: base file %s not around, "
445 "returning ENOENT\n", smb_fname
->base_name
));
450 if ((!NT_STATUS_IS_OK(status
) && (flags
& O_CREAT
)) ||
453 * The attribute does not exist or needs to be truncated
457 * Darn, xattrs need at least 1 byte
461 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
462 xattr_name
, smb_fname
->base_name
));
464 if (fsp
->base_fsp
->fh
->fd
!= -1) {
465 if (SMB_VFS_FSETXATTR(
466 fsp
->base_fsp
, xattr_name
,
468 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
472 if (SMB_VFS_SETXATTR(
473 handle
->conn
, smb_fname
->base_name
,
474 xattr_name
, &null
, sizeof(null
),
475 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
481 sio
= (struct stream_io
*)VFS_ADD_FSP_EXTENSION(handle
, fsp
,
489 sio
->xattr_name
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
491 sio
->base
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
492 smb_fname
->base_name
);
493 sio
->fsp_name_ptr
= fsp
->fsp_name
;
494 sio
->handle
= handle
;
497 if ((sio
->xattr_name
== NULL
) || (sio
->base
== NULL
)) {
507 * BUGBUGBUG -- we would need to call fd_close_posix here, but
508 * we don't have a full fsp yet
510 fsp
->fh
->fd
= hostfd
;
517 static int streams_xattr_unlink(vfs_handle_struct
*handle
,
518 const struct smb_filename
*smb_fname
)
524 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
525 return SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
528 /* If the default stream is requested, just open the base file. */
529 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
530 struct smb_filename
*smb_fname_base
= NULL
;
532 smb_fname_base
= cp_smb_filename(talloc_tos(), smb_fname
);
533 if (smb_fname_base
== NULL
) {
538 ret
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname_base
);
540 TALLOC_FREE(smb_fname_base
);
544 status
= streams_xattr_get_name(talloc_tos(), smb_fname
->stream_name
,
546 if (!NT_STATUS_IS_OK(status
)) {
547 errno
= map_errno_from_nt_status(status
);
551 ret
= SMB_VFS_REMOVEXATTR(handle
->conn
, smb_fname
->base_name
, xattr_name
);
553 if ((ret
== -1) && (errno
== ENOATTR
)) {
561 TALLOC_FREE(xattr_name
);
565 static int streams_xattr_rename(vfs_handle_struct
*handle
,
566 const struct smb_filename
*smb_fname_src
,
567 const struct smb_filename
*smb_fname_dst
)
571 char *src_xattr_name
= NULL
;
572 char *dst_xattr_name
= NULL
;
573 bool src_is_stream
, dst_is_stream
;
578 src_is_stream
= is_ntfs_stream_smb_fname(smb_fname_src
);
579 dst_is_stream
= is_ntfs_stream_smb_fname(smb_fname_dst
);
581 if (!src_is_stream
&& !dst_is_stream
) {
582 return SMB_VFS_NEXT_RENAME(handle
, smb_fname_src
,
586 /* For now don't allow renames from or to the default stream. */
587 if (is_ntfs_default_stream_smb_fname(smb_fname_src
) ||
588 is_ntfs_default_stream_smb_fname(smb_fname_dst
)) {
593 /* Don't rename if the streams are identical. */
594 if (strcasecmp_m(smb_fname_src
->stream_name
,
595 smb_fname_dst
->stream_name
) == 0) {
599 /* Get the xattr names. */
600 status
= streams_xattr_get_name(talloc_tos(),
601 smb_fname_src
->stream_name
,
603 if (!NT_STATUS_IS_OK(status
)) {
604 errno
= map_errno_from_nt_status(status
);
607 status
= streams_xattr_get_name(talloc_tos(),
608 smb_fname_dst
->stream_name
,
610 if (!NT_STATUS_IS_OK(status
)) {
611 errno
= map_errno_from_nt_status(status
);
615 /* read the old stream */
616 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
,
617 smb_fname_src
->base_name
, src_xattr_name
, &ea
);
618 if (!NT_STATUS_IS_OK(status
)) {
623 /* (over)write the new stream */
624 nret
= SMB_VFS_SETXATTR(handle
->conn
, smb_fname_src
->base_name
,
625 dst_xattr_name
, ea
.value
.data
, ea
.value
.length
,
628 if (errno
== ENOATTR
) {
634 /* remove the old stream */
635 oret
= SMB_VFS_REMOVEXATTR(handle
->conn
, smb_fname_src
->base_name
,
638 if (errno
== ENOATTR
) {
648 TALLOC_FREE(src_xattr_name
);
649 TALLOC_FREE(dst_xattr_name
);
653 static NTSTATUS
walk_xattr_streams(connection_struct
*conn
, files_struct
*fsp
,
655 bool (*fn
)(struct ea_struct
*ea
,
662 size_t prefix_len
= strlen(SAMBA_XATTR_DOSSTREAM_PREFIX
);
664 status
= get_ea_names_from_file(talloc_tos(), conn
, fsp
, fname
,
666 if (!NT_STATUS_IS_OK(status
)) {
670 for (i
=0; i
<num_names
; i
++) {
673 if (strncmp(names
[i
], SAMBA_XATTR_DOSSTREAM_PREFIX
,
678 status
= get_ea_value(names
, conn
, fsp
, fname
, names
[i
], &ea
);
679 if (!NT_STATUS_IS_OK(status
)) {
680 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
681 names
[i
], fname
, nt_errstr(status
)));
685 ea
.name
= talloc_asprintf(ea
.value
.data
, ":%s",
686 names
[i
] + prefix_len
);
687 if (ea
.name
== NULL
) {
688 DEBUG(0, ("talloc failed\n"));
692 if (!fn(&ea
, private_data
)) {
693 TALLOC_FREE(ea
.value
.data
);
697 TALLOC_FREE(ea
.value
.data
);
704 static bool add_one_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
705 struct stream_struct
**streams
,
706 const char *name
, off_t size
,
709 struct stream_struct
*tmp
;
711 tmp
= talloc_realloc(mem_ctx
, *streams
, struct stream_struct
,
717 tmp
[*num_streams
].name
= talloc_strdup(tmp
, name
);
718 if (tmp
[*num_streams
].name
== NULL
) {
722 tmp
[*num_streams
].size
= size
;
723 tmp
[*num_streams
].alloc_size
= alloc_size
;
730 struct streaminfo_state
{
732 vfs_handle_struct
*handle
;
733 unsigned int num_streams
;
734 struct stream_struct
*streams
;
738 static bool collect_one_stream(struct ea_struct
*ea
, void *private_data
)
740 struct streaminfo_state
*state
=
741 (struct streaminfo_state
*)private_data
;
743 if (!add_one_stream(state
->mem_ctx
,
744 &state
->num_streams
, &state
->streams
,
745 ea
->name
, ea
->value
.length
-1,
746 smb_roundup(state
->handle
->conn
,
747 ea
->value
.length
-1))) {
748 state
->status
= NT_STATUS_NO_MEMORY
;
755 static NTSTATUS
streams_xattr_streaminfo(vfs_handle_struct
*handle
,
756 struct files_struct
*fsp
,
759 unsigned int *pnum_streams
,
760 struct stream_struct
**pstreams
)
762 SMB_STRUCT_STAT sbuf
;
765 struct streaminfo_state state
;
767 if ((fsp
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
768 ret
= SMB_VFS_FSTAT(fsp
, &sbuf
);
771 struct smb_filename
*smb_fname
= NULL
;
772 smb_fname
= synthetic_smb_fname(talloc_tos(), fname
, NULL
,
774 if (smb_fname
== NULL
) {
775 return NT_STATUS_NO_MEMORY
;
777 if (lp_posix_pathnames()) {
778 ret
= SMB_VFS_LSTAT(handle
->conn
, smb_fname
);
780 ret
= SMB_VFS_STAT(handle
->conn
, smb_fname
);
782 sbuf
= smb_fname
->st
;
783 TALLOC_FREE(smb_fname
);
787 return map_nt_error_from_unix(errno
);
790 state
.streams
= *pstreams
;
791 state
.num_streams
= *pnum_streams
;
792 state
.mem_ctx
= mem_ctx
;
793 state
.handle
= handle
;
794 state
.status
= NT_STATUS_OK
;
796 status
= walk_xattr_streams(handle
->conn
, fsp
, fname
,
797 collect_one_stream
, &state
);
799 if (!NT_STATUS_IS_OK(status
)) {
800 TALLOC_FREE(state
.streams
);
804 if (!NT_STATUS_IS_OK(state
.status
)) {
805 TALLOC_FREE(state
.streams
);
809 *pnum_streams
= state
.num_streams
;
810 *pstreams
= state
.streams
;
812 return SMB_VFS_NEXT_STREAMINFO(handle
, fsp
, fname
, mem_ctx
, pnum_streams
, pstreams
);
815 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct
*handle
,
816 enum timestamp_set_resolution
*p_ts_res
)
818 return SMB_VFS_NEXT_FS_CAPABILITIES(handle
, p_ts_res
) | FILE_NAMED_STREAMS
;
821 static ssize_t
streams_xattr_pwrite(vfs_handle_struct
*handle
,
822 files_struct
*fsp
, const void *data
,
823 size_t n
, off_t offset
)
825 struct stream_io
*sio
=
826 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
831 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n
));
834 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
837 if (!streams_xattr_recheck(sio
)) {
841 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
842 sio
->base
, sio
->xattr_name
, &ea
);
843 if (!NT_STATUS_IS_OK(status
)) {
847 if ((offset
+ n
) > ea
.value
.length
-1) {
850 tmp
= talloc_realloc(talloc_tos(), ea
.value
.data
, uint8
,
854 TALLOC_FREE(ea
.value
.data
);
859 ea
.value
.length
= offset
+ n
+ 1;
860 ea
.value
.data
[offset
+n
] = 0;
863 memcpy(ea
.value
.data
+ offset
, data
, n
);
865 if (fsp
->base_fsp
->fh
->fd
!= -1) {
866 ret
= SMB_VFS_FSETXATTR(fsp
->base_fsp
,
868 ea
.value
.data
, ea
.value
.length
, 0);
870 ret
= SMB_VFS_SETXATTR(fsp
->conn
,
871 fsp
->base_fsp
->fsp_name
->base_name
,
873 ea
.value
.data
, ea
.value
.length
, 0);
875 TALLOC_FREE(ea
.value
.data
);
884 static ssize_t
streams_xattr_pread(vfs_handle_struct
*handle
,
885 files_struct
*fsp
, void *data
,
886 size_t n
, off_t offset
)
888 struct stream_io
*sio
=
889 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
892 size_t length
, overlap
;
894 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
895 (int)offset
, (int)n
));
898 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
901 if (!streams_xattr_recheck(sio
)) {
905 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
906 sio
->base
, sio
->xattr_name
, &ea
);
907 if (!NT_STATUS_IS_OK(status
)) {
911 length
= ea
.value
.length
-1;
913 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
916 /* Attempt to read past EOF. */
917 if (length
<= offset
) {
921 overlap
= (offset
+ n
) > length
? (length
- offset
) : n
;
922 memcpy(data
, ea
.value
.data
+ offset
, overlap
);
924 TALLOC_FREE(ea
.value
.data
);
928 static int streams_xattr_ftruncate(struct vfs_handle_struct
*handle
,
929 struct files_struct
*fsp
,
936 struct stream_io
*sio
=
937 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
939 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
940 fsp_str_dbg(fsp
), (double)offset
));
943 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
946 if (!streams_xattr_recheck(sio
)) {
950 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
951 sio
->base
, sio
->xattr_name
, &ea
);
952 if (!NT_STATUS_IS_OK(status
)) {
956 tmp
= talloc_realloc(talloc_tos(), ea
.value
.data
, uint8
,
960 TALLOC_FREE(ea
.value
.data
);
965 /* Did we expand ? */
966 if (ea
.value
.length
< offset
+ 1) {
967 memset(&tmp
[ea
.value
.length
], '\0',
968 offset
+ 1 - ea
.value
.length
);
972 ea
.value
.length
= offset
+ 1;
973 ea
.value
.data
[offset
] = 0;
975 if (fsp
->base_fsp
->fh
->fd
!= -1) {
976 ret
= SMB_VFS_FSETXATTR(fsp
->base_fsp
,
978 ea
.value
.data
, ea
.value
.length
, 0);
980 ret
= SMB_VFS_SETXATTR(fsp
->conn
,
981 fsp
->base_fsp
->fsp_name
->base_name
,
983 ea
.value
.data
, ea
.value
.length
, 0);
986 TALLOC_FREE(ea
.value
.data
);
995 static int streams_xattr_fallocate(struct vfs_handle_struct
*handle
,
996 struct files_struct
*fsp
,
997 enum vfs_fallocate_mode mode
,
1001 struct stream_io
*sio
=
1002 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1004 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1006 fsp_str_dbg(fsp
), (double)offset
, (double)len
));
1009 return SMB_VFS_NEXT_FALLOCATE(handle
, fsp
, mode
, offset
, len
);
1012 if (!streams_xattr_recheck(sio
)) {
1016 /* Let the pwrite code path handle it. */
1021 static struct vfs_fn_pointers vfs_streams_xattr_fns
= {
1022 .fs_capabilities_fn
= streams_xattr_fs_capabilities
,
1023 .open_fn
= streams_xattr_open
,
1024 .stat_fn
= streams_xattr_stat
,
1025 .fstat_fn
= streams_xattr_fstat
,
1026 .lstat_fn
= streams_xattr_lstat
,
1027 .pread_fn
= streams_xattr_pread
,
1028 .pwrite_fn
= streams_xattr_pwrite
,
1029 .unlink_fn
= streams_xattr_unlink
,
1030 .rename_fn
= streams_xattr_rename
,
1031 .ftruncate_fn
= streams_xattr_ftruncate
,
1032 .fallocate_fn
= streams_xattr_fallocate
,
1033 .streaminfo_fn
= streams_xattr_streaminfo
,
1036 NTSTATUS
vfs_streams_xattr_init(void);
1037 NTSTATUS
vfs_streams_xattr_init(void)
1039 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "streams_xattr",
1040 &vfs_streams_xattr_fns
);