2 * Unix SMB/CIFS implementation.
4 * Support for OneFS Alternate Data Streams
6 * Copyright (C) Tim Prouty, 2008
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include <sys/isi_enc.h>
26 * OneFS stores streams without the explicit :$DATA at the end, so this strips
27 * it off. All onefs_stream functions must call through this instead of
28 * split_ntfs_stream_name directly.
30 NTSTATUS onefs_split_ntfs_stream_name(TALLOC_CTX *mem_ctx, const char *fname,
31 char **pbase, char **pstream)
36 status = split_ntfs_stream_name(mem_ctx, fname, pbase, pstream);
37 if (!NT_STATUS_IS_OK(status)) {
41 /* Default $DATA stream. */
42 if (pstream == NULL || *pstream == NULL) {
46 /* Strip off the $DATA. */
47 stream = strrchr_m(*pstream, ':');
54 int onefs_close(vfs_handle_struct *handle, struct files_struct *fsp)
59 ret = SMB_VFS_NEXT_CLOSE(handle, fsp->base_fsp);
61 ret2 = SMB_VFS_NEXT_CLOSE(handle, fsp);
63 return ret ? ret : ret2;
67 * Get the ADS directory fd for a file.
69 static int get_stream_dir_fd(connection_struct *conn, const char *base,
76 /* If a valid base_fdp was given, use it. */
77 if (base_fdp && *base_fdp >= 0) {
80 base_fd = onefs_sys_create_file(conn,
99 /* Open the ADS directory. */
100 dir_fd = onefs_sys_create_file(conn,
115 /* Close base_fd if it's not need or on error. */
116 if (!base_fdp || dir_fd < 0) {
122 /* Set the out base_fdp if successful and it was requested. */
123 if (base_fdp && dir_fd >= 0) {
130 int onefs_rename(vfs_handle_struct *handle, const char *oldname,
133 TALLOC_CTX *frame = NULL;
144 old_is_stream = is_ntfs_stream_name(oldname);
145 new_is_stream = is_ntfs_stream_name(newname);
147 if (!old_is_stream && !new_is_stream) {
148 return SMB_VFS_NEXT_RENAME(handle, oldname, newname);
151 frame = talloc_stackframe();
153 if (!NT_STATUS_IS_OK(onefs_split_ntfs_stream_name(talloc_tos(),
160 if (!NT_STATUS_IS_OK(onefs_split_ntfs_stream_name(talloc_tos(),
167 dir_fd = get_stream_dir_fd(handle->conn, obase, NULL);
172 DEBUG(8,("onefs_rename called for %s : %s => %s : %s\n",
173 obase, osname, nbase, nsname));
175 /* Handle rename of stream to default stream specially. */
176 if (nsname == NULL) {
177 ret = enc_renameat(dir_fd, osname, ENC_DEFAULT, AT_FDCWD,
180 ret = enc_renameat(dir_fd, osname, ENC_DEFAULT, dir_fd, nsname,
195 * Merge a base file's sbuf into the a streams's sbuf.
197 static void merge_stat(SMB_STRUCT_STAT *stream_sbuf,
198 const SMB_STRUCT_STAT *base_sbuf)
200 int dos_flags = (UF_DOS_NOINDEX | UF_DOS_ARCHIVE |
201 UF_DOS_HIDDEN | UF_DOS_RO | UF_DOS_SYSTEM);
202 stream_sbuf->st_mtime = base_sbuf->st_mtime;
203 stream_sbuf->st_ctime = base_sbuf->st_ctime;
204 stream_sbuf->st_atime = base_sbuf->st_atime;
205 stream_sbuf->st_flags &= ~dos_flags;
206 stream_sbuf->st_flags |= base_sbuf->st_flags & dos_flags;
209 static int stat_stream(vfs_handle_struct *handle, const char *base,
210 const char *stream, SMB_STRUCT_STAT *sbuf, int flags)
212 SMB_STRUCT_STAT base_sbuf;
213 int base_fd = -1, dir_fd, ret, saved_errno;
215 dir_fd = get_stream_dir_fd(handle->conn, base, &base_fd);
220 /* Stat the stream. */
221 ret = enc_fstatat(dir_fd, stream, ENC_DEFAULT, sbuf, flags);
223 /* Now stat the base file and merge the results. */
224 ret = sys_fstat(base_fd, &base_sbuf);
226 merge_stat(sbuf, &base_sbuf);
237 int onefs_stat(vfs_handle_struct *handle, const char *path,
238 SMB_STRUCT_STAT *sbuf)
243 if (!is_ntfs_stream_name(path)) {
244 return SMB_VFS_NEXT_STAT(handle, path, sbuf);
247 if (!NT_STATUS_IS_OK(onefs_split_ntfs_stream_name(talloc_tos(), path,
249 DEBUG(10, ("onefs_split_ntfs_stream_name failed\n"));
254 /* If it's the ::$DATA stream just stat the base file name. */
256 return SMB_VFS_NEXT_STAT(handle, base, sbuf);
259 return stat_stream(handle, base, stream, sbuf, 0);
262 int onefs_fstat(vfs_handle_struct *handle, struct files_struct *fsp,
263 SMB_STRUCT_STAT *sbuf)
265 SMB_STRUCT_STAT base_sbuf;
268 /* Stat the stream, by calling next_fstat on the stream's fd. */
269 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
274 /* Stat the base file and merge the results. */
275 if (fsp != NULL && fsp->base_fsp != NULL) {
276 ret = sys_fstat(fsp->base_fsp->fh->fd, &base_sbuf);
278 merge_stat(sbuf, &base_sbuf);
285 int onefs_lstat(vfs_handle_struct *handle, const char *path,
286 SMB_STRUCT_STAT *sbuf)
291 if (!is_ntfs_stream_name(path)) {
292 return SMB_VFS_NEXT_LSTAT(handle, path, sbuf);
295 if (!NT_STATUS_IS_OK(onefs_split_ntfs_stream_name(talloc_tos(), path,
297 DEBUG(10, ("onefs_split_ntfs_stream_name failed\n"));
302 /* If it's the ::$DATA stream just stat the base file name. */
304 return SMB_VFS_NEXT_LSTAT(handle, base, sbuf);
307 return stat_stream(handle, base, stream, sbuf, AT_SYMLINK_NOFOLLOW);
310 int onefs_unlink(vfs_handle_struct *handle, const char *path)
314 int dir_fd, ret, saved_errno;
316 if (!is_ntfs_stream_name(path)) {
317 return SMB_VFS_NEXT_UNLINK(handle, path);
320 if (!NT_STATUS_IS_OK(onefs_split_ntfs_stream_name(talloc_tos(), path,
322 DEBUG(10, ("onefs_split_ntfs_stream_name failed\n"));
327 /* If it's the ::$DATA stream just unlink the base file name. */
329 return SMB_VFS_NEXT_UNLINK(handle, base);
332 dir_fd = get_stream_dir_fd(handle->conn, base, NULL);
337 ret = enc_unlinkat(dir_fd, stream, ENC_DEFAULT, 0);
345 int onefs_chflags(vfs_handle_struct *handle, const char *path,
351 if (!NT_STATUS_IS_OK(onefs_split_ntfs_stream_name(talloc_tos(), path,
353 DEBUG(10, ("onefs_split_ntfs_stream_name failed\n"));
359 * Only set the attributes on the base file. ifs_createfile handles
360 * file creation attribute semantics.
362 return SMB_VFS_NEXT_CHFLAGS(handle, base, flags);
366 * Streaminfo enumeration functionality
368 struct streaminfo_state {
370 vfs_handle_struct *handle;
371 unsigned int num_streams;
372 struct stream_struct *streams;
376 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
377 struct stream_struct **streams,
378 const char *name, SMB_OFF_T size,
379 SMB_OFF_T alloc_size)
381 struct stream_struct *tmp;
383 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
389 tmp[*num_streams].name = talloc_asprintf(mem_ctx, ":%s:%s", name,
391 if (tmp[*num_streams].name == NULL) {
395 tmp[*num_streams].size = size;
396 tmp[*num_streams].alloc_size = alloc_size;
403 static NTSTATUS walk_onefs_streams(connection_struct *conn, files_struct *fsp,
405 struct streaminfo_state *state,
406 SMB_STRUCT_STAT *base_sbuf)
408 NTSTATUS status = NT_STATUS_OK;
409 bool opened_base_fd = false;
414 SMB_STRUCT_DIR *dirp = NULL;
415 SMB_STRUCT_DIRENT *dp = NULL;
416 files_struct fake_fs;
417 struct fd_handle fake_fh;
418 SMB_STRUCT_STAT stream_sbuf;
420 ZERO_STRUCT(fake_fh);
421 ZERO_STRUCT(fake_fs);
423 /* If the base file is already open, use its fd. */
424 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
425 base_fd = fsp->fh->fd;
427 opened_base_fd = true;
430 dir_fd = get_stream_dir_fd(conn, fname, &base_fd);
432 return map_nt_error_from_unix(errno);
435 /* Open the ADS directory. */
436 if ((dirp = fdopendir(dir_fd)) == NULL) {
437 DEBUG(0, ("Error on opendir %s. errno=%d (%s)\n",
438 fname, errno, strerror(errno)));
439 status = map_nt_error_from_unix(errno);
444 fake_fs.fh = &fake_fh;
445 fake_fs.fsp_name = SMB_STRDUP(fname);
447 /* Iterate over the streams in the ADS directory. */
448 while ((dp = SMB_VFS_READDIR(conn, dirp)) != NULL) {
449 /* Skip the "." and ".." entries */
450 if ((strcmp(dp->d_name, ".") == 0) ||
451 (strcmp(dp->d_name, "..") == 0))
454 /* Open actual stream */
455 if ((stream_fd = onefs_sys_create_file(conn,
469 DEBUG(0, ("Error opening stream %s:%s. "
470 "errno=%d (%s)\n", fname, dp->d_name, errno,
475 /* Figure out the stat info. */
476 fake_fh.fd = stream_fd;
477 ret = SMB_VFS_FSTAT(&fake_fs, &stream_sbuf);
481 DEBUG(0, ("Error fstating stream %s:%s. "
482 "errno=%d (%s)\n", fname, dp->d_name, errno,
487 merge_stat(&stream_sbuf, base_sbuf);
489 if (!add_one_stream(state->mem_ctx,
490 &state->num_streams, &state->streams,
491 dp->d_name, stream_sbuf.st_size,
492 get_allocation_size(conn, NULL,
494 state->status = NT_STATUS_NO_MEMORY;
500 /* Cleanup everything that was opened. */
502 SMB_VFS_CLOSEDIR(conn, dirp);
507 if (opened_base_fd) {
508 SMB_ASSERT(base_fd >= 0);
512 SAFE_FREE(fake_fs.fsp_name);
516 NTSTATUS onefs_streaminfo(vfs_handle_struct *handle,
517 struct files_struct *fsp,
520 unsigned int *num_streams,
521 struct stream_struct **streams)
523 SMB_STRUCT_STAT sbuf;
526 struct streaminfo_state state;
528 /* Get a valid stat. */
529 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
530 if (is_ntfs_stream_name(fsp->fsp_name)) {
531 return NT_STATUS_INVALID_PARAMETER;
533 ret = SMB_VFS_FSTAT(fsp, &sbuf);
535 if (is_ntfs_stream_name(fname)) {
536 return NT_STATUS_INVALID_PARAMETER;
538 ret = SMB_VFS_STAT(handle->conn, fname, &sbuf);
542 return map_nt_error_from_unix(errno);
545 state.streams = NULL;
546 state.num_streams = 0;
548 /* Add the default stream. */
549 if (S_ISREG(sbuf.st_mode)) {
550 if (!add_one_stream(mem_ctx,
551 &state.num_streams, &state.streams,
553 get_allocation_size(handle->conn, fsp,
555 return NT_STATUS_NO_MEMORY;
559 state.mem_ctx = mem_ctx;
560 state.handle = handle;
561 state.status = NT_STATUS_OK;
563 /* If there are more streams, add them too. */
564 if (sbuf.st_flags & UF_HASADS) {
566 status = walk_onefs_streams(handle->conn, fsp, fname,
569 if (!NT_STATUS_IS_OK(status)) {
570 TALLOC_FREE(state.streams);
574 if (!NT_STATUS_IS_OK(state.status)) {
575 TALLOC_FREE(state.streams);
580 *num_streams = state.num_streams;
581 *streams = state.streams;