- Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathisc_file.cpp
1938 lines (1655 loc) · 44.4 KB
/
isc_file.cpp
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
/*
* PROGRAM: JRD Access Method
* MODULE: isc_file.cpp
* DESCRIPTION: General purpose but non-user routines.
*
* The contents of this file are subject to the Interbase Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy
* of the License at http://www.Inprise.com/IPL.html
*
* Software distributed under the License is distributed on an
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
* or implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code was created by Inprise Corporation
* and its predecessors. Portions created by Inprise Corporation are
* Copyright (C) Inprise Corporation.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
* 2001.06.14: Claudio Valderrama: Possible buffer overrun in
* expand_share_name(TEXT*) has been closed. Parameter is return value, too.
* This function and its caller in this same file don't report error conditions.
* 2002.02.15 Sean Leyne - Code Cleanup, removed obsolete "EPSON" port
* 2002.02.15 Sean Leyne - Code Cleanup, removed obsolete "DELTA" port
* 2002-02-23 Sean Leyne - Code Cleanup, removed old M88K and NCR3000 port
*
* 2002.10.27 Sean Leyne - Code Cleanup, removed obsolete "UNIXWARE" port
* 2002.10.27 Sean Leyne - Code Cleanup, removed obsolete "Ultrix" port
*
* 2002.10.28 Sean Leyne - Completed removal of obsolete "DGUX" port
* 2002.10.28 Sean Leyne - Code cleanup, removed obsolete "DecOSF" port
*
* 2002.10.29 Sean Leyne - Removed support for obsolete IPX/SPX Protocol
* 2002.10.29 Sean Leyne - Removed obsolete "Netware" port
*
* 2002.10.30 Sean Leyne - Removed support for obsolete "PC_PLATFORM" define
* 2002.10.30 Sean Leyne - Code Cleanup, removed obsolete "SUN3_3" port
*
*/
#include"firebird.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include"iberror.h"
#include"../yvalve/gds_proto.h"
#include"../common/isc_proto.h"
#include"../common/isc_f_proto.h"
#include"../jrd/jrd_proto.h"
#include"../common/config/config.h"
#include"../common/config/dir_list.h"
#include"../common/classes/init.h"
#include"../common/classes/Aligner.h"
#include"../common/utils_proto.h"
#include"../common/os/os_utils.h"
#include"../common/os/path_utils.h"
#include<sys/types.h>
#ifdef HAVE_SYS_IPC_H
#include<sys/ipc.h>
#endif
#ifdef HAVE_SYS_FILE_H
#include<sys/file.h>
#endif
#include<errno.h>
#ifdef HAVE_UNISTD_H
#include<unistd.h>
#endif
#ifdef HAVE_PWD_H
#include<pwd.h>
#endif
#ifdef HAVE_SYS_PARAM_H
#include<sys/param.h>
#endif
#ifdef HAVE_SYS_MOUNT_H
#include<sys/mount.h>
#endif
#ifdef HAVE_LANGINFO_H
#include<langinfo.h>
#endif
#ifdef HAVE_ICONV_H
#include<iconv.h>
#endif
#ifdef LINUX
#include<sys/sysmacros.h>
#endif
#include"../common/config/config.h"
constchar INET_FLAG = ':';
// Unix/NFS specific stuff
#ifndef NO_NFS
#if defined(HAVE_MNTENT_H)
#include<mntent.h>// get setmntent/endmntent
#elif defined(HAVE_SYS_MNTTAB_H)
#include<sys/mnttab.h>// get MNTTAB/_PATH_MNTTAB
#elif defined(AIX)
#error ancient versions of AIX that do not provide "<mntent.h>" are not
#error supported. AIX 5.1+ provides this header.
#endif
#if defined(_PATH_MOUNTED)
constchar* const MTAB = _PATH_MOUNTED;
#elif defined(HPUX)
constchar* const MTAB = "/etc/mnttab";
#elif defined(SOLARIS)
constchar* const MTAB = "/etc/mnttab";
#elif defined(FREEBSD)
constchar* const MTAB = "/etc/fstab";
#else
constchar* const MTAB = "/etc/mtab";
#endif
#ifdef HAVE_SETMNTENT
#defineMTAB_OPEN(path, type) setmntent(path, "r")
#defineMTAB_CLOSE(stream) endmntent(stream)
#else
#defineMTAB_OPEN(path, type) os_utils::fopen(path, type)
#defineMTAB_CLOSE(stream) fclose(stream)
#endif
#endif//NO_NFS
#if defined(HPUX) && (!defined HP11)
#include<cluster.h>
#endif
#ifndef MAXHOSTLEN
#defineMAXHOSTLEN64
#endif
usingnamespaceFirebird;
namespace {
typedef Firebird::PathName tstring;
typedef tstring::size_type size;
typedef tstring::iterator iter;
const size npos = tstring::npos;
#ifndef NO_NFS
constchar* NFS_TYPE = "nfs";
Firebird::GlobalPtr<Firebird::Mutex> mntinfoMutex;
classMnt : publicFirebird::MutexLockGuard // Protect static values returned by getmntinfo()/getmntent()
{
#ifdef DARWIN
private:
structstatfs* mnt_info;
int mnt_cnt;
int mnt_i;
public:
Mnt()
: Firebird::MutexLockGuard(mntinfoMutex, FB_FUNCTION),
mnt_info(NULL), mnt_cnt(getmntinfo(&mnt_info, MNT_NOWAIT)), mnt_i(0)
{ }
boolok() const { returnthis->mnt_cnt > 0; }
#else
private:
FILE* mtab;
public:
Mnt()
: Firebird::MutexLockGuard(mntinfoMutex, FB_FUNCTION),
mtab(MTAB_OPEN(MTAB, "r"))
{ }
~Mnt()
{
if (mtab)
MTAB_CLOSE(mtab);
}
boolok() const { return mtab; }
#endif
public:
boolget();
tstring
mount, // local mount point
special, // mounted
type; // mount type
};
#endif//NO_NFS
} // anonymous namespace
#ifndef WIN_NT
staticvoidexpand_filename2(tstring&, bool);
#endif
#if defined(WIN_NT)
staticvoidtranslate_slashes(tstring&);
staticvoidexpand_share_name(tstring&);
staticvoidshare_name_from_resource(tstring&, LPNETRESOURCE);
staticvoidshare_name_from_unc(tstring&, LPREMOTE_NAME_INFO);
staticboolget_full_path(const tstring&, tstring&);
#endif
#if defined(HPUX) && (!defined HP11)
staticboolget_server(tstring&, tstring&);
#endif
#ifndef NO_NFS
boolISC_analyze_nfs(tstring& expanded_filename, tstring& node_name)
{
/**************************************
*
* I S C _ a n a l y z e _ n f s
*
**************************************
*
* Functional description
* Check a file name for an NFS mount point. If so,
* decompose into node name and remote file name.
*
**************************************/
// If we are ignoring NFS remote mounts then do not bother checking here
// and pretend it's only local. MOD 16-Nov-2002
if (Config::getRemoteFileOpenAbility())
returnfalse;
#ifdef LINUX
// In order to avoid analyzing mtab in most cases check for non-device mounts first
structstat fileStat;
unsigned m = 1; // use something that is known to be not non-device major
if (os_utils::stat(expanded_filename.c_str(), &fileStat) == 0)
m = major(fileStat.st_dev);
else// stat error - let's try with path component
{
tstring path, name;
PathUtils::splitLastComponent(path, name, expanded_filename);
if (path.hasData() && os_utils::stat(path.c_str(), &fileStat) == 0)
m = major(fileStat.st_dev);
}
if (m != 0 && m != 144 && m != 145 && m != 146)
{
// device mount or stat for file/path is impossible - definitely not NFS
returnfalse;
}
// proceed with deeper analysis
#endif
tstring max_node, max_path;
size_t len = 0;
// Search mount points
Mnt mount;
if (!mount.ok())
{
returnfalse;
}
while (mount.get())
{
tstring node, path;
// Include non-NFS (local) mounts - some may be longer than
// NFS mount points, therefore ignore mnt_type
if (mount.type == NFS_TYPE)
{
size colon = mount.special.find(':');
if (colon != tstring::npos)
{
node = mount.special.substr(0, colon);
path = mount.special.substr(colon + 1);
}
}
// first, expand any symbolic links in the mount point
ISC_expand_filename(mount.mount, false);
// if the whole mount point is not contained in the expanded_filename
// or the mount point is not a valid pathname in the expanded_filename,
// skip it
if (expanded_filename.length() <= mount.mount.length() ||
expanded_filename.compare(0, mount.mount.length(), mount.mount) != 0 ||
expanded_filename[mount.mount.length()] != '/')
{
if (mount.mount == "/" && path.hasData())
{
// root mount point = diskless client case
path += '/';
}
else
{
continue;
}
}
// the longest mount point contained in the expanded_filename wins
if (mount.mount.length() >= len)
{
len = mount.mount.length();
if (node.hasData())
{
max_node = node;
max_path = path;
}
else
{
max_node = "";
max_path = "";
}
}
}
/* If the longest mount point was a local one, max_path is empty.
Return false, leaving node_name empty and expanded_filename as is.
If the longest mount point is from a remote node, max_path
contains the root of the file's path as it is known on the
remote node. Return true, loading node_name with the remote
node name and expanded_filename with the remote file name. */
bool flag = !max_path.isEmpty();
if (flag)
{
expanded_filename.replace(0, len, max_path);
node_name = max_node;
}
#if defined(HPUX) && (!defined HP11)
else
{
flag = get_server(expanded_filename, node_name);
}
#endif
return flag;
}
#endif
#if defined(WIN_NT)
boolISC_analyze_pclan(tstring& expanded_name, tstring& node_name)
{
/**************************************
*
* I S C _ a n a l y z e _ p c l a n
*
**************************************
*
* Functional description
* Check a file name for a SMB mount point. If so,
* decompose into node name and remote file name.
*
**************************************/
ISC_expand_share(expanded_name);
if (expanded_name.length() < 2 ||
(expanded_name[0] != '\\' && expanded_name[0] != '/') ||
(expanded_name[1] != '\\' && expanded_name[1] != '/'))
{
returnfalse;
}
const size p = expanded_name.find_first_of("\\/", 2);
if (p == npos)
returnfalse;
if (Config::getRemoteFileOpenAbility())
{
if (expanded_name.find(':', p + 1) == npos)
returnfalse;
}
node_name = expanded_name.substr(2, p - 2);
expanded_name.erase(0, p + 1);
returntrue;
}
#endif
boolISC_analyze_protocol(constchar* protocol, tstring& expanded_name, tstring& node_name,
constchar* separator, bool need_file)
{
/**************************************
*
* I S C _ a n a l y z e _ p r o t o c o l
*
**************************************
*
* Functional description
* Analyze a filename for a known protocol prefix.
* If one is found, extract the node name, compute the residual
* file name, and return true. Otherwise return false.
*
**************************************/
node_name.erase();
const PathName prefix = PathName(protocol) + "://";
if (prefix.length() > expanded_name.length())
returnfalse;
if (IgnoreCaseComparator::compare(prefix.c_str(), expanded_name.c_str(), prefix.length()) != 0)
returnfalse;
PathName savedName = expanded_name;
expanded_name.erase(0, prefix.length());
if (separator) // this implies node name is expected!
{
size p = expanded_name.find_first_of('/');
if (p != 0 && p != npos)
{
node_name = p == npos ? expanded_name : expanded_name.substr(0, p);
expanded_name.erase(0, node_name.length() + 1);
if (node_name[0] == '[')
{
p = node_name.find_first_of(']');
if (p == npos)
p = 0;
}
else
p = 0;
p = node_name.find_first_of(':', p);
if (p != npos)
node_name[p] = *separator;
}
}
if (need_file && !expanded_name.hasData())
{
expanded_name = savedName;
returnfalse;
}
returntrue;
}
boolISC_analyze_tcp(tstring& file_name, tstring& node_name, bool need_file)
{
/**************************************
*
* I S C _ a n a l y z e _ t c p ( G E N E R I C )
*
**************************************
*
* Functional description
* Analyze a filename for a TCP node name on the front. If
* one is found, extract the node name, compute the residual
* file name, and return true. Otherwise return false.
*
**************************************/
// Avoid trivial case
if (!file_name.hasData())
returnfalse;
// Scan file name looking for separator character
node_name.erase();
size p = npos;
if (file_name[0] == '[')
{
// [host]:file or [host]/port:file
p = file_name.find(']');
if (p == npos || p == file_name.length() - 1)
returnfalse;
p = file_name.find(INET_FLAG, p + 1);
}
else
p = file_name.find(INET_FLAG);
if (p == npos || p == 0 || (need_file && (p == file_name.length() - 1)))
returnfalse;
node_name = file_name.substr(0, p);
#ifdef WIN_NT
// For Windows NT, insure that a single character node name does
// not conflict with an existing drive letter.
if (p == 1)
{
const ULONG dtype = GetDriveType((node_name + ":\\").c_str());
// Is it removable, fixed, cdrom or ramdisk?
if (dtype > DRIVE_NO_ROOT_DIR && (dtype != DRIVE_REMOTE || Config::getRemoteFileOpenAbility()))
{
// CVC: If we didn't match, clean our garbage or we produce side effects
// in the caller.
node_name.erase();
returnfalse;
}
}
#endif
file_name.erase(0, p + 1);
returntrue;
}
boolISC_check_if_remote(const tstring& file_name, bool implicit_flag)
{
/**************************************
*
* I S C _ c h e c k _ i f _ r e m o t e
*
**************************************
*
* Functional description
* Check to see if a path name resolves to a
* remote file. If implicit_flag is true, then
* analyze the path to see if it resolves to a
* file on a remote machine. Otherwise, simply
* check for an explicit node name.
*
**************************************/
tstring temp_name = file_name;
tstring host_name;
returnISC_extract_host(temp_name, host_name, implicit_flag) != ISC_PROTOCOL_LOCAL;
}
iscProtocol ISC_extract_host(Firebird::PathName& file_name,
Firebird::PathName& host_name,
bool implicit_flag)
{
/**************************************
*
* I S C _ e x t r a c t _ h o s t
*
**************************************
*
* Functional description
* Check to see if a file name resolves to a
* remote file. If implicit_flag is true, then
* analyze the path to see if it resolves to a
* file on a remote machine. Otherwise, simply
* check for an explicit node name.
* If file is found to be remote, extract
* the node name and compute the residual file name.
* Return protocol type.
*
**************************************/
// Always check for an explicit TCP node name
if (ISC_analyze_tcp(file_name, host_name))
return ISC_PROTOCOL_TCPIP;
if (implicit_flag)
{
// Check for a file on a network mount
#ifdef WIN_NT
if (ISC_analyze_pclan(file_name, host_name))
return ISC_PROTOCOL_TCPIP;
#endif
#ifndef NO_NFS
if (ISC_analyze_nfs(file_name, host_name))
return ISC_PROTOCOL_TCPIP;
#endif
}
return ISC_PROTOCOL_LOCAL;
}
#ifndef WIN_NT
boolISC_expand_filename(tstring& buff, bool expand_mounts)
{
/**************************************
*
* I S C _ e x p a n d _ f i l e n a m e ( N F S )
*
**************************************
*
* Functional description
* Expand a filename by following links. As soon as a TCP node name
* shows up, stop translating.
*
**************************************/
expand_filename2(buff, expand_mounts);
returntrue;
}
#endif
#ifdef WIN_NT
staticvoidtranslate_slashes(tstring& Path)
{
constchar sep = '\\';
constchar bad_sep = '/';
for (char *p = Path.begin(), *q = Path.end(); p < q; p++)
{
if (*p == bad_sep) {
*p = sep;
}
}
}
staticboolisDriveLetter(const tstring::char_type letter)
{
return (letter >= 'A' && letter <= 'Z') || (letter >= 'a' && letter <= 'z');
}
// Code of this function is a slightly changed version of this routine
// from Jim Barry (jim.barry@bigfoot.com) published at
// http://www.geocities.com/SiliconValley/2060/articles/longpaths.html
staticboolShortToLongPathName(tstring& Path)
{
// Special characters.
constchar sep = '\\';
constchar colon = ':';
// Copy the short path into the work buffer and convert forward
// slashes to backslashes.
translate_slashes(Path);
// We need a couple of markers for stepping through the path.
size left = 0;
size right = 0;
bool found_root = false;
// Parse the first bit of the path.
// Probably has to change to use GetDriveType.
if (Path.length() >= 2 && isDriveLetter(Path[0]) && colon == Path[1]) // Drive letter?
{
if (Path.length() == 2) // 'bare' drive letter
{
right = npos; // skip main block
}
elseif (sep == Path[2]) // drive letter + backslash
{
// FindFirstFile doesn't like "X:\"
if (Path.length() == 3)
{
right = npos; // skip main block
}
else
{
left = right = 3;
found_root = true;
}
}
else
{
returnfalse; // parsing failure
}
}
elseif (Path.length() >= 1 && sep == Path[0])
{
if (Path.length() == 1) // 'bare' backslash
{
right = npos; // skip main block
}
else
{
if (sep == Path[1]) // is it UNC?
{
// Find end of machine name
right = Path.find_first_of(sep, 2);
if (npos == right)
{
returnfalse;
}
// Find end of share name
right = Path.find_first_of(sep, right + 1);
if (npos == right)
{
returnfalse;
}
}
found_root = true;
++right;
}
}
// else FindFirstFile will handle relative paths
bool error = false;
if (npos != right)
{
// We don't allow wilcards as they will be processed by FindFirstFile
// and we would get the first matching file. Incidentally, we are disablimg
// escape sequences to produce long names beyond MAXPATHLEN with ??
if (Path.find_first_of("*") != npos || Path.find_first_of("?") != npos)
{
right = npos;
error = true;
}
else
{
// We'll assume there's a file at the end. If the user typed a dir,
// we'll go one dir above.
const size last = Path.find_last_of(sep);
if (npos != last)
{
Path[last] = 0;
const DWORD rc = GetFileAttributes(Path.c_str());
// Assuming the user included a file name (that's what we want),
// the path one level above should exist and should be a directory.
if (rc == 0xFFFFFFFF || !(rc & FILE_ATTRIBUTE_DIRECTORY))
{
right = npos;
error = true;
}
Path[last] = sep;
}
}
}
// The data block for FindFirstFile.
WIN32_FIND_DATA fd;
// Main parse block - step through path.
HANDLE hf = INVALID_HANDLE_VALUE;
const size leftmost = right;
while (npos != right)
{
left = right; // catch up
// Find next separator.
const size right2 = Path.find_first_of(sep, right);
// Temporarily replace the separator with a null character so that
// the path so far can be passed to FindFirstFile.
if (npos != right2)
{
Path[right2] = 0;
}
// Prevent the directory traversal attack and other anomalies like
// duplicate directory names.
// Take advantage of the previous statement (truncation) to compare directly
// with the special directory names, avoiding the overhead of substr().
// Please note that we are more thorough than GetFullPathName but we yield
// here different results because that API function interprets "." and ".."
// but we skip them here.
tstring::const_pointer special_dir = &Path.at(right);
if (!strcmp(special_dir, ".") || (!found_root || right < 2) && !strcmp(special_dir, ".."))
{
Path.erase(right, (npos == right2) ? npos : right2 - right + 1);
if (right >= Path.length())
right = npos;
continue;
}
if (found_root && !strcmp(special_dir, ".."))
{
// right being zero handled above
const size prev = Path.find_last_of(sep, right - 2);
if (prev >= leftmost && prev < right) // prev != npos implicit
right = prev + 1;
Path.erase(right, (npos == right2) ? npos : right2 - right + 1);
if (right >= Path.length())
right = npos;
continue;
}
right = right2;
// Call FindFirstFile on the path.
hf = FindFirstFile(Path.c_str(), &fd);
// Put back the separator.
if (npos != right)
{
Path[right] = sep;
}
// See what FindFirstFile makes of the path so far.
if (hf == INVALID_HANDLE_VALUE)
{
error = (npos != right);
break;
}
FindClose(hf);
// The file was found - replace the short name with the long.
const size old_len = (npos == right) ? Path.length() - left : right - left;
const size new_len = static_cast<size>(strlen(fd.cFileName));
Path.replace(left, old_len, fd.cFileName, new_len);
// More to do?
if (right != npos)
{
// Yes - move past separator .
right = left + new_len + 1;
// Did we overshoot the end? (i.e. path ends with a separator).
if (right >= Path.length())
{
right = npos;
}
}
}
// We failed to find this file.
if (hf == INVALID_HANDLE_VALUE && error)
{
returnfalse;
}
returntrue;
}
boolISC_expand_filename(tstring& file_name, bool expand_mounts)
{
/**************************************
*
* I S C _ e x p a n d _ f i l e n a m e ( W I N _ N T )
*
**************************************
*
* Functional description
* Fully expand a file name. If the file doesn't exist, do something
* intelligent.
*
**************************************/
// check for empty filename to avoid multiple checks later
if (file_name.isEmpty())
{
returnfalse;
}
bool fully_qualified_path = false;
tstring temp = file_name;
expand_share_name(temp);
// If there is an explicit node name of the form \\DOPEY or //DOPEY
// assume named pipes. Translate forward slashes to back slashes
// and return with no further processing.
if ((file_name.length() >= 2) &&
((file_name[0] == '\\' && file_name[1] == '\\') ||
(file_name[0] == '/' && file_name[1] == '/')))
{
file_name = temp;
// Translate forward slashes to back slashes
translate_slashes(file_name);
returntrue;
}
tstring device;
const size colon_pos = temp.find(INET_FLAG);
if (colon_pos != npos)
{
file_name = temp;
if (colon_pos != 1)
{
returntrue;
}
device = temp.substr(0, 1) + ":\\";
const USHORT dtype = GetDriveType(device.c_str());
if (dtype <= DRIVE_NO_ROOT_DIR)
{
returntrue;
}
// This happen if remote interface of our server
// rejected WNet connection or we were called with:
// localhost:R:\Path\To\Database, where R - remote disk
if (dtype == DRIVE_REMOTE && expand_mounts)
{
ISC_expand_share(file_name);
translate_slashes(file_name);
returntrue;
}
if ((temp.length() >= 3) && (temp[2] == '/' || temp[2] == '\\'))
{
fully_qualified_path = true;
}
}
// Translate forward slashes to back slashes
translate_slashes(temp);
// If there is an explicit node name of the form \\DOPEY don't do any
// additional translations -- everything will need to be applied at
// the other end.
if ((temp.length() >= 2) && (temp[0] == '\\' && temp[1] == '\\'))
{
file_name = temp;
returntrue;
}
if (temp[0] == '\\' || temp[0] == '/')
{
fully_qualified_path = true;
}
// Expand the file name
#ifdef SUPERSERVER
if (!fully_qualified_path)
{
fb_utils::getCwd(file_name);
if (device.hasData() && device[0] == file_name[0])
{
// case where temp is of the form "c:foo.fdb" and
// expanded_name is "c:\x\y".
file_name += '\\';
file_name.append (temp, 2, npos);
}
elseif (device.empty())
{
// case where temp is of the form "foo.fdb" and
// expanded_name is "c:\x\y".
file_name += '\\';
file_name += temp;
}
else
{
// case where temp is of the form "d:foo.fdb" and
// expanded_name is "c:\x\y".
// Discard expanded_name and use temp as it is.
// In this case use the temp but we need to ensure that we expand to
// temp from "d:foo.fdb" to "d:\foo.fdb"
if (!get_full_path(temp, file_name))
{
file_name = temp;
}
}
}
else
#endif
{
// Here we get "." and ".." translated by the API.
if (!get_full_path(temp, file_name))
{
file_name = temp;
}
}
// convert then name to its longer version ie. convert longfi~1.fdb
// to longfilename.fdb
bool rc = ShortToLongPathName(file_name);
// Filenames are case insensitive on NT. If filenames are
// typed in mixed cases, strcmp () used in various places
// results in incorrect behavior.
file_name.upper();
return rc;
}
#endif
#if defined(WIN_NT)
voidISC_expand_share(tstring& file_name)
{
/**************************************
*
* I S C _ e x p a n d _ s h a r e
*
**************************************
*
* Functional description
* Expand a file name by chasing shared disk
* information.
*
**************************************/
// see NT reference for WNetEnumResource for the following constants
DWORD nument = 0xffffffff, bufSize = 16384;
// Look for a drive letter and make sure that it corresponds to a remote disk
const size p = file_name.find(':');
if (p != 1)
{
return;
}