-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathWriteLinesToFile.cs
More file actions
414 lines (373 loc) · 17.8 KB
/
WriteLinesToFile.cs
File metadata and controls
414 lines (373 loc) · 17.8 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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.IO;
using System.Text;
using Microsoft.Build.Eventing;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using Microsoft.Build.Utilities;
#nullable disable
namespace Microsoft.Build.Tasks
{
/// <summary>
/// Appends a list of items to a file. One item per line with carriage returns in-between.
/// </summary>
[MSBuildMultiThreadableTask]
public class WriteLinesToFile : TaskExtension, IIncrementalTask, IMultiThreadableTask
{
// Default encoding taken from System.IO.WriteAllText()
private static readonly Encoding s_defaultEncoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
/// <inheritdoc />
public TaskEnvironment TaskEnvironment { get; set; } = TaskEnvironment.Fallback;
/// <summary>
/// File to write lines to.
/// </summary>
[Required]
public ITaskItem File { get; set; }
/// <summary>
/// Write each item as a line in the file.
/// </summary>
public ITaskItem[] Lines { get; set; }
/// <summary>
/// If true, overwrite any existing file contents.
/// </summary>
public bool Overwrite { get; set; }
/// <summary>
/// Encoding to be used.
/// </summary>
public string Encoding { get; set; }
/// <summary>
/// If true, the target file specified, if it exists, will be read first to compare against
/// what the task would have written. If identical, the file is not written to disk and the
/// timestamp will be preserved.
/// </summary>
public bool WriteOnlyWhenDifferent { get; set; }
/// <summary>
/// Question whether this task is incremental.
/// </summary>
/// <remarks>When question is true, then error out if WriteOnlyWhenDifferent would have
/// written to the file.</remarks>
public bool FailIfNotIncremental { get; set; }
[Obsolete]
public bool CanBeIncremental => WriteOnlyWhenDifferent;
/// <inheritdoc cref="ITask.Execute" />
public override bool Execute()
{
if (File == null)
{
return true;
}
ErrorUtilities.VerifyThrowArgumentLength(File.ItemSpec);
AbsolutePath filePath = FileUtilities.NormalizePath(TaskEnvironment.GetAbsolutePath(File.ItemSpec));
string contentsAsString = string.Empty;
if (Lines != null && Lines.Length > 0)
{
StringBuilder buffer = new StringBuilder(capacity: Lines.Length * 64);
foreach (ITaskItem line in Lines)
{
buffer.AppendLine(line.ItemSpec);
}
contentsAsString = buffer.ToString();
}
Encoding encoding = s_defaultEncoding;
if (Encoding != null)
{
try
{
encoding = System.Text.Encoding.GetEncoding(Encoding);
}
catch (ArgumentException)
{
Log.LogErrorWithCodeFromResources("General.InvalidValue", "Encoding", "WriteLinesToFile");
return false;
}
}
string directoryPath = Path.GetDirectoryName(filePath);
Directory.CreateDirectory(directoryPath);
// Handle WriteOnlyWhenDifferent check for Overwrite mode before executing
if (Overwrite && WriteOnlyWhenDifferent)
{
if (!ShouldWriteFileForOverwrite(filePath, contentsAsString))
{
return !Log.HasLoggedErrors;
}
}
// Use transactional mode by default when ChangeWave 18.3 is enabled
if (ChangeWaves.AreFeaturesEnabled(ChangeWaves.Wave18_3))
{
return ExecuteTransactional(filePath, directoryPath, contentsAsString, encoding);
}
else
{
return ExecuteNonTransactional(filePath, directoryPath, contentsAsString, encoding);
}
}
private bool ExecuteNonTransactional(AbsolutePath filePath, string directoryPath, string contentsAsString, Encoding encoding)
{
try
{
if (Overwrite)
{
System.IO.File.WriteAllText(filePath, contentsAsString, encoding);
}
else
{
if (WriteOnlyWhenDifferent)
{
Log.LogMessageFromResources(MessageImportance.Normal, "WriteLinesToFile.UnusedWriteOnlyWhenDifferent", filePath.OriginalValue);
}
System.IO.File.AppendAllText(filePath, contentsAsString, encoding);
}
return !Log.HasLoggedErrors;
}
catch (Exception e) when (ExceptionHandling.IsIoRelatedException(e))
{
string lockedFileMessage = LockCheck.GetLockedFileMessage(filePath);
Log.LogErrorWithCodeFromResources("WriteLinesToFile.ErrorOrWarning", filePath.OriginalValue, e.Message, lockedFileMessage);
return !Log.HasLoggedErrors;
}
}
private bool ExecuteTransactional(AbsolutePath filePath, string directoryPath, string contentsAsString, Encoding encoding)
{
try
{
if (Overwrite)
{
return SaveAtomically(filePath, contentsAsString, encoding);
}
else
{
if (WriteOnlyWhenDifferent)
{
Log.LogMessageFromResources(MessageImportance.Normal, "WriteLinesToFile.UnusedWriteOnlyWhenDifferent", filePath.OriginalValue);
}
// For append mode, use atomic write to append only the new content
// This avoids race conditions from reading-modifying-writing entire file
return SaveAtomicallyAppend(filePath, directoryPath, contentsAsString, encoding);
}
}
catch (Exception e) when (ExceptionHandling.IsIoRelatedException(e))
{
string lockedFileMessage = LockCheck.GetLockedFileMessage(filePath);
Log.LogErrorWithCodeFromResources("WriteLinesToFile.ErrorOrWarning", filePath.OriginalValue, e.Message, lockedFileMessage);
return !Log.HasLoggedErrors;
}
}
/// <summary>
/// Saves content to file atomically using a temporary file, following the Visual Studio editor pattern.
/// This is for overwrite mode where we write the entire content.
/// </summary>
private bool SaveAtomically(AbsolutePath filePath, string contentsAsString, Encoding encoding)
{
string temporaryFilePath = null;
try
{
string directoryPath = Path.GetDirectoryName(filePath);
// Create temporary file with ~ suffix (hides from GIT)
temporaryFilePath = Path.Combine(directoryPath, Path.GetRandomFileName() + "~");
// Write content to temporary file
System.IO.File.WriteAllText(temporaryFilePath, contentsAsString, encoding);
// Attempt to atomically replace target file with temporary file
try
{
// Replace the contents of filePath with the contents of the temporary using File.Replace
// to preserve the various attributes of the original file.
System.IO.File.Replace(temporaryFilePath, filePath, null, true);
temporaryFilePath = null; // Mark as successfully replaced
return !Log.HasLoggedErrors;
}
catch (FileNotFoundException)
{
// The target file doesn't exist, which is fine. Move the temp file to target.
try
{
System.IO.File.Move(temporaryFilePath, filePath);
temporaryFilePath = null; // Mark as successfully moved
return !Log.HasLoggedErrors;
}
catch (IOException moveEx)
{
// Only retry with Replace if the destination now exists (concurrent write race).
if (System.IO.File.Exists(filePath))
{
try
{
System.IO.File.Replace(temporaryFilePath, filePath, null, true);
temporaryFilePath = null; // Mark as successfully replaced
return !Log.HasLoggedErrors;
}
catch (IOException replaceEx)
{
// Both attempts failed; log the original move error as the root cause.
string lockedFileMessage = LockCheck.GetLockedFileMessage(filePath);
Log.LogErrorWithCodeFromResources("WriteLinesToFile.ErrorOrWarning", filePath.OriginalValue, moveEx.Message + " " + replaceEx.Message, lockedFileMessage);
return !Log.HasLoggedErrors;
}
}
// Destination doesn't exist; move failed for a different reason.
string lockedFileDiagnostics = LockCheck.GetLockedFileMessage(filePath);
Log.LogErrorWithCodeFromResources("WriteLinesToFile.ErrorOrWarning", filePath.OriginalValue, moveEx.Message, lockedFileDiagnostics);
return !Log.HasLoggedErrors;
}
}
catch (IOException)
{
// Replace failed (likely file is locked). Retry a few times with small delay.
for (int retry = 1; retry < 3; retry++)
{
try
{
System.Threading.Thread.Sleep(10);
System.IO.File.Replace(temporaryFilePath, filePath, null, true);
temporaryFilePath = null; // Mark as successfully replaced
return !Log.HasLoggedErrors;
}
catch (IOException)
{
// Continue to next retry
}
}
// Retries exhausted. Try simple write as fallback.
try
{
System.IO.File.WriteAllText(filePath, contentsAsString, encoding);
temporaryFilePath = null; // Mark temp as not needed
return !Log.HasLoggedErrors;
}
catch (Exception fallbackEx) when (ExceptionHandling.IsIoRelatedException(fallbackEx))
{
string lockedFileMessage = LockCheck.GetLockedFileMessage(filePath);
Log.LogErrorWithCodeFromResources("WriteLinesToFile.ErrorOrWarning", filePath.OriginalValue, fallbackEx.Message, lockedFileMessage);
return !Log.HasLoggedErrors;
}
}
}
catch (Exception e) when (ExceptionHandling.IsIoRelatedException(e))
{
string lockedFileMessage = LockCheck.GetLockedFileMessage(filePath);
Log.LogErrorWithCodeFromResources("WriteLinesToFile.ErrorOrWarning", filePath.OriginalValue, e.Message, lockedFileMessage);
return !Log.HasLoggedErrors;
}
finally
{
// Clean up temporary file if it still exists
if (temporaryFilePath != null)
{
try
{
if (System.IO.File.Exists(temporaryFilePath))
{
System.IO.File.Delete(temporaryFilePath);
}
}
catch
{
// Failing to clean up the temporary is an ignorable exception.
}
}
}
}
/// <summary>
/// Appends content to file atomically. For append mode, we simply append the new content
/// directly without reading the entire file, avoiding race conditions.
/// </summary>
private bool SaveAtomicallyAppend(AbsolutePath filePath, string directoryPath, string contentsAsString, Encoding encoding)
{
try
{
// For append mode, directly append new content to the file.
// This avoids the race condition of reading-modify-write entire file.
// Multiple processes can safely append without losing data.
System.IO.File.AppendAllText(filePath, contentsAsString, encoding);
return !Log.HasLoggedErrors;
}
catch (Exception e) when (ExceptionHandling.IsIoRelatedException(e))
{
string lockedFileMessage = LockCheck.GetLockedFileMessage(filePath);
Log.LogErrorWithCodeFromResources("WriteLinesToFile.ErrorOrWarning", filePath.OriginalValue, e.Message, lockedFileMessage);
return !Log.HasLoggedErrors;
}
}
/// <summary>
/// Checks if file should be written for Overwrite mode, considering WriteOnlyWhenDifferent option.
/// </summary>
/// <returns>True if file should be written, false if write should be skipped.</returns>
private bool ShouldWriteFileForOverwrite(AbsolutePath filePath, string contentsAsString)
{
if (!WriteOnlyWhenDifferent)
{
return true; // Always write if WriteOnlyWhenDifferent is false
}
MSBuildEventSource.Log.WriteLinesToFileUpToDateStart();
try
{
if (FileUtilities.FileExistsNoThrow(filePath))
{
// Use stream-based comparison to avoid loading entire file into memory
if (FilesAreIdentical(filePath, contentsAsString))
{
Log.LogMessageFromResources(MessageImportance.Low, "WriteLinesToFile.SkippingUnchangedFile", filePath.OriginalValue);
MSBuildEventSource.Log.WriteLinesToFileUpToDateStop(filePath.OriginalValue, true);
return false; // Skip write - content is identical
}
else if (FailIfNotIncremental)
{
Log.LogErrorWithCodeFromResources("WriteLinesToFile.ErrorReadingFile", filePath.OriginalValue);
MSBuildEventSource.Log.WriteLinesToFileUpToDateStop(filePath.OriginalValue, false);
return false; // Skip write - file differs and FailIfNotIncremental is set
}
}
}
catch (IOException)
{
Log.LogMessageFromResources(MessageImportance.Low, "WriteLinesToFile.ErrorReadingFile", filePath.OriginalValue);
}
MSBuildEventSource.Log.WriteLinesToFileUpToDateStop(filePath.OriginalValue, false);
return true; // Proceed with write
}
/// <summary>
/// Compares file contents with the given string using streams to avoid loading the entire file into memory.
/// Uses the default encoding for the comparison.
/// </summary>
/// <returns>True if file contents are identical to the provided string, false otherwise.</returns>
private bool FilesAreIdentical(AbsolutePath filePath, string contentsAsString)
{
try
{
byte[] newContentBytes = s_defaultEncoding.GetBytes(contentsAsString);
using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096))
{
// Quick check: file size must match
if (fileStream.Length != newContentBytes.Length)
{
return false;
}
// Compare bytes in chunks to avoid loading entire file into memory
byte[] fileBuffer = new byte[4096];
int newContentOffset = 0;
int bytesRead;
while ((bytesRead = fileStream.Read(fileBuffer, 0, fileBuffer.Length)) > 0)
{
// Compare current chunk with the corresponding part of new content
for (int i = 0; i < bytesRead; i++)
{
if (fileBuffer[i] != newContentBytes[newContentOffset + i])
{
return false; // Difference found, files are not identical
}
}
newContentOffset += bytesRead;
}
// All bytes matched
return true;
}
}
catch (Exception)
{
// If we can't read the file, treat it as different so write proceeds
return false;
}
}
}
}