Skip to content

Commit

Permalink
Refactored File Permissions structure
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Jul 19, 2022
1 parent 4688314 commit 20684cb
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 65 deletions.
17 changes: 8 additions & 9 deletions distribution/lib/Standard/Base/0.0.0-dev/src/System/File.enso
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ from Standard.Base import all

import Standard.Base.System.File.Option
import Standard.Base.System.File.Existing_File_Behavior
import Standard.Base.System.File.Posix_File_Permission
import Standard.Base.System.File.File_Permissions
import Standard.Base.Error.Problem_Behavior
import Standard.Base.Data.Text.Matching_Mode
import Standard.Base.Data.Text.Text_Sub_Range
Expand Down Expand Up @@ -379,19 +379,18 @@ type File

import Standard.Examples

example_permissions = Examples.csv.posix_permissions.contains Posix_File_Permission.Group_Read
posix_permissions : Vector Posix_File_Permission
example_permissions = Examples.csv.posix_permissions.group_read
posix_permissions : File_Permissions
posix_permissions =
java_permissions = Vector.Vector self.posix_permissions_builtin
java_permissions.map Posix_File_Permission.from_java
File_Permissions.from_java_set self.posix_permissions_builtin

## PRIVATE

Builtin method that gets this file's POSIX permissions.
Builtin method that gets this file's POSIX permissions as a Java Set.
Recommended to use `File.posix_permissions` instead which handles
potential exceptions and converts the array of Java enums into an Enso
Vector of atoms.
posix_permissions_builtin : Array
potential exceptions and converts an Enso representation of the
permissions.
posix_permissions_builtin : Set
posix_permissions_builtin = @Builtin_Method "File.posix_permissions_builtin"

## Checks whether the file exists and is a directory.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
from Standard.Base import all

polyglot java import java.nio.file.attribute.PosixFilePermission

type Permission
## Permission for read access for a given entity.
type Read

## Permission for write access for a given entity.
type Write

## Permission for execute access for a given entity.
type Execute

type File_Permissions
## Access permissions for a file.
type File_Permissions (owner : Vector Permission) (group : Vector Permission) (others : Vector Permission)

## Converts the Enso atom to its Java enum counterpart.
to_java : Vector PosixFilePermission
to_java =
result = Vector.new_builder
if self.owner.contains Read then
result.append PosixFilePermission.OWNER_READ
if self.owner.contains Write then
result.append PosixFilePermission.OWNER_WRITE
if self.owner.contains Execute then
result.append PosixFilePermission.OWNER_EXECUTE
if self.group.contains Read then
result.append PosixFilePermission.GROUP_READ
if self.group.contains Write then
result.append PosixFilePermission.GROUP_WRITE
if self.group.contains Execute then
result.append PosixFilePermission.GROUP_EXECUTE
if self.others.contains Read then
result.append PosixFilePermission.OTHERS_READ
if self.others.contains Write then
result.append PosixFilePermission.OTHERS_WRITE
if self.others.contains Execute then
result.append PosixFilePermission.OTHERS_EXECUTE
result.to_vector

## Checks if the given file can be read by the owner.
owner_read : Boolean
owner_read = self.owner.contains Read

## Checks if the given file can be written by the owner.
owner_write : Boolean
owner_write = self.owner.contains Write

## Checks if the given file can be executed by the owner.
owner_execute : Boolean
owner_execute = self.owner.contains Execute

## Checks if the given file can be read by the group.
group_read : Boolean
group_read = self.group.contains Read

## Checks if the given file can be written by the group.
group_write : Boolean
group_write = self.group.contains Write

## Checks if the given file can be executed by the group.
group_execute : Boolean
group_execute = self.group.contains Execute

## Checks if the given file can be read by others.
others_read : Boolean
others_read = self.others.contains Read

## Checks if the given file can be written by others.
others_write : Boolean
others_write = self.others.contains Write

## Checks if the given file can be executed by others.
others_execute : Boolean
others_execute = self.others.contains Execute

