s3:streams: check for :$DATA only in the backend (fix bug #6642)
[metze/samba/wip.git] / source3 / modules / onefs_streams.c
1 /*
2  * Unix SMB/CIFS implementation.
3  *
4  * Support for OneFS Alternate Data Streams
5  *
6  * Copyright (C) Tim Prouty, 2008
7  *
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.
12  *
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.
17  *
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/>.
20  */
21
22 #include "includes.h"
23 #include "onefs.h"
24 #include "onefs_config.h"
25
26 #include <sys/isi_enc.h>
27
28 NTSTATUS onefs_stream_prep_smb_fname(TALLOC_CTX *ctx,
29                                      const struct smb_filename *smb_fname_in,
30                                      struct smb_filename **smb_fname_out)
31 {
32         char *stream_name = NULL;
33         NTSTATUS status;
34
35         /*
36          * Only attempt to strip off the trailing :$DATA if there is an actual
37          * stream there.  If it is the default stream, the smb_fname_out will
38          * just have a NULL stream so the base file is opened.
39          */
40         if (smb_fname_in->stream_name &&
41             !is_ntfs_default_stream_smb_fname(smb_fname_in)) {
42                 char *str_tmp = smb_fname_in->stream_name;
43
44                 /* First strip off the leading ':' */
45                 if (str_tmp[0] == ':') {
46                         str_tmp++;
47                 }
48
49                 /* Create a new copy of the stream_name. */
50                 stream_name = talloc_strdup(ctx, str_tmp);
51                 if (stream_name == NULL) {
52                         return NT_STATUS_NO_MEMORY;
53                 }
54
55                 /* Strip off the :$DATA if one exists. */
56                 str_tmp = strrchr_m(stream_name, ':');
57                 if (str_tmp) {
58                         if (StrCaseCmp(str_tmp, ":$DATA") != 0) {
59                                 return NT_STATUS_INVALID_PARAMETER;
60                         }
61                         str_tmp[0] = '\0';
62                 }
63         }
64
65         /*
66          * If there was a stream that wasn't the default stream the leading
67          * colon and trailing :$DATA has now been stripped off.  Create a new
68          * smb_filename to pass back.
69          */
70         status = create_synthetic_smb_fname(ctx, smb_fname_in->base_name,
71                                             stream_name, &smb_fname_in->st,
72                                             smb_fname_out);
73         TALLOC_FREE(stream_name);
74         return status;
75 }
76
77 int onefs_close(vfs_handle_struct *handle, struct files_struct *fsp)
78 {
79         int ret2, ret = 0;
80
81         if (fsp->base_fsp) {
82                 ret = SMB_VFS_NEXT_CLOSE(handle, fsp->base_fsp);
83         }
84         ret2 = SMB_VFS_NEXT_CLOSE(handle, fsp);
85
86         return ret ? ret : ret2;
87 }
88
89 /*
90  * Get the ADS directory fd for a file.
91  */
92 static int get_stream_dir_fd(connection_struct *conn, const char *base,
93                              int *base_fdp)
94 {
95         int base_fd;
96         int dir_fd;
97         int saved_errno;
98
99         /* If a valid base_fdp was given, use it. */
100         if (base_fdp && *base_fdp >= 0) {
101                 base_fd = *base_fdp;
102         } else {
103                 base_fd = onefs_sys_create_file(conn,
104                                                 -1,
105                                                 base,
106                                                 0,
107                                                 0,
108                                                 0,
109                                                 0,
110                                                 0,
111                                                 0,
112                                                 INTERNAL_OPEN_ONLY,
113                                                 0,
114                                                 NULL,
115                                                 0,
116                                                 NULL);
117                 if (base_fd < 0) {
118                         return -1;
119                 }
120         }
121
122         /* Open the ADS directory. */
123         dir_fd = onefs_sys_create_file(conn,
124                                         base_fd,
125                                         ".",
126                                         0,
127                                         FILE_READ_DATA,
128                                         0,
129                                         0,
130                                         0,
131                                         0,
132                                         INTERNAL_OPEN_ONLY,
133                                         0,
134                                         NULL,
135                                         0,
136                                         NULL);
137
138         /* Close base_fd if it's not need or on error. */
139         if (!base_fdp || dir_fd < 0) {
140                 saved_errno = errno;
141                 close(base_fd);
142                 errno = saved_errno;
143         }
144
145         /* Set the out base_fdp if successful and it was requested. */
146         if (base_fdp && dir_fd >= 0) {
147                 *base_fdp = base_fd;
148         }
149
150         return dir_fd;
151 }
152
153 int onefs_rename(vfs_handle_struct *handle,
154                  const struct smb_filename *smb_fname_src,
155                  const struct smb_filename *smb_fname_dst)
156 {
157         struct smb_filename *smb_fname_src_onefs = NULL;
158         struct smb_filename *smb_fname_dst_onefs = NULL;
159         NTSTATUS status;
160         int saved_errno;
161         int dir_fd = -1;
162         int ret = -1;
163
164         START_PROFILE(syscall_rename_at);
165
166         if (!is_ntfs_stream_smb_fname(smb_fname_src) &&
167             !is_ntfs_stream_smb_fname(smb_fname_dst)) {
168                 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
169                                           smb_fname_dst);
170                 goto done;
171         }
172
173         /* For now don't allow renames from or to the default stream. */
174         if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
175             is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
176                 errno = ENOSYS;
177                 goto done;
178         }
179
180         /* prep stream smb_filename structs. */
181         status = onefs_stream_prep_smb_fname(talloc_tos(), smb_fname_src,
182                                              &smb_fname_src_onefs);
183         if (!NT_STATUS_IS_OK(status)) {
184                 errno = map_errno_from_nt_status(status);
185                 goto done;
186         }
187         status = onefs_stream_prep_smb_fname(talloc_tos(), smb_fname_dst,
188                                              &smb_fname_dst_onefs);
189         if (!NT_STATUS_IS_OK(status)) {
190                 errno = map_errno_from_nt_status(status);
191                 goto done;
192         }
193
194         dir_fd = get_stream_dir_fd(handle->conn, smb_fname_src->base_name,
195                                    NULL);
196         if (dir_fd < -1) {
197                 goto done;
198         }
199
200         DEBUG(8, ("onefs_rename called for %s => %s\n",
201                   smb_fname_str_dbg(smb_fname_src_onefs),
202                   smb_fname_str_dbg(smb_fname_dst_onefs)));
203
204         /* Handle rename of stream to default stream specially. */
205         if (smb_fname_dst_onefs->stream_name == NULL) {
206                 ret = enc_renameat(dir_fd, smb_fname_src_onefs->stream_name,
207                                    ENC_DEFAULT, AT_FDCWD,
208                                    smb_fname_dst_onefs->base_name,
209                                    ENC_DEFAULT);
210         } else {
211                 ret = enc_renameat(dir_fd, smb_fname_src_onefs->stream_name,
212                                    ENC_DEFAULT, dir_fd,
213                                    smb_fname_dst_onefs->stream_name,
214                                    ENC_DEFAULT);
215         }
216
217  done:
218         END_PROFILE(syscall_rename_at);
219         TALLOC_FREE(smb_fname_src_onefs);
220         TALLOC_FREE(smb_fname_dst_onefs);
221
222         saved_errno = errno;
223         if (dir_fd >= 0) {
224                 close(dir_fd);
225         }
226         errno = saved_errno;
227         return ret;
228 }
229
230 /*
231  * Merge a base file's sbuf into the a streams's sbuf.
232  */
233 static void merge_stat(SMB_STRUCT_STAT *stream_sbuf,
234                        const SMB_STRUCT_STAT *base_sbuf)
235 {
236         int dos_flags = (UF_DOS_NOINDEX | UF_DOS_ARCHIVE |
237             UF_DOS_HIDDEN | UF_DOS_RO | UF_DOS_SYSTEM);
238         stream_sbuf->st_ex_mtime = base_sbuf->st_ex_mtime;
239         stream_sbuf->st_ex_ctime = base_sbuf->st_ex_ctime;
240         stream_sbuf->st_ex_atime = base_sbuf->st_ex_atime;
241         stream_sbuf->st_ex_flags &= ~dos_flags;
242         stream_sbuf->st_ex_flags |= base_sbuf->st_ex_flags & dos_flags;
243 }
244
245 /* fake timestamps */
246 static void onefs_adjust_stat_time(struct connection_struct *conn,
247                                    const char *fname, SMB_STRUCT_STAT *sbuf)
248 {
249         struct onefs_vfs_share_config cfg;
250         struct timeval tv_now = {0, 0};
251         bool static_mtime = False;
252         bool static_atime = False;
253
254         if (!onefs_get_config(SNUM(conn),
255                               ONEFS_VFS_CONFIG_FAKETIMESTAMPS, &cfg)) {
256                 return;
257         }
258
259         if (IS_MTIME_STATIC_PATH(conn, &cfg, fname)) {
260                 sbuf->st_ex_mtime = sbuf->st_ex_btime;
261                 static_mtime = True;
262         }
263         if (IS_ATIME_STATIC_PATH(conn, &cfg, fname)) {
264                 sbuf->st_ex_atime = sbuf->st_ex_btime;
265                 static_atime = True;
266         }
267
268         if (IS_CTIME_NOW_PATH(conn, &cfg, fname)) {
269                 if (cfg.ctime_slop < 0) {
270                         sbuf->st_ex_btime.tv_sec = INT_MAX - 1;
271                 } else {
272                         GetTimeOfDay(&tv_now);
273                         sbuf->st_ex_btime.tv_sec = tv_now.tv_sec +
274                             cfg.ctime_slop;
275                 }
276         }
277
278         if (!static_mtime && IS_MTIME_NOW_PATH(conn,&cfg,fname)) {
279                 if (cfg.mtime_slop < 0) {
280                         sbuf->st_ex_mtime.tv_sec = INT_MAX - 1;
281                 } else {
282                         if (tv_now.tv_sec == 0)
283                                 GetTimeOfDay(&tv_now);
284                         sbuf->st_ex_mtime.tv_sec = tv_now.tv_sec +
285                             cfg.mtime_slop;
286                 }
287         }
288         if (!static_atime && IS_ATIME_NOW_PATH(conn,&cfg,fname)) {
289                 if (cfg.atime_slop < 0) {
290                         sbuf->st_ex_atime.tv_sec = INT_MAX - 1;
291                 } else {
292                         if (tv_now.tv_sec == 0)
293                                 GetTimeOfDay(&tv_now);
294                         sbuf->st_ex_atime.tv_sec = tv_now.tv_sec +
295                             cfg.atime_slop;
296                 }
297         }
298 }
299
300 static int stat_stream(struct connection_struct *conn, const char *base,
301                        const char *stream, SMB_STRUCT_STAT *sbuf, int flags)
302 {
303         SMB_STRUCT_STAT base_sbuf;
304         int base_fd = -1, dir_fd, ret, saved_errno;
305
306         dir_fd = get_stream_dir_fd(conn, base, &base_fd);
307         if (dir_fd < 0) {
308                 return -1;
309         }
310
311         /* Stat the stream. */
312         ret = onefs_sys_fstat_at(dir_fd, stream, sbuf, flags);
313         if (ret != -1) {
314                 /* Now stat the base file and merge the results. */
315                 ret = onefs_sys_fstat(base_fd, &base_sbuf);
316                 if (ret != -1) {
317                         merge_stat(sbuf, &base_sbuf);
318                 }
319         }
320
321         saved_errno = errno;
322         close(dir_fd);
323         close(base_fd);
324         errno = saved_errno;
325         return ret;
326 }
327
328 int onefs_stat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
329 {
330         struct smb_filename *smb_fname_onefs = NULL;
331         NTSTATUS status;
332         int ret;
333
334         status = onefs_stream_prep_smb_fname(talloc_tos(), smb_fname,
335                                              &smb_fname_onefs);
336         if (!NT_STATUS_IS_OK(status)) {
337                 errno = map_errno_from_nt_status(status);
338                 return -1;
339         }
340
341         /*
342          * If the smb_fname has no stream or is :$DATA, then just stat the
343          * base stream. Otherwise stat the stream.
344          */
345         if (!is_ntfs_stream_smb_fname(smb_fname_onefs)) {
346                 ret = onefs_sys_stat(smb_fname_onefs->base_name,
347                                      &smb_fname->st);
348         } else {
349                 ret = stat_stream(handle->conn, smb_fname_onefs->base_name,
350                                   smb_fname_onefs->stream_name, &smb_fname->st,
351                                   0);
352         }
353
354         onefs_adjust_stat_time(handle->conn, smb_fname->base_name,
355                                &smb_fname->st);
356
357         TALLOC_FREE(smb_fname_onefs);
358
359         return ret;
360 }
361
362 int onefs_fstat(vfs_handle_struct *handle, struct files_struct *fsp,
363                 SMB_STRUCT_STAT *sbuf)
364 {
365         SMB_STRUCT_STAT base_sbuf;
366         int ret;
367
368         /* Stat the stream, by calling next_fstat on the stream's fd. */
369         ret = onefs_sys_fstat(fsp->fh->fd, sbuf);
370         if (ret == -1) {
371                 return ret;
372         }
373
374         /* Stat the base file and merge the results. */
375         if (fsp != NULL && fsp->base_fsp != NULL) {
376                 ret = onefs_sys_fstat(fsp->base_fsp->fh->fd, &base_sbuf);
377                 if (ret != -1) {
378                         merge_stat(sbuf, &base_sbuf);
379                 }
380         }
381
382         onefs_adjust_stat_time(handle->conn, fsp->fsp_name->base_name, sbuf);
383         return ret;
384 }
385
386 int onefs_lstat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
387 {
388         struct smb_filename *smb_fname_onefs = NULL;
389         NTSTATUS status;
390         int ret;
391
392         status = onefs_stream_prep_smb_fname(talloc_tos(), smb_fname,
393                                              &smb_fname_onefs);
394         if (!NT_STATUS_IS_OK(status)) {
395                 errno = map_errno_from_nt_status(status);
396                 return -1;
397         }
398
399         /*
400          * If the smb_fname has no stream or is :$DATA, then just stat the
401          * base stream. Otherwise stat the stream.
402          */
403         if (!is_ntfs_stream_smb_fname(smb_fname_onefs)) {
404                 ret = onefs_sys_lstat(smb_fname_onefs->base_name,
405                                       &smb_fname->st);
406         } else {
407                 ret = stat_stream(handle->conn, smb_fname_onefs->base_name,
408                                   smb_fname_onefs->stream_name, &smb_fname->st,
409                                   AT_SYMLINK_NOFOLLOW);
410         }
411
412         onefs_adjust_stat_time(handle->conn, smb_fname->base_name,
413                                &smb_fname->st);
414
415         TALLOC_FREE(smb_fname_onefs);
416
417         return ret;
418 }
419
420 int onefs_unlink(vfs_handle_struct *handle,
421                  const struct smb_filename *smb_fname)
422 {
423         struct smb_filename *smb_fname_onefs = NULL;
424         int ret;
425         int dir_fd, saved_errno;
426         NTSTATUS status;
427
428         /* Not a stream. */
429         if (!is_ntfs_stream_smb_fname(smb_fname)) {
430                 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
431         }
432
433         status = onefs_stream_prep_smb_fname(talloc_tos(), smb_fname,
434                                              &smb_fname_onefs);
435         if (!NT_STATUS_IS_OK(status)) {
436                 errno = map_errno_from_nt_status(status);
437                 return -1;
438         }
439
440         /* Default stream (the ::$DATA was just stripped off). */
441         if (!is_ntfs_stream_smb_fname(smb_fname_onefs)) {
442                 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_onefs);
443                 goto out;
444         }
445
446         dir_fd = get_stream_dir_fd(handle->conn, smb_fname_onefs->base_name,
447                                    NULL);
448         if (dir_fd < 0) {
449                 ret = -1;
450                 goto out;
451         }
452
453         ret = enc_unlinkat(dir_fd, smb_fname_onefs->stream_name, ENC_DEFAULT,
454                            0);
455
456         saved_errno = errno;
457         close(dir_fd);
458         errno = saved_errno;
459  out:
460         TALLOC_FREE(smb_fname_onefs);
461         return ret;
462 }
463
464 int onefs_vtimes_streams(vfs_handle_struct *handle,
465                          const struct smb_filename *smb_fname,
466                          int flags, struct timespec times[3])
467 {
468         struct smb_filename *smb_fname_onefs = NULL;
469         int ret;
470         int dirfd;
471         int saved_errno;
472         NTSTATUS status;
473
474         START_PROFILE(syscall_ntimes);
475
476         if (!is_ntfs_stream_smb_fname(smb_fname)) {
477                 ret = vtimes(smb_fname->base_name, times, flags);
478                 return ret;
479         }
480
481         status = onefs_stream_prep_smb_fname(talloc_tos(), smb_fname,
482                                              &smb_fname_onefs);
483         if (!NT_STATUS_IS_OK(status)) {
484                 errno = map_errno_from_nt_status(status);
485                 return -1;
486         }
487
488         /* Default stream (the ::$DATA was just stripped off). */
489         if (!is_ntfs_stream_smb_fname(smb_fname_onefs)) {
490                 ret = vtimes(smb_fname_onefs->base_name, times, flags);
491                 goto out;
492         }
493
494         dirfd = get_stream_dir_fd(handle->conn, smb_fname->base_name, NULL);
495         if (dirfd < -1) {
496                 ret = -1;
497                 goto out;
498         }
499
500         ret = enc_vtimesat(dirfd, smb_fname_onefs->stream_name, ENC_DEFAULT,
501                            times, flags);
502
503         saved_errno = errno;
504         close(dirfd);
505         errno = saved_errno;
506
507  out:
508         END_PROFILE(syscall_ntimes);
509         TALLOC_FREE(smb_fname_onefs);
510         return ret;
511 }
512
513 /*
514  * Streaminfo enumeration functionality
515  */
516 struct streaminfo_state {
517         TALLOC_CTX *mem_ctx;
518         vfs_handle_struct *handle;
519         unsigned int num_streams;
520         struct stream_struct *streams;
521         NTSTATUS status;
522 };
523
524 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
525                            struct stream_struct **streams,
526                            const char *name, SMB_OFF_T size,
527                            SMB_OFF_T alloc_size)
528 {
529         struct stream_struct *tmp;
530
531         tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
532                                    (*num_streams)+1);
533         if (tmp == NULL) {
534                 return false;
535         }
536
537         tmp[*num_streams].name = talloc_asprintf(mem_ctx, ":%s:%s", name,
538                                                  "$DATA");
539         if (tmp[*num_streams].name == NULL) {
540                 return false;
541         }
542
543         tmp[*num_streams].size = size;
544         tmp[*num_streams].alloc_size = alloc_size;
545
546         *streams = tmp;
547         *num_streams += 1;
548         return true;
549 }
550
551 static NTSTATUS walk_onefs_streams(connection_struct *conn, files_struct *fsp,
552                                    const char *fname,
553                                    struct streaminfo_state *state,
554                                    SMB_STRUCT_STAT *base_sbuf)
555 {
556         NTSTATUS status = NT_STATUS_OK;
557         bool opened_base_fd = false;
558         int base_fd = -1;
559         int dir_fd = -1;
560         int stream_fd = -1;
561         int ret;
562         SMB_STRUCT_DIR *dirp = NULL;
563         SMB_STRUCT_DIRENT *dp = NULL;
564         files_struct fake_fs;
565         struct fd_handle fake_fh;
566         SMB_STRUCT_STAT stream_sbuf;
567
568         ZERO_STRUCT(fake_fh);
569         ZERO_STRUCT(fake_fs);
570
571         /* If the base file is already open, use its fd. */
572         if ((fsp != NULL) && (fsp->fh->fd != -1)) {
573                 base_fd = fsp->fh->fd;
574         } else {
575                 opened_base_fd = true;
576         }
577
578         dir_fd = get_stream_dir_fd(conn, fname, &base_fd);
579         if (dir_fd < 0) {
580                 return map_nt_error_from_unix(errno);
581         }
582
583         /* Open the ADS directory. */
584         if ((dirp = fdopendir(dir_fd)) == NULL) {
585                 DEBUG(0, ("Error on opendir %s. errno=%d (%s)\n",
586                           fname, errno, strerror(errno)));
587                 status = map_nt_error_from_unix(errno);
588                 goto out;
589         }
590
591         /* Initialize the dir state struct and add it to the list.
592          * This is a layer violation, and really should be handled by a
593          * VFS_FDOPENDIR() call which would properly setup the dir state.
594          * But since this is all within the onefs.so module, we cheat for
595          * now and call directly into the readdirplus code.
596          * NOTE: This state MUST be freed by a proper VFS_CLOSEDIR() call. */
597         ret = onefs_rdp_add_dir_state(conn, dirp);
598         if (ret) {
599                 DEBUG(0, ("Error adding dir_state to the list\n"));
600                 status = map_nt_error_from_unix(errno);
601                 goto out;
602         }
603
604         fake_fs.conn = conn;
605         fake_fs.fh = &fake_fh;
606         status = create_synthetic_smb_fname(talloc_tos(), fname, NULL, NULL,
607                                             &fake_fs.fsp_name);
608         if (!NT_STATUS_IS_OK(status)) {
609                 goto out;
610         }
611
612         /* Iterate over the streams in the ADS directory. */
613         while ((dp = SMB_VFS_READDIR(conn, dirp, NULL)) != NULL) {
614                 /* Skip the "." and ".." entries */
615                 if ((strcmp(dp->d_name, ".") == 0) ||
616                     (strcmp(dp->d_name, "..") == 0))
617                         continue;
618
619                 /* Open actual stream */
620                 if ((stream_fd = onefs_sys_create_file(conn,
621                                                          base_fd,
622                                                          dp->d_name,
623                                                          0,
624                                                          0,
625                                                          0,
626                                                          0,
627                                                          0,
628                                                          0,
629                                                          INTERNAL_OPEN_ONLY,
630                                                          0,
631                                                          NULL,
632                                                          0,
633                                                          NULL)) == -1) {
634                         DEBUG(0, ("Error opening stream %s:%s. "
635                                   "errno=%d (%s)\n", fname, dp->d_name, errno,
636                                   strerror(errno)));
637                         continue;
638                 }
639
640                 /* Figure out the stat info. */
641                 fake_fh.fd = stream_fd;
642                 ret = SMB_VFS_FSTAT(&fake_fs, &stream_sbuf);
643                 close(stream_fd);
644
645                 if (ret) {
646                         DEBUG(0, ("Error fstating stream %s:%s. "
647                                   "errno=%d (%s)\n", fname, dp->d_name, errno,
648                                   strerror(errno)));
649                         continue;
650                 }
651
652                 merge_stat(&stream_sbuf, base_sbuf);
653
654                 if (!add_one_stream(state->mem_ctx,
655                                     &state->num_streams, &state->streams,
656                                     dp->d_name, stream_sbuf.st_ex_size,
657                                     SMB_VFS_GET_ALLOC_SIZE(conn, NULL,
658                                                            &stream_sbuf))) {
659                         state->status = NT_STATUS_NO_MEMORY;
660                         break;
661                 }
662         }
663
664 out:
665         /* Cleanup everything that was opened. */
666         if (dirp != NULL) {
667                 SMB_VFS_CLOSEDIR(conn, dirp);
668         }
669         if (dir_fd >= 0) {
670                 close(dir_fd);
671         }
672         if (opened_base_fd) {
673                 SMB_ASSERT(base_fd >= 0);
674                 close(base_fd);
675         }
676
677         TALLOC_FREE(fake_fs.fsp_name);
678         return status;
679 }
680
681 NTSTATUS onefs_streaminfo(vfs_handle_struct *handle,
682                           struct files_struct *fsp,
683                           const char *fname,
684                           TALLOC_CTX *mem_ctx,
685                           unsigned int *num_streams,
686                           struct stream_struct **streams)
687 {
688         SMB_STRUCT_STAT sbuf;
689         int ret;
690         NTSTATUS status;
691         struct streaminfo_state state;
692
693         /* Get a valid stat. */
694         if ((fsp != NULL) && (fsp->fh->fd != -1)) {
695                 ret = SMB_VFS_FSTAT(fsp, &sbuf);
696         } else {
697                 struct smb_filename *smb_fname = NULL;
698
699                 status = create_synthetic_smb_fname(talloc_tos(), fname, NULL,
700                                                     NULL, &smb_fname);
701                 if (!NT_STATUS_IS_OK(status)) {
702                         return status;
703                 }
704                 ret = SMB_VFS_STAT(handle->conn, smb_fname);
705
706                 sbuf = smb_fname->st;
707
708                 TALLOC_FREE(smb_fname);
709         }
710
711         if (ret == -1) {
712                 return map_nt_error_from_unix(errno);
713         }
714
715         state.streams = NULL;
716         state.num_streams = 0;
717
718         if (lp_parm_bool(SNUM(handle->conn), PARM_ONEFS_TYPE,
719                 PARM_IGNORE_STREAMS, PARM_IGNORE_STREAMS_DEFAULT)) {
720                 goto out;
721         }
722
723         /* Add the default stream. */
724         if (S_ISREG(sbuf.st_ex_mode)) {
725                 if (!add_one_stream(mem_ctx,
726                                     &state.num_streams, &state.streams,
727                                     "", sbuf.st_ex_size,
728                                     SMB_VFS_GET_ALLOC_SIZE(handle->conn, fsp,
729                                                            &sbuf))) {
730                         return NT_STATUS_NO_MEMORY;
731                 }
732         }
733
734         state.mem_ctx = mem_ctx;
735         state.handle = handle;
736         state.status = NT_STATUS_OK;
737
738         /* If there are more streams, add them too. */
739         if (sbuf.st_ex_flags & UF_HASADS) {
740
741                 status = walk_onefs_streams(handle->conn, fsp, fname,
742                     &state, &sbuf);
743
744                 if (!NT_STATUS_IS_OK(status)) {
745                         TALLOC_FREE(state.streams);
746                         return status;
747                 }
748
749                 if (!NT_STATUS_IS_OK(state.status)) {
750                         TALLOC_FREE(state.streams);
751                         return state.status;
752                 }
753         }
754  out:
755         *num_streams = state.num_streams;
756         *streams = state.streams;
757         return NT_STATUS_OK;
758 }