AutoDeletedFilename.st
author Claus Gittinger <cg@exept.de>
Tue, 09 Jul 2019 20:55:17 +0200
changeset 24417 03b083548da2
parent 23666 7bc7a0c483d7
child 24424 9e79e32cc079
permissions -rw-r--r--
#REFACTORING by exept class: Smalltalk class changed: #recursiveInstallAutoloadedClassesFrom:rememberIn:maxLevels:noAutoload:packageTop:showSplashInLevels: Transcript showCR:(... bindWith:...) -> Transcript showCR:... with:...

"{ Encoding: utf8 }"

"
 COPYRIGHT (c) 2007 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libbasic' }"

"{ NameSpace: Smalltalk }"

Filename subclass:#AutoDeletedFilename
	instanceVariableNames:'concreteFilename'
	classVariableNames:'Lobby'
	poolDictionaries:''
	category:'System-Support'
!

!AutoDeletedFilename class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2007 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    Used with temporary files - these will automatically delete themself,
    when no longer referenced (i.e. when finalized)
    
    See -> Filename asAutoDeletedFilename

    [instance variables:]
        concreteFilename    UnixFilename|PCFilename     the concrete filename that
                                                        implements the OS-specific behavior
    [author:]
        cg - original code
        sv - fixed and enhanced
"
!

examples
"
    the following file will be automatically deleted after some time:
                                                    [exBegin]
    |f p|

    f := AutoDeletedFilename newTemporary.
    f writeStream
        nextPutLine:'hello';
        close.
    p := f pathName.
    Transcript showCR:p.
    f := f asAutoDeletedFilename.
    self assert:(p asFilename exists).
    ObjectMemory collectGarbage.
    Delay waitForSeconds:2.
    self assert:(p asFilename exists).
    f := nil.
    ObjectMemory collectGarbage.
    Delay waitForSeconds:2.
    self assert:(p asFilename exists not).
                                                    [exEnd]


    you can also delete it manually:
                                                    [exBegin]
    |f p|

    f := Filename newTemporary.
    f writeStream
        nextPutLine:'hello';
        close.
    p := f pathName.
    Transcript showCR:p.
    f := f asAutoDeletedFilename.
    self assert:(p asFilename exists).
    ObjectMemory collectGarbage.
    Delay waitForSeconds:2.
    self assert:(p asFilename exists).
    f remove.
    f := nil.
    ObjectMemory collectGarbage.
    Delay waitForSeconds:2.
    self assert:(p asFilename exists not).
                                                    [exEnd]
"
! !

!AutoDeletedFilename class methodsFor:'initialization'!

initialize
    Lobby isNil ifTrue:[
        Lobby := Registry new.
    ].
    Smalltalk addDependent:self.    "inform me when smalltalk exits"
! !

!AutoDeletedFilename class methodsFor:'instance creation'!

named:aString
    "return a filename for a directory named aString.
     This is the same as 'aString asFilename'."

    ^ self basicNew setName:aString

    "Created: / 06-02-2019 / 10:15:01 / Stefan Vogel"
! !

!AutoDeletedFilename class methodsFor:'change and update'!

update:anAspect with:aParameter from:changedObject
    "when Smalltalk exits, remove all auto deleted files"

    anAspect == #aboutToQuit ifTrue:[
        |currentFilename|
        "do it with timeout in case of a non-responding remote file server"
        ([
            Lobby do:[:each|
                currentFilename := each.
                each basicFinalize
            ].
        ] valueWithTimeout:1 minutes) isNil ifTrue:[
            'AutoDeletedFilename: timed out while removing: ' errorPrint. currentFilename errorPrintCR.
        ].
    ].
    super update:anAspect with:aParameter from:changedObject
! !

!AutoDeletedFilename methodsFor:'accessing'!

concreteFilename:aFilename
    concreteFilename := aFilename.

    "Modified (format): / 06-02-2019 / 11:45:44 / Stefan Vogel"
!

keep
    "do not delete the file on finalization"

    self unregisterForFinalization
!

setName:aString
    super setName:aString.
    concreteFilename := ConcreteClass named:aString.
    self registerForFinalization

    "Modified: / 06-02-2019 / 10:16:06 / Stefan Vogel"
!

species
    "filenames derived from me should not be autodeleted themself"

    ^ ConcreteClass.    "of Filename"

    "Modified: / 06-02-2019 / 10:06:23 / Stefan Vogel"
! !

!AutoDeletedFilename methodsFor:'converting'!

asAutoDeletedFilename
    "that's what I am"

    ^ self

    "Created: / 06-02-2019 / 10:10:43 / Stefan Vogel"
! !

!AutoDeletedFilename methodsFor:'copying'!

shallowCopy
    "when copying, return a real filename
     (to avoid mutiple removals)"

    ^ self species named:nameString

    "
        'blaFaselQall.mist' asFilename asAutoDeletedFilename copy
    "
! !

!AutoDeletedFilename methodsFor:'delegated to concrete filename'!

directoryName
    ^ concreteFilename directoryName.

    "Modified: / 07-09-1995 / 10:42:03 / claus"
    "Modified: / 21-10-1998 / 22:52:25 / cg"
    "Modified: / 27-10-1998 / 13:19:26 / ps"
    "Modified: / 06-02-2019 / 10:52:20 / Stefan Vogel"
!

fileType
    ^ concreteFilename fileType.

    "Modified: / 21-07-1998 / 11:25:56 / cg"
    "Modified: / 06-02-2019 / 10:50:51 / Stefan Vogel"
!

