AutoDeletedFilename.st
author Claus Gittinger <cg@exept.de>
Mon, 08 Jun 2015 21:22:21 +0200
changeset 18465 e9e4bb62235f
parent 16550 4bda4424570d
child 18120 e3a375d5f6a8
child 18632 5f99981d6418
permissions -rw-r--r--
added isOrdered query

"
 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' }"

Filename subclass:#AutoDeletedFilename
	instanceVariableNames:''
	classVariableNames:''
	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.
    See -> Filename asAutoDeletedFilename
"
!

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 methodsFor:'accessing'!

keep
    "do not delete the file on finalization"

    self unregisterForFinalization
!

setName:aString
    super setName:aString.
    self registerForFinalization    
! !

!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:'finalization'!

executor
    ^ self class basicNew nameString:nameString
!

finalize
    |linkInfo|

    "/ 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
        [
            linkInfo := self linkInfo.
            linkInfo notNil ifTrue:[
                linkInfo isDirectory ifTrue:[
                    super recursiveRemove
                ] ifFalse:[
                    super removeFile.
                ].
            ].
        ] valueWithTimeout:1 minutes.
    ] fork.
! !

!AutoDeletedFilename methodsFor:'queries'!

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

    ^ Filename concreteClass.
! !

!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: /cvs/stx/stx/libbasic/AutoDeletedFilename.st,v 1.12 2014-06-07 15:08:38 cg Exp $'
! !