summaryrefslogtreecommitdiffstats
path: root/source3/modules/vfs_unityed_media.c
blob: c848cf5f623b93d6f00610c2e81154435451f643 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
/*
 * Samba VFS module supporting multiple AVID clients sharing media.
 *
 * Copyright (C) 2005  Philip de Nier <philipn@users.sourceforge.net>
 * Copyright (C) 2012  Andrew Klaassen <clawsoon@yahoo.com>
 * Copyright (C) 2013  Milos Lukacek
 * Copyright (C) 2013  Ralph Boehme <slow@samba.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

/*
 * Unityed Media is a Samba VFS module that allows multiple AVID
 * clients to share media.
 *
 * Add this module to the vfs objects option in your Samba share
 * configuration.
 * eg.
 *
 *   [avid_win]
 *	path = /video
 *	vfs objects = unityed_media
 *	...
 *
 * It is recommended that you separate out Samba shares for Mac
 * and Windows clients, and add the following options to the shares
 * for Windows clients	(NOTE: replace @ with *):
 *
 *	veto files = /.DS_Store/._@/.Trash@/.Spotlight@/.hidden/.hotfiles@/.vol/
 *	delete veto files = yes
 *
 * This prevents hidden files from Mac clients interfering with Windows
 * clients. If you find any more problem hidden files then add them to
 * the list.
 *
 * Notes:
 * This module is designed to work with AVID editing applications that
 * look in the Avid MediaFiles or OMFI MediaFiles directory for media.
 * It is not designed to work as expected in all circumstances for
 * general use.
 */


#include "includes.h"
#include "system/filesys.h"
#include "smbd/smbd.h"
#include "../smbd/globals.h"
#include "auth.h"
#include "../lib/tsocket/tsocket.h"
#include "lib/util/smb_strtox.h"
#include <libgen.h>
#include "source3/lib/substitute.h"

#define UM_PARAM_TYPE_NAME "unityed_media"

static const char *AVID_MXF_DIRNAME = "Avid MediaFiles/MXF";
static const size_t AVID_MXF_DIRNAME_LEN = 19;
static const char *OMFI_MEDIAFILES_DIRNAME = "OMFI MediaFiles";
static const size_t OMFI_MEDIAFILES_DIRNAME_LEN = 15;
static const char *APPLE_DOUBLE_PREFIX = "._";
static const size_t APPLE_DOUBLE_PREFIX_LEN = 2;
static int vfs_um_debug_level = DBGC_VFS;

enum um_clientid {UM_CLIENTID_NAME, UM_CLIENTID_IP, UM_CLIENTID_HOSTNAME};

struct um_config_data {
	enum um_clientid clientid;
};

static const struct enum_list um_clientid[] = {
	{UM_CLIENTID_NAME, "user"},
	{UM_CLIENTID_IP, "ip"},
	{UM_CLIENTID_HOSTNAME, "hostname"},
	{-1, NULL}
};

/* supplements the directory list stream */
typedef struct um_dirinfo_struct {
	DIR* dirstream;
	char *dirpath;
	char *clientPath;
	bool isInMediaFiles;
	char *clientSubDirname;
} um_dirinfo_struct;

/**
 * Returns true and first group of digits in path, false and 0 otherwise
 **/
static bool get_digit_group(const char *path, uintmax_t *digit)
{
	const char *p = path;
	codepoint_t cp;
	size_t size;
	int error = 0;

	DEBUG(10, ("get_digit_group entering with path '%s'\n",
		   path));

	/*
	 * Delibiretly initialize to 0 because callers use this result
	 * even though the string doesn't contain any number and we
	 * returned false
	 */
	*digit = 0;

	while (*p) {
		cp = next_codepoint(p, &size);
		if (cp == -1) {
			return false;
		}
		if ((size == 1) && (isdigit(cp))) {
			*digit = (uintmax_t)smb_strtoul(p,
							NULL,
							10,
							&error,
							SMB_STR_STANDARD);
			if (error != 0) {
				return false;
			}
			DEBUG(10, ("num_suffix = '%ju'\n",
				   *digit));
			return true;
		}
		p += size;
	}

	return false;
}

/* Add "_<remote_name>.<number>" suffix to path or filename.
 *
 * Success: return 0
 * Failure: set errno, path NULL, return -1
 */

static int alloc_append_client_suffix(vfs_handle_struct *handle,
				      char **path)
{
	int status = 0;
	uintmax_t number;
	const char *clientid;
	struct um_config_data *config;

	DEBUG(10, ("Entering with path '%s'\n", *path));

	SMB_VFS_HANDLE_GET_DATA(handle, config,
				struct um_config_data,
				return -1);

	(void)get_digit_group(*path, &number);

	switch (config->clientid) {

	case UM_CLIENTID_IP:
		clientid = tsocket_address_inet_addr_string(
			handle->conn->sconn->remote_address, talloc_tos());
		if (clientid == NULL) {
			errno = ENOMEM;
			status = -1;
			goto err;
		}
		break;

	case UM_CLIENTID_HOSTNAME:
		clientid = get_remote_machine_name();
		break;

	case UM_CLIENTID_NAME:
	default:
		clientid = get_current_username();
		break;
	}

	*path = talloc_asprintf_append(*path, "_%s.%ju",
				       clientid, number);
	if (*path == NULL) {
		DEBUG(1, ("alloc_append_client_suffix "
				     "out of memory\n"));
		errno = ENOMEM;
		status = -1;
		goto err;
	}
	DEBUG(10, ("Leaving with *path '%s'\n", *path));
err:
	return status;
}

/* Returns true if the file or directory begins with the appledouble
 * prefix.
 */
