retrieving file attributes

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

retrieving file attributes

Chris Harrington-2
Hello all...

I couldn't find a method in Dolphin 4.xx that returns the file attributes
for a file in a readable manner.  Does anyone know if this exists?  I
believe that I would need to send the allMask: message to the file
attributes iterating over a collection of file attribute constants and
identifying each one.  Something along the lines of:
#(FILE_ATTRIBUTE_READONLY, etc..) select: [ :each |  attributes allMask:
each]

Is there perhaps a more elegant method?

Thanks,

Chris


Reply | Threaded
Open this post in threaded view
|

Re: retrieving file attributes

Don Rylander-3
Chris,

I didn't find a more elegant method, but I had to contend with this
a while back, so I threw some related stuff together into a
package (included below as text), which might be helpful.  I haven't
used this for a while, and I suspect I didn't quite finish it (please
forgive any messy or half thought-out code), but it should save some
typing at least.

Note that the usual caveats apply ("This software is guaranteed
unsuitable for any purpose whatsoever." ;-).  I'd recommend opening
it in a text editor to get an idea of what it does before opening it
in your working image.

Don

#### Copy the stuff below into a text editor and save it as a .PAC file
| package |
package := Package name: '(File Attributes)'.
package paxVersion: 0;
 basicComment: ''.

package basicPackageVersion: ''.

"Add the package scripts"
package basicScriptAt: #preinstall put: '"Add FILE_ATTRIBUTE_* constants
(FILE_ATTRIBUTE_NORMAL and FILE_ATTRIBUTE_READONLY are already there."
(Dictionary new)
 at: #FILE_ATTRIBUTE_READONLY put: 1;
 at: #FILE_ATTRIBUTE_HIDDEN put: 2;
 at: #FILE_ATTRIBUTE_SYSTEM put: 4;
 at: #FILE_ATTRIBUTE_ARCHIVE put: 32;
 at: #FILE_ATTRIBUTE_NORMAL put: 128;
 at: #FILE_ATTRIBUTE_TEMPORARY put: 256;
 associationsDo: [:assoc|
  Win32Constants at: assoc key ifAbsent: [Win32Constants at: assoc key put:
assoc value]
 ]'.

"Add the class names, loose method names, global names, resource names"
package classNames
 yourself.

package methodNames
 add: #File -> #beArchive;
 add: #File -> #beHidden;
 add: #File -> #beNotArchive;
 add: #File -> #beNotHidden;
 add: #File -> #beNotReadOnly;
 add: #File -> #beReadOnly;
 add: #File -> #isArchive;
 add: #File -> #isHidden;
 add: #File -> #isNormal;
 add: #File -> #isReadOnly;
 add: #File -> #isSystem;
 add: #File -> #isTemporary;
 add: #FileStream -> #beArchive;
 add: #FileStream -> #beHidden;
 add: #FileStream -> #beNotArchive;
 add: #FileStream -> #beNotHidden;
 add: #FileStream -> #beNotReadOnly;
 add: #FileStream -> #beReadOnly;
 add: #FileStream -> #isArchive;
 add: #FileStream -> #isHidden;
 add: #FileStream -> #isReadOnly;
 add: #FileStream -> #isSystem;
 add: #KernelLibrary -> #setFileAttributes:attributeBytes:;
 add: 'File class' -> #clearArchiveAttribute:;
 add: 'File class' -> #clearFileAttribute:for:;
 add: 'File class' -> #clearHiddenAttribute:;
 add: 'File class' -> #clearReadOnlyAttribute:;
 add: 'File class' -> #clearSystemAttribute:;
 add: 'File class' -> #isArchiveAttributeSet:;
 add: 'File class' -> #isFileAttribute:setForFile:;
 add: 'File class' -> #isHiddenAttributeSet:;
 add: 'File class' -> #isNormalAttributeSet:;
 add: 'File class' -> #isReadOnlyAttributeSet:;
 add: 'File class' -> #isSystemAttributeSet:;
 add: 'File class' -> #isTemporaryAttributeSet:;
 add: 'File class' -> #setArchiveAttribute:;
 add: 'File class' -> #setFileAttribute:for:;
 add: 'File class' -> #setHiddenAttribute:;
 add: 'File class' -> #setReadOnlyAttribute:;
 add: 'File class' -> #setSystemAttribute:;
 yourself.

package globalNames
 yourself.

package resourceNames
 yourself.

"Binary Global Names"
package binaryGlobalNames: (Set new
 yourself).
"Resource Names"
package allResourceNames: (Set new
 yourself).

"Add the prerequisite names"
package setPrerequisites: (IdentitySet new
 add: 'Dolphin';
 yourself).

package!

"Class Definitions"!

"Loose Methods"!

!File methodsFor!

beArchive

 self class setArchiveAttribute: spec!

beHidden

 self class setHiddenAttribute: spec!

beNotArchive

 self class clearArchiveAttribute: spec!

beNotHidden

 self class clearHiddenAttribute: spec!

beNotReadOnly

 self class clearReadOnlyAttribute: spec!

beReadOnly

 self class setReadOnlyAttribute: spec!

isArchive

 ^self class isArchiveAttributeSet: spec!

isHidden

 ^self class isHiddenAttributeSet: spec!

isNormal

 ^self class isNormalAttributeSet: spec!

isReadOnly

 ^self class isReadOnlyAttributeSet: spec!

isSystem

 ^self class isSystemAttributeSet: spec!

isTemporary

 ^self class isTemporaryAttributeSet: spec! !
!File categoriesFor: #beArchive!«DR Categories»-Added by DR!public! !
!File categoriesFor: #beHidden!«DR Categories»-Added by DR!public! !
!File categoriesFor: #beNotArchive!«DR Categories»-Added by DR!public! !
!File categoriesFor: #beNotHidden!«DR Categories»-Added by DR!public! !
!File categoriesFor: #beNotReadOnly!«DR Categories»-Added by DR!public! !
!File categoriesFor: #beReadOnly!«DR Categories»-Added by DR!public! !
!File categoriesFor: #isArchive!«DR Categories»-Added by DR!public! !
!File categoriesFor: #isHidden!«DR Categories»-Added by DR!public! !
!File categoriesFor: #isNormal!«DR Categories»-Added by DR!public! !
!File categoriesFor: #isReadOnly!«DR Categories»-Added by DR!public! !
!File categoriesFor: #isSystem!«DR Categories»-Added by DR!public! !
!File categoriesFor: #isTemporary!«DR Categories»-Added by DR!public! !

!File class methodsFor!

clearArchiveAttribute: aFilenameString
 "FILE_ATTRIBUTE_ARCHIVE = 32"

 #drAdded.
 self clearFileAttribute: FILE_ATTRIBUTE_ARCHIVE for: aFilenameString!

clearFileAttribute: anIntegerFileAttribute for: aFilenameString

 | currentAttributes newAttributes |
 #drAdded.
 currentAttributes := KernelLibrary default getFileAttributes: aFilenameString.
 currentAttributes ~= -1
  ifTrue: [(currentAttributes allMask: anIntegerFileAttribute)
   ifTrue: [newAttributes := currentAttributes - anIntegerFileAttribute.
    KernelLibrary default setFileAttributes: aFilenameString attributeBytes:
newAttributes]]
  ifFalse: [NotFoundError signalWith: ('File ', aFilenameString)].
!

clearHiddenAttribute: aFilenameString
 "FILE_ATTRIBUTE_HIDDEN = 2"

 #drAdded.
 self clearFileAttribute: FILE_ATTRIBUTE_HIDDEN for: aFilenameString!

clearReadOnlyAttribute: aFilenameString
 "FILE_ATTRIBUTE_READONLY = 1"

 #drAdded.
 self clearFileAttribute: FILE_ATTRIBUTE_READONLY for: aFilenameString!

clearSystemAttribute: aFilenameString
 "FILE_ATTRIBUTE_SYSTEM = 4"

 #drAdded.
 self clearFileAttribute: FILE_ATTRIBUTE_SYSTEM for: aFilenameString!

isArchiveAttributeSet: aFilenameString

 ^self isFileAttribute: FILE_ATTRIBUTE_ARCHIVE setForFile: aFilenameString!

isFileAttribute: anIntegerFileAttribute setForFile: aFilenameString

 ^(KernelLibrary default getFileAttributes: aFilenameString) allMask:
anIntegerFileAttribute!

isHiddenAttributeSet: aFilenameString

 ^self isFileAttribute: FILE_ATTRIBUTE_HIDDEN setForFile: aFilenameString!

isNormalAttributeSet: aFilenameString

 ^self isFileAttribute: FILE_ATTRIBUTE_NORMAL setForFile: aFilenameString!

isReadOnlyAttributeSet: aFilenameString

 ^self isFileAttribute: FILE_ATTRIBUTE_READONLY setForFile: aFilenameString!

isSystemAttributeSet: aFilenameString

 ^self isFileAttribute: FILE_ATTRIBUTE_SYSTEM setForFile: aFilenameString!

isTemporaryAttributeSet: aFilenameString

 ^self isFileAttribute: FILE_ATTRIBUTE_TEMPORARY setForFile: aFilenameString!

setArchiveAttribute: aFilenameString
 "FILE_ATTRIBUTE_ARCHIVE = 32"

 #drAdded.
 self setFileAttribute: FILE_ATTRIBUTE_ARCHIVE for: aFilenameString!

setFileAttribute: anIntegerFileAttribute for: aFilenameString

 | currentAttributes newAttributes |
 #drAdded.
 currentAttributes := KernelLibrary default getFileAttributes: aFilenameString.
 currentAttributes ~= -1
  ifTrue: [newAttributes := currentAttributes bitOr: anIntegerFileAttribute.
   KernelLibrary default setFileAttributes: aFilenameString attributeBytes:
newAttributes.]
  ifFalse: [NotFoundError signalWith: ('File ', aFilenameString)].

!

setHiddenAttribute: aFilenameString
 "FILE_ATTRIBUTE_HIDDEN = 2"

 #drAdded.
 self setFileAttribute: FILE_ATTRIBUTE_HIDDEN for: aFilenameString!

setReadOnlyAttribute: aFilenameString
 "FILE_ATTRIBUTE_READONLY = 1"

 #drAdded.
 self setFileAttribute: FILE_ATTRIBUTE_READONLY for: aFilenameString!

setSystemAttribute: aFilenameString
 "FILE_ATTRIBUTE_SYSTEM = 4"

 #drAdded.
 self setFileAttribute: FILE_ATTRIBUTE_SYSTEM for: aFilenameString! !
!File class categoriesFor: #clearArchiveAttribute:!«DR Categories»-Added by
DR!public! !
!File class categoriesFor: #clearFileAttribute:for:!«DR Categories»-Added by
DR!private! !
!File class categoriesFor: #clearHiddenAttribute:!«DR Categories»-Added by
DR!public! !
!File class categoriesFor: #clearReadOnlyAttribute:!«DR Categories»-Added by
DR!public! !
!File class categoriesFor: #clearSystemAttribute:!«DR Categories»-Added by
DR!public! !
!File class categoriesFor: #isArchiveAttributeSet:!«DR Categories»-Added by
DR!public! !
!File class categoriesFor: #isFileAttribute:setForFile:!«DR Categories»-Added
by DR!private! !
!File class categoriesFor: #isHiddenAttributeSet:!«DR Categories»-Added by
DR!public! !
!File class categoriesFor: #isNormalAttributeSet:!«DR Categories»-Added by
DR!public! !
!File class categoriesFor: #isReadOnlyAttributeSet:!«DR Categories»-Added by
DR!public! !
!File class categoriesFor: #isSystemAttributeSet:!«DR Categories»-Added by
DR!public! !
!File class categoriesFor: #isTemporaryAttributeSet:!«DR Categories»-Added by
DR!public! !
!File class categoriesFor: #setArchiveAttribute:!«DR Categories»-Added by
DR!public! !
!File class categoriesFor: #setFileAttribute:for:!«DR Categories»-Added by
DR!private! !
!File class categoriesFor: #setHiddenAttribute:!«DR Categories»-Added by
DR!public! !
!File class categoriesFor: #setReadOnlyAttribute:!«DR Categories»-Added by
DR!public! !
!File class categoriesFor: #setSystemAttribute:!«DR Categories»-Added by
DR!public! !

!FileStream methodsFor!

beArchive

 ^file beArchive!

beHidden

 ^file beHidden!

beNotArchive

 ^file beNotArchive!

beNotHidden

 ^file beNotHidden!

beNotReadOnly

 ^file beNotReadOnly!

beReadOnly

 ^file beReadOnly!

isArchive

 ^file isArchive!

isHidden

 ^file isHidden!

isReadOnly

 ^file isReadOnly!

isSystem

 ^file isSystem! !
!FileStream categoriesFor: #beArchive!«DR Categories»-Added by DR!public! !
!FileStream categoriesFor: #beHidden!«DR Categories»-Added by DR!public! !
!FileStream categoriesFor: #beNotArchive!«DR Categories»-Added by DR!public! !
!FileStream categoriesFor: #beNotHidden!«DR Categories»-Added by DR!public! !
!FileStream categoriesFor: #beNotReadOnly!«DR Categories»-Added by DR!public! !
!FileStream categoriesFor: #beReadOnly!«DR Categories»-Added by DR!public! !
!FileStream categoriesFor: #isArchive!«DR Categories»-Added by DR!public! !
!FileStream categoriesFor: #isHidden!«DR Categories»-Added by DR!public! !
!FileStream categoriesFor: #isReadOnly!«DR Categories»-Added by DR!public! !
!FileStream categoriesFor: #isSystem!«DR Categories»-Added by DR!public! !

!KernelLibrary methodsFor!

setFileAttributes: aFileName attributeBytes: anIntegerAttributeBytes
 "Set attributes for the specified file or directory.  Supported attributes
are:
 FILE_ATTRIBUTE_READONLY  1
 FILE_ATTRIBUTE_HIDDEN  2
 FILE_ATTRIBUTE_SYSTEM  4
 FILE_ATTRIBUTE_ARCHIVE  32
 FILE_ATTRIBUTE_NORMAL  128
 FILE_ATTRIBUTE_TEMPORARY 256

  BOOL SetFileAttributes(
   LPCTSTR  lpFileName  // address of the name of a file or directory
   DWORD dwFileAttributes // attributes
  );"
 <stdcall: bool SetFileAttributesA lpstr dword>
 #drAdded.
 ^self invalidCall
! !
!KernelLibrary categoriesFor:
#setFileAttributes:attributeBytes:!*-primitives!«DR Categories»-Added by
DR!public!win32 functions-console! !

"End of package definition"!


"Binary Globals"!

"Resources"!