summaryrefslogtreecommitdiffstats
path: root/source3/modules/vfs_media_harmony.c
blob: a027254c6b384d986af5e638d3f37062987108fa (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
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
/*
 * $Id: media_harmony.c,v 1.1 2007/11/06 10:07:22 stuart_hc Exp $
 *
 * 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>
 *
 * 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.
 */


/*
 * Media Harmony is a Samba VFS module that allows multiple AVID
 * clients to share media. Each client sees their own copy of the
 * AVID msmMMOB.mdb and msmFMID.pmr files and Creating directories.
 *
 * Add this module to the vfs objects option in your Samba share
 * configuration.
 * eg.
 *
 *   [avid_win]
 *	path = /video
 *	vfs objects = media_harmony
 *	...
 *
 * 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.
 *
 *
 * Andrew Klaassen, 2012-03-14
 * To prevent Avid clients from interrupting each other (via Avid's habit
 * of launching a database refresh whenever it notices an mtime update
 * on media directories, i.e. whenever one editor adds new material to a
 * shared share), I've added code that causes stat information for anything
 * directly under "Avid MediaFile/MXF" to be taken from
 * dirname_clientaddr_clientuser if it exists.  These files ~aren't~
 * hidden, unlike the client-suffixed database files.
 *
 * For example, stat information for
 *	Avid MediaFiles/MXF/1
 * will come from
 *	Avid MediaFiles/MXF/1_192.168.1.10_dave
 * for dave working on 192.168.1.10, but will come from
 *	Avid MediaFile/MXF/1_192.168.1.11_susan
 * for susan working on 192.168.1.11.  If those alternate
 * directories don't exist, the user will get the actual directory's stat
 * info.  When an editor wants to force a database refresh, they update
 * the mtime on "their" file.  This will cause Avid
 * on that client to see an updated mtime for "Avid MediaFiles/MXF/1",
 * which will trigger an Avid database refresh just for that editor.
 *
 *
 * 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. For example: it is possibly to open client specific
 * files such as msmMMOB.mdb_192.168.1.10_userx even though is doesn't
 * show up in a directory listing.
 *
 */


#include "includes.h"
#include "system/filesys.h"
#include "smbd/smbd.h"
#include "../smbd/globals.h"
#include "auth.h"
#include "../lib/tsocket/tsocket.h"

#define MH_INFO_DEBUG 10
#define MH_ERR_DEBUG 0

static const char* MDB_FILENAME = "msmMMOB.mdb";
static const size_t MDB_FILENAME_LEN = 11;
static const char* PMR_FILENAME = "msmFMID.pmr";
static const size_t PMR_FILENAME_LEN = 11;
static const char* CREATING_DIRNAME = "Creating";
static const size_t CREATING_DIRNAME_LEN = 8;
static const char* AVID_MEDIAFILES_DIRNAME = "Avid MediaFiles";
static const size_t AVID_MEDIAFILES_DIRNAME_LEN = 15;
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 const char* AVID_MXF_DIRNAME = "Avid MediaFiles/MXF";
static const size_t AVID_MXF_DIRNAME_LEN = 19;

static int vfs_mh_debug_level = DBGC_VFS;

/* supplements the directory list stream */
typedef struct mh_dirinfo_struct
{
	DIR* dirstream;
	char *dirpath;
	char *clientPath;
	bool isInMediaFiles;
	char *clientMDBFilename;
	char *clientPMRFilename;
	char *clientCreatingDirname;
} mh_dirinfo_struct;


/* Add "_<ip address>_<user name>" 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;
	char *raddr = NULL;

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

	raddr = tsocket_address_inet_addr_string(
			handle->conn->sconn->remote_address, talloc_tos());
	if (raddr == NULL)
	{
		errno = ENOMEM;
		status = -1;
		goto err;
	}

	/* talloc_asprintf_append uses talloc_realloc, which
	 * frees original 'path' memory so we don't have to.
	 */
	*path = talloc_asprintf_append(*path, "_%s_%s",
		raddr,
		handle->conn->session_info->unix_info->sanitized_username);
	if (*path == NULL)
	{
		DEBUG(MH_ERR_DEBUG, ("alloc_append_client_suffix "
					"out of memory\n"));
		errno = ENOMEM;
		status = -1;
		goto err;
	}
	DEBUG(MH_INFO_DEBUG, ("Leaving with *path '%s'\n", *path));
err:
	TALLOC_FREE(raddr);
	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(MH_INFO_DEBUG, ("Entering with fname '%s'\n", fname));

	if (strncmp(APPLE_DOUBLE_PREFIX, fname, APPLE_DOUBLE_PREFIX_LEN)
			== 0)
	{
		ret = True;
	}
	DEBUG(MH_INFO_DEBUG, ("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;

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

	/* Sometimes Samba gives us "./OMFI MediaFiles". */
	if (strncmp(path, "./", 2) == 0)
	{
		path_start = &path[2];
	}
	else {
		path_start = path;
	}

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

	DEBUG(MH_INFO_DEBUG, ("Leaving with ret '%s'\n",
				ret == True ? "True" : "False"));
	return ret;
}

/*
 * 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(MH_INFO_DEBUG, ("Entering with path '%s'\n", path));

	if (
		starts_with_media_dir(AVID_MEDIAFILES_DIRNAME,
				AVID_MEDIAFILES_DIRNAME_LEN, path)
		||
		starts_with_media_dir(OMFI_MEDIAFILES_DIRNAME,
				OMFI_MEDIAFILES_DIRNAME_LEN, path)
	)
	{
		ret = True;
	}
	DEBUG(MH_INFO_DEBUG, ("Leaving with ret '%s'\n",
				ret == True ? "True" : "False"));
	return ret;
}

/*
 * Returns depth of path under media directory.  Deals with the
 * occasional ..../. and ..../.. paths that get passed to stat.
 *
 * Assumes is_in_media_files has already been called and has returned
 * true for the path; if it hasn't, this function will likely crash
 * and burn.
 *
 * Not foolproof; something like "Avid MediaFiles/MXF/../foo/1"
 * would fool it.  Haven't seen paths like that getting to the
 * stat function yet, so ignoring that possibility for now.
 */
static int depth_from_media_dir(const char* media_dirname,
		size_t media_dirname_len, const char* path)
{
	int transition_count = 0;
	const char *path_start;
	const char *pathPtr;

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

	/* Sometimes Samba gives us "./OMFI MediaFiles". */
	if (strncmp(path, "./", 2) == 0)
	{
		path_start = &path[2];
	}
	else {
		path_start = path;
	}

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

	pathPtr = &path_start[media_dirname_len + 1];

	while(1)
	{
		if (*pathPtr == '\0' || *pathPtr == '/')
		{
			if (
				*(pathPtr - 1) == '.'
					&&
				*(pathPtr - 2) == '.'
					&&
				*(pathPtr - 3) == '/'
			)
			{
				transition_count--;
			}
			else if (
				!
				(
					*(pathPtr - 1) == '/'
					||
					(
						*(pathPtr - 1) == '.'
							&&
						*(pathPtr - 2) == '/'
					)
				)
			)
			{
				transition_count++;
			}
		}
		if (*pathPtr == '\0')
		{
			break;
		}
		pathPtr++;
	}

	DEBUG(MH_INFO_DEBUG, ("Leaving with transition_count '%i'\n",
				transition_count));
out:
	return transition_count;
}

/* Identifies MDB and PMR files at end of path. */
static bool is_avid_database(
		char *path,
		size_t path_len,
		const char *avid_db_filename,
		const size_t avid_db_filename_len)
{
	bool ret = False;

	DEBUG(MH_INFO_DEBUG, ("Entering with path '%s', "
			      "avid_db_filename '%s', "
			      "path_len '%i', "
			      "avid_db_filename_len '%i'\n",
			      path, avid_db_filename,
			      (int)path_len, (int)avid_db_filename_len));

	if (
		path_len > avid_db_filename_len
			&&
		strcmp(&path[path_len - avid_db_filename_len],
				avid_db_filename) == 0
			&&
		(
			path[path_len - avid_db_filename_len - 1] == '/'
			||
			(path_len > avid_db_filename_len
				+ APPLE_DOUBLE_PREFIX_LEN
				&&
			path[path_len - avid_db_filename_len
				- APPLE_DOUBLE_PREFIX_LEN - 1] == '/'
				&&
			is_apple_double(&path[path_len
				- avid_db_filename_len
				- APPLE_DOUBLE_PREFIX_LEN]))
		)
	)
	{
		ret = True;
	}
	DEBUG(MH_INFO_DEBUG, ("Leaving with ret '%s'\n",
				ret == True ? "True" : "False"));
	return ret;
}


/* Add client suffix to paths to MDB_FILENAME, PMR_FILENAME and
 * CREATING_SUBDIRNAME.
 *
 * 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,
		char **newPath)
{
	/* replace /CREATING_DIRNAME/ or /._CREATING_DIRNAME/
	 * directory in path - potentially in middle of path
	 * - with suffixed name.
	 */
	int status = 0;
	char* pathPtr;
	size_t intermPathLen;

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

	*newPath = talloc_strdup(ctx, path);
	if (*newPath == NULL)
	{
		DEBUG(MH_ERR_DEBUG, ("alloc_get_client_path ENOMEM #1\n"));
		errno = ENOMEM;
		status = -1;
		goto out;
	}
	DEBUG(MH_INFO_DEBUG, ("newPath #1 %s\n", *newPath));
	if (
		(pathPtr = strstr(path, CREATING_DIRNAME)) != NULL
			&&
		(
			*(pathPtr + CREATING_DIRNAME_LEN) == '\0'
			||
			*(pathPtr + CREATING_DIRNAME_LEN) == '/'
		)
			&&
		(
			(pathPtr - path > 0
				&&
			 *(pathPtr - 1) == '/')
			||
			(pathPtr - path > APPLE_DOUBLE_PREFIX_LEN
				&&
			*(pathPtr - APPLE_DOUBLE_PREFIX_LEN - 1) == '/'
				&&
			 is_apple_double(pathPtr - APPLE_DOUBLE_PREFIX_LEN))
		)
	)
	{
		/* Insert client suffix into path. */
		(*newPath)[pathPtr - path + CREATING_DIRNAME_LEN] = '\0';
		DEBUG(MH_INFO_DEBUG, ("newPath #2 %s\n", *newPath));

		if ((status = alloc_append_client_suffix(handle, newPath)))
		{
			goto out;
		}

		DEBUG(MH_INFO_DEBUG, ("newPath #3 %s\n", *newPath));
		*newPath = talloc_strdup_append(*newPath,
				pathPtr + CREATING_DIRNAME_LEN);
		if (*newPath == NULL)
		{
			DEBUG(MH_ERR_DEBUG, ("alloc_get_client_path "
						"ENOMEM #2\n"));
			errno = ENOMEM;
			status = -1;
			goto out;
		}
		DEBUG(MH_INFO_DEBUG, ("newPath #4 %s\n", *newPath));
	}

	/* replace /MDB_FILENAME or /PMR_FILENAME or /._MDB_FILENAME
	 * or /._PMR_FILENAME at newPath end with suffixed name.
	 */
	intermPathLen = strlen(*newPath);
	if (
		is_avid_database(*newPath, intermPathLen,
			MDB_FILENAME, MDB_FILENAME_LEN)
		||
		is_avid_database(*newPath, intermPathLen,
			PMR_FILENAME, PMR_FILENAME_LEN)
	)
	{
		DEBUG(MH_INFO_DEBUG, ("newPath #5 %s\n", *newPath));
		if ((status = alloc_append_client_suffix(handle, newPath)))
		{
			goto out;
		}
		DEBUG(MH_INFO_DEBUG, ("newPath #6 %s\n", *newPath));
	}
out:
	/* newPath must be freed in caller. */
	DEBUG(MH_INFO_DEBUG, ("Leaving with *newPath '%s'\n", *newPath));
	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 **clientFname)
{
	int status = 0;

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

	*clientFname = cp_smb_filename(ctx, smb_fname);
	if ((*clientFname) == NULL) {
		DEBUG(MH_ERR_DEBUG, ("alloc_get_client_smb_fname "
					"NTERR\n"));
		errno = ENOMEM;
		status = -1;
		goto err;
	}
	if ((status = alloc_get_client_path(handle, ctx,
				smb_fname->base_name,
				&(*clientFname)->base_name)))
	{
		goto err;
	}
	DEBUG(MH_INFO_DEBUG, ("Leaving with (*clientFname)->base_name "
				"'%s'\n", (*clientFname)->base_name));
err:
	return status;
}


/*
 * 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 *avid_db_filename)
{
	int status = 0;

	DEBUG(MH_INFO_DEBUG, ("Entering with avid_db_filename '%s'\n",
			      avid_db_filename));

	if ((*path = talloc_strdup(ctx, avid_db_filename)) == NULL)
	{
		DEBUG(MH_ERR_DEBUG, ("alloc_set_client_dirinfo_path "
					"ENOMEM\n"));
		errno = ENOMEM;
		status = -1;
		goto err;
	}
	if ((status = alloc_append_client_suffix(handle, path)))
	{
		goto err;
	}
	DEBUG(MH_INFO_DEBUG, ("Leaving with *path '%s'\n", *path));
err:
	return status;
}

/*
 * Replace mtime on clientFname with mtime from client-suffixed
 * equivalent, if it exists.
 *
 * Success: return 0
 * Failure: set errno, return -1
 */
static int set_fake_mtime(vfs_handle_struct *handle,
		TALLOC_CTX *ctx,
		struct smb_filename **clientFname,
		int (*statFn)(const char *, SMB_STRUCT_STAT *, bool))
{
	int status = 0;
	char *statPath;
	SMB_STRUCT_STAT fakeStat;
	int copy_len;

	DEBUG(MH_INFO_DEBUG, ("Entering with (*clientFname)->base_name "
			      "'%s', (*clientFname)->st.st_ex_mtime %s",
			      (*clientFname)->base_name,
			      ctime(&((*clientFname)->st.st_ex_mtime.tv_sec))));

	if (
		depth_from_media_dir(AVID_MXF_DIRNAME,
				AVID_MXF_DIRNAME_LEN,
				(*clientFname)->base_name)
			!= 1
			&&
		depth_from_media_dir(OMFI_MEDIAFILES_DIRNAME,
				OMFI_MEDIAFILES_DIRNAME_LEN,
				(*clientFname)->base_name)
			!= 0
	)
	{
		goto out;
	}

	copy_len = strlen((*clientFname)->base_name);

	/* Hack to deal with occasional "Avid MediaFiles/MXF/1/." paths.
	 * We know we're under a media dir, so paths are at least 2 chars
	 * long.
	 */
	if ((*clientFname)->base_name[copy_len - 1] == '.' &&
			(*clientFname)->base_name[copy_len - 2] == '/')
	{
		copy_len -= 2;
	}

	if (((statPath = talloc_strndup(ctx,
			(*clientFname)->base_name, copy_len)) == NULL))
	{
		errno = ENOMEM;
		status = -1;
		goto err;
	}
	if ((status = alloc_append_client_suffix(handle, &statPath)))
	{
		goto err;
	}

	DEBUG(MH_INFO_DEBUG, ("Fake stat'ing '%s'\n", statPath));
	if (statFn(statPath, &fakeStat,
			lp_fake_directory_create_times(SNUM(handle->conn))))
	{
		/* This can fail for legitimate reasons - i.e. the
		 * fakeStat directory doesn't exist, which is okay
		 * - so we don't set status.  But if it does fail,
		 * we need to skip over the mtime assignment.
		 */
		goto err;
	}

	DEBUG(MH_INFO_DEBUG, ("Setting fake mtime from '%s'\n", statPath));
	(*clientFname)->st.st_ex_mtime = fakeStat.st_ex_mtime;
err:
	TALLOC_FREE(statPath);
out:
	DEBUG(MH_INFO_DEBUG, ("Leaving with (*clientFname)->base_name "
			"'%s', (*clientFname)->st.st_ex_mtime %s",
			(*clientFname)->base_name,
			ctime(&((*clientFname)->st.st_ex_mtime.tv_sec))));
	return status;
}

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

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

	if (!is_in_media_files(smb_fname->base_name))
	{
		status = SMB_VFS_NEXT_STATVFS(handle, smb_fname, statbuf);
		goto out;
	}

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

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

static int alloc_set_client_dirinfo(vfs_handle_struct *handle,
		const char *fname,
		struct mh_dirinfo_struct **dirInfo)
{
	int status = 0;
	char *clientPath;
	TALLOC_CTX *ctx;

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

	*dirInfo = talloc(NULL, struct mh_dirinfo_struct);
	if (*dirInfo == NULL)
	{
		goto err;
	}

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

	if (!is_in_media_files(fname))
	{
		(*dirInfo)->clientPath = NULL;
		(*dirInfo)->clientMDBFilename = NULL;
		(*dirInfo)->clientPMRFilename = NULL;
		(*dirInfo)->clientCreatingDirname = NULL;
		(*dirInfo)->isInMediaFiles = False;
		goto out;
	}

	(*dirInfo)->isInMediaFiles = True;

	if (alloc_set_client_dirinfo_path(handle,
				*dirInfo,
				&((*dirInfo)->clientMDBFilename),
				MDB_FILENAME))
	{
		goto err;
	}

	if (alloc_set_client_dirinfo_path(handle,
				*dirInfo,
				&((*dirInfo)->clientPMRFilename),
				PMR_FILENAME))
	{
		goto err;
	}

	if (alloc_set_client_dirinfo_path(handle,
				*dirInfo,
				&((*dirInfo)->clientCreatingDirname),
				CREATING_DIRNAME))
	{
		goto err;
	}

	clientPath = NULL;
	ctx = talloc_tos();

	if (alloc_get_client_path(handle, ctx,
				fname,
				&clientPath))
	{
		goto err;
	}

	(*dirInfo)->clientPath = talloc_strdup(*dirInfo, clientPath);
	if ((*dirInfo)->clientPath == NULL)
	{
		goto err;
	}

	TALLOC_FREE(clientPath);

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

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

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

	DEBUG(MH_INFO_DEBUG, ("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) {
		goto out;
	}

	if (set_fake_mtime(handle, fsp, &(fsp->fsp_name), sys_stat))
	{
		goto err;
	}

out:
	DEBUG(MH_INFO_DEBUG, ("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))));
	/* Success is freed in closedir. */
	return (DIR *) dirInfo;
err:
	/* Failure is freed here. */
	DEBUG(MH_ERR_DEBUG, ("Failing with fsp->fsp_name->base_name '%s'\n",
			fsp->fsp_name->base_name));
	TALLOC_FREE(dirInfo);
	return NULL;
}

/*
 * skip MDB_FILENAME and PMR_FILENAME filenames and CREATING_DIRNAME
 * directory, skip other client's suffixed MDB_FILENAME and PMR_FILENAME
 * filenames and CREATING_DIRNAME directory, replace this client's
 * suffixed MDB_FILENAME and PMR_FILENAME filenames and CREATING_DIRNAME
 * directory with non suffixed.
 *
 * Success: return dirent
 * End of data: return NULL
 * Failure: set errno, return NULL
 */
static struct dirent *
mh_readdir(vfs_handle_struct *handle, struct files_struct *dirfsp, DIR *dirp)
{
	mh_dirinfo_struct* dirInfo = (mh_dirinfo_struct*)dirp;
	struct dirent *d = NULL;
	int skip;

	DEBUG(MH_INFO_DEBUG, ("Entering mh_readdir\n"));

	DEBUG(MH_INFO_DEBUG, ("dirInfo->dirpath '%s', "
			      "dirInfo->clientPath '%s', "
			      "dirInfo->isInMediaFiles '%s', "
			      "dirInfo->clientMDBFilename '%s', "
			      "dirInfo->clientPMRFilename '%s', "
			      "dirInfo->clientCreatingDirname '%s'\n",
			      dirInfo->dirpath,
			      dirInfo->clientPath,
			      dirInfo->isInMediaFiles ? "True" : "False",
			      dirInfo->clientMDBFilename,
			      dirInfo->clientPMRFilename,
			      dirInfo->clientCreatingDirname));

	if (! dirInfo->isInMediaFiles)
	{
		d = SMB_VFS_NEXT_READDIR(handle, dirfsp, dirInfo->dirstream);
		goto out;
	}

	do
	{
		const char* dname;
		bool isAppleDouble;

		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;
		}

		/* skip Avid-special files with no client suffix */
		if (
			strcmp(dname, MDB_FILENAME) == 0
			||
			strcmp(dname, PMR_FILENAME) == 0
			||
			strcmp(dname, CREATING_DIRNAME) == 0
		)
		{
			skip = True;
		}
		/* chop client suffix off this client's suffixed files */
		else if (strcmp(dname, dirInfo->clientMDBFilename) == 0)
		{
			if (isAppleDouble)
			{
				d->d_name[MDB_FILENAME_LEN
					+ APPLE_DOUBLE_PREFIX_LEN] = '\0';
			}
			else
			{
				d->d_name[MDB_FILENAME_LEN] = '\0';
			}
		}
		else if (strcmp(dname, dirInfo->clientPMRFilename) == 0)
		{
			if (isAppleDouble)
			{
				d->d_name[PMR_FILENAME_LEN
					+ APPLE_DOUBLE_PREFIX_LEN] = '\0';
			}
			else
			{
				d->d_name[PMR_FILENAME_LEN] = '\0';
			}
		}
		else if (strcmp(dname, dirInfo->clientCreatingDirname)
				== 0)
		{
			if (isAppleDouble)
			{
				d->d_name[CREATING_DIRNAME_LEN
					+ APPLE_DOUBLE_PREFIX_LEN] = '\0';
			}
			else
			{
				d->d_name[CREATING_DIRNAME_LEN] = '\0';
			}
		}
		/*
		 * Anything that starts as an Avid-special file
		 * that's made it this far should be skipped.  This
		 * is different from the original behaviour, which
		 * only skipped other client's suffixed files.
		 */
		else if (
			strncmp(MDB_FILENAME, dname,
				MDB_FILENAME_LEN) == 0
			||
			strncmp(PMR_FILENAME, dname,
				PMR_FILENAME_LEN) == 0
			||
			strncmp(CREATING_DIRNAME, dname,
				CREATING_DIRNAME_LEN) == 0
		)
		{
			skip = True;
		}
	}
	while (skip);

out:
	DEBUG(MH_INFO_DEBUG, ("Leaving mh_readdir\n"));
	return d;
}

/*
 * Success: no success result defined.
 * Failure: no failure result defined.
 */
static void mh_rewinddir(vfs_handle_struct *handle,
		DIR *dirp)
{
	DEBUG(MH_INFO_DEBUG, ("Entering and leaving mh_rewinddir\n"));
	SMB_VFS_NEXT_REWINDDIR(handle,
			((mh_dirinfo_struct*)dirp)->dirstream);
}

/*
 * Success: return 0
 * Failure: set errno, return -1
 */
static int mh_mkdirat(vfs_handle_struct *handle,
		struct files_struct *dirfsp,
		const struct smb_filename *smb_fname,
		mode_t mode)
{
	int status;
	struct smb_filename *clientFname = NULL;
	const char *path = smb_fname->base_name;
	struct smb_filename *full_fname = NULL;

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

	if (!is_in_media_files(path)) {
		status = SMB_VFS_NEXT_MKDIRAT(handle,
				dirfsp,
				smb_fname,
				mode);
		goto out;
	}

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

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

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

/*
 * Success: return 0
 * Failure: set errno, return -1
 */
static int mh_closedir(vfs_handle_struct *handle,
		DIR *dirp)
{
	DIR *realdirp = ((mh_dirinfo_struct*)dirp)->dirstream;

	DEBUG(MH_INFO_DEBUG, ("Entering mh_closedir\n"));
	// Will this talloc_free destroy realdirp?
	TALLOC_FREE(dirp);

	DEBUG(MH_INFO_DEBUG, ("Leaving mh_closedir\n"));
	return SMB_VFS_NEXT_CLOSEDIR(handle, realdirp);
}

/*
 * Success: return non-negative file descriptor
 * Failure: set errno, return -1
 */
static int mh_openat(struct vfs_handle_struct *handle,
		     const struct files_struct *dirfsp,
		     const struct smb_filename *smb_fname,
		     files_struct *fsp,
		     const struct vfs_open_how *how)
{
	int ret;
	struct smb_filename *clientFname;
	TALLOC_CTX *ctx;

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

	if (!is_in_media_files(smb_fname->base_name)) {
		ret = SMB_VFS_NEXT_OPENAT(handle,
					  dirfsp,
					  smb_fname,
					  fsp,
					  how);
		goto out;
	}

	clientFname = NULL;
	ctx = talloc_tos();

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

	/*
	 * 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(MH_INFO_DEBUG, ("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, clientFname, fsp, how);
err:
	TALLOC_FREE(clientFname);
out:
	DEBUG(MH_INFO_DEBUG, ("Leaving with smb_fname->base_name '%s'\n",
				smb_fname->base_name));
	return ret;
}

/*
 * Success: return non-negative file descriptor
 * Failure: set errno, return -1
 */
static NTSTATUS mh_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 *clientFname;
	TALLOC_CTX *ctx;


	DEBUG(MH_INFO_DEBUG, ("Entering with smb_fname->base_name '%s'\n",
				smb_fname->base_name));
	if (!is_in_media_files(smb_fname->base_name))
	{
		status = 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);
		goto out;
	}

	clientFname = NULL;
	ctx = talloc_tos();

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

	/* 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,
		clientFname,
		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(clientFname);
out:
	DEBUG(MH_INFO_DEBUG, ("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;
}

/*
 * Success: return 0
 * Failure: set errno, return -1
 */
static int mh_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 = -1;
	struct smb_filename *full_fname_src = NULL;
	struct smb_filename *full_fname_dst = NULL;
	struct smb_filename *srcClientFname = NULL;
	struct smb_filename *dstClientFname = NULL;

	DEBUG(MH_INFO_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(smb_fname_src->base_name)
				&&
			!is_in_media_files(smb_fname_dst->base_name))
	{
		status = SMB_VFS_NEXT_RENAMEAT(handle,
				srcfsp,
				smb_fname_src,
				dstfsp,
				smb_fname_dst);
		goto out;
	}

	full_fname_src = full_path_from_dirfsp_atname(talloc_tos(),
						      srcfsp,
						      smb_fname_src);
	if (full_fname_src == NULL) {
		errno = ENOMEM;
		goto out;
        }
	full_fname_dst = full_path_from_dirfsp_atname(talloc_tos(),
						      dstfsp,
						      smb_fname_dst);
	if (full_fname_dst == NULL) {
		errno = ENOMEM;
		goto out;
        }

	if ((status = alloc_get_client_smb_fname(handle,
				talloc_tos(),
				full_fname_src,
				&srcClientFname)))
	{
		goto err;
	}

	if ((status = alloc_get_client_smb_fname(handle,
				talloc_tos(),
				full_fname_dst,
				&dstClientFname)))
	{
		goto err;
	}

	status = SMB_VFS_NEXT_RENAMEAT(handle,
				srcfsp->conn->cwd_fsp,
				srcClientFname,
				dstfsp->conn->cwd_fsp,
				dstClientFname);
err:
	TALLOC_FREE(full_fname_src);
	TALLOC_FREE(full_fname_dst);
	TALLOC_FREE(dstClientFname);
	TALLOC_FREE(srcClientFname);
out:
	DEBUG(MH_INFO_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 mh_stat(vfs_handle_struct *handle,
		struct smb_filename *smb_fname)
{
	int status = 0;
	struct smb_filename *clientFname;
	TALLOC_CTX *ctx;


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

	if (!is_in_media_files(smb_fname->base_name))
	{
		status = SMB_VFS_NEXT_STAT(handle, smb_fname);
		goto out;
	}

	clientFname = NULL;
	ctx = talloc_tos();

	if ((status = alloc_get_client_smb_fname(handle, ctx,
				smb_fname,
				&clientFname)))
	{
		goto err;
	}
	DEBUG(MH_INFO_DEBUG, ("Stat'ing clientFname->base_name '%s'\n",
				clientFname->base_name));
	if ((status = SMB_VFS_NEXT_STAT(handle, clientFname)))
	{
		goto err;
	}
	if ((status = set_fake_mtime(handle, ctx, &clientFname, sys_stat)))
	{
		goto err;
	}

	/* Unlike functions with const smb_filename, we have to
	 * modify smb_fname itself to pass our info back up.
	 */
	DEBUG(MH_INFO_DEBUG, ("Setting smb_fname '%s' stat "
				"from clientFname '%s'\n",
				smb_fname->base_name,
				clientFname->base_name));
	smb_fname->st = clientFname->st;
err:
	TALLOC_FREE(clientFname);
out:
	DEBUG(MH_INFO_DEBUG, ("Leaving with smb_fname->st.st_ex_mtime %s",
			ctime(&(smb_fname->st.st_ex_mtime.tv_sec))));
	return status;
}

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

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

	if (!is_in_media_files(smb_fname->base_name))
	{
		status = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
		goto out;
	}

	clientFname = NULL;
	ctx = talloc_tos();

	if ((status = alloc_get_client_smb_fname(handle, ctx,
				smb_fname,
				&clientFname)))
	{
		goto err;
	}
	if ((status = SMB_VFS_NEXT_LSTAT(handle, clientFname)))
	{
		goto err;
	}

	if ((status = set_fake_mtime(handle, ctx, &clientFname, sys_lstat)))
	{
		goto err;
	}
	/* Unlike functions with const smb_filename, we have to
	 * modify smb_fname itself to pass our info back up.
	 */
	smb_fname->st = clientFname->st;
err:
	TALLOC_FREE(clientFname);
out:
	DEBUG(MH_INFO_DEBUG, ("Leaving with smb_fname->st.st_ex_mtime %s",
			ctime(&(smb_fname->st.st_ex_mtime.tv_sec))));
	return status;
}

/*
 * Success: return 0
 * Failure: set errno, return -1
 */
static int mh_fstat(vfs_handle_struct *handle,
		files_struct *fsp, SMB_STRUCT_STAT *sbuf)
{
	int status = 0;

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

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

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

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

	*sbuf = fsp->fsp_name->st;
out:
	DEBUG(MH_INFO_DEBUG, ("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"));
	return status;
}

/*
 * Success: return 0
 * Failure: set errno, return -1
 */
static int mh_unlinkat(vfs_handle_struct *handle,
		struct files_struct *dirfsp,
		const struct smb_filename *smb_fname,
		int flags)
{
	int status;
	struct smb_filename *full_fname = NULL;
	struct smb_filename *clientFname;
	TALLOC_CTX *ctx;

	DEBUG(MH_INFO_DEBUG, ("Entering mh_unlinkat\n"));
	if (!is_in_media_files(smb_fname->base_name)) {
		status = SMB_VFS_NEXT_UNLINKAT(handle,
				dirfsp,
				smb_fname,
				flags);
		goto out;
	}

	clientFname = NULL;
	ctx = talloc_tos();

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

	if ((status = alloc_get_client_smb_fname(handle, ctx,
				full_fname,
				&clientFname))) {
		goto err;
	}

	status = SMB_VFS_NEXT_UNLINKAT(handle,
				dirfsp->conn->cwd_fsp,
				clientFname,
				flags);
err:
	TALLOC_FREE(full_fname);
	TALLOC_FREE(clientFname);
out:
	return status;
}

/*
 * Success: return 0
 * Failure: set errno, return -1
 */
static int mh_lchown(vfs_handle_struct *handle,
		const struct smb_filename *smb_fname,
		uid_t uid,
		gid_t gid)
{
	int status;
	struct smb_filename *clientFname = NULL;

	DEBUG(MH_INFO_DEBUG, ("Entering mh_lchown\n"));
	if (!is_in_media_files(smb_fname->base_name))
	{
		status = SMB_VFS_NEXT_LCHOWN(handle, smb_fname, uid, gid);
		goto out;
	}

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

	status = SMB_VFS_NEXT_LCHOWN(handle, clientFname, uid, gid);
err:
	TALLOC_FREE(clientFname);
out:
	return status;
}

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

	DEBUG(MH_INFO_DEBUG, ("Entering mh_chdir\n"));
	if (!is_in_media_files(smb_fname->base_name)) {
		status = SMB_VFS_NEXT_CHDIR(handle, smb_fname);
		goto out;
	}

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

	status = SMB_VFS_NEXT_CHDIR(handle, clientFname);
err:
	TALLOC_FREE(clientFname);
out:
	return status;
}

/*
 * Success: return 0
 * Failure: set errno, return -1
 */

static int mh_symlinkat(vfs_handle_struct *handle,
		const struct smb_filename *link_contents,
		struct files_struct *dirfsp,
		const struct smb_filename *new_smb_fname)
{
	int status = -1;
	struct smb_filename *full_fname = NULL;
	struct smb_filename *new_link_target = NULL;
	struct smb_filename *newclientFname = NULL;

	DEBUG(MH_INFO_DEBUG, ("Entering mh_symlinkat\n"));

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

	if (!is_in_media_files(link_contents->base_name) &&
			!is_in_media_files(full_fname->base_name)) {
		status = SMB_VFS_NEXT_SYMLINKAT(handle,
				link_contents,
				dirfsp,
				new_smb_fname);
		goto out;
	}

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

	status = SMB_VFS_NEXT_SYMLINKAT(handle,
				new_link_target,
				handle->conn->cwd_fsp,
				newclientFname);
err:
	TALLOC_FREE(new_link_target);
	TALLOC_FREE(newclientFname);
out:
	TALLOC_FREE(full_fname);
	return status;
}

/*
 * Success: return byte count
 * Failure: set errno, return -1
 */
static int mh_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 *full_fname = NULL;
	struct smb_filename *clientFname = NULL;

	DEBUG(MH_INFO_DEBUG, ("Entering mh_readlinkat\n"));
	full_fname = full_path_from_dirfsp_atname(talloc_tos(),
						dirfsp,
						smb_fname);
	if (full_fname == NULL) {
		status = -1;
		goto err;
	}

	if (!is_in_media_files(full_fname->base_name)) {
		status = SMB_VFS_NEXT_READLINKAT(handle,
				dirfsp,
				smb_fname,
				buf,
				bufsiz);
		goto out;
	}

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

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

err:
	TALLOC_FREE(clientFname);
out:
	TALLOC_FREE(full_fname);
	return status;
}

/*
 * Success: return 0
 * Failure: set errno, return -1
 */
static int mh_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 *oldclientFname = NULL;
	struct smb_filename *new_full_fname = NULL;
	struct smb_filename *newclientFname = NULL;

	DEBUG(MH_INFO_DEBUG, ("Entering mh_linkat\n"));

	old_full_fname = full_path_from_dirfsp_atname(talloc_tos(),
						srcfsp,
						old_smb_fname);
	if (old_full_fname == NULL) {
		status = -1;
		goto err;
	}

	new_full_fname = full_path_from_dirfsp_atname(talloc_tos(),
						dstfsp,
						new_smb_fname);
	if (new_full_fname == NULL) {
		status = -1;
		goto err;
	}

	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);

		status = SMB_VFS_NEXT_LINKAT(handle,
				srcfsp,
				old_smb_fname,
				dstfsp,
				new_smb_fname,
				flags);
		goto out;
	}

	if ((status = alloc_get_client_smb_fname(handle, talloc_tos(),
				old_full_fname,
				&oldclientFname))) {
		goto err;
	}
	if ((status = alloc_get_client_smb_fname(handle, talloc_tos(),
				new_full_fname,
				&newclientFname))) {
		goto err;
	}

	status = SMB_VFS_NEXT_LINKAT(handle,
				handle->conn->cwd_fsp,
				oldclientFname,
				handle->conn->cwd_fsp,
				newclientFname,
				flags);

