Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
FileSystem.cs
Go to the documentation of this file.
5
6namespace System.IO;
7
8internal static class FileSystem
9{
10 internal static void VerifyValidPath(string path, string argName)
11 {
12 if (path == null)
13 {
15 }
16 if (path.Length == 0)
17 {
19 }
20 if (path.Contains('\0'))
21 {
23 }
24 }
25
26 public static bool DirectoryExists(string fullPath)
27 {
28 int lastError;
30 }
31
32 private static bool DirectoryExists(string path, out int lastError)
33 {
36 if (lastError == 0 && data.dwFileAttributes != -1)
37 {
38 return (data.dwFileAttributes & 0x10) != 0;
39 }
40 return false;
41 }
42
43 public static bool FileExists(string fullPath)
44 {
46 if (FillAttributeInfo(fullPath, ref data, returnErrorOnNotFound: true) == 0 && data.dwFileAttributes != -1)
47 {
48 return (data.dwFileAttributes & 0x10) == 0;
49 }
50 return false;
51 }
52
54 {
55 int num = 0;
58 {
60 {
62 if (!IsPathUnreachableError(num))
63 {
66 if (safeFindHandle.IsInvalid)
67 {
69 }
70 else
71 {
72 num = 0;
73 data.PopulateFrom(ref data2);
74 }
75 }
76 }
77 }
78 if (num != 0 && !returnErrorOnNotFound && ((uint)(num - 2) <= 1u || num == 21))
79 {
80 data.dwFileAttributes = -1;
81 return 0;
82 }
83 return num;
84 }
85
86 internal static bool IsPathUnreachableError(int errorCode)
87 {
88 switch (errorCode)
89 {
90 case 2:
91 case 3:
92 case 6:
93 case 21:
94 case 53:
95 case 65:
96 case 67:
97 case 87:
98 case 123:
99 case 161:
100 case 206:
101 case 1231:
102 return true;
103 default:
104 return false;
105 }
106 }
107
108 public unsafe static void CreateDirectory(string fullPath, byte[] securityDescriptor = null)
109 {
111 {
112 return;
113 }
115 bool flag = false;
116 int num = fullPath.Length;
117 if (num >= 2 && PathInternal.EndsInDirectorySeparator(fullPath.AsSpan()))
118 {
119 num--;
120 }
122 if (num > rootLength)
123 {
124 int num2 = num - 1;
125 while (num2 >= rootLength && !flag)
126 {
127 string text = fullPath.Substring(0, num2 + 1);
128 if (!DirectoryExists(text))
129 {
130 list.Add(text);
131 }
132 else
133 {
134 flag = true;
135 }
137 {
138 num2--;
139 }
140 num2--;
141 }
142 }
143 int count = list.Count;
144 bool flag2 = true;
145 int num3 = 0;
146 string path = fullPath;
148 {
150 sECURITY_ATTRIBUTES.nLength = (uint)sizeof(Interop.Kernel32.SECURITY_ATTRIBUTES);
151 sECURITY_ATTRIBUTES.lpSecurityDescriptor = (IntPtr)ptr;
153 while (list.Count > 0)
154 {
155 string text2 = list[list.Count - 1];
156 list.RemoveAt(list.Count - 1);
158 if (!flag2 && num3 == 0)
159 {
161 if (lastError != 183)
162 {
163 num3 = lastError;
164 }
165 else if (FileExists(text2) || (!DirectoryExists(text2, out lastError) && lastError == 5))
166 {
167 num3 = lastError;
168 path = text2;
169 }
170 }
171 }
172 }
173 if (count == 0 && !flag)
174 {
177 {
179 }
180 }
181 else if (!flag2 && num3 != 0)
182 {
184 }
185 }
186
187 public static void Encrypt(string path)
188 {
189 string fullPath = Path.GetFullPath(path);
191 {
193 }
194 }
195
196 public static void Decrypt(string path)
197 {
198 string fullPath = Path.GetFullPath(path);
200 {
202 }
203 }
204
226
227 public static void CopyFile(string sourceFullPath, string destFullPath, bool overwrite)
228 {
230 if (num == 0)
231 {
232 return;
233 }
234 string path = destFullPath;
235 if (num != 80)
236 {
238 {
239 if (safeFileHandle.IsInvalid)
240 {
242 }
243 }
244 if (num == 5 && DirectoryExists(destFullPath))
245 {
247 }
248 }
250 }
251
260
261 public static void DeleteFile(string fullPath)
262 {
264 {
266 if (lastWin32Error != 2)
267 {
269 }
270 }
271 }
272
274 {
276 int num = FillAttributeInfo(fullPath, ref data, returnErrorOnNotFound: true);
277 if (num != 0)
278 {
280 }
281 return (FileAttributes)data.dwFileAttributes;
282 }
283
285 {
287 int num = FillAttributeInfo(fullPath, ref data, returnErrorOnNotFound: false);
288 if (num != 0)
289 {
291 }
292 return data.ftCreationTime.ToDateTimeOffset();
293 }
294
296 {
298 int num = FillAttributeInfo(fullPath, ref data, returnErrorOnNotFound: false);
299 if (num != 0)
300 {
302 }
304 }
305
307 {
309 int num = FillAttributeInfo(fullPath, ref data, returnErrorOnNotFound: false);
310 if (num != 0)
311 {
313 }
314 return data.ftLastWriteTime.ToDateTimeOffset();
315 }
316
335
336 public static void MoveFile(string sourceFullPath, string destFullPath, bool overwrite)
337 {
339 {
341 }
342 }
343
344 private static SafeFileHandle OpenHandle(string fullPath, bool asDirectory)
345 {
346 string text = fullPath.Substring(0, PathInternal.GetRootLength(fullPath.AsSpan()));
347 if (text == fullPath && text[1] == Path.VolumeSeparatorChar)
348 {
349 throw new ArgumentException(SR.Arg_PathIsVolume, "path");
350 }
351 SafeFileHandle safeFileHandle = Interop.Kernel32.CreateFile(fullPath, 1073741824, FileShare.ReadWrite | FileShare.Delete, FileMode.Open, asDirectory ? 33554432 : 0);
352 if (safeFileHandle.IsInvalid)
353 {
354 int num = Marshal.GetLastWin32Error();
355 if (!asDirectory && num == 3 && fullPath.Equals(Directory.GetDirectoryRoot(fullPath)))
356 {
357 num = 5;
358 }
360 }
361 return safeFileHandle;
362 }
363
381
383 {
385 if (safeFindHandle.IsInvalid)
386 {
387 int num = Marshal.GetLastWin32Error();
388 if (isDirectory && num == 2)
389 {
390 num = 3;
391 }
392 if (!(isDirectory && num == 5 && ignoreAccessDenied))
393 {
395 }
396 }
397 }
398
400 {
401 if ((data.dwFileAttributes & 0x400u) != 0)
402 {
403 return (data.dwReserved0 & 0x20000000) != 0;
404 }
405 return false;
406 }
407
409 {
410 Exception ex = null;
412 {
413 if (safeFindHandle.IsInvalid)
414 {
416 }
417 int lastWin32Error;
418 do
419 {
420 if ((findData.dwFileAttributes & 0x10) == 0)
421 {
422 string stringFromFixedBuffer = findData.cFileName.GetStringFromFixedBuffer();
424 {
426 if (lastWin32Error != 2)
427 {
429 }
430 }
431 }
432 else
433 {
434 if (findData.cFileName.FixedBufferEqualsString(".") || findData.cFileName.FixedBufferEqualsString(".."))
435 {
436 continue;
437 }
438 string stringFromFixedBuffer2 = findData.cFileName.GetStringFromFixedBuffer();
440 {
441 try
442 {
444 }
445 catch (Exception ex2)
446 {
447 if (ex == null)
448 {
449 ex = ex2;
450 }
451 }
452 continue;
453 }
454 if (findData.dwReserved0 == 2684354563u)
455 {
458 {
460 if (lastWin32Error != 0 && lastWin32Error != 3)
461 {
463 }
464 }
465 }
467 {
469 if (lastWin32Error != 3)
470 {
472 }
473 }
474 }
475 }
477 if (ex != null)
478 {
479 throw ex;
480 }
482 if (lastWin32Error != 0 && lastWin32Error != 18)
483 {
485 }
486 }
488 }
489
490 private static void RemoveDirectoryInternal(string fullPath, bool topLevel, bool allowDirectoryNotEmpty = false)
491 {
493 {
494 return;
495 }
496 int num = Marshal.GetLastWin32Error();
497 switch (num)
498 {
499 case 2:
500 num = 3;
501 goto case 3;
502 case 3:
503 if (!topLevel)
504 {
505 return;
506 }
507 break;
508 case 145:
510 {
511 return;
512 }
513 break;
514 case 5:
516 }
518 }
519
520 public static void SetAttributes(string fullPath, FileAttributes attributes)
521 {
522 if (!Interop.Kernel32.SetFileAttributes(fullPath, (int)attributes))
523 {
525 if (lastWin32Error == 87)
526 {
527 throw new ArgumentException(SR.Arg_InvalidFileAttrs, "attributes");
528 }
530 }
531 }
532
533 private unsafe static void SetFileTime(string fullPath, bool asDirectory, long creationTime = -1L, long lastAccessTime = -1L, long lastWriteTime = -1L, long changeTime = -1L, uint fileAttributes = 0u)
534 {
537 fILE_BASIC_INFO.CreationTime = creationTime;
538 fILE_BASIC_INFO.LastAccessTime = lastAccessTime;
539 fILE_BASIC_INFO.LastWriteTime = lastWriteTime;
540 fILE_BASIC_INFO.ChangeTime = changeTime;
541 fILE_BASIC_INFO.FileAttributes = fileAttributes;
544 {
546 }
547 }
548
549 public static void SetCreationTime(string fullPath, DateTimeOffset time, bool asDirectory)
550 {
551 SetFileTime(fullPath, asDirectory, time.ToFileTime(), -1L, -1L, -1L);
552 }
553
554 public static void SetLastAccessTime(string fullPath, DateTimeOffset time, bool asDirectory)
555 {
556 SetFileTime(fullPath, asDirectory, -1L, time.ToFileTime(), -1L, -1L);
557 }
558
559 public static void SetLastWriteTime(string fullPath, DateTimeOffset time, bool asDirectory)
560 {
561 SetFileTime(fullPath, asDirectory, -1L, -1L, time.ToFileTime(), -1L);
562 }
563
564 public static string[] GetLogicalDrives()
565 {
567 }
568
574
576 {
578 if (text != null)
579 {
580 if (!isDirectory)
581 {
582 return new FileInfo(text);
583 }
584 return new DirectoryInfo(text);
585 }
586 return null;
587 }
588
589 internal static string GetLinkTarget(string linkPath, bool isDirectory)
590 {
592 }
593
594 internal unsafe static string GetImmediateLinkTarget(string linkPath, bool isDirectory, bool throwOnError, bool returnFullPath)
595 {
597 {
598 if (safeFileHandle.IsInvalid)
599 {
600 if (!throwOnError)
601 {
602 return null;
603 }
604 int num = Marshal.GetLastWin32Error();
605 if (isDirectory && num == 2)
606 {
607 num = 3;
608 }
610 }
611 byte[] array = ArrayPool<byte>.Shared.Rent(16384);
612 try
613 {
615 {
616 if (!throwOnError)
617 {
618 return null;
619 }
621 if (lastWin32Error == 4390)
622 {
623 return null;
624 }
626 }
629 if (value.ReparseTag == 2684354572u)
630 {
632 int substituteNameLength = value.SubstituteNameLength;
633 Span<char> span2 = MemoryMarshal.Cast<byte, char>(span.Slice(start, substituteNameLength));
634 if ((value.Flags & 1) == 0)
635 {
636 if (span2.StartsWith("\\??\\UNC\\".AsSpan()))
637 {
638 return Path.Join("\\\\".AsSpan(), span2.Slice("\\??\\UNC\\".Length));
639 }
641 }
642 if (returnFullPath)
643 {
644 return Path.Join(Path.GetDirectoryName(linkPath.AsSpan()), span2);
645 }
646 return span2.ToString();
647 }
648 if (value.ReparseTag == 2684354563u)
649 {
651 int start2 = sizeof(Interop.Kernel32.MountPointReparseBuffer) + value2.SubstituteNameOffset;
652 int substituteNameLength2 = value2.SubstituteNameLength;
655 }
656 return null;
657 }
658 finally
659 {
660 ArrayPool<byte>.Shared.Return(array);
661 }
662 }
664 {
665 return targetPath.Slice("\\??\\".Length).ToString();
666 }
667 }
668
669 private static string GetFinalLinkTarget(string linkPath, bool isDirectory)
670 {
673 if ((findData.dwFileAttributes & 0x400) == 0 || (findData.dwReserved0 != 2684354572u && findData.dwReserved0 != 2684354563u))
674 {
675 return null;
676 }
678 {
679 if (safeFileHandle.IsInvalid)
680 {
683 {
685 }
687 }
688 char[] array = ArrayPool<char>.Shared.Rent(4096);
689 try
690 {
691 uint num = GetFinalPathNameByHandle(safeFileHandle, array);
692 if (num > array.Length)
693 {
694 char[] array2 = array;
695 array = ArrayPool<char>.Shared.Rent((int)num);
696 ArrayPool<char>.Shared.Return(array2);
697 num = GetFinalPathNameByHandle(safeFileHandle, array);
698 }
699 if (num == 0)
700 {
702 }
703 int num2 = ((!PathInternal.IsExtended(linkPath.AsSpan())) ? 4 : 0);
704 return new string(array, num2, (int)num - num2);
705 }
706 finally
707 {
708 ArrayPool<char>.Shared.Return(array);
709 }
710 }
711 string GetFinalLinkTargetSlow(string linkPath)
712 {
714 string result = null;
715 while (immediateLinkTarget != null)
716 {
717 result = immediateLinkTarget;
719 }
720 return result;
721 }
722 unsafe static uint GetFinalPathNameByHandle(SafeFileHandle handle, char[] buffer)
723 {
724 fixed (char* lpszFilePath = buffer)
725 {
727 }
728 }
729 }
730
731 private unsafe static SafeFileHandle OpenSafeFileHandle(string path, int flags)
732 {
733 return Interop.Kernel32.CreateFile(path, 0, FileShare.ReadWrite | FileShare.Delete, (Interop.Kernel32.SECURITY_ATTRIBUTES*)(void*)IntPtr.Zero, FileMode.Open, flags, IntPtr.Zero);
734 }
735}
static bool EncryptFile(string path)
Definition Interop.cs:1302
static bool DecryptFile(string path)
Definition Interop.cs:1311
static bool ReplaceFile(string replacedFileName, string replacementFileName, string backupFileName, int dwReplaceFlags, IntPtr lpExclude, IntPtr lpReserved)
Definition Interop.cs:1018
static unsafe bool GetVolumeInformation(string drive, char *volumeName, int volumeNameBufLen, int *volSerialNumber, int *maxFileNameLen, out int fileSystemFlags, char *fileSystemName, int fileSystemNameBufLen)
static bool RemoveDirectory(string path)
Definition Interop.cs:1009
static unsafe SafeFileHandle CreateFile(string lpFileName, int dwDesiredAccess, FileShare dwShareMode, SECURITY_ATTRIBUTES *lpSecurityAttributes, FileMode dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile)
Definition Interop.cs:105
static int CopyFile(string src, string dst, bool failIfExists)
Definition Interop.cs:702
static bool MoveFile(string src, string dst, bool overwrite)
Definition Interop.cs:969
static bool DeleteFile(string path)
Definition Interop.cs:750
static unsafe bool SetFileInformationByHandle(SafeFileHandle hFile, int FileInformationClass, void *lpFileInformation, uint dwBufferSize)
static bool GetFileAttributesEx(string name, GET_FILEEX_INFO_LEVELS fileInfoLevel, ref WIN32_FILE_ATTRIBUTE_DATA lpFileInformation)
Definition Interop.cs:179
static bool CreateDirectory(string path, ref SECURITY_ATTRIBUTES lpSecurityAttributes)
Definition Interop.cs:192
static bool DeleteVolumeMountPoint(string mountPoint)
Definition Interop.cs:759
static bool SetFileAttributes(string name, int attr)
Definition Interop.cs:1035
static bool FindNextFile(SafeFindHandle hndFindFile, ref WIN32_FIND_DATA lpFindFileData)
static bool DeviceIoControl(SafeHandle hDevice, uint dwIoControlCode, IntPtr lpInBuffer, uint nInBufferSize, byte[] lpOutBuffer, uint nOutBufferSize, out uint lpBytesReturned, IntPtr lpOverlapped)
static void CreateSymbolicLink(string symlinkFileName, string targetFileName, bool isDirectory)
Definition Interop.cs:777
static Microsoft.Win32.SafeHandles.SafeFindHandle FindFirstFile(string fileName, ref WIN32_FIND_DATA data)
Definition Interop.cs:117
static unsafe uint GetFinalPathNameByHandle(SafeFileHandle hFile, char *lpszFilePath, uint cchFilePath, uint dwFlags)
bool ICollection< KeyValuePair< TKey, TValue > >. Contains(KeyValuePair< TKey, TValue > keyValuePair)
void Add(TKey key, TValue value)
static string GetDirectoryRoot(string path)
Definition Directory.cs:276
static string[] GetLogicalDrives()
static string NormalizeDriveName(string driveName)
static unsafe void ThrowExceptionEncryptDecryptFail(string fullPath)
static bool DirectoryExists(string fullPath)
Definition FileSystem.cs:26
static string GetFinalLinkTarget(string linkPath, bool isDirectory)
static unsafe void CreateDirectory(string fullPath, byte[] securityDescriptor=null)
static DateTimeOffset GetLastAccessTime(string fullPath)
static void DeleteFile(string fullPath)
static unsafe string GetImmediateLinkTarget(string linkPath, bool isDirectory, bool throwOnError, bool returnFullPath)
static unsafe SafeFileHandle OpenSafeFileHandle(string path, int flags)
static void Encrypt(string path)
static bool IsNameSurrogateReparsePoint(ref Interop.Kernel32.WIN32_FIND_DATA data)
static FileSystemInfo ResolveLinkTarget(string linkPath, bool returnFinalTarget, bool isDirectory)
static DateTimeOffset GetLastWriteTime(string fullPath)
static void MoveFile(string sourceFullPath, string destFullPath, bool overwrite)
static SafeFileHandle OpenHandle(string fullPath, bool asDirectory)
static void CopyFile(string sourceFullPath, string destFullPath, bool overwrite)
static void GetFindData(string fullPath, bool isDirectory, bool ignoreAccessDenied, ref Interop.Kernel32.WIN32_FIND_DATA findData)
static void SetCreationTime(string fullPath, DateTimeOffset time, bool asDirectory)
static bool FileExists(string fullPath)
Definition FileSystem.cs:43
static void Decrypt(string path)
static string[] GetLogicalDrives()
static bool DirectoryExists(string path, out int lastError)
Definition FileSystem.cs:32
static void RemoveDirectory(string fullPath, bool recursive)
static void RemoveDirectoryInternal(string fullPath, bool topLevel, bool allowDirectoryNotEmpty=false)
static int FillAttributeInfo(string path, ref Interop.Kernel32.WIN32_FILE_ATTRIBUTE_DATA data, bool returnErrorOnNotFound)
Definition FileSystem.cs:53
static void SetLastWriteTime(string fullPath, DateTimeOffset time, bool asDirectory)
static void RemoveDirectoryRecursive(string fullPath, ref Interop.Kernel32.WIN32_FIND_DATA findData, bool topLevel)
static DateTimeOffset GetCreationTime(string fullPath)
static int FillAttributeInfo(string path, ref global::Interop.Kernel32.WIN32_FILE_ATTRIBUTE_DATA data, bool returnErrorOnNotFound)
Definition FileSystem.cs:36
static void VerifyValidPath(string path, string argName)
Definition FileSystem.cs:10
static void SetAttributes(string fullPath, FileAttributes attributes)
static string GetLinkTarget(string linkPath, bool isDirectory)
static void ReplaceFile(string sourceFullPath, string destFullPath, string destBackupFullPath, bool ignoreMetadataErrors)
static void CreateSymbolicLink(string path, string pathToTarget, bool isDirectory)
static void SetLastAccessTime(string fullPath, DateTimeOffset time, bool asDirectory)
static bool IsPathUnreachableError(int errorCode)
Definition FileSystem.cs:69
static void MoveDirectory(string sourceFullPath, string destFullPath)
static FileAttributes GetAttributes(string fullPath)
static unsafe void SetFileTime(string fullPath, bool asDirectory, long creationTime=-1L, long lastAccessTime=-1L, long lastWriteTime=-1L, long changeTime=-1L, uint fileAttributes=0u)
static bool EndsInDirectorySeparator(string path)
static int GetRootLength(ReadOnlySpan< char > path)
static string TrimEndingDirectorySeparator(string path)
static string GetLinkTargetFullPath(string path, string pathToTarget)
static bool IsDirectorySeparator(char c)
static string EnsureExtendedPrefix(string path)
static bool IsExtended(ReadOnlySpan< char > path)
static string Combine(string path1, string path2)
Definition Path.cs:304
static string GetFullPath(string path)
Definition Path.cs:881
static string TrimEndingDirectorySeparator(string path)
Definition Path.cs:838
static string Join(ReadOnlySpan< char > path1, ReadOnlySpan< char > path2)
Definition Path.cs:387
static ? string GetPathRoot(string? path)
Definition Path.cs:1008
static readonly char VolumeSeparatorChar
Definition Path.cs:75
static ? string GetDirectoryName(string? path)
Definition Path.cs:121
static int MakeHRFromErrorCode(int errorCode)
static Exception GetExceptionForLastWin32Error(string path="")
static Exception GetExceptionForWin32Error(int errorCode, string path="")
static string Arg_FileIsDirectory_Name
Definition SR.cs:2194
static string Arg_InvalidFileAttrs
Definition SR.cs:2196
static string Argument_InvalidPathChars
Definition SR.cs:730
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string UnauthorizedAccess_IODenied_Path
Definition SR.cs:50
static string Arg_PathEmpty
Definition SR.cs:54
static string Arg_PathIsVolume
Definition SR.cs:2200
static string PlatformNotSupported_FileEncryption
Definition SR.cs:2208
Definition SR.cs:7
DateTimeOffset ToDateTimeOffset()
Definition Interop.cs:244
static System.IO.DisableMediaInsertionPrompt Create()
static readonly IntPtr Zero
Definition IntPtr.cs:18