Two class side extensions to CfsDirectoryDescriptor

48 views
Skip to first unread message

Louis LaBrunda

unread,
Jun 9, 2011, 10:28:59 AM6/9/11
to va-sma...@googlegroups.com
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.

Marten Feldtmann

unread,
Jun 9, 2011, 11:47:54 AM6/9/11
to va-sma...@googlegroups.com
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 ...

Louis LaBrunda

unread,
Jun 9, 2011, 1:44:55 PM6/9/11
to va-sma...@googlegroups.com
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

Wayne Johnston

unread,
Jun 10, 2011, 3:46:20 PM6/10/11
to va-sma...@googlegroups.com
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."
Reply all
Reply to author
Forward
0 new messages