Skip to content

Commit

Permalink
Allow open of read-only files
Browse files Browse the repository at this point in the history
The retry-as-r/o code was expecting an IOException rather than an
UnauthorizedAccessException.  The CLI was mostly working, because
commands are specifically r/o or r/w and the initial open uses the
appropriate mode, but the GUI was failing.

This adds a read-only test to the CLI test suite.  It wouldn't
have failed before, because it's just detecting success/failure,
but it seems like a useful thing to exercise.

(Issue #9)
  • Loading branch information
fadden committed Jan 20, 2024
1 parent c827e55 commit 6b46044
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
2 changes: 1 addition & 1 deletion AppCommon/WorkTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public WorkTree(string hostPathName, DepthLimiter depthLimitFunc, bool asReadOnl
FileAccess access = asReadOnly ? FileAccess.Read : FileAccess.ReadWrite;
hostStream = new FileStream(hostPathName, FileMode.Open, access,
FileShare.Read);
} catch (IOException ex) {
} catch (UnauthorizedAccessException ex) {
// Retry with read-only access unless we did that the first time around.
if (asReadOnly) {
throw;
Expand Down
2 changes: 1 addition & 1 deletion cp2/ExtArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ private static bool OpenComponents(List<string> components, bool isReadOnly,
try {
hostStream = new FileStream(hostFileName, FileMode.Open,
isReadOnly ? FileAccess.Read : FileAccess.ReadWrite, FileShare.Read);
} catch (IOException ex) {
} catch (Exception ex) {
Console.Error.WriteLine("Unable to open '" + hostFileName + "': " + ex.Message);
return false;
}
Expand Down
36 changes: 36 additions & 0 deletions cp2/Tests/TestMisc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static void RunTest(ParamsBag parms) {
parms.FromNAPS = true;

TestCtrlNames(parms);
TestReadOnly(parms);

// AppleWorks filename formatting has been disabled.
//TestAppleWorksName(parms);
Expand Down Expand Up @@ -203,5 +204,40 @@ private static void TestAppleWorksName(ParamsBag parms) {
Environment.CurrentDirectory = oldCurrentDir;
}
}

private static void TestReadOnly(ParamsBag parms) {
// Grab a copy of a ProDOS disk image.
string proTestName = "simple-dir-test.po";
FileUtil.CopyFile(Path.Join(Controller.TEST_DATA, "prodos", proTestName),
Path.Join(Controller.TEST_TMP, proTestName));

string oldCurrentDir = Environment.CurrentDirectory;
try {
Environment.CurrentDirectory = Controller.TEST_TMP;

// Delete a file to make sure things are set up correctly.
if (!Delete.HandleDelete("rm",
new string[] { proTestName, "FILES.ADD.WITH" }, parms)) {
throw new Exception("Initial rm failed");
}

// Mark the disk read-only.
FileInfo finfo = new FileInfo(proTestName);
finfo.IsReadOnly = true;

// Try another deletion, should fail cleanly.
if (Delete.HandleDelete("rm",
new string[] { proTestName, "FILES.ADD.WITH" }, parms)) {
throw new Exception("Read-only rm succeeded");
}

// The read-only "list" operation should succeed.
if (!Catalog.HandleList("list", new string[] { proTestName }, parms)) {
throw new Exception("Read-only list failed");
}
} finally {
Environment.CurrentDirectory = oldCurrentDir;
}
}
}
}
4 changes: 2 additions & 2 deletions cp2_wpf/MainController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ private void DoOpenWorkFile(string pathName, bool asReadOnly) {
// File is fully parsed. Generate the archive tree.
PopulateArchiveTree();
} catch (Exception ex) {
// Expecting IOException and InvalidDataException.
// Expecting IOException, UnauthorizedAccessException, and InvalidDataException.
ShowFileError("Unable to open file: " + ex.Message);
return;
} finally {
Expand Down Expand Up @@ -613,7 +613,7 @@ private void DoOpenPhysicalDrive(string deviceName, bool asReadOnly) {
// File is fully parsed. Generate the archive tree.
PopulateArchiveTree();
} catch (Exception ex) {
// Expecting IOException and InvalidDataException.
// Expecting IOException, UnauthorizedAccessException, and InvalidDataException.
ShowFileError("Unable to open file: " + ex.Message);
return;
} finally {
Expand Down

0 comments on commit 6b46044

Please sign in to comment.