-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathFile.fs
280 lines (241 loc) · 10.7 KB
/
File.fs
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
////
//// File and path management
////
module Microsoft.FSharpLu.File
open System.IO
open Microsoft.FSharpLu.Option
open Microsoft.FSharpLu
/// Path combine infix operator
let (++) x y = System.IO.Path.Combine(x,y)
/// Ensure specified file exists
let public getExistingFile path =
if System.IO.File.Exists path then
Some path
else
None
/// Ensure specified directory exists
let public getExistingDir path =
if System.IO.Directory.Exists path then
Some path
else
None
/// Return the size in bytes of a file
let fileLength filePath =
let f = System.IO.FileInfo(filePath)
f.Length
/// Append a line to a text file
let public appendLine filepath line =
System.IO.File.AppendAllLines(filepath, [line])
/// Append lines to a text file
let public appendLines filepath (lines:seq<string>) =
System.IO.File.AppendAllLines(filepath, lines)
/// Write lines to a text file
let public writeLines filepath (lines:seq<string>) =
System.IO.File.WriteAllLines(filepath, lines)
/// Create an empty file
let public createEmptyFile filepath =
(
use file = System.IO.File.Create(filepath)
()
)
/// Write to a text file atomically while allowing concurrent reads.
/// **Atomicity is guaranteed only if file content is < 8kb**
///
// NOTE: An implementation based on System.IO.File.Replace
// would not guarantee atomicity for files residing on SMB shares!
// (http://msdn.microsoft.com/en-us/library/windows/desktop/aa365512(v=vs.85).aspx)
let public atomaticWriteAllLines filePath lines =
/// Replace content of an existing file atomically using the
/// whitespace padding hack.
let replaceExistingContentWithPaddingHack () =
// CAUTION: Atomicity breaks if bufferSize < |content to be written|
let BufferSize = 8192
// Should not use FileMode.Create otherwise the file will be empty until the next flush.
use fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read, BufferSize, FileOptions.WriteThrough)
use streamWriter = new StreamWriter(fs)
streamWriter.AutoFlush <- false
let newContent = Text.join streamWriter.NewLine lines
// If new content is smaller than previous content
// then we pad the content with spaces to
// prevent concurrent readers to see inconsistent content
// after we flushed and before the file is closed.
let oldLength = fs.Length |> int64
streamWriter.Write(newContent)
let newLength = newContent.Length |> int64
let diff = oldLength - newLength
if diff > 0L then
streamWriter.Write(streamWriter.NewLine)
streamWriter.Write(Array.create(diff |> int) ' ')
streamWriter.Flush()
// Trim the extra padding
fs.SetLength(newLength)
if newLength > int64 BufferSize then
failwithf "File too big to guarantee atomicity: %d bytes. Maximum supported size is %d" newLength BufferSize
// Write content to a temp file in the target directory
let writeToTempFile () =
let targetDir = System.IO.Path.GetDirectoryName(filePath)
let tempFileName = System.Guid.NewGuid().ToString().Replace("-","")
let tempFile = targetDir ++ tempFileName
System.IO.File.WriteAllLines(tempFile, lines |> Seq.toArray)
tempFile
// Logic differs depending on whether the file exists.
if System.IO.File.Exists filePath then
replaceExistingContentWithPaddingHack ()
else
// If the file does not exists then the previous logic does not work:
// creating the file stream will leave the file empty until the file is flushed!
// Instead we write to a separate file and then atomically rename it.
let tempFile = writeToTempFile()
try
System.IO.File.Move(tempFile, filePath)
with
:? System.IO.IOException ->
if System.IO.File.Exists filePath then
// the target file has just been created by
// another process: let the other process win
System.IO.File.Delete tempFile
else
reraise()
/// Read a text file atomically while allowing concurrent writes.
/// Atomicity is guaranteed only if file content is < 8kb
let public atomaticReadAllText filePath =
let bufferSize = 8192 // CAUTION: if < content to be written then atomicity breaks
use fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, bufferSize, FileOptions.SequentialScan)
use streamReader = new StreamReader(fs)
streamReader.ReadToEnd()
/// Read a text file atomically while allowing concurrent writes.
/// Returns result as array of lines
let public atomaticReadLines filePath =
atomaticReadAllText filePath |> Text.splitOnString [|"\r\n"|]
/// Parse any batch file containing variable definitions of the following format
/// rem Some comment
/// set VAR=somevalue
/// set VAR2=some other value
let public parseBatchSetFile file =
file
|> atomaticReadLines
|> Seq.filter (Text.startsWith "::" >> not)
|> Seq.filter (Text.startsWith "rem" >> not)
|> Seq.filter (Text.startsWith "REM" >> not)
|> Seq.filter (System.String.IsNullOrWhiteSpace >> not)
|> Seq.map (Text.skipPrefixCaseInsensitive "set " >> Text.splitOnce '=')
|> Seq.toList
/// Serialize a sequence of key/value pairs to batch file
let public serializeToBatch filepath keypairs =
keypairs |> Seq.map (fun (k,v) -> sprintf "set %s=%O" k v)
|> atomaticWriteAllLines filepath
/// Wait until the specified file exists on disk.
/// Note: the immediate parent directory must already exist
let waitUntilExists (log:Logger.Logger<unit,'B>) filepath =
async {
if System.IO.File.Exists filepath then
log.write "File %s already exists" filepath
return filepath
else
let parentDir = System.IO.Path.GetDirectoryName filepath
let fileName = System.IO.Path.GetFileName filepath
use w = new System.IO.FileSystemWatcher(parentDir, fileName, IncludeSubdirectories = false, EnableRaisingEvents = true)
let waitForFileAsync =
Async.Compete
[
async {
let! v = Async.AwaitEvent w.Created
return v.FullPath
}
async {
let! v = Async.AwaitEvent w.Renamed
return v.FullPath
}
]
// (a) Potential race condition if the file is created here,
// taken care of by (b).
let! waitForFile = Async.StartChild(waitForFileAsync)
/// (b) Check again to handle race condition (a)
if System.IO.File.Exists filepath then
return filepath
else
log.write "awaiting for %s" filepath
return! waitForFile
}
/// Deletes the directory if it exists.
let deleteDirIfExists dir =
if Directory.Exists(dir) then
Directory.Delete(dir, true)
/// Check if the target directory exists, if not, create it.
let createDirIfNotExists dir =
if not <| Directory.Exists(dir) then
Directory.CreateDirectory(dir) |> ignore
/// Create the target directory exists, deleting it first if it already exists.
let recreateDir dir =
if Directory.Exists(dir) then
Directory.Delete(dir, true)
Directory.CreateDirectory(dir) |> ignore
/// Directory copy (ported from MSDN https://msdn.microsoft.com/en-us/library/system.io.directoryinfo.aspx)
/// Usage:
///
/// CopyDirectory (DirectoryInfo(sourceDirectory)) (DirectoryInfo(targetDirectory))
///
let copyDirectory sourcePath targetPath =
let rec aux (source:DirectoryInfo) (target:DirectoryInfo) =
if System.String.Compare(source.FullName, target.FullName, true) <> 0 then
createDirIfNotExists target.FullName
// Copy each file into it's new directory.
source.GetFiles()
|> Seq.iter
(fun fi ->
// printf @"Copying %s\%s" target.FullName fi.Name
fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true) |> ignore)
// Copy each subdirectory using recursion.
source.GetDirectories()
|> Seq.iter(fun diSourceSubDir ->
let nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name)
aux diSourceSubDir nextTargetSubDir)
in aux (DirectoryInfo(sourcePath)) (DirectoryInfo(targetPath))
/// A string that represents a valid file path.
type FilePath = string
/// Validates that the argument is a valid path on the current platform.
/// The validation does not require the file to exist.
let validateFilePath (pathAsString:string) =
// Use FileInfo to check that the path is valid.
// FileInfo performs validity checks on the path for the current platform, but
// does not check for the existence of the file.
if pathAsString.IndexOfAny(Path.GetInvalidPathChars()) >= 0 then
None
else
Some pathAsString
let contains (textToSearchFor:string) (text:string) =
text.IndexOf(textToSearchFor, System.StringComparison.InvariantCultureIgnoreCase) > -1
let findMatchingLines (file:string) textToSearchFor =
System.IO.File.ReadAllLines(file)
|> Seq.filter (contains textToSearchFor)
/// Settings are expected to be in the form "key=value"
/// Return exactly one setting.
let getIniValue (file:string) textToSearchFor =
let line =
findMatchingLines file textToSearchFor
|> Seq.exactlyOne
let splits = line.Split('=')
splits.[1]
/// This is standard Windows HRESULT for file already exists
module private Constants =
let [<Literal>] FILE_ALREADY_EXISTS = 0x80070050
/// Create a new file if it does not already exist and atomically write to it using the specified FileStream function.
/// If the file already exists then do nothing and return None.
let asyncTryCreateFile filePath (f: System.IO.FileStream -> Async<'a>) =
async {
if System.IO.File.Exists(filePath) then
return None
else
let fs =
try
Some (new System.IO.FileStream(filePath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None))
with
| :? System.IO.IOException as ex when ex.HResult = Constants.FILE_ALREADY_EXISTS -> None
match fs with
| Some fileStream ->
use stream = fileStream
let! result = f stream
return Some result
| None ->
return None
}