forked from microsoft/json-document-transforms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow source and transform files to be read-only
Fixed microsoft#16
- Loading branch information
Showing
3 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE file in the project root for full license information. | ||
|
||
namespace Microsoft.VisualStudio.Jdt.Tests | ||
{ | ||
using System; | ||
using System.IO; | ||
|
||
/// <summary> | ||
/// Read-Only Temp File. | ||
/// </summary> | ||
/// <seealso cref="System.IDisposable" /> | ||
internal class ReadOnlyTempFile : IDisposable | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ReadOnlyTempFile"/> class. | ||
/// </summary> | ||
/// <param name="contents">The contents.</param> | ||
public ReadOnlyTempFile(string contents) | ||
{ | ||
// create temp file | ||
this.FilePath = Path.GetTempFileName(); | ||
|
||
// write contents | ||
File.WriteAllText(this.FilePath, contents); | ||
|
||
// set the file as read-only | ||
File.SetAttributes(this.FilePath, FileAttributes.ReadOnly); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the file path. | ||
/// </summary> | ||
/// <value>The file path.</value> | ||
public string FilePath { get; private set; } | ||
|
||
/// <summary> | ||
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
// delete file | ||
if (this.FilePath == null) | ||
{ | ||
// nothing to delete | ||
return; | ||
} | ||
|
||
if (!File.Exists(this.FilePath)) | ||
{ | ||
// nothing to delete | ||
return; | ||
} | ||
|
||
// remove read-only attribute if it exists | ||
FileAttributes attributes = File.GetAttributes(this.FilePath); | ||
if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) | ||
{ | ||
// read-only attribute exists | ||
// remove read-only attribute | ||
File.SetAttributes(this.FilePath, attributes ^ FileAttributes.ReadOnly); | ||
} | ||
|
||
// delete the file | ||
File.Delete(this.FilePath); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters