Skip to content

Commit 15466c7

Browse files
sgebbiedipinhora
authored andcommitted
Change directory.open_file() to use readonly open (ponylang#2697)
* Change directory.open_file() to use readonly open On non-Linux and non-BSD platforms the direction.open_file() logic defaults to a named path base open, rather than using `openat` calls. However, this was using the `File.create(...)` call which opens files for read/write, rather than just readonly. This in turn fails since the `FileWrite` capability is explicitly dropped. This commit simply switches to using `File.open(...)` which opens the file in a readonly mode. This fixes: ponylang#2695 * Introduce a unit test Directory.open_file Note, care needs to be taken not the mask the `error` in a: ``` try ... then ... end ``` that does not have an `else` block. * Tweak to match stdlib style guide. Space between : and type.
1 parent 8b9f908 commit 15466c7

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

packages/files/_test.pony

+29
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ actor Main is TestList
1111
test(_TestMkdtemp)
1212
test(_TestWalk)
1313
test(_TestDirectoryOpen)
14+
test(_TestDirectoryFileOpen)
1415
test(_TestPathClean)
1516
test(_TestPathJoin)
1617
test(_TestPathRel)
@@ -138,6 +139,34 @@ class iso _TestDirectoryOpen is UnitTest
138139
h.assert_true(tmp.remove())
139140
end
140141

142+
class iso _TestDirectoryFileOpen is UnitTest
143+
fun name(): String => "files/Directory.open-file"
144+
fun apply(h: TestHelper) =>
145+
try
146+
// make a temporary directory
147+
let dir_path = FilePath.mkdtemp(
148+
h.env.root as AmbientAuth,
149+
"tmp.directory.open-file")?
150+
try
151+
let dir = Directory(dir_path)?
152+
153+
// create a file (rw)
154+
let created: File = dir.create_file("created")?
155+
h.assert_true(created.valid())
156+
created.dispose()
157+
158+
// open a file (ro)
159+
let readonly: File = dir.open_file("created")?
160+
h.assert_true(readonly.valid())
161+
readonly.dispose()
162+
else
163+
h.fail("Unhandled inner error!")
164+
then
165+
dir_path.remove()
166+
end
167+
else
168+
h.fail("Unhandled error!")
169+
end
141170

142171
class iso _TestPathClean is UnitTest
143172
fun name(): String => "files/Path.clean"

packages/files/directory.pony

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ class Directory
255255
I32(0x1B6))
256256
recover File._descriptor(fd', path')? end
257257
else
258-
recover File(path') end
258+
recover File.open(path') end
259259
end
260260

261261
fun info(): FileInfo ? =>

0 commit comments

Comments
 (0)