static bool is_apple_double(const char* fname)
{
	bool ret = false;

	DEBUG(10, ("Entering with fname '%s'\n", fname));

	if (strnequal(APPLE_DOUBLE_PREFIX, fname, APPLE_DOUBLE_PREFIX_LEN)) {
		ret = true;
	}
	DEBUG(10, ("Leaving with ret '%s'\n",
			      ret == true ? "true" : "false"));
	return ret;
}

static bool starts_with_media_dir(const char* media_dirname,
				  size_t media_dirname_len,
				  const char *path)
{
	bool ret = false;
	const char *path_start = path;

	DEBUG(10, ("Entering with media_dirname '%s' "
			      "path '%s'\n", media_dirname, path));

	/* Sometimes Samba gives us "./OMFI MediaFiles". */
	if (strnequal(path, "./", 2)) {
		path_start += 2;
	}

	if (strnequal(media_dirname, path_start, media_dirname_len)
	    &&
	    ((path_start[media_dirname_len] == '\0') ||
	     (path_start[media_dirname_len] == '/'))) {
		ret = true;
	}

	DEBUG(10, ("Leaving with ret '%s'\n",
			      ret == true ? "true" : "false"));
	return ret;
}

/*
 * Returns true if the file or directory referenced by the path is ONE
 * LEVEL below the AVID_MXF_DIRNAME or OMFI_MEDIAFILES_DIRNAME
 * directory
 */
static bool is_in_media_dir(const char *path)
{
	int transition_count = 0;
	const char *path_start = path;
	const char *p;
	const char *media_dirname;
	size_t media_dirname_len;

	DEBUG(10, ("Entering with path '%s'\n", path));

	/* Sometimes Samba gives us "./OMFI MediaFiles". */
	if (strnequal(path, "./", 2)) {
		path_start += 2;
	}

	if (strnequal(path_start, AVID_MXF_DIRNAME, AVID_MXF_DIRNAME_LEN)) {
		media_dirname = AVID_MXF_DIRNAME;
		media_dirname_len = AVID_MXF_DIRNAME_LEN;
	} else if (strnequal(path_start,
			     OMFI_MEDIAFILES_DIRNAME,
			     OMFI_MEDIAFILES_DIRNAME_LEN)) {
		media_dirname = OMFI_MEDIAFILES_DIRNAME;
		media_dirname_len = OMFI_MEDIAFILES_DIRNAME_LEN;
	} else {
		return false;
	}

	if (path_start[media_dirname_len] == '\0') {
		goto out;
	}

	p = path_start + media_dirname_len + 1;

	while (true) {
		if (*p == '\0' || *p == '/') {
			if (strnequal(p - 3, "/..", 3)) {
				transition_count--;
			} else if ((p[-1] != '/') || !strnequal(p - 2, "/.", 2)) {
				transition_count++;
			}
		}
		if (*p == '\0') {
			break;
		}
		p++;
	}

out:
	DEBUG(10, ("Going out with transition_count '%i'\n",
			      transition_count));
	if (((transition_count == 1) && (media_dirname == AVID_MXF_DIRNAME))
	    ||
	    ((transition_count == 0) && (media_dirname == OMFI_MEDIAFILES_DIRNAME))) {
		return true;
	}
	else return false;
}

/*
 * Returns true if the file or directory referenced by the path is
 * below the AVID_MEDIAFILES_DIRNAME or OMFI_MEDIAFILES_DIRNAME
 * directory The AVID_MEDIAFILES_DIRNAME and OMFI_MEDIAFILES_DIRNAME
 * are assumed to be in the root directory, which is generally a safe
 * assumption in the fixed-path world of Avid.
 */
static bool is_in_media_files(const char *path)
{
	bool ret = false;

	DEBUG(10, ("Entering with path '%s'\n", path));

	if (starts_with_media_dir(AVID_MXF_DIRNAME,
				  AVID_MXF_DIRNAME_LEN, path) ||
	    starts_with_media_dir(OMFI_MEDIAFILES_DIRNAME,
				  OMFI_MEDIAFILES_DIRNAME_LEN, path)) {
		ret = true;
	}
	DEBUG(10, ("Leaving with ret '%s'\n",
			      ret == true ? "true" : "false"));
	return ret;
}


/* Add client suffix to "pure-number" path.
 *
 * Caller must free newPath.
 *
 * Success: return 0
 * Failure: set errno, newPath NULL, return -1
 */
static int alloc_get_client_path(vfs_handle_struct *handle,
				 TALLOC_CTX *ctx,
				 const char *path_in,
				 char **path_out)
{
	int status = 0;
	char *p;
	char *digits;
	size_t digits_len;
	uintmax_t number;

	*path_out = talloc_strdup(ctx, path_in);
        if (*path_out == NULL) {
		DEBUG(1, ("alloc_get_client_path ENOMEM\n"));
		return -1;
	}

	(void)get_digit_group(*path_out, &number);

	digits = talloc_asprintf(NULL, "%ju", number);
        if (digits == NULL) {
		DEBUG(1, ("alloc_get_client_path ENOMEM\n"));
		return -1;
	}
	digits_len = strlen(digits);

	p = strstr_m(path_in, digits);
	if ((p)
	    &&
	    ((p[digits_len] == '\0') || (p[digits_len] == '/'))
	    &&
	    (((p - path_in > 0) && (p[-1] == '/'))
	     ||
	     (((p - path_in) > APPLE_DOUBLE_PREFIX_LEN)
	      &&
	      is_apple_double(p - APPLE_DOUBLE_PREFIX_LEN)
	      &&
	      (p[-(APPLE_DOUBLE_PREFIX_LEN + 1)] == '/'))))
	{
		(*path_out)[p - path_in + digits_len] = '\0';

		status = alloc_append_client_suffix(handle, path_out);
		if (status != 0) {
			goto out;
		}

                *path_out = talloc_strdup_append(*path_out, p + digits_len);
		if (*path_out == NULL) {
			DEBUG(1, ("alloc_get_client_path ENOMEM\n"));
			status = -1;
			goto out;
		}
	}
out:
	/* path_out must be freed in caller. */
	DEBUG(10, ("Result:'%s'\n", *path_out));
	return status;
}