## Converts a Java `Set` of Java `PosixFilePermission` to `File_Permissions`.
from_java_set java_set =
owner = Vector.new_builder
group = Vector.new_builder
others = Vector.new_builder

if java_set.contains PosixFilePermission.OWNER_READ then
owner.append Read
if java_set.contains PosixFilePermission.OWNER_WRITE then
owner.append Write
if java_set.contains PosixFilePermission.OWNER_EXECUTE then
owner.append Execute
if java_set.contains PosixFilePermission.GROUP_READ then
group.append Read
if java_set.contains PosixFilePermission.GROUP_WRITE then
group.append Write
if java_set.contains PosixFilePermission.GROUP_EXECUTE then
group.append Execute
if java_set.contains PosixFilePermission.OTHERS_READ then
others.append Read
if java_set.contains PosixFilePermission.OTHERS_WRITE then
others.append Write
if java_set.contains PosixFilePermission.OTHERS_EXECUTE then
others.append Execute
File_Permissions owner.to_vector group.to_vector others.to_vector

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from Standard.Base import all

import Standard.Base.System.Platform
import Standard.Base.System.File.Posix_File_Permission
import Standard.Base.System.File.File_Permissions

polyglot java import java.lang.StringBuilder as Java_String_Builder

Expand Down Expand Up @@ -75,9 +75,8 @@ verify file = case Platform.os of
False -> False
True ->
permissions = file.posix_permissions
owner_only = [Posix_File_Permission.Owner_Read, Posix_File_Permission.Owner_Write, Posix_File_Permission.Owner_Execute]
only_owner_can_access = permissions.all owner_only.contains
only_owner_can_access
can_others_access = permissions.group.not_empty || permissions.others.not_empty
can_others_access.not

## PRIVATE
parse_file file =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import java.io.OutputStream;
import java.nio.file.CopyOption;
import java.nio.file.OpenOption;
import java.nio.file.attribute.PosixFilePermission;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Set;

/**
* A wrapper for {@link TruffleFile} objects exposed to the language. For methods documentation
Expand Down Expand Up @@ -82,12 +84,9 @@ public ZonedDateTime getLastModifiedTime() throws IOException {

@Builtin.Method(name = "posix_permissions_builtin")
@Builtin.WrapException(from = IOException.class, to = PolyglotError.class, propagate = true)
@Builtin.Specialize
public Array getPosixPermissions(Context ctx) throws IOException {
return new Array(
truffleFile.getPosixPermissions().stream()
.map(ctx.getEnvironment()::asGuestValue)
.toArray());
@Builtin.ReturningGuestObject
public Set<PosixFilePermission> getPosixPermissions() throws IOException {
return truffleFile.getPosixPermissions();
}

@Builtin.Method(name = "parent")
Expand Down
11 changes: 6 additions & 5 deletions test/Tests/src/System/File_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ from Standard.Base.System.File import File_Already_Exists_Error
from Standard.Base.Error.Problem_Behavior import all
import Standard.Base.System
import Standard.Base.System.Process
import Standard.Base.System.File.Posix_File_Permission
from Standard.Base.System.File.File_Permissions as File_Permissions_Module import all
from Standard.Base.System.Process.Exit_Code import Exit_Success

import Standard.Test
Expand Down Expand Up @@ -73,12 +73,13 @@ spec =
"foobar".write f

Process.run "chmod" ["0777", f.absolute.path] . should_equal Exit_Success
f.posix_permissions . should_contain_the_same_elements_as <|
Posix_File_Permission.possible_values
rwx = [Read, Write, Execute]
f.posix_permissions . should_equal <|
File_Permissions rwx rwx rwx

Process.run "chmod" ["0421", f.absolute.path] . should_equal Exit_Success
f.posix_permissions . should_contain_the_same_elements_as <|
[Posix_File_Permission.Owner_Read, Posix_File_Permission.Group_Write, Posix_File_Permission.Others_Execute]
f.posix_permissions . should_equal <|
File_Permissions [Read] [Write] [Execute]

f.delete

Expand Down

0 comments on commit 20684cb

Please sign in to comment.