From 9430d88a98781bd3c9569d3c3b16453af10f41a4 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Thu, 30 Jul 2015 08:47:04 -0500 Subject: [PATCH] (GH-338) WIP --- .../chocolatey.tests.integration.csproj | 3 + .../filesystem/DotNetFileSystemSpecs.cs | 51 ++++++++++++++++ .../filesystem/context/attributes.txt | 32 ++++++++++ .../filesystem/DotNetFileSystem.cs | 58 +++++++++++++++++-- .../infrastructure/filesystem/IFileSystem.cs | 13 +++++ 5 files changed, 153 insertions(+), 4 deletions(-) create mode 100644 src/chocolatey.tests.integration/infrastructure/filesystem/context/attributes.txt diff --git a/src/chocolatey.tests.integration/chocolatey.tests.integration.csproj b/src/chocolatey.tests.integration/chocolatey.tests.integration.csproj index 30fddbaff6..83f2f1732a 100644 --- a/src/chocolatey.tests.integration/chocolatey.tests.integration.csproj +++ b/src/chocolatey.tests.integration/chocolatey.tests.integration.csproj @@ -227,6 +227,9 @@ Always + + Always + Always diff --git a/src/chocolatey.tests.integration/infrastructure/filesystem/DotNetFileSystemSpecs.cs b/src/chocolatey.tests.integration/infrastructure/filesystem/DotNetFileSystemSpecs.cs index 653fb137c3..b082492896 100644 --- a/src/chocolatey.tests.integration/infrastructure/filesystem/DotNetFileSystemSpecs.cs +++ b/src/chocolatey.tests.integration/infrastructure/filesystem/DotNetFileSystemSpecs.cs @@ -138,6 +138,57 @@ public void GetDirectories_should_return_a_string_array_with_directories() } } + [Category("Integration")] + public class when_setting_file_attributes_with_dotNetFileSystem : DotNetFileSystemSpecsBase + { + public override void Context() + { + base.Context(); + SourceFile = Path.Combine(DestinationPath, "attributes.txt"); + File.SetAttributes(SourceFile, (FileSystem.get_file_info_for(SourceFile).Attributes | FileAttributes.Hidden)); + } + + public override void Because() + { + FileSystem.ensure_file_attribute_set(SourceFile,FileAttributes.Hidden); + } + + [Fact] + public void visible_file_should_now_be_hidden() + { + (FileSystem.get_file_info_for(SourceFile).Attributes & FileAttributes.Hidden).ShouldEqual(FileAttributes.Hidden); + } + + public override void AfterObservations() + { + base.AfterObservations(); + File.SetAttributes(SourceFile, (FileSystem.get_file_info_for(SourceFile).Attributes | FileAttributes.Hidden)); + } + } + + + [Category("Integration")] + public class when_removing_readonly_attributes_with_dotNetFileSystem : DotNetFileSystemSpecsBase + { + public override void Context() + { + base.Context(); + SourceFile = Path.Combine(DestinationPath, "attributes.txt"); + File.SetAttributes(SourceFile, (FileSystem.get_file_info_for(SourceFile).Attributes | FileAttributes.ReadOnly)); + } + + public override void Because() + { + FileSystem.ensure_file_attribute_removed(SourceFile,FileAttributes.ReadOnly); + } + + [Fact] + public void readonly_file_should_no_longer_be_readonly() + { + (FileSystem.get_file_info_for(SourceFile).Attributes & FileAttributes.ReadOnly).ShouldNotEqual(FileAttributes.ReadOnly); + } + } + [Category("Integration")] public class when_running_fileMove_with_dotNetFileSystem : DotNetFileSystemSpecsBase { diff --git a/src/chocolatey.tests.integration/infrastructure/filesystem/context/attributes.txt b/src/chocolatey.tests.integration/infrastructure/filesystem/context/attributes.txt new file mode 100644 index 0000000000..bf452abdca --- /dev/null +++ b/src/chocolatey.tests.integration/infrastructure/filesystem/context/attributes.txt @@ -0,0 +1,32 @@ + + +The path of the righteous man is beset on all sides by the iniquities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy My brothers. And you will know My name is the Lord when I lay My vengeance upon thee. + +You see? It's curious. Ted did figure it out - time travel. And when we get back, we gonna tell everyone. How it's possible, how it's done, what the dangers are. But then why fifty years in the future when the spacecraft encounters a black hole does the computer call it an 'unknown entry event'? Why don't they know? If they don't know, that means we never told anyone. And if we never told anyone it means we never made it back. Hence we die down here. Just as a matter of deductive logic. + +You see? It's curious. Ted did figure it out - time travel. And when we get back, we gonna tell everyone. How it's possible, how it's done, what the dangers are. But then why fifty years in the future when the spacecraft encounters a black hole does the computer call it an 'unknown entry event'? Why don't they know? If they don't know, that means we never told anyone. And if we never told anyone it means we never made it back. Hence we die down here. Just as a matter of deductive logic. + +The lysine contingency - it's intended to prevent the spread of the animals is case they ever got off the island. Dr. Wu inserted a gene that makes a single faulty enzyme in protein metabolism. The animals can't manufacture the amino acid lysine. Unless they're continually supplied with lysine by us, they'll slip into a coma and die. + +Do you see any Teletubbies in here? Do you see a slender plastic tag clipped to my shirt with my name printed on it? Do you see a little Asian child with a blank expression on his face sitting outside on a mechanical helicopter that shakes when you put quarters in it? No? Well, that's what you see at a toy store. And you must think you're in a toy store, because you're here shopping for an infant named Jeb. + +Yeah, I like animals better than people sometimes... Especially dogs. Dogs are the best. Every time you come home, they act like they haven't seen you in a year. And the good thing about dogs... is they got different dogs for different people. Like pit bulls. The dog of dogs. Pit bull can be the right man's best friend... or the wrong man's worst enemy. You going to give me a dog for a pet, give me a pit bull. Give me... Raoul. Right, Omar? Give me Raoul. + +Like you, I used to think the world was this great place where everybody lived by the same standards I did, then some kid with a nail showed me I was living in his world, a world where chaos rules not order, a world where righteousness is not rewarded. That's Cesar's world, and if you're not willing to play by his rules, then you're gonna have to pay the price. + +The lysine contingency - it's intended to prevent the spread of the animals is case they ever got off the island. Dr. Wu inserted a gene that makes a single faulty enzyme in protein metabolism. The animals can't manufacture the amino acid lysine. Unless they're continually supplied with lysine by us, they'll slip into a coma and die. + +You see? It's curious. Ted did figure it out - time travel. And when we get back, we gonna tell everyone. How it's possible, how it's done, what the dangers are. But then why fifty years in the future when the spacecraft encounters a black hole does the computer call it an 'unknown entry event'? Why don't they know? If they don't know, that means we never told anyone. And if we never told anyone it means we never made it back. Hence we die down here. Just as a matter of deductive logic. + +You think water moves fast? You should see ice. It moves like it has a mind. Like it knows it killed the world once and got a taste for murder. After the avalanche, it took us a week to climb out. Now, I don't know exactly when we turned on each other, but I know that seven of us survived the slide... and only five made it out. Now we took an oath, that I'm breaking now. We said we'd say it was the snow that killed the other two, but it wasn't. Nature is lethal but it doesn't hold a candle to man. + +Like you, I used to think the world was this great place where everybody lived by the same standards I did, then some kid with a nail showed me I was living in his world, a world where chaos rules not order, a world where righteousness is not rewarded. That's Cesar's world, and if you're not willing to play by his rules, then you're gonna have to pay the price. + +Do you see any Teletubbies in here? Do you see a slender plastic tag clipped to my shirt with my name printed on it? Do you see a little Asian child with a blank expression on his face sitting outside on a mechanical helicopter that shakes when you put quarters in it? No? Well, that's what you see at a toy store. And you must think you're in a toy store, because you're here shopping for an infant named Jeb. + + + +
+lorem ipsum
+ + diff --git a/src/chocolatey/infrastructure/filesystem/DotNetFileSystem.cs b/src/chocolatey/infrastructure/filesystem/DotNetFileSystem.cs index 77aac30f4d..03e40eec1b 100644 --- a/src/chocolatey/infrastructure/filesystem/DotNetFileSystem.cs +++ b/src/chocolatey/infrastructure/filesystem/DotNetFileSystem.cs @@ -158,6 +158,24 @@ public bool is_system_file(FileInfo file) } return isSystemFile; + } + + public bool is_readonly_file(FileInfo file) + { + bool isReadOnlyFile = ((file.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly); + if (!isReadOnlyFile) + { + //check the directory to be sure + DirectoryInfo directoryInfo = get_directory_info_for(file.DirectoryName); + isReadOnlyFile = ((directoryInfo.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly); + this.Log().Debug(() => "Is directory \"{0}\" a readonly directory? {1}".format_with(file.DirectoryName, isReadOnlyFile.to_string())); + } + else + { + this.Log().Debug(() => "File \"{0}\" is a readonly file.".format_with(file.FullName)); + } + + return isReadOnlyFile; } public bool is_encrypted_file(FileInfo file) @@ -431,17 +449,49 @@ public void ensure_file_attribute_set(string path, FileAttributes attributes) if (directory_exists(path)) { var directoryInfo = get_directory_info_for(path); - if ((directoryInfo.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden) + if ((directoryInfo.Attributes & attributes) != attributes) + { + directoryInfo.Attributes |= attributes; + } + } + if (file_exists(path)) + { + var fileInfo = get_file_info_for(path); + if ((fileInfo.Attributes & attributes) != attributes) + { + fileInfo.Attributes |= attributes; + } + } + } + + public bool remove_read_only_attribute(string filePath) + { + if (!file_exists(filePath)) return false; + + var fileInfo = get_file_info_for(filePath); + if (!fileInfo.IsReadOnly) return true; + + File.SetAttributes(filePath, (fileInfo.Attributes & ~FileAttributes.ReadOnly)); + + return (get_file_info_for(filePath).Attributes & FileAttributes.ReadOnly) != FileAttributes.ReadOnly; + } + + public void ensure_file_attribute_removed(string path, FileAttributes attributes) + { + if (directory_exists(path)) + { + var directoryInfo = get_directory_info_for(path); + if ((directoryInfo.Attributes & attributes) == attributes) { - directoryInfo.Attributes |= FileAttributes.Hidden; + directoryInfo.Attributes &= ~attributes; } } if (file_exists(path)) { var fileInfo = get_file_info_for(path); - if ((fileInfo.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden) + if ((fileInfo.Attributes & attributes) == attributes) { - fileInfo.Attributes |= FileAttributes.Hidden; + fileInfo.Attributes &= ~attributes; } } } diff --git a/src/chocolatey/infrastructure/filesystem/IFileSystem.cs b/src/chocolatey/infrastructure/filesystem/IFileSystem.cs index 959a9205c9..a8ef5153fe 100644 --- a/src/chocolatey/infrastructure/filesystem/IFileSystem.cs +++ b/src/chocolatey/infrastructure/filesystem/IFileSystem.cs @@ -138,6 +138,13 @@ public interface IFileSystem /// True if the file has the System attribute marked, otherwise false bool is_system_file(FileInfo file); + /// + /// Determines if a file is a read only file + /// + /// File to check + /// True if the file has the ReadOnly attribute marked, otherwise false + bool is_readonly_file(FileInfo file); + /// /// Determines if a file is encrypted or not /// @@ -338,5 +345,11 @@ public interface IFileSystem /// The attributes. void ensure_file_attribute_set(string path, FileAttributes attributes); + /// + /// Ensure file attributes are removed from a specified path. + /// + /// The path. + /// The attributes. + void ensure_file_attribute_removed(string path, FileAttributes attributes); } } \ No newline at end of file