/*
 * Success: return 0
 * Failure: set errno, return -1
 */
static int alloc_get_client_smb_fname(struct vfs_handle_struct *handle,
				      TALLOC_CTX *ctx,
				      const struct smb_filename *smb_fname,
				      struct smb_filename **client_fname)
{
	int status ;

	DEBUG(10, ("Entering with smb_fname->base_name '%s'\n",
		   smb_fname->base_name));

	*client_fname = cp_smb_filename(ctx, smb_fname);
	if (*client_fname == NULL) {
		DEBUG(1, ("cp_smb_filename returned NULL\n"));
		return -1;
	}
	status = alloc_get_client_path(handle, ctx,
				       smb_fname->base_name,
				       &(*client_fname)->base_name);
	if (status != 0) {
		return -1;
	}

	DEBUG(10, ("Leaving with (*client_fname)->base_name "
		   "'%s'\n", (*client_fname)->base_name));

	return 0;
}


/*
 * Success: return 0
 * Failure: set errno, return -1
 */
static int alloc_set_client_dirinfo_path(struct vfs_handle_struct *handle,
					 TALLOC_CTX *ctx,
					 char **path,
					 const char *suffix_number)
{
	int status;

	DEBUG(10, ("Entering with suffix_number '%s'\n",
		   suffix_number));

	*path = talloc_strdup(ctx, suffix_number);
	if (*path == NULL) {
		DEBUG(1, ("alloc_set_client_dirinfo_path ENOMEM\n"));
		return -1;
	}
	status = alloc_append_client_suffix(handle, path);
	if (status != 0) {
		return -1;
	}

	DEBUG(10, ("Leaving with *path '%s'\n", *path));

	return 0;
}

static int alloc_set_client_dirinfo(vfs_handle_struct *handle,
				    const char *fname,
				    struct um_dirinfo_struct **di_result)
{
	int status = 0;
	char *digits;
	uintmax_t number;
	struct um_dirinfo_struct *dip;

	DEBUG(10, ("Entering with fname '%s'\n", fname));

	*di_result = talloc(NULL, struct um_dirinfo_struct);
	if (*di_result == NULL) {
		goto err;
	}
	dip = *di_result;

	dip->dirpath = talloc_strdup(dip, fname);
	if (dip->dirpath == NULL) {
		goto err;
	}

	if (!is_in_media_files(fname)) {
		dip->isInMediaFiles = false;
		dip->clientPath = NULL;
		dip->clientSubDirname = NULL;
		goto out;
	}

	dip->isInMediaFiles = true;

	(void)get_digit_group(fname, &number);
	digits = talloc_asprintf(talloc_tos(), "%ju", number);
	if (digits == NULL) {
		goto err;
	}

	status = alloc_set_client_dirinfo_path(handle, dip,
					       &dip->clientSubDirname,
					       digits);
	if (status != 0) {
		goto err;
	}

	status = alloc_get_client_path(handle, dip, fname,
				       &dip->clientPath);
	if (status != 0 || dip->clientPath == NULL) {
		goto err;
	}

out:
	DEBUG(10, ("Leaving with (*dirInfo)->dirpath '%s', "
			      "(*dirInfo)->clientPath '%s'\n",
			      dip->dirpath, dip->clientPath));
	return status;

err:
	DEBUG(1, ("Failing with fname '%s'\n", fname));
	TALLOC_FREE(*di_result);
	status = -1;
	errno = ENOMEM;
	return status;
}

/**********************************************************************
 * VFS functions
 **********************************************************************/

/*
 * Success: return 0
 * Failure: set errno, return -1
 */
static int um_statvfs(struct vfs_handle_struct *handle,
		      const struct smb_filename *smb_fname,
		      struct vfs_statvfs_struct *statbuf)
{
	int status;
	struct smb_filename *client_fname = NULL;

	DEBUG(10, ("Entering with path '%s'\n", smb_fname->base_name));

	if (!is_in_media_files(smb_fname->base_name)) {
		return SMB_VFS_NEXT_STATVFS(handle, smb_fname, statbuf);
	}

	status = alloc_get_client_smb_fname(handle,
				talloc_tos(),
				smb_fname,
				&client_fname);
	if (status != 0) {
		goto err;
	}

	status = SMB_VFS_NEXT_STATVFS(handle, client_fname, statbuf);
err:
	TALLOC_FREE(client_fname);
	DEBUG(10, ("Leaving with path '%s'\n", smb_fname->base_name));
	return status;
}

static DIR *um_fdopendir(vfs_handle_struct *handle,
			 files_struct *fsp,
			 const char *mask,
			 uint32_t attr)
{
	struct um_dirinfo_struct *dirInfo = NULL;
	DIR *dirstream;

	DEBUG(10, ("Entering with fsp->fsp_name->base_name '%s'\n",
		   fsp->fsp_name->base_name));

	dirstream = SMB_VFS_NEXT_FDOPENDIR(handle, fsp, mask, attr);
	if (!dirstream) {
		goto err;
	}

	if (alloc_set_client_dirinfo(handle,
				     fsp->fsp_name->base_name,
				     &dirInfo)) {
		goto err;
	}

	dirInfo->dirstream = dirstream;

	if (!dirInfo->isInMediaFiles) {
		/*
		 * FIXME: this is the original code, something must be
		 * missing here, but what? -slow
		 */
		goto out;
	}

out:
	DEBUG(10, ("Leaving with dirInfo->dirpath '%s', "
		   "dirInfo->clientPath '%s', "
		   "fsp->fsp_name->st.st_ex_mtime %s",
		   dirInfo->dirpath,
		   dirInfo->clientPath,
		   ctime(&(fsp->fsp_name->st.st_ex_mtime.tv_sec))));
	return (DIR *) dirInfo;

err:
	DEBUG(1, ("Failing with fsp->fsp_name->base_name '%s'\n",
		  fsp->fsp_name->base_name));
	TALLOC_FREE(dirInfo);
	return NULL;
}

