Repository navigation
Expand file tree
/
Copy pathFileSystem.cs
More file actions
165 lines (136 loc) · 6.86 KB
/
Copy pathFileSystem.cs
File metadata and controls
165 lines (136 loc) · 6.86 KB
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
using System;
using System.Collections.Generic;
using System.IO;
namespace GitCredentialManager
{
/// <summary>
/// Represents a file system and operations that can be performed.
/// </summary>
public interface IFileSystem
{
/// <summary>
/// Get the path to the user's home profile directory (Unix: $HOME, Windows: %USERPROFILE%).
/// </summary>
string UserHomePath { get; }
/// <summary>
/// Get the path the the user's Git Credential Manager data directory.
/// </summary>
string UserDataDirectoryPath { get; }
/// <summary>
/// Check if two paths are the same for the current platform and file system. Symbolic links are not followed.
/// </summary>
/// <param name="a">File path.</param>
/// <param name="b">File path.</param>
/// <returns>True if both file paths are the same, false otherwise.</returns>
bool IsSamePath(string a, string b);
/// <summary>
/// Check if a file exists at the specified path.
/// </summary>
/// <param name="path">Full path to file to test.</param>
/// <returns>True if a file exists, false otherwise.</returns>
bool FileExists(string path);
/// <summary>
/// Check if a file has execute permissions.
/// On Windows this always returns true. On POSIX it checks for any execute bit.
/// </summary>
/// <param name="path">Full path to file to test.</param>
/// <returns>True if the file is executable, false otherwise.</returns>
bool FileIsExecutable(string path);
/// <summary>
/// Check if a directory exists at the specified path.
/// </summary>
/// <param name="path">Full path to directory to test.</param>
/// <returns>True if a directory exists, false otherwise.</returns>
bool DirectoryExists(string path);
/// <summary>
/// Get the path to the current directory of the currently executing process.
/// </summary>
/// <returns>Current process directory.</returns>
string GetCurrentDirectory();
/// <summary>
/// Open a file stream at the specified path with the given access and mode settings.
/// </summary>
/// <param name="path">Full file path.</param>
/// <param name="fileMode">File mode settings.</param>
/// <param name="fileAccess">File access settings.</param>
/// <param name="fileShare">File share settings.</param>
/// <returns></returns>
Stream OpenFileStream(string path, FileMode fileMode, FileAccess fileAccess, FileShare fileShare);
/// <summary>
/// Creates all directories and subdirectories in the specified path unless they already exist.
/// </summary>
/// <param name="path">The directory to create.</param>
void CreateDirectory(string path);
/// <summary>
/// Deletes the specified file.
/// </summary>
/// <param name="path">Name of the file to be deleted.</param>
void DeleteFile(string path);
/// <summary>
/// Returns an enumerable collection of full file names that match a search pattern in a specified path.
/// </summary>
/// <param name="path">The relative or absolute path to the directory to search.</param>
/// <param name="searchPattern">
/// The search string to match against the names of files in path.
/// This parameter can contain a combination of valid literal path and wildcard (* and ?) characters,
/// but it doesn't support regular expressions.
/// </param>
/// <returns>
/// An enumerable collection of the full names (including paths) for the files in the directory
/// specified by path and that match the specified search pattern.
/// </returns>
IEnumerable<string> EnumerateFiles(string path, string searchPattern);
/// <summary>
/// Returns an enumerable collection of directory full names in a specified path.
/// </summary>
/// <param name="path">The relative or absolute path to the directory to search. This string is not case-sensitive.</param>
/// <returns>
/// An enumerable collection of the full names (including paths) for the directories
/// in the directory specified by path.
/// </returns>
IEnumerable<string> EnumerateDirectories(string path);
/// <summary>
/// Opens a text file, reads all the text in the file, and then closes the file
/// </summary>
/// <param name="path">The file to open for reading.</param>
/// <returns>A string containing all the text in the file.</returns>
string ReadAllText(string path);
/// <summary>
/// Opens a text file, reads all lines of the file, and then closes the file.
/// </summary>
/// <param name="path">The file to open for reading.</param>
/// <returns>A string array containing all lines of the file.</returns>
string[] ReadAllLines(string path);
}
/// <summary>
/// The real file system.
/// </summary>
public abstract class FileSystem : IFileSystem
{
public string UserHomePath => Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
public string UserDataDirectoryPath => Path.Combine(UserHomePath, Constants.GcmDataDirectoryName);
public abstract bool IsSamePath(string a, string b);
public bool FileExists(string path) => File.Exists(path);
public bool FileIsExecutable(string path)
{
if (!PlatformUtils.IsPosix())
return true;
#pragma warning disable CA1416 // Platform guard via PlatformUtils.IsPosix()
var mode = File.GetUnixFileMode(path);
return (mode & (UnixFileMode.UserExecute |
UnixFileMode.GroupExecute |
UnixFileMode.OtherExecute)) != 0;
#pragma warning restore CA1416
}
public bool DirectoryExists(string path) => Directory.Exists(path);
public string GetCurrentDirectory() => Directory.GetCurrentDirectory();
public Stream OpenFileStream(string path, FileMode fileMode, FileAccess fileAccess, FileShare fileShare)
=> File.Open(path, fileMode, fileAccess, fileShare);
public void CreateDirectory(string path) => Directory.CreateDirectory(path);
public void DeleteFile(string path) => File.Delete(path);
public IEnumerable<string> EnumerateFiles(string path, string searchPattern) => Directory.EnumerateFiles(path, searchPattern);
public IEnumerable<string> EnumerateDirectories(string path) => Directory.EnumerateDirectories(path);
public string ReadAllText(string path) => File.ReadAllText(path);
public string[] ReadAllLines(string path) => File.ReadAllLines(path);
}
}