err:
	TALLOC_FREE(old_full_fname);
	TALLOC_FREE(new_full_fname);
	TALLOC_FREE(newclientFname);
	TALLOC_FREE(oldclientFname);
out:
	return status;
}

/*
 * Success: return 0
 * Failure: set errno, return -1
 */
static int mh_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 *full_fname = NULL;
	struct smb_filename *clientFname = NULL;
	TALLOC_CTX *ctx;

	DEBUG(MH_INFO_DEBUG, ("Entering mh_mknodat\n"));

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

	if (!is_in_media_files(full_fname->base_name)) {
		status = SMB_VFS_NEXT_MKNODAT(handle,
				dirfsp,
				smb_fname,
				mode,
				dev);
		goto out;
	}

	ctx = talloc_tos();

	if ((status = alloc_get_client_smb_fname(handle, ctx,
				full_fname,
				&clientFname))) {
		goto err;
	}

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

err:
	TALLOC_FREE(clientFname);
out:
	TALLOC_FREE(full_fname);
	return status;
}

/*
 * Success: return path pointer
 * Failure: set errno, return NULL pointer
 */
static struct smb_filename *mh_realpath(vfs_handle_struct *handle,
				TALLOC_CTX *ctx,
				const struct smb_filename *smb_fname)
{
	struct smb_filename *result_fname = NULL;
	struct smb_filename *clientFname = NULL;

	DEBUG(MH_INFO_DEBUG, ("Entering mh_realpath\n"));
	if (!is_in_media_files(smb_fname->base_name)) {
		return SMB_VFS_NEXT_REALPATH(handle, ctx, smb_fname);
	}

	if (alloc_get_client_smb_fname(handle, ctx,
				smb_fname,
				&clientFname) != 0) {
		goto err;
	}

	result_fname = SMB_VFS_NEXT_REALPATH(handle, ctx, clientFname);
err:
	TALLOC_FREE(clientFname);
	return result_fname;
}

