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
, (unsigned char *)&(sbuf
->st_ex_dev
),
56 sizeof(sbuf
->st_ex_dev
));
57 MD5Update(&ctx
, (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 strupper_m(strrchr_m(*xattr_name
, ':') + 1);
125 DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name
,
131 static bool streams_xattr_recheck(struct stream_io
*sio
)
134 char *xattr_name
= NULL
;
136 if (sio
->fsp
->fsp_name
== sio
->fsp_name_ptr
) {
140 if (sio
->fsp
->fsp_name
->stream_name
== NULL
) {
141 /* how can this happen */
146 status
= streams_xattr_get_name(talloc_tos(),
147 sio
->fsp
->fsp_name
->stream_name
,
149 if (!NT_STATUS_IS_OK(status
)) {
153 TALLOC_FREE(sio
->xattr_name
);
154 TALLOC_FREE(sio
->base
);
155 sio
->xattr_name
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio
->handle
, sio
->fsp
),
157 sio
->base
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio
->handle
, sio
->fsp
),
158 sio
->fsp
->fsp_name
->base_name
);
159 sio
->fsp_name_ptr
= sio
->fsp
->fsp_name
;
161 TALLOC_FREE(xattr_name
);
163 if ((sio
->xattr_name
== NULL
) || (sio
->base
== NULL
)) {
171 * Helper to stat/lstat the base file of an smb_fname.
173 static int streams_xattr_stat_base(vfs_handle_struct
*handle
,
174 struct smb_filename
*smb_fname
,
177 char *tmp_stream_name
;
180 tmp_stream_name
= smb_fname
->stream_name
;
181 smb_fname
->stream_name
= NULL
;
183 result
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
185 result
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
187 smb_fname
->stream_name
= tmp_stream_name
;
191 static int streams_xattr_fstat(vfs_handle_struct
*handle
, files_struct
*fsp
,
192 SMB_STRUCT_STAT
*sbuf
)
194 struct smb_filename
*smb_fname_base
= NULL
;
197 struct stream_io
*io
= (struct stream_io
*)
198 VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
200 DEBUG(10, ("streams_xattr_fstat called for %d\n", fsp
->fh
->fd
));
202 if (io
== NULL
|| fsp
->base_fsp
== NULL
) {
203 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
206 if (!streams_xattr_recheck(io
)) {
210 /* Create an smb_filename with stream_name == NULL. */
211 status
= create_synthetic_smb_fname(talloc_tos(),
215 if (!NT_STATUS_IS_OK(status
)) {
216 errno
= map_errno_from_nt_status(status
);
220 if (lp_posix_pathnames()) {
221 ret
= SMB_VFS_LSTAT(handle
->conn
, smb_fname_base
);
223 ret
= SMB_VFS_STAT(handle
->conn
, smb_fname_base
);
225 *sbuf
= smb_fname_base
->st
;
226 TALLOC_FREE(smb_fname_base
);
232 sbuf
->st_ex_size
= get_xattr_size(handle
->conn
, fsp
->base_fsp
,
233 io
->base
, io
->xattr_name
);
234 if (sbuf
->st_ex_size
== -1) {
238 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf
->st_ex_size
));
240 sbuf
->st_ex_ino
= stream_inode(sbuf
, io
->xattr_name
);
241 sbuf
->st_ex_mode
&= ~S_IFMT
;
242 sbuf
->st_ex_mode
|= S_IFREG
;
243 sbuf
->st_ex_blocks
= sbuf
->st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
248 static int streams_xattr_stat(vfs_handle_struct
*handle
,
249 struct smb_filename
*smb_fname
)
253 char *xattr_name
= NULL
;
255 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
256 return SMB_VFS_NEXT_STAT(handle
, smb_fname
);
259 /* Note if lp_posix_paths() is true, we can never
260 * get here as is_ntfs_stream_smb_fname() is
261 * always false. So we never need worry about
262 * not following links here. */
264 /* If the default stream is requested, just stat the base file. */
265 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
266 return streams_xattr_stat_base(handle
, smb_fname
, true);
269 /* Populate the stat struct with info from the base file. */
270 if (streams_xattr_stat_base(handle
, smb_fname
, true) == -1) {
274 /* Derive the xattr name to lookup. */
275 status
= streams_xattr_get_name(talloc_tos(), smb_fname
->stream_name
,
277 if (!NT_STATUS_IS_OK(status
)) {
278 errno
= map_errno_from_nt_status(status
);
282 /* Augment the base file's stat information before returning. */
283 smb_fname
->st
.st_ex_size
= get_xattr_size(handle
->conn
, NULL
,
284 smb_fname
->base_name
,
286 if (smb_fname
->st
.st_ex_size
== -1) {
292 smb_fname
->st
.st_ex_ino
= stream_inode(&smb_fname
->st
, xattr_name
);
293 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
294 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
295 smb_fname
->st
.st_ex_blocks
=
296 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
300 TALLOC_FREE(xattr_name
);
304 static int streams_xattr_lstat(vfs_handle_struct
*handle
,
305 struct smb_filename
*smb_fname
)
309 char *xattr_name
= NULL
;
311 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
312 return SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
315 /* If the default stream is requested, just stat the base file. */
316 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
317 return streams_xattr_stat_base(handle
, smb_fname
, false);
320 /* Populate the stat struct with info from the base file. */
321 if (streams_xattr_stat_base(handle
, smb_fname
, false) == -1) {
325 /* Derive the xattr name to lookup. */
326 status
= streams_xattr_get_name(talloc_tos(), smb_fname
->stream_name
,
328 if (!NT_STATUS_IS_OK(status
)) {
329 errno
= map_errno_from_nt_status(status
);
333 /* Augment the base file's stat information before returning. */
334 smb_fname
->st
.st_ex_size
= get_xattr_size(handle
->conn
, NULL
,
335 smb_fname
->base_name
,
337 if (smb_fname
->st
.st_ex_size
== -1) {
343 smb_fname
->st
.st_ex_ino
= stream_inode(&smb_fname
->st
, xattr_name
);
344 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
345 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
346 smb_fname
->st
.st_ex_blocks
=
347 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
352 TALLOC_FREE(xattr_name
);
356 static int streams_xattr_open(vfs_handle_struct
*handle
,
357 struct smb_filename
*smb_fname
,
358 files_struct
*fsp
, int flags
, mode_t mode
)
361 struct smb_filename
*smb_fname_base
= NULL
;
362 struct stream_io
*sio
;
364 char *xattr_name
= NULL
;
368 DEBUG(10, ("streams_xattr_open called for %s\n",
369 smb_fname_str_dbg(smb_fname
)));
371 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
372 return SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
375 /* If the default stream is requested, just open the base file. */
376 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
377 char *tmp_stream_name
;
380 tmp_stream_name
= smb_fname
->stream_name
;
381 smb_fname
->stream_name
= NULL
;
383 ret
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
385 smb_fname
->stream_name
= tmp_stream_name
;
390 status
= streams_xattr_get_name(talloc_tos(), smb_fname
->stream_name
,
392 if (!NT_STATUS_IS_OK(status
)) {
393 errno
= map_errno_from_nt_status(status
);
397 /* Create an smb_filename with stream_name == NULL. */
398 status
= create_synthetic_smb_fname(talloc_tos(),
399 smb_fname
->base_name
,
402 if (!NT_STATUS_IS_OK(status
)) {
403 errno
= map_errno_from_nt_status(status
);
408 * We use baseflags to turn off nasty side-effects when opening the
412 baseflags
&= ~O_TRUNC
;
413 baseflags
&= ~O_EXCL
;
414 baseflags
&= ~O_CREAT
;
416 hostfd
= SMB_VFS_OPEN(handle
->conn
, smb_fname_base
, fsp
,
419 TALLOC_FREE(smb_fname_base
);
421 /* It is legit to open a stream on a directory, but the base
422 * fd has to be read-only.
424 if ((hostfd
== -1) && (errno
== EISDIR
)) {
425 baseflags
&= ~O_ACCMODE
;
426 baseflags
|= O_RDONLY
;
427 hostfd
= SMB_VFS_OPEN(handle
->conn
, smb_fname
, fsp
, baseflags
,
435 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
,
436 smb_fname
->base_name
, xattr_name
, &ea
);
438 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status
)));
440 if (!NT_STATUS_IS_OK(status
)
441 && !NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
443 * The base file is not there. This is an error even if we got
444 * O_CREAT, the higher levels should have created the base
447 DEBUG(10, ("streams_xattr_open: base file %s not around, "
448 "returning ENOENT\n", smb_fname
->base_name
));
453 if (!NT_STATUS_IS_OK(status
)) {
455 * The attribute does not exist
458 if (flags
& O_CREAT
) {
460 * Darn, xattrs need at least 1 byte
464 DEBUG(10, ("creating attribute %s on file %s\n",
465 xattr_name
, smb_fname
->base_name
));
467 if (fsp
->base_fsp
->fh
->fd
!= -1) {
468 if (SMB_VFS_FSETXATTR(
469 fsp
->base_fsp
, xattr_name
,
471 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
475 if (SMB_VFS_SETXATTR(
476 handle
->conn
, smb_fname
->base_name
,
477 xattr_name
, &null
, sizeof(null
),
478 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
485 if (flags
& O_TRUNC
) {
487 if (fsp
->base_fsp
->fh
->fd
!= -1) {
488 if (SMB_VFS_FSETXATTR(
489 fsp
->base_fsp
, xattr_name
,
491 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
495 if (SMB_VFS_SETXATTR(
496 handle
->conn
, smb_fname
->base_name
,
497 xattr_name
, &null
, sizeof(null
),
498 flags
& O_EXCL
? XATTR_CREATE
: 0) == -1) {
504 sio
= (struct stream_io
*)VFS_ADD_FSP_EXTENSION(handle
, fsp
,
512 sio
->xattr_name
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
514 sio
->base
= talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle
, fsp
),
515 smb_fname
->base_name
);
516 sio
->fsp_name_ptr
= fsp
->fsp_name
;
517 sio
->handle
= handle
;
520 if ((sio
->xattr_name
== NULL
) || (sio
->base
== NULL
)) {
530 * BUGBUGBUG -- we would need to call fd_close_posix here, but
531 * we don't have a full fsp yet
539 static int streams_xattr_unlink(vfs_handle_struct
*handle
,
540 const struct smb_filename
*smb_fname
)
546 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
547 return SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
550 /* If the default stream is requested, just open the base file. */
551 if (is_ntfs_default_stream_smb_fname(smb_fname
)) {
552 struct smb_filename
*smb_fname_base
= NULL
;
554 status
= copy_smb_filename(talloc_tos(), smb_fname
,
556 if (!NT_STATUS_IS_OK(status
)) {
557 errno
= map_errno_from_nt_status(status
);
561 ret
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname_base
);
563 TALLOC_FREE(smb_fname_base
);
567 status
= streams_xattr_get_name(talloc_tos(), smb_fname
->stream_name
,
569 if (!NT_STATUS_IS_OK(status
)) {
570 errno
= map_errno_from_nt_status(status
);
574 ret
= SMB_VFS_REMOVEXATTR(handle
->conn
, smb_fname
->base_name
, xattr_name
);
576 if ((ret
== -1) && (errno
== ENOATTR
)) {
584 TALLOC_FREE(xattr_name
);
588 static int streams_xattr_rename(vfs_handle_struct
*handle
,
589 const struct smb_filename
*smb_fname_src
,
590 const struct smb_filename
*smb_fname_dst
)
594 char *src_xattr_name
= NULL
;
595 char *dst_xattr_name
= NULL
;
596 bool src_is_stream
, dst_is_stream
;
601 src_is_stream
= is_ntfs_stream_smb_fname(smb_fname_src
);
602 dst_is_stream
= is_ntfs_stream_smb_fname(smb_fname_dst
);
604 if (!src_is_stream
&& !dst_is_stream
) {
605 return SMB_VFS_NEXT_RENAME(handle
, smb_fname_src
,
609 /* For now don't allow renames from or to the default stream. */
610 if (is_ntfs_default_stream_smb_fname(smb_fname_src
) ||
611 is_ntfs_default_stream_smb_fname(smb_fname_dst
)) {
616 /* Don't rename if the streams are identical. */
617 if (StrCaseCmp(smb_fname_src
->stream_name
,
618 smb_fname_dst
->stream_name
) == 0) {
622 /* Get the xattr names. */
623 status
= streams_xattr_get_name(talloc_tos(),
624 smb_fname_src
->stream_name
,
626 if (!NT_STATUS_IS_OK(status
)) {
627 errno
= map_errno_from_nt_status(status
);
630 status
= streams_xattr_get_name(talloc_tos(),
631 smb_fname_dst
->stream_name
,
633 if (!NT_STATUS_IS_OK(status
)) {
634 errno
= map_errno_from_nt_status(status
);
638 /* read the old stream */
639 status
= get_ea_value(talloc_tos(), handle
->conn
, NULL
,
640 smb_fname_src
->base_name
, src_xattr_name
, &ea
);
641 if (!NT_STATUS_IS_OK(status
)) {
646 /* (over)write the new stream */
647 nret
= SMB_VFS_SETXATTR(handle
->conn
, smb_fname_src
->base_name
,
648 dst_xattr_name
, ea
.value
.data
, ea
.value
.length
,
651 if (errno
== ENOATTR
) {
657 /* remove the old stream */
658 oret
= SMB_VFS_REMOVEXATTR(handle
->conn
, smb_fname_src
->base_name
,
661 if (errno
== ENOATTR
) {
671 TALLOC_FREE(src_xattr_name
);
672 TALLOC_FREE(dst_xattr_name
);
676 static NTSTATUS
walk_xattr_streams(connection_struct
*conn
, files_struct
*fsp
,
678 bool (*fn
)(struct ea_struct
*ea
,
685 size_t prefix_len
= strlen(SAMBA_XATTR_DOSSTREAM_PREFIX
);
687 status
= get_ea_names_from_file(talloc_tos(), conn
, fsp
, fname
,
689 if (!NT_STATUS_IS_OK(status
)) {
693 for (i
=0; i
<num_names
; i
++) {
696 if (strncmp(names
[i
], SAMBA_XATTR_DOSSTREAM_PREFIX
,
701 status
= get_ea_value(names
, conn
, fsp
, fname
, names
[i
], &ea
);
702 if (!NT_STATUS_IS_OK(status
)) {
703 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
704 names
[i
], fname
, nt_errstr(status
)));
708 ea
.name
= talloc_asprintf(ea
.value
.data
, ":%s",
709 names
[i
] + prefix_len
);
710 if (ea
.name
== NULL
) {
711 DEBUG(0, ("talloc failed\n"));
715 if (!fn(&ea
, private_data
)) {
716 TALLOC_FREE(ea
.value
.data
);
720 TALLOC_FREE(ea
.value
.data
);
727 static bool add_one_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
728 struct stream_struct
**streams
,
729 const char *name
, SMB_OFF_T size
,
730 SMB_OFF_T alloc_size
)
732 struct stream_struct
*tmp
;
734 tmp
= TALLOC_REALLOC_ARRAY(mem_ctx
, *streams
, struct stream_struct
,
740 tmp
[*num_streams
].name
= talloc_strdup(tmp
, name
);
741 if (tmp
[*num_streams
].name
== NULL
) {
745 tmp
[*num_streams
].size
= size
;
746 tmp
[*num_streams
].alloc_size
= alloc_size
;
753 struct streaminfo_state
{
755 vfs_handle_struct
*handle
;
756 unsigned int num_streams
;
757 struct stream_struct
*streams
;
761 static bool collect_one_stream(struct ea_struct
*ea
, void *private_data
)
763 struct streaminfo_state
*state
=
764 (struct streaminfo_state
*)private_data
;
766 if (!add_one_stream(state
->mem_ctx
,
767 &state
->num_streams
, &state
->streams
,
768 ea
->name
, ea
->value
.length
-1,
769 smb_roundup(state
->handle
->conn
,
770 ea
->value
.length
-1))) {
771 state
->status
= NT_STATUS_NO_MEMORY
;
778 static NTSTATUS
streams_xattr_streaminfo(vfs_handle_struct
*handle
,
779 struct files_struct
*fsp
,
782 unsigned int *pnum_streams
,
783 struct stream_struct
**pstreams
)
785 SMB_STRUCT_STAT sbuf
;
788 struct streaminfo_state state
;
790 if ((fsp
!= NULL
) && (fsp
->fh
->fd
!= -1)) {
791 ret
= SMB_VFS_FSTAT(fsp
, &sbuf
);
794 struct smb_filename
*smb_fname
= NULL
;
795 status
= create_synthetic_smb_fname(talloc_tos(), fname
, NULL
,
797 if (!NT_STATUS_IS_OK(status
)) {
800 if (lp_posix_pathnames()) {
801 ret
= SMB_VFS_LSTAT(handle
->conn
, smb_fname
);
803 ret
= SMB_VFS_STAT(handle
->conn
, smb_fname
);
805 sbuf
= smb_fname
->st
;
806 TALLOC_FREE(smb_fname
);
810 return map_nt_error_from_unix(errno
);
813 state
.streams
= *pstreams
;
814 state
.num_streams
= *pnum_streams
;
815 state
.mem_ctx
= mem_ctx
;
816 state
.handle
= handle
;
817 state
.status
= NT_STATUS_OK
;
819 status
= walk_xattr_streams(handle
->conn
, fsp
, fname
,
820 collect_one_stream
, &state
);
822 if (!NT_STATUS_IS_OK(status
)) {
823 TALLOC_FREE(state
.streams
);
827 if (!NT_STATUS_IS_OK(state
.status
)) {
828 TALLOC_FREE(state
.streams
);
832 *pnum_streams
= state
.num_streams
;
833 *pstreams
= state
.streams
;
835 return SMB_VFS_NEXT_STREAMINFO(handle
, fsp
, fname
, mem_ctx
, pnum_streams
, pstreams
);
838 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct
*handle
,
839 enum timestamp_set_resolution
*p_ts_res
)
841 return SMB_VFS_NEXT_FS_CAPABILITIES(handle
, p_ts_res
) | FILE_NAMED_STREAMS
;
844 static ssize_t
streams_xattr_pwrite(vfs_handle_struct
*handle
,
845 files_struct
*fsp
, const void *data
,
846 size_t n
, SMB_OFF_T offset
)
848 struct stream_io
*sio
=
849 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
854 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n
));
857 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
860 if (!streams_xattr_recheck(sio
)) {
864 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
865 sio
->base
, sio
->xattr_name
, &ea
);
866 if (!NT_STATUS_IS_OK(status
)) {
870 if ((offset
+ n
) > ea
.value
.length
-1) {
873 tmp
= TALLOC_REALLOC_ARRAY(talloc_tos(), ea
.value
.data
, uint8
,
877 TALLOC_FREE(ea
.value
.data
);
882 ea
.value
.length
= offset
+ n
+ 1;
883 ea
.value
.data
[offset
+n
] = 0;
886 memcpy(ea
.value
.data
+ offset
, data
, n
);
888 if (fsp
->base_fsp
->fh
->fd
!= -1) {
889 ret
= SMB_VFS_FSETXATTR(fsp
->base_fsp
,
891 ea
.value
.data
, ea
.value
.length
, 0);
893 ret
= SMB_VFS_SETXATTR(fsp
->conn
,
894 fsp
->base_fsp
->fsp_name
->base_name
,
896 ea
.value
.data
, ea
.value
.length
, 0);
898 TALLOC_FREE(ea
.value
.data
);
907 static ssize_t
streams_xattr_pread(vfs_handle_struct
*handle
,
908 files_struct
*fsp
, void *data
,
909 size_t n
, SMB_OFF_T offset
)
911 struct stream_io
*sio
=
912 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
915 size_t length
, overlap
;
917 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
918 (int)offset
, (int)n
));
921 return SMB_VFS_NEXT_PREAD(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 length
= ea
.value
.length
-1;
936 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
939 /* Attempt to read past EOF. */
940 if (length
<= offset
) {
944 overlap
= (offset
+ n
) > length
? (length
- offset
) : n
;
945 memcpy(data
, ea
.value
.data
+ offset
, overlap
);
947 TALLOC_FREE(ea
.value
.data
);
951 static int streams_xattr_ftruncate(struct vfs_handle_struct
*handle
,
952 struct files_struct
*fsp
,
959 struct stream_io
*sio
=
960 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
962 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
963 fsp_str_dbg(fsp
), (double)offset
));
966 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
969 if (!streams_xattr_recheck(sio
)) {
973 status
= get_ea_value(talloc_tos(), handle
->conn
, fsp
->base_fsp
,
974 sio
->base
, sio
->xattr_name
, &ea
);
975 if (!NT_STATUS_IS_OK(status
)) {
979 tmp
= TALLOC_REALLOC_ARRAY(talloc_tos(), ea
.value
.data
, uint8
,
983 TALLOC_FREE(ea
.value
.data
);
988 /* Did we expand ? */
989 if (ea
.value
.length
< offset
+ 1) {
990 memset(&tmp
[ea
.value
.length
], '\0',
991 offset
+ 1 - ea
.value
.length
);
995 ea
.value
.length
= offset
+ 1;
996 ea
.value
.data
[offset
] = 0;
998 if (fsp
->base_fsp
->fh
->fd
!= -1) {
999 ret
= SMB_VFS_FSETXATTR(fsp
->base_fsp
,
1001 ea
.value
.data
, ea
.value
.length
, 0);
1003 ret
= SMB_VFS_SETXATTR(fsp
->conn
,
1004 fsp
->base_fsp
->fsp_name
->base_name
,
1006 ea
.value
.data
, ea
.value
.length
, 0);
1009 TALLOC_FREE(ea
.value
.data
);
1018 static int streams_xattr_fallocate(struct vfs_handle_struct
*handle
,
1019 struct files_struct
*fsp
,
1020 enum vfs_fallocate_mode mode
,
1024 struct stream_io
*sio
=
1025 (struct stream_io
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1027 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1029 fsp_str_dbg(fsp
), (double)offset
, (double)len
));
1032 return SMB_VFS_NEXT_FALLOCATE(handle
, fsp
, mode
, offset
, len
);
1035 if (!streams_xattr_recheck(sio
)) {
1039 /* Let the pwrite code path handle it. */
1044 static struct vfs_fn_pointers vfs_streams_xattr_fns
= {
1045 .fs_capabilities
= streams_xattr_fs_capabilities
,
1046 .open_fn
= streams_xattr_open
,
1047 .stat
= streams_xattr_stat
,
1048 .fstat
= streams_xattr_fstat
,
1049 .lstat
= streams_xattr_lstat
,
1050 .pread
= streams_xattr_pread
,
1051 .pwrite
= streams_xattr_pwrite
,
1052 .unlink
= streams_xattr_unlink
,
1053 .rename
= streams_xattr_rename
,
1054 .ftruncate
= streams_xattr_ftruncate
,
1055 .fallocate
= streams_xattr_fallocate
,
1056 .streaminfo
= streams_xattr_streaminfo
,
1059 NTSTATUS
vfs_streams_xattr_init(void);
1060 NTSTATUS
vfs_streams_xattr_init(void)
1062 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "streams_xattr",
1063 &vfs_streams_xattr_fns
);