/*
 * skip own suffixed directory
 * replace own suffixed directory with non suffixed.
 *
 * Success: return dirent
 * End of data: return NULL
 * Failure: set errno, return NULL
 */
static struct dirent *
um_readdir(vfs_handle_struct *handle, struct files_struct *dirfsp, DIR *dirp)
{
	um_dirinfo_struct* dirInfo = (um_dirinfo_struct*)dirp;
	struct dirent *d = NULL;
	int skip;

	DEBUG(10, ("dirInfo->dirpath '%s', "
		   "dirInfo->clientPath '%s', "
		   "dirInfo->isInMediaFiles '%s', "
		   "dirInfo->clientSubDirname '%s'\n",
		   dirInfo->dirpath,
		   dirInfo->clientPath,
		   dirInfo->isInMediaFiles ? "true" : "false",
		   dirInfo->clientSubDirname));

	if (!dirInfo->isInMediaFiles) {
		return SMB_VFS_NEXT_READDIR(handle, dirfsp, dirInfo->dirstream);
	}

	do {
		const char* dname;
		bool isAppleDouble;
		char *digits;
		size_t digits_len;
		uintmax_t number;

		skip = false;
		d = SMB_VFS_NEXT_READDIR(handle, dirfsp, dirInfo->dirstream);

		if (d == NULL) {
			break;
		}

		/* ignore apple double prefix for logic below */
		if (is_apple_double(d->d_name)) {
			dname = &d->d_name[APPLE_DOUBLE_PREFIX_LEN];
			isAppleDouble = true;
		} else {
			dname = d->d_name;
			isAppleDouble = false;
		}

		DEBUG(10, ("dname = '%s'\n", dname));

		(void)get_digit_group(dname, &number);
		digits = talloc_asprintf(talloc_tos(), "%ju", number);
		if (digits == NULL) {
			DEBUG(1, ("out of memory\n"));
			goto err;
		}
		digits_len = strlen(digits);

		if (alloc_set_client_dirinfo_path(handle,
						  dirInfo,
						  &((dirInfo)->clientSubDirname),
						  digits)) {
			goto err;
		}

		/*
		 * If set to "true", vfs shows digits-only
		 * non-suffixed subdirectories.  Normally, such
		 * subdirectories can exists only in non-media
		 * directories, so we set it to "false".  Otherwise,
		 * if we have such subdirectories (probably created
		 * over not "unityed" connection), it can be little
		 * bit confusing.
		 */
		if (strequal(dname, digits)) {
			skip = false;
		} else if (strequal(dname, dirInfo->clientSubDirname)) {
			/*
			 * Remove suffix of this client's suffixed
			 * subdirectories
			 */
			if (isAppleDouble) {
				d->d_name[digits_len + APPLE_DOUBLE_PREFIX_LEN] = '\0';
			} else {
				d->d_name[digits_len] = '\0';
			}
		} else if (strnequal(digits, dname, digits_len)) {
			/*
			 * Set to false to see another clients subdirectories
			 */
			skip = false;
		}
	} while (skip);

	DEBUG(10, ("Leaving um_readdir\n"));
	return d;
err:
	TALLOC_FREE(dirInfo);
	return NULL;
}

static void um_rewinddir(vfs_handle_struct *handle,
			 DIR *dirp)
{
	DEBUG(10, ("Entering and leaving um_rewinddir\n"));
	SMB_VFS_NEXT_REWINDDIR(handle,
			       ((um_dirinfo_struct*)dirp)->dirstream);
}

static int um_mkdirat(vfs_handle_struct *handle,
			struct files_struct *dirfsp,
			const struct smb_filename *smb_fname,
			mode_t mode)
{
	int status;
	const char *path = NULL;
	struct smb_filename *client_fname = NULL;
	struct smb_filename *full_fname = NULL;

	full_fname = full_path_from_dirfsp_atname(talloc_tos(),
						  dirfsp,
						  smb_fname);
	if (full_fname == NULL) {
		return -1;
	}

	path = full_fname->base_name;
	DEBUG(10, ("Entering with path '%s'\n", path));

	if (!is_in_media_files(path) || !is_in_media_dir(path)) {
		TALLOC_FREE(full_fname);
		return SMB_VFS_NEXT_MKDIRAT(handle,
				dirfsp,
				smb_fname,
				mode);
	}

	status = alloc_get_client_smb_fname(handle,
				talloc_tos(),
				full_fname,
				&client_fname);
	if (status != 0) {
		goto err;
	}

	status = SMB_VFS_NEXT_MKDIRAT(handle,
				handle->conn->cwd_fsp,
				client_fname,
				mode);
err:
	DEBUG(10, ("Leaving with path '%s'\n", path));
	TALLOC_FREE(client_fname);
	TALLOC_FREE(full_fname);
	return status;
}

