Two class side extensions to CfsDirectoryDescriptor

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

Two class side extensions to CfsDirectoryDescriptor

Louis LaBrunda
Hi,

I have two class side extensions to CfsDirectoryDescriptor, one I wrote a while ago and one I just added that I would like to share here. The first makes all the directories in a given path.  The second removes all the files in a directory and sub-directories (including files in sub-directories) then removes the directory.  I have used both on Windows but I don't see any reason why they wouldn't work on UNIX or Linux.

I hope they are of use to someone and if Instantiations wants to put them in the base code, that's fine with me.

Lou

makePath: aPath
"Make all the directories in the path."
"Answer the path if it already existed or we were able to create it."
"Answer a CfsError if there were any other problems (see mkdir:)."
| aPathParts pathPart result |

(aPath size < 4) ifTrue: [^(CfsError new) errno: EINVAL]. "Don't allow nil, non or very short strings."
aPathParts := aPath subStrings: self pathSeparator.
(aPathParts size < 2) ifTrue: [^(CfsError new) errno: EINVAL]. "We need at least a drive letter and folder name."
pathPart := aPathParts first, self pathSeparatorString. "Start with the drive letter."
aPathParts from: 2 to: aPathParts size do: [:folderName |
pathPart := pathPart, folderName, self pathSeparatorString. "Build the path up with each folder name."
result := self mkdir: pathPart. "Try to make the folder."
(result isCfsError and: [result errno ~= EEXIST]) ifTrue: [^result]. "It's okay if it was already there."
].
^aPath. "If we get here the folders were made or were there already."


emptyAndRemoveDir: path
"Remove all files and sub-directories (including files in sub-directories) then remove the directory."
"Answer any CfsError encountered along the way or whatever #rmdir: answers."
| dStream dEntry tempPath tempName recursiveResult |

(path isNil or: [path isEmpty]) ifTrue: [^nil].
tempPath := (path last = self pathSeparator) ifTrue: [path] ifFalse: [path, self pathSeparatorString].
dStream := CfsDirectoryDescriptor opendir: tempPath pattern: nil mode: (FREG + FDIR).
dStream isCfsError ifTrue: [^dStream].
[
dEntry := recursiveResult isCfsError ifTrue: [] ifFalse: [dStream readdir].
dEntry notNil & dEntry isCfsError not & recursiveResult isCfsError not.
] whileTrue: [
tempName := tempPath, dEntry dName.
dEntry isReg ifTrue: [CfsFileDescriptor remove: tempName].
(dEntry isDir and: [(dEntry dName ~= '.') & (dEntry dName ~= '..')]) ifTrue: [recursiveResult := self emptyAndRemoveDir: tempName].
].
dStream closedir.
dEntry isCfsError ifTrue: [^dEntry].
recursiveResult isCfsError ifTrue: [^recursiveResult].
^self rmdir: path.

P.S.  I miss the way we could post code in the old forum.

--
You received this message because you are subscribed to the Google Groups "VA Smalltalk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/va-smalltalk/-/SwznuZMj8O0J.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/va-smalltalk?hl=en.
Reply | Threaded
Open this post in threaded view
|

Aw: Two class side extensions to CfsDirectoryDescriptor

Marten Feldtmann-2
Hello Louis,
 
makePath: aPath
"Make all the directories in the path."
"Answer the path if it already existed or we were able to create it."

 
I would think, that aPath realize does the same thing ...

--
You received this message because you are subscribed to the Google Groups "VA Smalltalk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/va-smalltalk/-/9dEi-KpvIIMJ.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/va-smalltalk?hl=en.
Reply | Threaded
Open this post in threaded view
|

Re: Aw: Two class side extensions to CfsDirectoryDescriptor

Louis LaBrunda
Hi Marten,

You are quite right.  And it seems deleteAll does what my emptyAndRemoveDir: does.  Both are instance methods of CfsPath.  I hadn't thought to look at CfsPath when I wrote mine.  I'm not sure I like creating an instance of CfsPath to create of remove directories but this is so seldom done that it really doesn't matter.  So, I will remove both of my extension in favor of using CfsPath.

Thanks, Lou

--
You received this message because you are subscribed to the Google Groups "VA Smalltalk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/va-smalltalk/-/PU8OsmKIOJIJ.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/va-smalltalk?hl=en.
Reply | Threaded
Open this post in threaded view
|

Re: Aw: Two class side extensions to CfsDirectoryDescriptor

Wayne Johnston
Interesting #deleteAll does what you wanted in #emptyAndRemoveDir:.  The comment says it doesn't, but it does:

"Delete the receiver.  If it is a directory and it contains files
or other directories, the operation may fail."

--
You received this message because you are subscribed to the Google Groups "VA Smalltalk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/va-smalltalk/-/b3aXENX0ZoIJ.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/va-smalltalk?hl=en.