fullAlternativePathName
    ^ concreteFilename fullAlternativePathName.

    "Modified: / 06-02-2019 / 10:53:56 / Stefan Vogel"
!

isDirectory
    ^ concreteFilename isDirectory.

    "Modified: / 21-09-1998 / 15:53:10 / cg"
    "Modified: / 06-02-2019 / 10:51:48 / Stefan Vogel"
!

isExecutableProgram
    ^ concreteFilename isExecutableProgram.

    "Modified: / 06-02-2019 / 10:54:15 / Stefan Vogel"
!

isExplicitRelative
    ^ concreteFilename isExplicitRelative.

    "Modified: / 06-02-2019 / 10:52:48 / Stefan Vogel"
!

isHidden
    ^ concreteFilename isHidden.

    "Modified: / 06-02-2019 / 10:54:30 / Stefan Vogel"
!

isImplicit
    ^ concreteFilename isImplicit.

    "Created: / 18-09-1997 / 18:04:51 / stefan"
    "Modified: / 06-02-2019 / 10:53:08 / Stefan Vogel"
!

isRootDirectory
    ^ concreteFilename isRootDirectory.

    "Modified: / 06-02-2019 / 10:20:31 / Stefan Vogel"
!

isVolumeAbsolute
    ^ concreteFilename isVolumeAbsolute.

    "Created: / 07-09-1997 / 23:54:20 / cg"
    "Modified: / 09-09-1998 / 20:38:54 / cg"
    "Modified: / 06-02-2019 / 10:20:43 / Stefan Vogel"
!

localPathName
    ^ concreteFilename localPathName.

    "Modified: / 24-09-1998 / 19:09:53 / cg"
    "Modified (format): / 06-02-2019 / 10:21:08 / Stefan Vogel"
!

makeLegalFilename
    ^ concreteFilename makeLegalFilename.

    "Modified: / 01-12-2010 / 18:53:59 / cg"
    "Modified: / 06-02-2019 / 10:21:28 / Stefan Vogel"
!

osNameForAccess
    ^ concreteFilename osNameForAccess.

    "Modified: / 06-02-2019 / 10:21:47 / Stefan Vogel"
!

osNameForDirectory
    ^ concreteFilename osNameForDirectory.

    "Modified: / 20-01-1998 / 15:39:06 / md"
    "Modified: / 17-08-1998 / 10:04:01 / cg"
    "Modified: / 06-02-2019 / 10:22:02 / Stefan Vogel"
!

osNameForDirectoryContents
    ^ concreteFilename osNameForDirectoryContents.

    "Modified: / 20-01-1998 / 15:39:06 / md"
    "Created: / 03-08-1998 / 21:37:46 / cg"
    "Modified: / 17-08-1998 / 10:04:22 / cg"
    "Modified: / 06-02-2019 / 10:22:21 / Stefan Vogel"
!

osNameForFile
    ^ concreteFilename osNameForFile.

    "Modified: / 21-07-2012 / 19:35:19 / cg"
    "Modified: / 06-02-2019 / 10:22:32 / Stefan Vogel"
!

pathName
    ^ concreteFilename pathName.

    "Modified: / 24-09-1998 / 19:09:53 / cg"
    "Modified: / 06-02-2019 / 10:22:43 / Stefan Vogel"
!

renameTo:newName
    ^ concreteFilename renameTo:newName.

    "Modified: / 20-01-1998 / 15:33:00 / md"
    "Modified: / 21-09-2006 / 18:19:47 / cg"
    "Modified (format): / 12-12-2017 / 12:45:47 / stefan"
    "Modified: / 06-02-2019 / 10:22:59 / Stefan Vogel"
!

setHidden
    ^ concreteFilename setHidden.

    "Modified: / 06-02-2019 / 10:23:25 / Stefan Vogel"
!

volume
    ^ concreteFilename volume.

    "Modified: / 24-09-1998 / 19:04:27 / cg"
    "Modified: / 06-02-2019 / 10:23:54 / Stefan Vogel"
! !

!AutoDeletedFilename methodsFor:'finalization'!

basicFinalize
    |linkInfo|

    linkInfo := self linkInfo.
    linkInfo notNil ifTrue:[
        linkInfo isDirectory ifTrue:[
            concreteFilename recursiveRemove
        ] ifFalse:[
            concreteFilename removeFile.
        ].
    ].

    "Modified: / 06-02-2019 / 12:08:40 / Stefan Vogel"
!

executor
    ^ self class basicNew 
            nameString:nameString;
            concreteFilename:concreteFilename;
            yourself.

    "Modified: / 06-02-2019 / 11:39:20 / Stefan Vogel"
!

finalizationLobby
    "answer the registry used for finalization.
     we have our own Lobby."

    ^ Lobby
!

finalize
    "/ do this in a forked process to avoid blocking
    "/ in case of an autodeleted remote file of a broken connection
    [
        "/ with timeout to avoid waiting forever
        [
            self basicFinalize.
        ] valueWithTimeout:1 minutes.
    ] fork.
! !

!AutoDeletedFilename methodsFor:'removing'!

recursiveRemove
    super recursiveRemove.
    self unregisterForFinalization
!

remove
    super remove.
    self unregisterForFinalization
!

removeDirectory
    super removeDirectory.
    self unregisterForFinalization
!

removeFile
    super removeFile.
    self unregisterForFinalization
! !

!AutoDeletedFilename class methodsFor:'documentation'!

version
    ^ '$Header$'
!

version_CVS
    ^ '$Header$'
! !


AutoDeletedFilename initialize!