static int um_closedir(vfs_handle_struct *handle,
		       DIR *dirp)
{
	DIR *realdirp = ((um_dirinfo_struct*)dirp)->dirstream;

	TALLOC_FREE(dirp);

	return SMB_VFS_NEXT_CLOSEDIR(handle, realdirp);
}

static int um_openat(struct vfs_handle_struct *handle,
		     const struct files_struct *dirfsp,
		     const struct smb_filename *smb_fname,
		     struct files_struct *fsp,
		     const struct vfs_open_how *how)
{
	struct smb_filename *client_fname = NULL;
	int ret;

	DBG_DEBUG("Entering with smb_fname->base_name '%s'\n",
		  smb_fname->base_name);

	if (!is_in_media_files(smb_fname->base_name)) {
		return SMB_VFS_NEXT_OPENAT(handle,
					   dirfsp,
					   smb_fname,
					   fsp,
					   how);
	}

	if (alloc_get_client_smb_fname(handle, talloc_tos(),
				       smb_fname,
				       &client_fname)) {
		ret = -1;
		goto err;
	}

	/*
	 * FIXME:
	 * What about fsp->fsp_name?  We also have to get correct stat
	 * info into fsp and smb_fname for DB files, don't we?
	 */

	DEBUG(10, ("Leaving with smb_fname->base_name '%s' "
		   "smb_fname->st.st_ex_mtime %s"
		   "fsp->fsp_name->st.st_ex_mtime %s",
		   smb_fname->base_name,
		   ctime(&(smb_fname->st.st_ex_mtime.tv_sec)),
		   ctime(&(fsp->fsp_name->st.st_ex_mtime.tv_sec))));

	ret = SMB_VFS_NEXT_OPENAT(handle,
				  dirfsp,
				  client_fname,
				  fsp,
				  how);
err:
	TALLOC_FREE(client_fname);
	DEBUG(10, ("Leaving with smb_fname->base_name '%s'\n",
			      smb_fname->base_name));
	return ret;
}

static NTSTATUS um_create_file(vfs_handle_struct *handle,
			       struct smb_request *req,
			       struct files_struct *dirfsp,
			       struct smb_filename *smb_fname,
			       uint32_t access_mask,
			       uint32_t share_access,
			       uint32_t create_disposition,
			       uint32_t create_options,
			       uint32_t file_attributes,
			       uint32_t oplock_request,
			       const struct smb2_lease *lease,
			       uint64_t allocation_size,
			       uint32_t private_flags,
			       struct security_descriptor *sd,
			       struct ea_list *ea_list,
			       files_struct **result_fsp,
			       int *pinfo,
			       const struct smb2_create_blobs *in_context_blobs,
			       struct smb2_create_blobs *out_context_blobs)
{
	NTSTATUS status;
	struct smb_filename *client_fname = NULL;

	DEBUG(10, ("Entering with smb_fname->base_name '%s'\n",
		   smb_fname->base_name));

	if (!is_in_media_files(smb_fname->base_name)) {
		return SMB_VFS_NEXT_CREATE_FILE(
			handle,
			req,
			dirfsp,
			smb_fname,
			access_mask,
			share_access,
			create_disposition,
			create_options,
			file_attributes,
			oplock_request,
			lease,
			allocation_size,
			private_flags,
			sd,
			ea_list,
			result_fsp,
			pinfo,
			in_context_blobs,
			out_context_blobs);
	}

	if (alloc_get_client_smb_fname(handle, talloc_tos(),
				       smb_fname,
				       &client_fname)) {
		status = map_nt_error_from_unix(errno);
		goto err;
	}

	/*
	 * FIXME:
	 * This only creates files, so we don't have to worry about
	 * our fake directory stat'ing here.  But we still need to
	 * route stat calls for DB files properly, right?
	 */
	status = SMB_VFS_NEXT_CREATE_FILE(
		handle,
		req,
		dirfsp,
		client_fname,
		access_mask,
		share_access,
		create_disposition,
		create_options,
		file_attributes,
		oplock_request,
		lease,
		allocation_size,
		private_flags,
		sd,
		ea_list,
		result_fsp,
		pinfo,
		in_context_blobs,
		out_context_blobs);
err:
	TALLOC_FREE(client_fname);
	DEBUG(10, ("Leaving with smb_fname->base_name '%s'"
		   "smb_fname->st.st_ex_mtime %s"
		   " fsp->fsp_name->st.st_ex_mtime %s",
		   smb_fname->base_name,
		   ctime(&(smb_fname->st.st_ex_mtime.tv_sec)),
		   (*result_fsp) && VALID_STAT((*result_fsp)->fsp_name->st) ?
		   ctime(&((*result_fsp)->fsp_name->st.st_ex_mtime.tv_sec)) :
		   "No fsp time\n"));
	return status;
}

