65e4d4cbbcd0f64cc44f1c218e92fa0bf078cc9f
[kamenim/samba-autobuild/.git] / source3 / modules / vfs_unityed_media.c
1 /*
2  * Samba VFS module supporting multiple AVID clients sharing media.
3  *
4  * Copyright (C) 2005  Philip de Nier <philipn@users.sourceforge.net>
5  * Copyright (C) 2012  Andrew Klaassen <clawsoon@yahoo.com>
6  * Copyright (C) 2013  Milos Lukacek
7  * Copyright (C) 2013  Ralph Boehme <slow@samba.org>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301, USA.
23  */
24
25 /*
26  * Unityed Media is a Samba VFS module that allows multiple AVID
27  * clients to share media.
28  *
29  * Add this module to the vfs objects option in your Samba share
30  * configuration.
31  * eg.
32  *
33  *   [avid_win]
34  *      path = /video
35  *      vfs objects = unityed_media
36  *      ...
37  *
38  * It is recommended that you separate out Samba shares for Mac
39  * and Windows clients, and add the following options to the shares
40  * for Windows clients  (NOTE: replace @ with *):
41  *
42  *      veto files = /.DS_Store/._@/.Trash@/.Spotlight@/.hidden/.hotfiles@/.vol/
43  *      delete veto files = yes
44  *
45  * This prevents hidden files from Mac clients interfering with Windows
46  * clients. If you find any more problem hidden files then add them to
47  * the list.
48  *
49  * Notes:
50  * This module is designed to work with AVID editing applications that
51  * look in the Avid MediaFiles or OMFI MediaFiles directory for media.
52  * It is not designed to work as expected in all circumstances for
53  * general use.
54  */
55
56
57 #include "includes.h"
58 #include "system/filesys.h"
59 #include "smbd/smbd.h"
60 #include "../smbd/globals.h"
61 #include "auth.h"
62 #include "../lib/tsocket/tsocket.h"
63 #include <libgen.h>
64
65 #define UM_PARAM_TYPE_NAME "unityed_media"
66
67 static const char *AVID_MXF_DIRNAME = "Avid MediaFiles/MXF";
68 static const size_t AVID_MXF_DIRNAME_LEN = 19;
69 static const char *OMFI_MEDIAFILES_DIRNAME = "OMFI MediaFiles";
70 static const size_t OMFI_MEDIAFILES_DIRNAME_LEN = 15;
71 static const char *APPLE_DOUBLE_PREFIX = "._";
72 static const size_t APPLE_DOUBLE_PREFIX_LEN = 2;
73 static int vfs_um_debug_level = DBGC_VFS;
74
75 enum um_clientid {UM_CLIENTID_NAME, UM_CLIENTID_IP, UM_CLIENTID_HOSTNAME};
76
77 struct um_config_data {
78         enum um_clientid clientid;
79 };
80
81 static const struct enum_list um_clientid[] = {
82         {UM_CLIENTID_NAME, "user"},
83         {UM_CLIENTID_IP, "ip"},
84         {UM_CLIENTID_HOSTNAME, "hostname"},
85         {-1, NULL}
86 };
87
88 /* supplements the directory list stream */
89 typedef struct um_dirinfo_struct {
90         DIR* dirstream;
91         char *dirpath;
92         char *clientPath;
93         bool isInMediaFiles;
94         char *clientSubDirname;
95 } um_dirinfo_struct;
96
97 /**
98  * Returns true and first group of digits in path, false and 0 otherwise
99  **/
100 static bool get_digit_group(const char *path, uintmax_t *digit)
101 {
102         const char *p = path;
103         char *endp = NULL;
104         codepoint_t cp;
105         size_t size;
106
107         DEBUG(10, ("get_digit_group entering with path '%s'\n",
108                    path));
109
110         /*
111          * Delibiretly initialize to 0 because callers use this result
112          * even though the string doesn't contain any number and we
113          * returned false
114          */
115         *digit = 0;
116
117         while (*p) {
118                 cp = next_codepoint(p, &size);
119                 if (cp == -1) {
120                         return false;
121                 }
122                 if ((size == 1) && (isdigit(cp))) {
123                         *digit = (uintmax_t)strtoul(p, &endp, 10);
124                         DEBUG(10, ("num_suffix = '%ju'\n",
125                                    *digit));
126                         return true;
127                 }
128                 p += size;
129         }
130
131         return false;
132 }
133
134 /* Add "_<remote_name>.<number>" suffix to path or filename.
135  *
136  * Success: return 0
137  * Failure: set errno, path NULL, return -1
138  */
139
140 static int alloc_append_client_suffix(vfs_handle_struct *handle,
141                                       char **path)
142 {
143         int status = 0;
144         uintmax_t number;
145         const char *clientid;
146         struct um_config_data *config;
147
148         DEBUG(10, ("Entering with path '%s'\n", *path));
149
150         SMB_VFS_HANDLE_GET_DATA(handle, config,
151                                 struct um_config_data,
152                                 return -1);
153
154         (void)get_digit_group(*path, &number);
155
156         switch (config->clientid) {
157
158         case UM_CLIENTID_IP:
159                 clientid = tsocket_address_inet_addr_string(
160                         handle->conn->sconn->remote_address, talloc_tos());
161                 if (clientid == NULL) {
162                         errno = ENOMEM;
163                         status = -1;
164                         goto err;
165                 }
166                 break;
167
168         case UM_CLIENTID_HOSTNAME:
169                 clientid = get_remote_machine_name();
170                 break;
171
172         case UM_CLIENTID_NAME:
173         default:
174                 clientid = get_current_username();
175                 break;
176         }
177
178         *path = talloc_asprintf_append(*path, "_%s.%ju",
179                                        clientid, number);
180         if (*path == NULL) {
181                 DEBUG(1, ("alloc_append_client_suffix "
182                                      "out of memory\n"));
183                 errno = ENOMEM;
184                 status = -1;
185                 goto err;
186         }
187         DEBUG(10, ("Leaving with *path '%s'\n", *path));
188 err:
189         return status;
190 }
191
192 /* Returns true if the file or directory begins with the appledouble
193  * prefix.
194  */
195 static bool is_apple_double(const char* fname)
196 {
197         bool ret = false;
198
199         DEBUG(10, ("Entering with fname '%s'\n", fname));
200
201         if (strnequal(APPLE_DOUBLE_PREFIX, fname, APPLE_DOUBLE_PREFIX_LEN)) {
202                 ret = true;
203         }
204         DEBUG(10, ("Leaving with ret '%s'\n",
205                               ret == true ? "true" : "false"));
206         return ret;
207 }
208
209 static bool starts_with_media_dir(const char* media_dirname,
210                                   size_t media_dirname_len,
211                                   const char *path)
212 {
213         bool ret = false;
214         const char *path_start = path;
215
216         DEBUG(10, ("Entering with media_dirname '%s' "
217                               "path '%s'\n", media_dirname, path));
218
219         /* Sometimes Samba gives us "./OMFI MediaFiles". */
220         if (strnequal(path, "./", 2)) {
221                 path_start += 2;
222         }
223
224         if (strnequal(media_dirname, path_start, media_dirname_len)
225             &&
226             ((path_start[media_dirname_len] == '\0') ||
227              (path_start[media_dirname_len] == '/'))) {
228                 ret = true;
229         }
230
231         DEBUG(10, ("Leaving with ret '%s'\n",
232                               ret == true ? "true" : "false"));
233         return ret;
234 }
235
236 /*
237  * Returns true if the file or directory referenced by the path is ONE
238  * LEVEL below the AVID_MXF_DIRNAME or OMFI_MEDIAFILES_DIRNAME
239  * directory
240  */
241 static bool is_in_media_dir(const char *path)
242 {
243         int transition_count = 0;
244         const char *path_start = path;
245         const char *p;
246         const char *media_dirname;
247         size_t media_dirname_len;
248
249         DEBUG(10, ("Entering with path'%s' ", path));
250
251         /* Sometimes Samba gives us "./OMFI MediaFiles". */
252         if (strnequal(path, "./", 2)) {
253                 path_start += 2;
254         }
255
256         if (strnequal(path_start, AVID_MXF_DIRNAME, AVID_MXF_DIRNAME_LEN)) {
257                 media_dirname = AVID_MXF_DIRNAME;
258                 media_dirname_len = AVID_MXF_DIRNAME_LEN;
259         } else if (strnequal(path_start,
260                              OMFI_MEDIAFILES_DIRNAME,
261                              OMFI_MEDIAFILES_DIRNAME_LEN)) {
262                 media_dirname = OMFI_MEDIAFILES_DIRNAME;
263                 media_dirname_len = OMFI_MEDIAFILES_DIRNAME_LEN;
264         } else {
265                 return false;
266         }
267
268         if (path_start[media_dirname_len] == '\0') {
269                 goto out;
270         }
271
272         p = path_start + media_dirname_len + 1;
273
274         while (true) {
275                 if (*p == '\0' || *p == '/') {
276                         if (strnequal(p - 3, "/..", 3)) {
277                                 transition_count--;
278                         } else if ((p[-1] != '/') || !strnequal(p - 2, "/.", 2)) {
279                                 transition_count++;
280                         }
281                 }
282                 if (*p == '\0') {
283                         break;
284                 }
285                 p++;
286         }
287
288 out:
289         DEBUG(10, ("Going out with transition_count '%i'\n",
290                               transition_count));
291         if (((transition_count == 1) && (media_dirname == AVID_MXF_DIRNAME))
292             ||
293             ((transition_count == 0) && (media_dirname == OMFI_MEDIAFILES_DIRNAME))) {
294                 return true;
295         }
296         else return false;
297 }
298
299 /*
300  * Returns true if the file or directory referenced by the path is
301  * below the AVID_MEDIAFILES_DIRNAME or OMFI_MEDIAFILES_DIRNAME
302  * directory The AVID_MEDIAFILES_DIRNAME and OMFI_MEDIAFILES_DIRNAME
303  * are assumed to be in the root directory, which is generally a safe
304  * assumption in the fixed-path world of Avid.
305  */
306 static bool is_in_media_files(const char *path)
307 {
308         bool ret = false;
309
310         DEBUG(10, ("Entering with path '%s'\n", path));
311
312         if (starts_with_media_dir(AVID_MXF_DIRNAME,
313                                   AVID_MXF_DIRNAME_LEN, path) ||
314             starts_with_media_dir(OMFI_MEDIAFILES_DIRNAME,
315                                   OMFI_MEDIAFILES_DIRNAME_LEN, path)) {
316                 ret = true;
317         }
318         DEBUG(10, ("Leaving with ret '%s'\n",
319                               ret == true ? "true" : "false"));
320         return ret;
321 }
322
323
324 /* Add client suffix to "pure-number" path.
325  *
326  * Caller must free newPath.
327  *
328  * Success: return 0
329  * Failure: set errno, newPath NULL, return -1
330  */
331 static int alloc_get_client_path(vfs_handle_struct *handle,
332                                  TALLOC_CTX *ctx,
333                                  const char *path_in,
334                                  char **path_out)
335 {
336         int status = 0;
337         char *p;
338         char *digits;
339         size_t digits_len;
340         uintmax_t number;
341
342         *path_out = talloc_strdup(ctx, path_in);
343         if (*path_out == NULL) {
344                 DEBUG(1, ("alloc_get_client_path ENOMEM\n"));
345                 return -1;
346         }
347
348         (void)get_digit_group(*path_out, &number);
349
350         digits = talloc_asprintf(NULL, "%ju", number);
351         if (digits == NULL) {
352                 DEBUG(1, ("alloc_get_client_path ENOMEM\n"));
353                 return -1;
354         }
355         digits_len = strlen(digits);
356
357         p = strstr_m(path_in, digits);
358         if ((p)
359             &&
360             ((p[digits_len] == '\0') || (p[digits_len] == '/'))
361             &&
362             (((p - path_in > 0) && (p[-1] == '/'))
363              ||
364              (((p - path_in) > APPLE_DOUBLE_PREFIX_LEN)
365               &&
366               is_apple_double(p - APPLE_DOUBLE_PREFIX_LEN)
367               &&
368               (p[-(APPLE_DOUBLE_PREFIX_LEN + 1)] == '/'))))
369         {
370                 (*path_out)[p - path_in + digits_len] = '\0';
371
372                 status = alloc_append_client_suffix(handle, path_out);
373                 if (status != 0) {
374                         goto out;
375                 }
376
377                 *path_out = talloc_strdup_append(*path_out, p + digits_len);
378                 if (*path_out == NULL) {
379                         DEBUG(1, ("alloc_get_client_path ENOMEM\n"));
380                         status = -1;
381                         goto out;
382                 }
383         }
384 out:
385         /* path_out must be freed in caller. */
386         DEBUG(10, ("Result:'%s'\n", *path_out));
387         return status;
388 }
389
390 /*
391  * Success: return 0
392  * Failure: set errno, return -1
393  */
394 static int alloc_get_client_smb_fname(struct vfs_handle_struct *handle,
395                                       TALLOC_CTX *ctx,
396                                       const struct smb_filename *smb_fname,
397                                       struct smb_filename **client_fname)
398 {
399         int status ;
400
401         DEBUG(10, ("Entering with smb_fname->base_name '%s'\n",
402                    smb_fname->base_name));
403
404         *client_fname = cp_smb_filename(ctx, smb_fname);
405         if (*client_fname == NULL) {
406                 DEBUG(1, ("cp_smb_filename returned NULL\n"));
407                 return -1;
408         }
409         status = alloc_get_client_path(handle, ctx,
410                                        smb_fname->base_name,
411                                        &(*client_fname)->base_name);
412         if (status != 0) {
413                 return -1;
414         }
415
416         DEBUG(10, ("Leaving with (*client_fname)->base_name "
417                    "'%s'\n", (*client_fname)->base_name));
418
419         return 0;
420 }
421
422
423 /*
424  * Success: return 0
425  * Failure: set errno, return -1
426  */
427 static int alloc_set_client_dirinfo_path(struct vfs_handle_struct *handle,
428                                          TALLOC_CTX *ctx,
429                                          char **path,
430                                          const char *suffix_number)
431 {
432         int status;
433
434         DEBUG(10, ("Entering with suffix_number '%s'\n",
435                    suffix_number));
436
437         *path = talloc_strdup(ctx, suffix_number);
438         if (*path == NULL) {
439                 DEBUG(1, ("alloc_set_client_dirinfo_path ENOMEM\n"));
440                 return -1;
441         }
442         status = alloc_append_client_suffix(handle, path);
443         if (status != 0) {
444                 return -1;
445         }
446
447         DEBUG(10, ("Leaving with *path '%s'\n", *path));
448
449         return 0;
450 }
451
452 static int alloc_set_client_dirinfo(vfs_handle_struct *handle,
453                                     const char *fname,
454                                     struct um_dirinfo_struct **di_result)
455 {
456         int status = 0;
457         char *digits;
458         uintmax_t number;
459         struct um_dirinfo_struct *dip;
460
461         DEBUG(10, ("Entering with fname '%s'\n", fname));
462
463         *di_result = talloc(NULL, struct um_dirinfo_struct);
464         if (*di_result == NULL) {
465                 goto err;
466         }
467         dip = *di_result;
468
469         dip->dirpath = talloc_strdup(dip, fname);
470         if (dip->dirpath == NULL) {
471                 goto err;
472         }
473
474         if (!is_in_media_files(fname)) {
475                 dip->isInMediaFiles = false;
476                 dip->clientPath = NULL;
477                 dip->clientSubDirname = NULL;
478                 goto out;
479         }
480
481         dip->isInMediaFiles = true;
482
483         (void)get_digit_group(fname, &number);
484         digits = talloc_asprintf(talloc_tos(), "%ju", number);
485         if (digits == NULL) {
486                 goto err;
487         }
488
489         status = alloc_set_client_dirinfo_path(handle, dip,
490                                                &dip->clientSubDirname,
491                                                digits);
492         if (status != 0) {
493                 goto err;
494         }
495
496         status = alloc_get_client_path(handle, dip, fname,
497                                        &dip->clientPath);
498         if (status != 0 || dip->clientPath == NULL) {
499                 goto err;
500         }
501
502 out:
503         DEBUG(10, ("Leaving with (*dirInfo)->dirpath '%s', "
504                               "(*dirInfo)->clientPath '%s'\n",
505                               dip->dirpath, dip->clientPath));
506         return status;
507
508 err:
509         DEBUG(1, ("Failing with fname '%s'\n", fname));
510         TALLOC_FREE(*di_result);
511         status = -1;
512         errno = ENOMEM;
513         return status;
514 }
515
516 /**********************************************************************
517  * VFS functions
518  **********************************************************************/
519
520 /*
521  * Success: return 0
522  * Failure: set errno, return -1
523  */
524 static int um_statvfs(struct vfs_handle_struct *handle,
525                       const char *path,
526                       struct vfs_statvfs_struct *statbuf)
527 {
528         int status;
529         char *clientPath = NULL;
530
531         DEBUG(10, ("Entering with path '%s'\n", path));
532
533         if (!is_in_media_files(path)) {
534                 return SMB_VFS_NEXT_STATVFS(handle, path, statbuf);
535         }
536
537         status = alloc_get_client_path(handle, talloc_tos(),
538                                        path, &clientPath);
539         if (status != 0) {
540                 goto err;
541         }
542
543         status = SMB_VFS_NEXT_STATVFS(handle, clientPath, statbuf);
544 err:
545         TALLOC_FREE(clientPath);
546         DEBUG(10, ("Leaving with path '%s'\n", path));
547         return status;
548 }
549
550 /* Success: return a um_dirinfo_struct cast as a DIR
551  * Failure: set errno, return NULL
552  */
553 static DIR *um_opendir(vfs_handle_struct *handle,
554                        const struct smb_filename *smb_fname,
555                        const char *mask,
556                        uint32_t attr)
557 {
558         struct um_dirinfo_struct *dirInfo;
559
560         DEBUG(10, ("Entering with fname '%s'\n", smb_fname->base_name));
561
562         if (alloc_set_client_dirinfo(handle, smb_fname->base_name, &dirInfo)) {
563                 goto err;
564         }
565
566         if (!dirInfo->isInMediaFiles) {
567                 dirInfo->dirstream = SMB_VFS_NEXT_OPENDIR(
568                         handle, smb_fname, mask, attr);
569         } else {
570                 struct smb_filename *client_smb_fname =
571                         synthetic_smb_fname(talloc_tos(),
572                                         dirInfo->clientPath,
573                                         NULL,
574                                         NULL);
575                 if (client_smb_fname == NULL) {
576                         goto err;
577                 }
578
579                 dirInfo->dirstream = SMB_VFS_NEXT_OPENDIR(
580                         handle, client_smb_fname, mask, attr);
581
582                 TALLOC_FREE(client_smb_fname);
583         }
584
585         if (dirInfo->dirstream == NULL) {
586                 goto err;
587         }
588
589         DEBUG(10, ("Leaving with dirInfo->dirpath '%s', "
590                               "dirInfo->clientPath '%s'\n",
591                               dirInfo->dirpath,
592                               dirInfo->clientPath));
593         return (DIR*)dirInfo;
594
595 err:
596         DEBUG(1, ("Failing with fname '%s'\n", smb_fname->base_name));
597         TALLOC_FREE(dirInfo);
598         return NULL;
599 }
600
601 static DIR *um_fdopendir(vfs_handle_struct *handle,
602                          files_struct *fsp,
603                          const char *mask,
604                          uint32_t attr)
605 {
606         struct um_dirinfo_struct *dirInfo = NULL;
607         DIR *dirstream;
608
609         DEBUG(10, ("Entering with fsp->fsp_name->base_name '%s'\n",
610                    fsp->fsp_name->base_name));
611
612         dirstream = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask, attr);
613         if (!dirstream) {
614                 goto err;
615         }
616
617         if (alloc_set_client_dirinfo(handle,
618                                      fsp->fsp_name->base_name,
619                                      &dirInfo)) {
620                 goto err;
621         }
622
623         dirInfo->dirstream = dirstream;
624
625         if (!dirInfo->isInMediaFiles) {
626                 /*
627                  * FIXME: this is the original code, something must be
628                  * missing here, but what? -slow
629                  */
630                 goto out;
631         }
632
633 out:
634         DEBUG(10, ("Leaving with dirInfo->dirpath '%s', "
635                    "dirInfo->clientPath '%s', "
636                    "fsp->fsp_name->st.st_ex_mtime %s",
637                    dirInfo->dirpath,
638                    dirInfo->clientPath,
639                    ctime(&(fsp->fsp_name->st.st_ex_mtime.tv_sec))));
640         return (DIR *) dirInfo;
641
642 err:
643         DEBUG(1, ("Failing with fsp->fsp_name->base_name '%s'\n",
644                   fsp->fsp_name->base_name));
645         TALLOC_FREE(dirInfo);
646         return NULL;
647 }
648
649 /*
650  * skip own suffixed directory
651  * replace own suffixed directory with non suffixed.
652  *
653  * Success: return dirent
654  * End of data: return NULL
655  * Failure: set errno, return NULL
656  */
657 static struct dirent *um_readdir(vfs_handle_struct *handle,
658                                  DIR *dirp,
659                                  SMB_STRUCT_STAT *sbuf)
660 {
661         um_dirinfo_struct* dirInfo = (um_dirinfo_struct*)dirp;
662         struct dirent *d = NULL;
663         int skip;
664
665         DEBUG(10, ("dirInfo->dirpath '%s', "
666                    "dirInfo->clientPath '%s', "
667                    "dirInfo->isInMediaFiles '%s', "
668                    "dirInfo->clientSubDirname '%s'\n",
669                    dirInfo->dirpath,
670                    dirInfo->clientPath,
671                    dirInfo->isInMediaFiles ? "true" : "false",
672                    dirInfo->clientSubDirname));
673
674         if (!dirInfo->isInMediaFiles) {
675                 return SMB_VFS_NEXT_READDIR(handle, dirInfo->dirstream, sbuf);
676         }
677
678         do {
679                 const char* dname;
680                 bool isAppleDouble;
681                 char *digits;
682                 size_t digits_len;
683                 uintmax_t number;
684
685                 skip = false;
686                 d = SMB_VFS_NEXT_READDIR(handle, dirInfo->dirstream, sbuf);
687
688                 if (d == NULL) {
689                         break;
690                 }
691
692                 /* ignore apple double prefix for logic below */
693                 if (is_apple_double(d->d_name)) {
694                         dname = &d->d_name[APPLE_DOUBLE_PREFIX_LEN];
695                         isAppleDouble = true;
696                 } else {
697                         dname = d->d_name;
698                         isAppleDouble = false;
699                 }
700
701                 DEBUG(10, ("dname = '%s'\n", dname));
702
703                 (void)get_digit_group(dname, &number);
704                 digits = talloc_asprintf(talloc_tos(), "%ju", number);
705                 if (digits == NULL) {
706                         DEBUG(1, ("out of memory"));
707                         goto err;
708                 }
709                 digits_len = strlen(digits);
710
711                 if (alloc_set_client_dirinfo_path(handle,
712                                                   dirInfo,
713                                                   &((dirInfo)->clientSubDirname),
714                                                   digits)) {
715                         goto err;
716                 }
717
718                 /*
719                  * If set to "true", vfs shows digits-only
720                  * non-suffixed subdirectories.  Normally, such
721                  * subdirectories can exists only in non-media
722                  * directories, so we set it to "false".  Otherwise,
723                  * if we have such subdirectories (probably created
724                  * over not "unityed" connection), it can be little
725                  * bit confusing.
726                  */
727                 if (strequal(dname, digits)) {
728                         skip = false;
729                 } else if (strequal(dname, dirInfo->clientSubDirname)) {
730                         /*
731                          * Remove suffix of this client's suffixed
732                          * subdirectories
733                          */
734                         if (isAppleDouble) {
735                                 d->d_name[digits_len + APPLE_DOUBLE_PREFIX_LEN] = '\0';
736                         } else {
737                                 d->d_name[digits_len] = '\0';
738                         }
739                 } else if (strnequal(digits, dname, digits_len)) {
740                         /*
741                          * Set to false to see another clients subdirectories
742                          */
743                         skip = false;
744                 }
745         } while (skip);
746
747         DEBUG(10, ("Leaving um_readdir\n"));
748         return d;
749 err:
750         TALLOC_FREE(dirInfo);
751         return NULL;
752 }
753
754 static void um_seekdir(vfs_handle_struct *handle,
755                        DIR *dirp,
756                        long offset)
757 {
758         DEBUG(10, ("Entering and leaving um_seekdir\n"));
759         SMB_VFS_NEXT_SEEKDIR(handle,
760                              ((um_dirinfo_struct*)dirp)->dirstream, offset);
761 }
762
763 static long um_telldir(vfs_handle_struct *handle,
764                        DIR *dirp)
765 {
766         DEBUG(10, ("Entering and leaving um_telldir\n"));
767         return SMB_VFS_NEXT_TELLDIR(handle,
768                                     ((um_dirinfo_struct*)dirp)->dirstream);
769 }
770
771 static void um_rewinddir(vfs_handle_struct *handle,
772                          DIR *dirp)
773 {
774         DEBUG(10, ("Entering and leaving um_rewinddir\n"));
775         SMB_VFS_NEXT_REWINDDIR(handle,
776                                ((um_dirinfo_struct*)dirp)->dirstream);
777 }
778
779 static int um_mkdir(vfs_handle_struct *handle,
780                     const struct smb_filename *smb_fname,
781                     mode_t mode)
782 {
783         int status;
784         const char *path = smb_fname->base_name;
785         struct smb_filename *client_fname = NULL;
786
787         DEBUG(10, ("Entering with path '%s'\n", path));
788
789         if (!is_in_media_files(path) || !is_in_media_dir(path)) {
790                 return SMB_VFS_NEXT_MKDIR(handle, smb_fname, mode);
791         }
792
793         status = alloc_get_client_smb_fname(handle,
794                                 talloc_tos(),
795                                 smb_fname,
796                                 &client_fname);
797         if (status != 0) {
798                 goto err;
799         }
800
801         status = SMB_VFS_NEXT_MKDIR(handle, client_fname, mode);
802 err:
803         TALLOC_FREE(client_fname);
804         DEBUG(10, ("Leaving with path '%s'\n", path));
805         return status;
806 }
807
808 static int um_rmdir(vfs_handle_struct *handle,
809                     const struct smb_filename *smb_fname)
810 {
811         int status;
812         const char *path = smb_fname->base_name;
813         struct smb_filename *client_fname = NULL;
814
815         DEBUG(10, ("Entering with path '%s'\n", path));
816
817         if (!is_in_media_files(path)) {
818                 return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
819         }
820
821         status = alloc_get_client_smb_fname(handle,
822                                 talloc_tos(),
823                                 smb_fname,
824                                 &client_fname);
825         if (status != 0) {
826                 goto err;
827         }
828
829         status = SMB_VFS_NEXT_RMDIR(handle, client_fname);
830 err:
831         TALLOC_FREE(client_fname);
832         DEBUG(10, ("Leaving with path '%s'\n", path));
833         return status;
834 }
835
836 static int um_closedir(vfs_handle_struct *handle,
837                        DIR *dirp)
838 {
839         DIR *realdirp = ((um_dirinfo_struct*)dirp)->dirstream;
840
841         TALLOC_FREE(dirp);
842
843         return SMB_VFS_NEXT_CLOSEDIR(handle, realdirp);
844 }
845
846 static void um_init_search_op(vfs_handle_struct *handle,
847                               DIR *dirp)
848 {
849         DEBUG(10, ("Entering and leaving um_init_search_op\n"));
850
851         SMB_VFS_NEXT_INIT_SEARCH_OP(handle,
852                                     ((um_dirinfo_struct*)dirp)->dirstream);
853 }
854
855 static int um_open(vfs_handle_struct *handle,
856                    struct smb_filename *smb_fname,
857                    files_struct *fsp,
858                    int flags,
859                    mode_t mode)
860 {
861         int ret;
862         struct smb_filename *client_fname = NULL;
863
864         DEBUG(10, ("Entering with smb_fname->base_name '%s'\n",
865                               smb_fname->base_name));
866
867         if (!is_in_media_files(smb_fname->base_name)) {
868                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
869         }
870
871         if (alloc_get_client_smb_fname(handle, talloc_tos(),
872                                        smb_fname,
873                                        &client_fname)) {
874                 ret = -1;
875                 goto err;
876         }
877
878         /*
879          * FIXME:
880          * What about fsp->fsp_name?  We also have to get correct stat
881          * info into fsp and smb_fname for DB files, don't we?
882          */
883
884         DEBUG(10, ("Leaving with smb_fname->base_name '%s' "
885                    "smb_fname->st.st_ex_mtime %s"
886                    "fsp->fsp_name->st.st_ex_mtime %s",
887                               smb_fname->base_name,
888                               ctime(&(smb_fname->st.st_ex_mtime.tv_sec)),
889                               ctime(&(fsp->fsp_name->st.st_ex_mtime.tv_sec))));
890
891         ret = SMB_VFS_NEXT_OPEN(handle, client_fname, fsp, flags, mode);
892 err:
893         TALLOC_FREE(client_fname);
894         DEBUG(10, ("Leaving with smb_fname->base_name '%s'\n",
895                               smb_fname->base_name));
896         return ret;
897 }
898
899 static NTSTATUS um_create_file(vfs_handle_struct *handle,
900                                struct smb_request *req,
901                                uint16_t root_dir_fid,
902                                struct smb_filename *smb_fname,
903                                uint32_t access_mask,
904                                uint32_t share_access,
905                                uint32_t create_disposition,
906                                uint32_t create_options,
907                                uint32_t file_attributes,
908                                uint32_t oplock_request,
909                                struct smb2_lease *lease,
910                                uint64_t allocation_size,
911                                uint32_t private_flags,
912                                struct security_descriptor *sd,
913                                struct ea_list *ea_list,
914                                files_struct **result_fsp,
915                                int *pinfo,
916                                const struct smb2_create_blobs *in_context_blobs,
917                                struct smb2_create_blobs *out_context_blobs)
918 {
919         NTSTATUS status;
920         struct smb_filename *client_fname = NULL;
921
922         DEBUG(10, ("Entering with smb_fname->base_name '%s'\n",
923                    smb_fname->base_name));
924
925         if (!is_in_media_files(smb_fname->base_name)) {
926                 return SMB_VFS_NEXT_CREATE_FILE(
927                         handle,
928                         req,
929                         root_dir_fid,
930                         smb_fname,
931                         access_mask,
932                         share_access,
933                         create_disposition,
934                         create_options,
935                         file_attributes,
936                         oplock_request,
937                         lease,
938                         allocation_size,
939                         private_flags,
940                         sd,
941                         ea_list,
942                         result_fsp,
943                         pinfo,
944                         in_context_blobs,
945                         out_context_blobs);
946         }
947
948         if (alloc_get_client_smb_fname(handle, talloc_tos(),
949                                        smb_fname,
950                                        &client_fname)) {
951                 status = map_nt_error_from_unix(errno);
952                 goto err;
953         }
954
955         /*
956          * FIXME:
957          * This only creates files, so we don't have to worry about
958          * our fake directory stat'ing here.  But we still need to
959          * route stat calls for DB files properly, right?
960          */
961         status = SMB_VFS_NEXT_CREATE_FILE(
962                 handle,
963                 req,
964                 root_dir_fid,
965                 client_fname,
966                 access_mask,
967                 share_access,
968                 create_disposition,
969                 create_options,
970                 file_attributes,
971                 oplock_request,
972                 lease,
973                 allocation_size,
974                 private_flags,
975                 sd,
976                 ea_list,
977                 result_fsp,
978                 pinfo,
979                 in_context_blobs,
980                 out_context_blobs);
981 err:
982         TALLOC_FREE(client_fname);
983         DEBUG(10, ("Leaving with smb_fname->base_name '%s'"
984                    "smb_fname->st.st_ex_mtime %s"
985                    " fsp->fsp_name->st.st_ex_mtime %s",
986                    smb_fname->base_name,
987                    ctime(&(smb_fname->st.st_ex_mtime.tv_sec)),
988                    (*result_fsp) && VALID_STAT((*result_fsp)->fsp_name->st) ?
989                    ctime(&((*result_fsp)->fsp_name->st.st_ex_mtime.tv_sec)) :
990                    "No fsp time\n"));
991         return status;
992 }
993
994 static int um_rename(vfs_handle_struct *handle,
995                      const struct smb_filename *smb_fname_src,
996                      const struct smb_filename *smb_fname_dst)
997 {
998         int status;
999         struct smb_filename *src_client_fname = NULL;
1000         struct smb_filename *dst_client_fname = NULL;
1001
1002         DEBUG(10, ("Entering with "
1003                    "smb_fname_src->base_name '%s', "
1004                    "smb_fname_dst->base_name '%s'\n",
1005                    smb_fname_src->base_name,
1006                    smb_fname_dst->base_name));
1007
1008         if (!is_in_media_files(smb_fname_src->base_name)
1009             &&
1010             !is_in_media_files(smb_fname_dst->base_name)) {
1011                 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
1012                                            smb_fname_dst);
1013         }
1014
1015         status = alloc_get_client_smb_fname(handle, talloc_tos(),
1016                                             smb_fname_src,
1017                                             &src_client_fname);
1018         if (status != 0) {
1019                 goto err;
1020         }
1021
1022         status = alloc_get_client_smb_fname(handle, talloc_tos(),
1023                                             smb_fname_dst,
1024                                             &dst_client_fname);
1025
1026         if (status != 0) {
1027                 goto err;
1028         }
1029
1030         status = SMB_VFS_NEXT_RENAME(handle, src_client_fname,
1031                                      dst_client_fname);
1032 err:
1033         TALLOC_FREE(dst_client_fname);
1034         TALLOC_FREE(src_client_fname);
1035         DEBUG(10, ("Leaving with smb_fname_src->base_name '%s',"
1036                    " smb_fname_dst->base_name '%s'\n",
1037                    smb_fname_src->base_name,
1038                    smb_fname_dst->base_name));
1039         return status;
1040 }
1041
1042 /*
1043  * Success: return 0
1044  * Failure: set errno, return -1
1045  */
1046 static int um_stat(vfs_handle_struct *handle,
1047                    struct smb_filename *smb_fname)
1048 {
1049         int status = 0;
1050         struct smb_filename *client_fname = NULL;
1051
1052         DEBUG(10, ("Entering with smb_fname->base_name '%s'\n",
1053                    smb_fname->base_name));
1054
1055         if (!is_in_media_files(smb_fname->base_name)) {
1056                 return SMB_VFS_NEXT_STAT(handle, smb_fname);
1057         }
1058
1059         status = alloc_get_client_smb_fname(handle, talloc_tos(),
1060                                             smb_fname,
1061                                             &client_fname);
1062         if (status != 0) {
1063                 goto err;
1064         }
1065         DEBUG(10, ("Stat'ing client_fname->base_name '%s'\n",
1066                    client_fname->base_name));
1067
1068         status = SMB_VFS_NEXT_STAT(handle, client_fname);
1069         if (status != 0) {
1070                 goto err;
1071         }
1072
1073         /*
1074          * Unlike functions with const smb_filename, we have to modify
1075          * smb_fname itself to pass our info back up.
1076          */
1077         DEBUG(10, ("Setting smb_fname '%s' stat from client_fname '%s'\n",
1078                    smb_fname->base_name, client_fname->base_name));
1079         smb_fname->st = client_fname->st;
1080
1081 err:
1082         TALLOC_FREE(client_fname);
1083         DEBUG(10, ("Leaving with smb_fname->st.st_ex_mtime %s",
1084                    ctime(&(smb_fname->st.st_ex_mtime.tv_sec))));
1085         return status;
1086 }
1087
1088 static int um_lstat(vfs_handle_struct *handle,
1089                     struct smb_filename *smb_fname)
1090 {
1091         int status = 0;
1092         struct smb_filename *client_fname = NULL;
1093
1094         DEBUG(10, ("Entering with smb_fname->base_name '%s'\n",
1095                    smb_fname->base_name));
1096
1097         if (!is_in_media_files(smb_fname->base_name)) {
1098                 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1099         }
1100
1101         client_fname = NULL;
1102
1103         status = alloc_get_client_smb_fname(handle, talloc_tos(),
1104                                             smb_fname,
1105                                             &client_fname);
1106         if (status != 0) {
1107                 goto err;
1108         }
1109         status = SMB_VFS_NEXT_LSTAT(handle, client_fname);
1110         if (status != 0) {
1111                 goto err;
1112         }
1113
1114         smb_fname->st = client_fname->st;
1115
1116 err:
1117         TALLOC_FREE(client_fname);
1118         DEBUG(10, ("Leaving with smb_fname->st.st_ex_mtime %s",
1119                    ctime(&(smb_fname->st.st_ex_mtime.tv_sec))));
1120         return status;
1121 }
1122
1123 static int um_fstat(vfs_handle_struct *handle,
1124                     files_struct *fsp, SMB_STRUCT_STAT *sbuf)
1125 {
1126         int status = 0;
1127
1128         DEBUG(10, ("Entering with fsp->fsp_name->base_name "
1129                    "'%s'\n", fsp_str_dbg(fsp)));
1130
1131         status = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1132         if (status != 0) {
1133                 goto out;
1134         }
1135
1136         if ((fsp->fsp_name == NULL) ||
1137             !is_in_media_files(fsp->fsp_name->base_name)) {
1138                 goto out;
1139         }
1140
1141         status = um_stat(handle, fsp->fsp_name);
1142         if (status != 0) {
1143                 goto out;
1144         }
1145
1146         *sbuf = fsp->fsp_name->st;
1147
1148 out:
1149         DEBUG(10, ("Leaving with fsp->fsp_name->st.st_ex_mtime %s\n",
1150                    fsp->fsp_name != NULL ?
1151                    ctime(&(fsp->fsp_name->st.st_ex_mtime.tv_sec)) : "0"));
1152         return status;
1153 }
1154
1155 static int um_unlink(vfs_handle_struct *handle,
1156                      const struct smb_filename *smb_fname)
1157 {
1158         int status;
1159         struct smb_filename *client_fname = NULL;
1160
1161         DEBUG(10, ("Entering um_unlink\n"));
1162
1163         if (!is_in_media_files(smb_fname->base_name)) {
1164                 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
1165         }
1166
1167         status = alloc_get_client_smb_fname(handle, talloc_tos(),
1168                                             smb_fname,
1169                                             &client_fname);
1170         if (status != 0) {
1171                 goto err;
1172         }
1173
1174         status = SMB_VFS_NEXT_UNLINK(handle, client_fname);
1175
1176 err:
1177         TALLOC_FREE(client_fname);
1178         return status;
1179 }
1180
1181 static int um_chmod(vfs_handle_struct *handle,
1182                         const struct smb_filename *smb_fname,
1183                         mode_t mode)
1184 {
1185         int status;
1186         struct smb_filename *client_fname = NULL;
1187
1188         DEBUG(10, ("Entering um_chmod\n"));
1189
1190         if (!is_in_media_files(smb_fname->base_name)) {
1191                 return SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
1192         }
1193
1194         status = alloc_get_client_smb_fname(handle,
1195                                 talloc_tos(),
1196                                 smb_fname,
1197                                 &client_fname);
1198         if (status != 0) {
1199                 goto err;
1200         }
1201
1202         status = SMB_VFS_NEXT_CHMOD(handle, client_fname, mode);
1203
1204 err:
1205         TALLOC_FREE(client_fname);
1206         return status;
1207 }
1208
1209 static int um_chown(vfs_handle_struct *handle,
1210                     const char *path,
1211                     uid_t uid,
1212                     gid_t gid)
1213 {
1214         int status;
1215         char *client_path = NULL;
1216
1217         DEBUG(10, ("Entering um_chown\n"));
1218
1219         if (!is_in_media_files(path)) {
1220                 return SMB_VFS_NEXT_CHOWN(handle, path, uid, gid);
1221         }
1222
1223         status = alloc_get_client_path(handle, talloc_tos(),
1224                                        path, &client_path);
1225         if (status != 0) {
1226                 goto err;
1227         }
1228
1229         status = SMB_VFS_NEXT_CHOWN(handle, client_path, uid, gid);
1230
1231 err:
1232         TALLOC_FREE(client_path);
1233         return status;
1234 }
1235
1236 static int um_lchown(vfs_handle_struct *handle,
1237                      const char *path,
1238                      uid_t uid,
1239                      gid_t gid)
1240 {
1241         int status;
1242         char *client_path = NULL;
1243
1244         DEBUG(10, ("Entering um_lchown\n"));
1245         if (!is_in_media_files(path)) {
1246                 return SMB_VFS_NEXT_LCHOWN(handle, path, uid, gid);
1247         }
1248
1249         status = alloc_get_client_path(handle, talloc_tos(),
1250                                        path, &client_path);
1251         if (status != 0) {
1252                 goto err;
1253         }
1254
1255         status = SMB_VFS_NEXT_LCHOWN(handle, client_path, uid, gid);
1256
1257 err:
1258         TALLOC_FREE(client_path);
1259         return status;
1260 }
1261
1262 static int um_chdir(vfs_handle_struct *handle,
1263                     const char *path)
1264 {
1265         int status;
1266         char *client_path = NULL;
1267
1268         DEBUG(10, ("Entering um_chdir\n"));
1269
1270         if (!is_in_media_files(path)) {
1271                 return SMB_VFS_NEXT_CHDIR(handle, path);
1272         }
1273
1274         status = alloc_get_client_path(handle, talloc_tos(),
1275                                        path, &client_path);
1276         if (status != 0) {
1277                 goto err;
1278         }
1279
1280         status = SMB_VFS_NEXT_CHDIR(handle, client_path);
1281
1282 err:
1283         TALLOC_FREE(client_path);
1284         return status;
1285 }
1286
1287 static int um_ntimes(vfs_handle_struct *handle,
1288                      const struct smb_filename *smb_fname,
1289                      struct smb_file_time *ft)
1290 {
1291         int status;
1292         struct smb_filename *client_fname = NULL;
1293
1294         DEBUG(10, ("Entering um_ntimes\n"));
1295
1296         if (!is_in_media_files(smb_fname->base_name)) {
1297                 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
1298         }
1299
1300         status = alloc_get_client_smb_fname(handle, talloc_tos(),
1301                                             smb_fname, &client_fname);
1302         if (status != 0) {
1303                 goto err;
1304         }
1305
1306         status = SMB_VFS_NEXT_NTIMES(handle, client_fname, ft);
1307
1308 err:
1309         TALLOC_FREE(client_fname);
1310         return status;
1311 }
1312
1313 static int um_symlink(vfs_handle_struct *handle,
1314                       const char *oldpath,
1315                       const char *newpath)
1316 {
1317         int status;
1318         char *old_client_path = NULL;
1319         char *new_client_path = NULL;
1320
1321         DEBUG(10, ("Entering um_symlink\n"));
1322
1323         if (!is_in_media_files(oldpath) && !is_in_media_files(newpath)) {
1324                 return SMB_VFS_NEXT_SYMLINK(handle, oldpath, newpath);
1325         }
1326
1327         status = alloc_get_client_path(handle, talloc_tos(),
1328                                        oldpath, &old_client_path);
1329         if (status != 0) {
1330                 goto err;
1331         }
1332
1333         status = alloc_get_client_path(handle, talloc_tos(),
1334                                        newpath, &new_client_path);
1335         if (status != 0) {
1336                 goto err;
1337         }
1338
1339         status = SMB_VFS_NEXT_SYMLINK(handle,
1340                                       old_client_path,
1341                                       new_client_path);
1342
1343 err:
1344         TALLOC_FREE(new_client_path);
1345         TALLOC_FREE(old_client_path);
1346         return status;
1347 }
1348
1349 static int um_readlink(vfs_handle_struct *handle,
1350                        const char *path,
1351                        char *buf,
1352                        size_t bufsiz)
1353 {
1354         int status;
1355         char *client_path = NULL;
1356
1357         DEBUG(10, ("Entering um_readlink\n"));
1358
1359         if (!is_in_media_files(path)) {
1360                 return SMB_VFS_NEXT_READLINK(handle, path, buf, bufsiz);
1361         }
1362
1363         status = alloc_get_client_path(handle, talloc_tos(),
1364                                        path, &client_path);
1365         if (status != 0) {
1366                 goto err;
1367         }
1368
1369         status = SMB_VFS_NEXT_READLINK(handle, client_path, buf, bufsiz);
1370
1371 err:
1372         TALLOC_FREE(client_path);
1373         return status;
1374 }
1375
1376 static int um_link(vfs_handle_struct *handle,
1377                    const char *oldpath,
1378                    const char *newpath)
1379 {
1380         int status;
1381         char *old_client_path = NULL;
1382         char *new_client_path = NULL;
1383
1384         DEBUG(10, ("Entering um_link\n"));
1385         if (!is_in_media_files(oldpath) && !is_in_media_files(newpath)) {
1386                 return SMB_VFS_NEXT_LINK(handle, oldpath, newpath);
1387         }
1388
1389         status = alloc_get_client_path(handle, talloc_tos(),
1390                                        oldpath, &old_client_path);
1391         if (status != 0) {
1392                 goto err;
1393         }
1394
1395         status = alloc_get_client_path(handle, talloc_tos(),
1396                                        newpath, &new_client_path);
1397         if (status != 0) {
1398                 goto err;
1399         }
1400
1401         status = SMB_VFS_NEXT_LINK(handle, old_client_path, new_client_path);
1402
1403 err:
1404         TALLOC_FREE(new_client_path);
1405         TALLOC_FREE(old_client_path);
1406         return status;
1407 }
1408
1409 static int um_mknod(vfs_handle_struct *handle,
1410                     const char *pathname,
1411                     mode_t mode,
1412                     SMB_DEV_T dev)
1413 {
1414         int status;
1415         char *client_path = NULL;
1416
1417         DEBUG(10, ("Entering um_mknod\n"));
1418         if (!is_in_media_files(pathname)) {
1419                 return SMB_VFS_NEXT_MKNOD(handle, pathname, mode, dev);
1420         }
1421
1422         status = alloc_get_client_path(handle, talloc_tos(),
1423                                        pathname, &client_path);
1424         if (status != 0) {
1425                 goto err;
1426         }
1427
1428         status = SMB_VFS_NEXT_MKNOD(handle, client_path, mode, dev);
1429
1430 err:
1431         TALLOC_FREE(client_path);
1432         return status;
1433 }
1434
1435 static char *um_realpath(vfs_handle_struct *handle,
1436                          const char *path)
1437 {
1438         char *buf = NULL;
1439         char *client_path = NULL;
1440         int status;
1441
1442         DEBUG(10, ("Entering um_realpath\n"));
1443
1444         if (!is_in_media_files(path)) {
1445                 return SMB_VFS_NEXT_REALPATH(handle, path);
1446         }
1447
1448         status = alloc_get_client_path(handle, talloc_tos(),
1449                                        path, &client_path);
1450         if (status != 0) {
1451                 goto err;
1452         }
1453
1454         buf = SMB_VFS_NEXT_REALPATH(handle, client_path);
1455
1456 err:
1457         TALLOC_FREE(client_path);
1458         return buf;
1459 }
1460
1461 static int um_chflags(vfs_handle_struct *handle,
1462                       const char *path,
1463                       unsigned int flags)
1464 {
1465         int status;
1466         char *client_path = NULL;
1467
1468         DEBUG(10, ("Entering um_chflags\n"));
1469
1470         if (!is_in_media_files(path)) {
1471                 return SMB_VFS_NEXT_CHFLAGS(handle, path, flags);
1472         }
1473
1474         status = alloc_get_client_path(handle, talloc_tos(),
1475                                        path, &client_path);
1476         if (status != 0) {
1477                 goto err;
1478         }
1479
1480         status = SMB_VFS_NEXT_CHFLAGS(handle, client_path, flags);
1481 err:
1482         TALLOC_FREE(client_path);
1483         return status;
1484 }
1485
1486 static NTSTATUS um_streaminfo(struct vfs_handle_struct *handle,
1487                               struct files_struct *fsp,
1488                               const char *fname,
1489                               TALLOC_CTX *ctx,
1490                               unsigned int *num_streams,
1491                               struct stream_struct **streams)
1492 {
1493         NTSTATUS status;
1494         char *client_path = NULL;
1495         int ret;
1496
1497         DEBUG(10, ("Entering um_streaminfo\n"));
1498
1499         if (!is_in_media_files(fname)) {
1500                 return SMB_VFS_NEXT_STREAMINFO(handle, fsp, fname,
1501                                                ctx, num_streams, streams);
1502         }
1503
1504         ret = alloc_get_client_path(handle, talloc_tos(),
1505                                     fname, &client_path);
1506         if (ret != 0) {
1507                 status = map_nt_error_from_unix(errno);
1508                 goto err;
1509         }
1510
1511         /*
1512          * This only works on files, so we don't have to worry about
1513          * our fake directory stat'ing here.  But what does this
1514          * function do, exactly?  Does it need extra modifications for
1515          * the Avid stuff?
1516          */
1517         status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, client_path,
1518                                          ctx, num_streams, streams);
1519 err:
1520         TALLOC_FREE(client_path);
1521         return status;
1522 }
1523
1524 /*
1525  * Ignoring get_real_filename function because the default doesn't do
1526  * anything.
1527  */
1528
1529 static NTSTATUS um_get_nt_acl(vfs_handle_struct *handle,
1530                               const struct smb_filename *smb_fname,
1531                               uint32_t security_info,
1532                               TALLOC_CTX *mem_ctx,
1533                               struct security_descriptor **ppdesc)
1534 {
1535         NTSTATUS status;
1536         char *client_path = NULL;
1537         struct smb_filename *client_smb_fname = NULL;
1538         int ret;
1539
1540         DEBUG(10, ("Entering um_get_nt_acl\n"));
1541
1542         if (!is_in_media_files(smb_fname->base_name)) {
1543                 return SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname,
1544                                                security_info,
1545                                                mem_ctx, ppdesc);
1546         }
1547
1548         ret = alloc_get_client_path(handle, talloc_tos(),
1549                                     smb_fname->base_name, &client_path);
1550         if (ret != 0) {
1551                 status = map_nt_error_from_unix(errno);
1552                 goto err;
1553         }
1554
1555         client_smb_fname = synthetic_smb_fname(talloc_tos(),
1556                                         client_path,
1557                                         NULL,
1558                                         NULL);
1559         if (client_smb_fname == NULL) {
1560                 TALLOC_FREE(client_path);
1561                 return NT_STATUS_NO_MEMORY;
1562         }
1563
1564         status = SMB_VFS_NEXT_GET_NT_ACL(handle, client_smb_fname,
1565                                          security_info,
1566                                          mem_ctx, ppdesc);
1567 err:
1568         TALLOC_FREE(client_smb_fname);
1569         TALLOC_FREE(client_path);
1570         return status;
1571 }
1572
1573 static int um_chmod_acl(vfs_handle_struct *handle,
1574                         const char *path,
1575                         mode_t mode)
1576 {
1577         int status;
1578         char *client_path = NULL;
1579
1580         DEBUG(10, ("Entering um_chmod_acl\n"));
1581
1582         if (!is_in_media_files(path)) {
1583                 return SMB_VFS_NEXT_CHMOD_ACL(handle, path, mode);
1584         }
1585
1586         status = alloc_get_client_path(handle, talloc_tos(),
1587                                        path, &client_path);
1588         if (status != 0) {
1589                 goto err;
1590         }
1591
1592         status = SMB_VFS_NEXT_CHMOD_ACL(handle, client_path, mode);
1593
1594 err:
1595         TALLOC_FREE(client_path);
1596         return status;
1597 }
1598
1599 static SMB_ACL_T um_sys_acl_get_file(vfs_handle_struct *handle,
1600                                      const char *path_p,
1601                                      SMB_ACL_TYPE_T type,
1602                                      TALLOC_CTX *mem_ctx)
1603 {
1604         SMB_ACL_T ret;
1605         char *client_path = NULL;
1606         int status;
1607
1608         DEBUG(10, ("Entering um_sys_acl_get_file\n"));
1609
1610         if (!is_in_media_files(path_p)) {
1611                 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, path_p,
1612                                                      type, mem_ctx);
1613         }
1614
1615         status = alloc_get_client_path(handle, talloc_tos(),
1616                                        path_p, &client_path);
1617         if (status != 0) {
1618                 ret = NULL;
1619                 goto err;
1620         }
1621
1622         ret = SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, client_path, type, mem_ctx);
1623
1624 err:
1625         TALLOC_FREE(client_path);
1626         return ret;
1627 }
1628
1629 static int um_sys_acl_set_file(vfs_handle_struct *handle,
1630                                const char *name,
1631                                SMB_ACL_TYPE_T acltype,
1632                                SMB_ACL_T theacl)
1633 {
1634         int status;
1635         char *client_path = NULL;
1636
1637         DEBUG(10, ("Entering um_sys_acl_set_file\n"));
1638
1639         if (!is_in_media_files(name)) {
1640                 return SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, name,
1641                                                      acltype, theacl);
1642         }
1643
1644         status = alloc_get_client_path(handle, talloc_tos(),
1645                                        name, &client_path);
1646         if (status != 0) {
1647                 goto err;
1648         }
1649
1650         status = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, client_path,
1651                                                acltype, theacl);
1652
1653 err:
1654         TALLOC_FREE(client_path);
1655         return status;
1656 }
1657
1658 static int um_sys_acl_delete_def_file(vfs_handle_struct *handle,
1659                                       const char *path)
1660 {
1661         int status;
1662         char *client_path = NULL;
1663
1664         DEBUG(10, ("Entering um_sys_acl_delete_def_file\n"));
1665
1666         if (!is_in_media_files(path)) {
1667                 return SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, path);
1668         }
1669
1670         status = alloc_get_client_path(handle, talloc_tos(),
1671                                             path, &client_path);
1672         if (status != 0) {
1673                 goto err;
1674         }
1675
1676         status = SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, client_path);
1677
1678 err:
1679         TALLOC_FREE(client_path);
1680         return status;
1681 }
1682
1683 static ssize_t um_getxattr(struct vfs_handle_struct *handle,
1684                            const char *path,
1685                            const char *name,
1686                            void *value,
1687                            size_t size)
1688 {
1689         ssize_t ret;
1690         char *client_path = NULL;
1691         int status;
1692
1693         DEBUG(10, ("Entering um_getxattr\n"));
1694         if (!is_in_media_files(path)) {
1695                 return SMB_VFS_NEXT_GETXATTR(handle, path, name, value, size);
1696         }
1697
1698         status = alloc_get_client_path(handle, talloc_tos(),
1699                                        path, &client_path);
1700         if (status != 0) {
1701                 ret = -1;
1702                 goto err;
1703         }
1704
1705         ret = SMB_VFS_NEXT_GETXATTR(handle, client_path, name, value, size);
1706 err:
1707         TALLOC_FREE(client_path);
1708         return ret;
1709 }
1710
1711 static ssize_t um_listxattr(struct vfs_handle_struct *handle,
1712                             const char *path,
1713                             char *list,
1714                             size_t size)
1715 {
1716         ssize_t ret;
1717         char *client_path = NULL;
1718         int status;
1719
1720         DEBUG(10, ("Entering um_listxattr\n"));
1721
1722         if (!is_in_media_files(path)) {
1723                 return SMB_VFS_NEXT_LISTXATTR(handle, path, list, size);
1724         }
1725
1726         status = alloc_get_client_path(handle, talloc_tos(),
1727                                        path, &client_path);
1728         if (status != 0) {
1729                 ret = -1;
1730                 goto err;
1731         }
1732
1733         ret = SMB_VFS_NEXT_LISTXATTR(handle, client_path, list, size);
1734
1735 err:
1736         TALLOC_FREE(client_path);
1737         return ret;
1738 }
1739
1740 static int um_removexattr(struct vfs_handle_struct *handle,
1741                           const char *path,
1742                           const char *name)
1743 {
1744         int status;
1745         char *client_path = NULL;
1746
1747         DEBUG(10, ("Entering um_removexattr\n"));
1748
1749         if (!is_in_media_files(path)) {
1750                 return SMB_VFS_NEXT_REMOVEXATTR(handle, path, name);
1751         }
1752
1753         status = alloc_get_client_path(handle, talloc_tos(),
1754                                        path, &client_path);
1755         if (status != 0) {
1756                 goto err;
1757         }
1758
1759         status = SMB_VFS_NEXT_REMOVEXATTR(handle, client_path, name);
1760
1761 err:
1762         TALLOC_FREE(client_path);
1763         return status;
1764 }
1765
1766 static int um_setxattr(struct vfs_handle_struct *handle,
1767                        const char *path,
1768                        const char *name,
1769                        const void *value,
1770                        size_t size,
1771                        int flags)
1772 {
1773         int status;
1774         char *client_path = NULL;
1775
1776         DEBUG(10, ("Entering um_setxattr\n"));
1777
1778         if (!is_in_media_files(path)) {
1779                 return SMB_VFS_NEXT_SETXATTR(handle, path, name, value,
1780                                              size, flags);
1781         }
1782
1783         status = alloc_get_client_path(handle, talloc_tos(),
1784                                        path, &client_path);
1785         if (status != 0) {
1786                 goto err;
1787         }
1788
1789         status = SMB_VFS_NEXT_SETXATTR(handle, client_path, name, value,
1790                                        size, flags);
1791
1792 err:
1793         TALLOC_FREE(client_path);
1794         return status;
1795 }
1796
1797 static bool um_is_offline(struct vfs_handle_struct *handle,
1798                           const struct smb_filename *fname,
1799                           SMB_STRUCT_STAT *sbuf)
1800 {
1801         bool ret;
1802         struct smb_filename *client_fname = NULL;
1803         int status;
1804
1805         DEBUG(10, ("Entering um_is_offline\n"));
1806
1807         if (!is_in_media_files(fname->base_name)) {
1808                 return SMB_VFS_NEXT_IS_OFFLINE(handle, fname, sbuf);
1809         }
1810
1811         status = alloc_get_client_smb_fname(handle, talloc_tos(),
1812                                             fname, &client_fname);
1813         if (status != 0) {
1814                 ret = false;
1815                 goto err;
1816         }
1817
1818         ret = SMB_VFS_NEXT_IS_OFFLINE(handle, client_fname, sbuf);
1819
1820 err:
1821         TALLOC_FREE(client_fname);
1822         return ret;
1823 }
1824
1825 static int um_set_offline(struct vfs_handle_struct *handle,
1826                           const struct smb_filename *fname)
1827 {
1828         int status;
1829         struct smb_filename *client_fname = NULL;
1830
1831         DEBUG(10, ("Entering um_set_offline\n"));
1832
1833         if (!is_in_media_files(fname->base_name)) {
1834                 return SMB_VFS_NEXT_SET_OFFLINE(handle, fname);
1835         }
1836
1837         status = alloc_get_client_smb_fname(handle, talloc_tos(),
1838                                             fname, &client_fname);
1839         if (status != 0) {
1840                 goto err;
1841         }
1842
1843         status = SMB_VFS_NEXT_SET_OFFLINE(handle, client_fname);
1844
1845 err:
1846         TALLOC_FREE(client_fname);
1847         return status;
1848 }
1849
1850 static int um_connect(vfs_handle_struct *handle,
1851                          const char *service,
1852                          const char *user)
1853 {
1854         int rc;
1855         struct um_config_data *config;
1856         int enumval;
1857
1858         rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
1859         if (rc != 0) {
1860                 return rc;
1861         }
1862
1863         config = talloc_zero(handle->conn, struct um_config_data);
1864         if (!config) {
1865                 DEBUG(1, ("talloc_zero() failed\n"));
1866                 errno = ENOMEM;
1867                 return -1;
1868         }
1869
1870         enumval = lp_parm_enum(SNUM(handle->conn), UM_PARAM_TYPE_NAME,
1871                                "clientid", um_clientid, UM_CLIENTID_NAME);
1872         if (enumval == -1) {
1873                 DEBUG(1, ("value for %s: type unknown\n",
1874                           UM_PARAM_TYPE_NAME));
1875                 return -1;
1876         }
1877         config->clientid = (enum um_clientid)enumval;
1878
1879         SMB_VFS_HANDLE_SET_DATA(handle, config,
1880                                 NULL, struct um_config_data,
1881                                 return -1);
1882
1883         return 0;
1884 }
1885
1886 /* VFS operations structure */
1887
1888 static struct vfs_fn_pointers vfs_um_fns = {
1889         .connect_fn = um_connect,
1890
1891         /* Disk operations */
1892
1893         .statvfs_fn = um_statvfs,
1894
1895         /* Directory operations */
1896
1897         .opendir_fn = um_opendir,
1898         .fdopendir_fn = um_fdopendir,
1899         .readdir_fn = um_readdir,
1900         .seekdir_fn = um_seekdir,
1901         .telldir_fn = um_telldir,
1902         .rewind_dir_fn = um_rewinddir,
1903         .mkdir_fn = um_mkdir,
1904         .rmdir_fn = um_rmdir,
1905         .closedir_fn = um_closedir,
1906         .init_search_op_fn = um_init_search_op,
1907
1908         /* File operations */
1909
1910         .open_fn = um_open,
1911         .create_file_fn = um_create_file,
1912         .rename_fn = um_rename,
1913         .stat_fn = um_stat,
1914         .lstat_fn = um_lstat,
1915         .fstat_fn = um_fstat,
1916         .unlink_fn = um_unlink,
1917         .chmod_fn = um_chmod,
1918         .chown_fn = um_chown,
1919         .lchown_fn = um_lchown,
1920         .chdir_fn = um_chdir,
1921         .ntimes_fn = um_ntimes,
1922         .symlink_fn = um_symlink,
1923         .readlink_fn = um_readlink,
1924         .link_fn = um_link,
1925         .mknod_fn = um_mknod,
1926         .realpath_fn = um_realpath,
1927         .chflags_fn = um_chflags,
1928         .streaminfo_fn = um_streaminfo,
1929
1930         /* NT ACL operations. */
1931
1932         .get_nt_acl_fn = um_get_nt_acl,
1933
1934         /* POSIX ACL operations. */
1935
1936         .chmod_acl_fn = um_chmod_acl,
1937
1938         .sys_acl_get_file_fn = um_sys_acl_get_file,
1939         .sys_acl_set_file_fn = um_sys_acl_set_file,
1940         .sys_acl_delete_def_file_fn = um_sys_acl_delete_def_file,
1941
1942         /* EA operations. */
1943         .getxattr_fn = um_getxattr,
1944         .listxattr_fn = um_listxattr,
1945         .removexattr_fn = um_removexattr,
1946         .setxattr_fn = um_setxattr,
1947
1948         /* aio operations */
1949
1950         /* offline operations */
1951         .is_offline_fn = um_is_offline,
1952         .set_offline_fn = um_set_offline
1953 };
1954
1955 NTSTATUS vfs_unityed_media_init(void);
1956 NTSTATUS vfs_unityed_media_init(void)
1957 {
1958         NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
1959                                         "unityed_media", &vfs_um_fns);
1960         if (!NT_STATUS_IS_OK(ret)) {
1961                 return ret;
1962         }
1963
1964         vfs_um_debug_level = debug_add_class("unityed_media");
1965
1966         if (vfs_um_debug_level == -1) {
1967                 vfs_um_debug_level = DBGC_VFS;
1968                 DEBUG(1, ("unityed_media_init: Couldn't register custom "
1969                           "debugging class.\n"));
1970         }
1971
1972         return ret;
1973 }