Skip to content
Permalink
Browse files

FileAttributesPlugin: Add ability to set file ownership and permissions

  • Loading branch information
akgrant committed Oct 3, 2018
1 parent 0808fe6 commit bedbadf6fff109ac967b75adb12f8b589a07333b
@@ -446,6 +446,13 @@ FileReference >> permissions [
^ filesystem permissions: self path
]

{ #category : #operations }
FileReference >> permissions: permissions [
"Set the receivers mode to anInteger (as defined by chmod())"

^filesystem file: self path posixPermissions: permissions posixPermission
]

{ #category : #printing }
FileReference >> printOn: aStream [
filesystem forReferencePrintOn: aStream.
@@ -516,6 +523,14 @@ FileReference >> symlinkEntry [
^ filesystem symlinkEntryAt: path
]

{ #category : #'as yet unclassified' }
FileReference >> symlinkUid: uid gid: gid [
"Set the owner and group of the receiver by numeric id.
An id of nil leaves it unchanged."

^filesystem file: self path symlinkUid: uid gid: gid.
]

{ #category : #accessing }
FileReference >> targetPath [
"Return the target file of the File described by aPath. For a regular file, this is itself, for a symbolic link, it is the file pointed to by the symbolic link"
@@ -528,6 +543,14 @@ FileReference >> uid [
^ filesystem uidOf: path
]

{ #category : #'as yet unclassified' }
FileReference >> uid: uid gid: gid [
"Set the owner and group of the receiver by numeric id.
An id of nil leaves it unchanged."

^filesystem file: self path uid: uid gid: gid
]

{ #category : #streams }
FileReference >> writeStream [

@@ -335,6 +335,27 @@ FileSystem >> extensionDelimiter [
^ $.
]

{ #category : #public }
FileSystem >> file: aResolvable posixPermissions: anInteger [
"Set the mode of aResolvable to anInteger (as defined by chmod())"

^store file: (self resolve: aResolvable) posixPermissions: anInteger
]

{ #category : #public }
FileSystem >> file: aResolvable symlinkUid: uidInteger gid: gidInteger [
"Set the owner and group of aResolvable by numeric id."

^store file: (self resolve: aResolvable) symlinkUid: uidInteger gid: gidInteger.
]

{ #category : #public }
FileSystem >> file: aResolvable uid: uidInteger gid: gidInteger [
"Set the owner and group of aResolvable by numeric id."

^store file: (self resolve: aResolvable) uid: uidInteger gid: gidInteger.
]

{ #category : #'public-enumerating' }
FileSystem >> fileNamesAt: aResolvable [
^ Array streamContents: [ :out |
@@ -258,6 +258,12 @@ FileSystemDirectoryEntry >> permissions [
^FileAttributeNotSupported signalWith: self
]

{ #category : #accessing }
FileSystemDirectoryEntry >> permissions: permissions [

^FileAttributeNotSupported signalWith: self
]

{ #category : #printing }
FileSystemDirectoryEntry >> printAttribute: anAttribute on: aStream [

@@ -37,6 +37,11 @@ FileSystemPermission >> <= other [
^ (posixPermission > other posixPermission) not
]

{ #category : #comparing }
FileSystemPermission >> = other [
^ other posixPermission = posixPermission
]

{ #category : #comparing }
FileSystemPermission >> > other [
^ other posixPermission < posixPermission
@@ -62,6 +67,12 @@ FileSystemPermission >> groupWrite [
^ self permissionBitAt: 5
]

{ #category : #comparing }
FileSystemPermission >> hash [

^posixPermission hash
]

{ #category : #initialization }
FileSystemPermission >> initialize: aNumber [
posixPermission := aNumber.
@@ -364,6 +364,27 @@ DiskStore >> exists: aPath [
^File exists: (self stringFromPath: aPath).
]

{ #category : #public }
DiskStore >> file: path posixPermissions: anInteger [
"Set the mode of pathString to anInteger (as defined by chmod())"

^File file:(self stringFromPath: path) posixPermissions: anInteger
]

{ #category : #public }
DiskStore >> file: path symlinkUid: uidInteger gid: gidInteger [
"Set the owner and group of path by numeric id."

^File file: (self stringFromPath: path) symlinkUid: uidInteger gid: gidInteger
]

{ #category : #public }
DiskStore >> file: path uid: uidInteger gid: gidInteger [
"Set the owner and group of path by numeric id."

^File file: (self stringFromPath: path) uid: uidInteger gid: gidInteger
]

{ #category : #printing }
DiskStore >> forReferencePrintOn: aStream [
aStream nextPutAll: 'File @ '
@@ -211,6 +211,52 @@ DiskFileAttributesTests >> testNLink [
self collectionAssert: [ :each | each numberOfHardLinks ] equals: 1.
]

{ #category : #tests }
DiskFileAttributesTests >> testPermissions [

| fileReference oldPermissions newPermissions |

fileReference := (FileLocator temp / ('FileAttributesTests-', UUIDGenerator next asString, '.txt')) resolve.

fileReference createFile.
[
oldPermissions := self tempFileEntry permissions.
newPermissions := FileSystemPermission posixPermissions: 8r007.
"Check that the existing permissions don't happen to match the test permissions"
self assert: oldPermissions ~= newPermissions.

"Setting permissions aren't supported on Windows"
OSPlatform current isWindows ifTrue: [
self should: [ fileReference permissions: newPermissions ] raise: Error ]
ifFalse: [
fileReference permissions: newPermissions.
self assert: fileReference permissions equals: newPermissions ].
] ensure: [ fileReference delete ]
]

{ #category : #tests }
DiskFileAttributesTests >> testSymlinkUidGid [

| fileReference oldUid oldGid |

fileReference := FileLocator temp / ('FileAttributesTests-', UUIDGenerator next asString, '.txt').
fileReference createFile.
[
oldUid := fileReference uid.
oldGid := fileReference gid.

"Setting ownership isn't supported on Windows"
OSPlatform current isWindows ifTrue: [
self should: [ fileReference symlinkUid: nil gid: nil ] raise: Error ]
ifFalse: [
"Since we can't reliably determine new test ids, call the method without changing values.
This will at least test that the primitive can be called and doesn't do something unexpected."
fileReference symlinkUid: nil gid: nil.
self assert: fileReference uid equals: oldUid.
self assert: fileReference gid equals: oldGid ].
] ensure: [ fileReference delete ]
]

{ #category : #tests }
DiskFileAttributesTests >> testTargetFile [

@@ -219,3 +265,26 @@ DiskFileAttributesTests >> testTargetFile [
self should: [ each targetFileReference ] raise: FileAttributeNotSupported ].

]

{ #category : #tests }
DiskFileAttributesTests >> testUidGid [

| fileReference oldUid oldGid |

fileReference := FileLocator temp / ('FileAttributesTests-', UUIDGenerator next asString, '.txt').
fileReference createFile.
[
oldUid := fileReference uid.
oldGid := fileReference gid.

"Setting ownership isn't supported on Windows"
OSPlatform current isWindows ifTrue: [
self should: [ fileReference uid: nil gid: nil ] raise: Error ]
ifFalse: [
"Since we can't reliably determine new test ids, call the method without changing values.
This will at least test that the primitive can be called and doesn't do something unexpected."
fileReference uid: nil gid: nil.
self assert: fileReference uid equals: oldUid.
self assert: fileReference gid equals: oldGid ].
] ensure: [ fileReference delete ]
]
@@ -200,6 +200,34 @@ File class >> exists: aString [
^self primExists: (self encodePathString: aString)
]

{ #category : #attributes }
File class >> file: pathString posixPermissions: anInteger [
"Set the mode of pathString to anInteger (as defined by chmod())"

^self primFile: (self encodePathString: pathString) posixPermissions: anInteger
]

{ #category : #attributes }
File class >> file: pathString symlinkUid: uidInteger gid: gidInteger [
"Set the owner and group of pathString by numeric id."

^self primFile: (self encodePathString: pathString)
symlinkUid: (uidInteger ifNil: [ -1 ])
gid: (gidInteger ifNil: [ -1 ])

]

{ #category : #attributes }
File class >> file: pathString uid: uidInteger gid: gidInteger [
"Set the owner and group of pathString by numeric id.
An id of nil leaves it unchanged."

^self primFile: (self encodePathString: pathString)
uid: (uidInteger ifNil: [ -1 ])
gid: (gidInteger ifNil: [ -1 ])

]

{ #category : #attributes }
File class >> fileAttribute: aString number: attributeNumber [
"Answer the attribute identified by attributeNumber for the specified file (aString).
@@ -583,6 +611,30 @@ File class >> primExists: aByteArray [

]

{ #category : #'primitives-file attributes' }
File class >> primFile: pathByteArray posixPermissions: anInteger [
"Set the mode of pathByateArray to anInteger (as defined by chmod())"

<primitive: 'primitiveChangeMode' module: 'FileAttributesPlugin' error: error>
^self signalError: error for: pathByteArray
]

{ #category : #'primitives-file attributes' }
File class >> primFile: pathByteArray symlinkUid: uidInteger gid: gidInteger [
"Set the owner and group of path by numeric id."

<primitive: 'primitiveSymlinkChangeOwner' module: 'FileAttributesPlugin' error: error>
^self signalError: error for: pathByteArray
]

{ #category : #'primitives-file attributes' }
File class >> primFile: pathByteArray uid: uidInteger gid: gidInteger [
"Set the owner and group of path by numeric id."

<primitive: 'primitiveChangeOwner' module: 'FileAttributesPlugin' error: error>
^self signalError: error for: pathByteArray
]

{ #category : #'primitives-file attributes' }
File class >> primFileAttribute: aByteArray number: attributeNumber [
"Answer a single attribute for the supplied file.

0 comments on commit bedbadf

Please sign in to comment.
You can’t perform that action at this time.