static int um_renameat(vfs_handle_struct *handle,
		files_struct *srcfsp,
		const struct smb_filename *smb_fname_src,
		files_struct *dstfsp,
		const struct smb_filename *smb_fname_dst)
{
	int status;
	struct smb_filename *src_full_fname = NULL;
	struct smb_filename *dst_full_fname = NULL;
	struct smb_filename *src_client_fname = NULL;
	struct smb_filename *dst_client_fname = NULL;

	src_full_fname = full_path_from_dirfsp_atname(talloc_tos(),
						  srcfsp,
						  smb_fname_src);
	if (src_full_fname == NULL) {
		errno = ENOMEM;
		return -1;
	}
	dst_full_fname = full_path_from_dirfsp_atname(talloc_tos(),
						  dstfsp,
						  smb_fname_dst);
	if (dst_full_fname == NULL) {
		TALLOC_FREE(src_full_fname);
		errno = ENOMEM;
		return -1;
	}

	DBG_DEBUG( "Entering with "
		   "smb_fname_src->base_name '%s', "
		   "smb_fname_dst->base_name '%s'\n",
		   smb_fname_src->base_name,
		   smb_fname_dst->base_name);

	if (!is_in_media_files(src_full_fname->base_name)
	    &&
	    !is_in_media_files(dst_full_fname->base_name)) {
		TALLOC_FREE(src_full_fname);
		TALLOC_FREE(dst_full_fname);
		return SMB_VFS_NEXT_RENAMEAT(handle,
					srcfsp,
					smb_fname_src,
					dstfsp,
					smb_fname_dst);
	}

	status = alloc_get_client_smb_fname(handle, talloc_tos(),
					    src_full_fname,
					    &src_client_fname);
	if (status != 0) {
		goto err;
	}

	status = alloc_get_client_smb_fname(handle, talloc_tos(),
					    dst_full_fname,
					    &dst_client_fname);

	if (status != 0) {
		goto err;
	}

	status = SMB_VFS_NEXT_RENAMEAT(handle,
				handle->conn->cwd_fsp,
				src_client_fname,
				handle->conn->cwd_fsp,
				dst_client_fname);

err:
	TALLOC_FREE(dst_client_fname);
	TALLOC_FREE(src_client_fname);
	TALLOC_FREE(src_full_fname);
	TALLOC_FREE(dst_full_fname);
	DBG_DEBUG( "Leaving with smb_fname_src->base_name '%s',"
		   " smb_fname_dst->base_name '%s'\n",
		   smb_fname_src->base_name,
		   smb_fname_dst->base_name);
	return status;
}


/*
 * Success: return 0
 * Failure: set errno, return -1
 */
static int um_stat(vfs_handle_struct *handle,
		   struct smb_filename *smb_fname)
{
	int status = 0;
	struct smb_filename *client_fname = NULL;

	DEBUG(10, ("Entering with smb_fname->base_name '%s'\n",
		   smb_fname->base_name));

	if (!is_in_media_files(smb_fname->base_name)) {
		return SMB_VFS_NEXT_STAT(handle, smb_fname);
	}

	status = alloc_get_client_smb_fname(handle, talloc_tos(),
					    smb_fname,
					    &client_fname);
	if (status != 0) {
		goto err;
	}
	DEBUG(10, ("Stat'ing client_fname->base_name '%s'\n",
		   client_fname->base_name));

	status = SMB_VFS_NEXT_STAT(handle, client_fname);
	if (status != 0) {
		goto err;
	}

	/*
	 * Unlike functions with const smb_filename, we have to modify
	 * smb_fname itself to pass our info back up.
	 */
	DEBUG(10, ("Setting smb_fname '%s' stat from client_fname '%s'\n",
		   smb_fname->base_name, client_fname->base_name));
	smb_fname->st = client_fname->st;

err:
	TALLOC_FREE(client_fname);
	DEBUG(10, ("Leaving with smb_fname->st.st_ex_mtime %s",
		   ctime(&(smb_fname->st.st_ex_mtime.tv_sec))));
	return status;
}

static int um_lstat(vfs_handle_struct *handle,
		    struct smb_filename *smb_fname)
{
	int status = 0;
	struct smb_filename *client_fname = NULL;

	DEBUG(10, ("Entering with smb_fname->base_name '%s'\n",
		   smb_fname->base_name));

	if (!is_in_media_files(smb_fname->base_name)) {
		return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
	}

	client_fname = NULL;

	status = alloc_get_client_smb_fname(handle, talloc_tos(),
					    smb_fname,
					    &client_fname);
	if (status != 0) {
		goto err;
	}
	status = SMB_VFS_NEXT_LSTAT(handle, client_fname);
	if (status != 0) {
		goto err;
	}

	smb_fname->st = client_fname->st;

err:
	TALLOC_FREE(client_fname);
	DEBUG(10, ("Leaving with smb_fname->st.st_ex_mtime %s",
		   ctime(&(smb_fname->st.st_ex_mtime.tv_sec))));
	return status;
}

static int um_fstat(vfs_handle_struct *handle,
		    files_struct *fsp, SMB_STRUCT_STAT *sbuf)
{
	int status = 0;

	DEBUG(10, ("Entering with fsp->fsp_name->base_name "
		   "'%s'\n", fsp_str_dbg(fsp)));

	status = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
	if (status != 0) {
		goto out;
	}

	if ((fsp->fsp_name == NULL) ||
	    !is_in_media_files(fsp->fsp_name->base_name)) {
		goto out;
	}

	status = um_stat(handle, fsp->fsp_name);
	if (status != 0) {
		goto out;
	}

	*sbuf = fsp->fsp_name->st;

out:
	DEBUG(10, ("Leaving with fsp->fsp_name->st.st_ex_mtime %s",
		   fsp->fsp_name != NULL ?
		   ctime(&(fsp->fsp_name->st.st_ex_mtime.tv_sec)) : "0\n"));
	return status;
}

static int um_unlinkat(vfs_handle_struct *handle,
			struct files_struct *dirfsp,
			const struct smb_filename *smb_fname,
			int flags)
{
	int ret;
	struct smb_filename *full_fname = NULL;
	struct smb_filename *client_fname = NULL;

	DEBUG(10, ("Entering um_unlinkat\n"));

	if (!is_in_media_files(smb_fname->base_name)) {
		return SMB_VFS_NEXT_UNLINKAT(handle,
				dirfsp,
				smb_fname,
				flags);
	}

	full_fname = full_path_from_dirfsp_atname(talloc_tos(),
						  dirfsp,
						  smb_fname);
	if (full_fname == NULL) {
		return -1;
	}

	ret = alloc_get_client_smb_fname(handle, talloc_tos(),
					    full_fname,
					    &client_fname);
	if (ret != 0) {
		goto err;
	}

	ret = SMB_VFS_NEXT_UNLINKAT(handle,
				dirfsp->conn->cwd_fsp,
				client_fname,
				flags);

err:
	TALLOC_FREE(full_fname);
	TALLOC_FREE(client_fname);
	return ret;
}