/* Ignoring get_real_filename function because the default
 * doesn't do anything.
 */

/*
 * Success: return 0
 * Failure: set errno, return -1
 * In this case, "name" is an attr name.
 */

/* VFS operations structure */

static struct vfs_fn_pointers vfs_mh_fns = {
	/* Disk operations */

	.statvfs_fn = mh_statvfs,

	/* Directory operations */

	.fdopendir_fn = mh_fdopendir,
	.readdir_fn = mh_readdir,
	.rewind_dir_fn = mh_rewinddir,
	.mkdirat_fn = mh_mkdirat,
	.closedir_fn = mh_closedir,

	/* File operations */

	.openat_fn = mh_openat,
	.create_file_fn = mh_create_file,
	.renameat_fn = mh_renameat,
	.stat_fn = mh_stat,
	.lstat_fn = mh_lstat,
	.fstat_fn = mh_fstat,
	.unlinkat_fn = mh_unlinkat,
	.lchown_fn = mh_lchown,
	.chdir_fn = mh_chdir,
	.symlinkat_fn = mh_symlinkat,
	.readlinkat_fn = mh_readlinkat,
	.linkat_fn = mh_linkat,
	.mknodat_fn = mh_mknodat,
	.realpath_fn = mh_realpath,

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

	/* aio operations */
};

static_decl_vfs;
NTSTATUS vfs_media_harmony_init(TALLOC_CTX *ctx)
{
	NTSTATUS ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
				"media_harmony", &vfs_mh_fns);
	if (!NT_STATUS_IS_OK(ret))
	{
		goto out;
	}

	vfs_mh_debug_level = debug_add_class("media_harmony");

	if (vfs_mh_debug_level == -1) {
		vfs_mh_debug_level = DBGC_VFS;
		DEBUG(1, ("media_harmony_init: Couldn't register custom "
				"debugging class.\n"));
	} else {
		DEBUG(3, ("media_harmony_init: Debug class number of "
				"'media_harmony': %d\n",
				vfs_mh_debug_level));
	}

out:
	return ret;
}