Repository navigation
Expand file tree
/
Copy pathWriteLinesToFile.cs
More file actions
404 lines (361 loc) · 17.1 KB
/
Copy pathWriteLinesToFile.cs
File metadata and controls
404 lines (361 loc) · 17.1 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
// 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.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);
/// <summary>
/// Moves <paramref name="source"/> to <paramref name="destination"/>, overwriting it if it exists.
/// Uses <c>Microsoft.IO.File</c> on .NET Framework to access the overwrite overload,
/// which is available natively in <c>System.IO.File</c> on .NET 6+.
/// </summary>
private static void MoveFileWithOverwrite(string source, string destination)
{
#if NETFRAMEWORK
// Microsoft.IO.Redist backports File.Move(overwrite) to .NET Framework.
Microsoft.IO.File.Move(source, destination, overwrite: true);
#elif NET
// File.Move(overwrite) is available natively on .NET 5+.
System.IO.File.Move(source, destination, overwrite: true);
#else
// netstandard2.0 output is ref asm only and never executed at runtime.
throw new PlatformNotSupportedException();
#endif
}
/// <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;
}
ArgumentException.ThrowIfNullOrEmpty(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, encoding))
{
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.
/// Writes to a temp file first, then moves it to the target with overwrite,
/// which handles both the "target exists" and "target doesn't exist" cases in a
/// single call and eliminates the race window present in a check-then-act pattern.
/// </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);
// Atomically move temp file to target, overwriting if it already exists.
// Using overwrite: true handles concurrent writes without a race condition —
// both "target doesn't exist" and "target already exists" cases are covered
// by a single operation, with no window between them.
const int maxAttempts = 3;
Exception lastException = null;
for (int attempt = 1; attempt <= maxAttempts; attempt++)
{
if (attempt > 1)
{
// Log the retry so concurrency issues are visible when diagnosing builds.
Log.LogMessageFromResources(MessageImportance.High, "WriteLinesToFile.Retry",
filePath.OriginalValue, attempt, maxAttempts, lastException.Message);
System.Threading.Thread.Sleep(10);
}
try
{
MoveFileWithOverwrite(temporaryFilePath, filePath);
temporaryFilePath = null;
return !Log.HasLoggedErrors;
}
catch (Exception ex) when (attempt < maxAttempts && ExceptionHandling.IsIoRelatedException(ex))
{
lastException = ex;
}
// On the last attempt, the exception propagates to the outer handler.
}
}
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.
}
}
}
return !Log.HasLoggedErrors;
}
/// <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, Encoding encoding)
{
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, encoding))
{
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.
/// </summary>
/// <remarks>
/// The comparison must use the same <paramref name="encoding"/> the task writes with, and must
/// account for the encoding preamble (BOM), because <see cref="System.IO.File.WriteAllText(string, string, Encoding)"/>
/// prepends it. Otherwise the byte length and content never match and the file is rewritten
/// on every build even when unchanged.
/// </remarks>
/// <returns>True if file contents are identical to the provided string, false otherwise.</returns>
private bool FilesAreIdentical(AbsolutePath filePath, string contentsAsString, Encoding encoding)
{
try
{
byte[] preamble = encoding.GetPreamble();
byte[] newContentBytes = encoding.GetBytes(contentsAsString);
long expectedLength = (long)preamble.Length + newContentBytes.Length;
using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096))
{
// Quick check: file size must match (preamble + content)
if (fileStream.Length != expectedLength)
{
return false;
}
// The on-disk stream starts with the preamble (BOM), if any. Compare it once up front
// (it is at most a few bytes) so the content comparison below stays a simple offset.
for (int i = 0; i < preamble.Length; i++)
{
if (fileStream.ReadByte() != preamble[i])
{
return false;
}
}
// Compare the encoded content in chunks to avoid loading the entire file into memory.
byte[] fileBuffer = new byte[4096];
int contentOffset = 0;
int bytesRead;
while ((bytesRead = fileStream.Read(fileBuffer, 0, fileBuffer.Length)) > 0)
{
for (int i = 0; i < bytesRead; i++)
{
if (fileBuffer[i] != newContentBytes[contentOffset + i])
{
return false; // Difference found, files are not identical
}
}
contentOffset += bytesRead;
}
// All bytes matched
return true;
}
}
catch (Exception)
{
// If we can't read the file, treat it as different so write proceeds
return false;
}
}
}
}