static int um_lchown(vfs_handle_struct *handle,
			const struct smb_filename *smb_fname,
			uid_t uid,
			gid_t gid)
{
	int status;
	struct smb_filename *client_fname = NULL;

	DEBUG(10, ("Entering um_lchown\n"));
	if (!is_in_media_files(smb_fname->base_name)) {
		return SMB_VFS_NEXT_LCHOWN(handle, smb_fname, uid, gid);
	}

	status = alloc_get_client_smb_fname(handle,
				talloc_tos(),
				smb_fname,
				&client_fname);
	if (status != 0) {
		goto err;
	}

	status = SMB_VFS_NEXT_LCHOWN(handle, client_fname, uid, gid);

err:
	TALLOC_FREE(client_fname);
	return status;
}

static int um_chdir(vfs_handle_struct *handle,
			const struct smb_filename *smb_fname)
{
	int status;
	struct smb_filename *client_fname = NULL;

	DEBUG(10, ("Entering um_chdir\n"));

	if (!is_in_media_files(smb_fname->base_name)) {
		return SMB_VFS_NEXT_CHDIR(handle, smb_fname);
	}

	status = alloc_get_client_smb_fname(handle,
				talloc_tos(),
				smb_fname,
				&client_fname);
	if (status != 0) {
		goto err;
	}

	status = SMB_VFS_NEXT_CHDIR(handle, client_fname);

err:
	TALLOC_FREE(client_fname);
	return status;
}

static int um_symlinkat(vfs_handle_struct *handle,
			const struct smb_filename *link_contents,
			struct files_struct *dirfsp,
			const struct smb_filename *new_smb_fname)
{
	int status;
	struct smb_filename *new_link_target = NULL;
	struct smb_filename *new_client_fname = NULL;
	struct smb_filename *full_fname = NULL;

	DEBUG(10, ("Entering um_symlinkat\n"));

	full_fname = full_path_from_dirfsp_atname(talloc_tos(),
						dirfsp,
						new_smb_fname);
	if (full_fname == NULL) {
		return -1;
	}

	if (!is_in_media_files(link_contents->base_name) &&
			!is_in_media_files(full_fname->base_name)) {
		TALLOC_FREE(full_fname);
		return SMB_VFS_NEXT_SYMLINKAT(handle,
				link_contents,
				dirfsp,
				new_smb_fname);
	}

	status = alloc_get_client_smb_fname(handle, talloc_tos(),
				link_contents, &new_link_target);
	if (status != 0) {
		goto err;
	}
	status = alloc_get_client_smb_fname(handle, talloc_tos(),
					    full_fname, &new_client_fname);
	if (status != 0) {
		goto err;
	}

	status = SMB_VFS_NEXT_SYMLINKAT(handle,
					new_link_target,
					handle->conn->cwd_fsp,
					new_client_fname);

err:
	TALLOC_FREE(new_link_target);
	TALLOC_FREE(new_client_fname);
	TALLOC_FREE(full_fname);
	return status;
}

static int um_readlinkat(vfs_handle_struct *handle,
			const struct files_struct *dirfsp,
			const struct smb_filename *smb_fname,
			char *buf,
			size_t bufsiz)
{
	int status;
	struct smb_filename *client_fname = NULL;
	struct smb_filename *full_fname = NULL;

	DEBUG(10, ("Entering um_readlinkat\n"));

	full_fname = full_path_from_dirfsp_atname(talloc_tos(),
						dirfsp,
						smb_fname);
	if (full_fname == NULL) {
		return -1;
	}

	if (!is_in_media_files(full_fname->base_name)) {
		TALLOC_FREE(full_fname);
		return SMB_VFS_NEXT_READLINKAT(handle,
				dirfsp,
				smb_fname,
				buf,
				bufsiz);
	}

	status = alloc_get_client_smb_fname(handle, talloc_tos(),
					    full_fname, &client_fname);
	if (status != 0) {
		goto err;
	}

	status = SMB_VFS_NEXT_READLINKAT(handle,
				handle->conn->cwd_fsp,
				client_fname,
				buf,
				bufsiz);

err:
	TALLOC_FREE(full_fname);
	TALLOC_FREE(client_fname);
	return status;
}

static int um_linkat(vfs_handle_struct *handle,
			files_struct *srcfsp,
			const struct smb_filename *old_smb_fname,
			files_struct *dstfsp,
			const struct smb_filename *new_smb_fname,
			int flags)
{
	int status;
	struct smb_filename *old_full_fname = NULL;
	struct smb_filename *new_full_fname = NULL;
	struct smb_filename *old_client_fname = NULL;
	struct smb_filename *new_client_fname = NULL;

	old_full_fname = full_path_from_dirfsp_atname(talloc_tos(),
						  srcfsp,
						  old_smb_fname);
	if (old_full_fname == NULL) {
		return -1;
	}
	new_full_fname = full_path_from_dirfsp_atname(talloc_tos(),
						  dstfsp,
						  new_smb_fname);
	if (new_full_fname == NULL) {
		TALLOC_FREE(old_full_fname);
		return -1;
	}

	DEBUG(10, ("Entering um_linkat\n"));
	if (!is_in_media_files(old_full_fname->base_name) &&
				!is_in_media_files(new_full_fname->base_name)) {
		TALLOC_FREE(old_full_fname);
		TALLOC_FREE(new_full_fname);
		return SMB_VFS_NEXT_LINKAT(handle,
				srcfsp,
				old_smb_fname,
				dstfsp,
				new_smb_fname,
				flags);
	}

	status = alloc_get_client_smb_fname(handle, talloc_tos(),
					    old_full_fname, &old_client_fname);
	if (status != 0) {
		goto err;
	}
	status = alloc_get_client_smb_fname(handle, talloc_tos(),
					    new_full_fname, &new_client_fname);
	if (status != 0) {
		goto err;
	}

	status = SMB_VFS_NEXT_LINKAT(handle,
				handle->conn->cwd_fsp,
				old_client_fname,
				handle->conn->cwd_fsp,
				new_client_fname,
				flags);

err:
	TALLOC_FREE(old_full_fname);
	TALLOC_FREE(new_full_fname);
	TALLOC_FREE(old_client_fname);
	TALLOC_FREE(new_client_fname);
	return status;
}

static int um_mknodat(vfs_handle_struct *handle,
		files_struct *dirfsp,
		const struct smb_filename *smb_fname,
		mode_t mode,
		SMB_DEV_T dev)
{
	int status;
	struct smb_filename *client_fname = NULL;
	struct smb_filename *full_fname = NULL;

	full_fname = full_path_from_dirfsp_atname(talloc_tos(),
						  dirfsp,
						  smb_fname);
	if (full_fname == NULL) {
		return -1;
	}

	DEBUG(10, ("Entering um_mknodat\n"));
	if (!is_in_media_files(full_fname->base_name)) {
		TALLOC_FREE(full_fname);
		return SMB_VFS_NEXT_MKNODAT(handle,
				dirfsp,
				smb_fname,
				mode,
				dev);
	}

	status = alloc_get_client_smb_fname(handle, talloc_tos(),
					    full_fname, &client_fname);
	if (status != 0) {
		goto err;
	}

	status = SMB_VFS_NEXT_MKNODAT(handle,
			handle->conn->cwd_fsp,
			client_fname,
			mode,
			dev);

err:
	TALLOC_FREE(client_fname);
	TALLOC_FREE(full_fname);
	return status;
}

static struct smb_filename *um_realpath(vfs_handle_struct *handle,
				TALLOC_CTX *ctx,
				const struct smb_filename *smb_fname)
{
	struct smb_filename *client_fname = NULL;
	struct smb_filename *result_fname = NULL;
	int status;

	DEBUG(10, ("Entering um_realpath\n"));

	if (!is_in_media_files(smb_fname->base_name)) {
		return SMB_VFS_NEXT_REALPATH(handle, ctx, smb_fname);
	}

	status = alloc_get_client_smb_fname(handle, talloc_tos(),
					    smb_fname, &client_fname);
	if (status != 0) {
		goto err;
	}

	result_fname = SMB_VFS_NEXT_REALPATH(handle, ctx, client_fname);

err:
	TALLOC_FREE(client_fname);
	return result_fname;
}

static int um_connect(vfs_handle_struct *handle,
			 const char *service,
			 const char *user)
{
	int rc;
	struct um_config_data *config;
	int enumval;

	rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
	if (rc != 0) {
		return rc;
	}

	config = talloc_zero(handle->conn, struct um_config_data);
	if (!config) {
		DEBUG(1, ("talloc_zero() failed\n"));
		errno = ENOMEM;
		return -1;
	}

	enumval = lp_parm_enum(SNUM(handle->conn), UM_PARAM_TYPE_NAME,
			       "clientid", um_clientid, UM_CLIENTID_NAME);
	if (enumval == -1) {
		DEBUG(1, ("value for %s: type unknown\n",
			  UM_PARAM_TYPE_NAME));
		return -1;
	}
	config->clientid = (enum um_clientid)enumval;

	SMB_VFS_HANDLE_SET_DATA(handle, config,
				NULL, struct um_config_data,
				return -1);

	return 0;
}

/* VFS operations structure */

static struct vfs_fn_pointers vfs_um_fns = {
	.connect_fn = um_connect,

	/* Disk operations */

	.statvfs_fn = um_statvfs,

	/* Directory operations */

	.fdopendir_fn = um_fdopendir,
	.readdir_fn = um_readdir,
	.rewind_dir_fn = um_rewinddir,
	.mkdirat_fn = um_mkdirat,
	.closedir_fn = um_closedir,

	/* File operations */

	.openat_fn = um_openat,
	.create_file_fn = um_create_file,
	.renameat_fn = um_renameat,
	.stat_fn = um_stat,
	.lstat_fn = um_lstat,
	.fstat_fn = um_fstat,
	.unlinkat_fn = um_unlinkat,
	.lchown_fn = um_lchown,
	.chdir_fn = um_chdir,
	.symlinkat_fn = um_symlinkat,
	.readlinkat_fn = um_readlinkat,
	.linkat_fn = um_linkat,
	.mknodat_fn = um_mknodat,
	.realpath_fn = um_realpath,

	/* EA operations. */
	.getxattrat_send_fn = vfs_not_implemented_getxattrat_send,
	.getxattrat_recv_fn = vfs_not_implemented_getxattrat_recv,
};

static_decl_vfs;
NTSTATUS vfs_unityed_media_init(TALLOC_CTX *ctx)
{
	NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
					"unityed_media", &vfs_um_fns);
	if (!NT_STATUS_IS_OK(ret)) {
		return ret;
	}

	vfs_um_debug_level = debug_add_class("unityed_media");

	if (vfs_um_debug_level == -1) {
		vfs_um_debug_level = DBGC_VFS;
		DEBUG(1, ("unityed_media_init: Couldn't register custom "
			  "debugging class.\n"));
	}

	return ret;
}