Re: temp file api

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

Re: temp file api

Dave Stevenson-2
Switching lists - I should have sent this to vwnc initially. I'm looking
for cross-platform api for creating temp files.

Yes, the process id seems like a reasonable component for generating a
default temp file name. But don't at least some OS's provide api for
temp files already? Some advantages I'm hoping for:

1) automatically select an appropriate (and writable) directory

2) relieve me from having to worry about whether my "default" temp file
name already exists

3) ensure the file gets cleaned up at the end of the VW session, if I do
not explicitly delete it before then

Just thought I'd check if anyone has already implemented this...

Thanks,

Dave

[hidden email] wrote:

> Dave Stevenson <[hidden email]> wrote:
>
>
> | Anyone have cross-platform api for creating and deleting temp files?
>
> e.g.
>
> ('vw', OSHandle currentProcessID printString, '.tmp') asFilename
>
> will give you a filename unique to the VM process on all platforms.
> ---
> Eliot Miranda                 ,,,^..^,,,                mailto:[hidden email]
> VisualWorks Engineering, Cincom  Smalltalk: scene not herd  Tel +1 408 216 4581
> 3350 Scott Blvd, Bldg 36 Suite B, Santa Clara, CA 95054 USA Fax +1 408 216 4500
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: temp file api

Dave Stevenson-2
For example, for Win I found the following example (I expect there are
other examples for linux/unix), but I'm not much of a C guy, so I'm
looking for Smalltalk code to access this kind of api on Win and other
plats:

FUNCTION GetTempWorkFileName (szPrefix AS ASCIIZ * 4, szTempFile AS
ASCIIZ * %MAX_PATH) AS LONG
    ' returns: 0 = success, the temp file name is in szTempFile (fully
qualified)
    '        other = System error number
    LOCAL szTempPrefix AS ASCIIZ * 4   ' 3 + null
    LOCAL szTempPath   AS ASCIIZ * %MAX_PATH
    LOCAL Stat AS LONG
    szTempPrefix = szPrefix     ' not that I care, but...
    Stat =  GetTempPath(SIZEOF(szTempPath), szTempPath)
    IF ISTRUE Stat THEN    ' GetTempPathSucceeded
        Stat = GetTempFileName (szTempPath, szTempPrefix, 0&, szTempFile)
    END IF
    IF ISTRUE Stat THEN  ' function succeeded, return zero
       FUNCTION = 0
    ELSE
       FUNCTION = GetLastError  ' GetTempFileName returns non-zero on
success
    END IF
END FUNCTION

Dave

Dave Stevenson wrote:

> Switching lists - I should have sent this to vwnc initially. I'm looking
> for cross-platform api for creating temp files.
>
> Yes, the process id seems like a reasonable component for generating a
> default temp file name. But don't at least some OS's provide api for
> temp files already? Some advantages I'm hoping for:
>
> 1) automatically select an appropriate (and writable) directory
>
> 2) relieve me from having to worry about whether my "default" temp file
> name already exists
>
> 3) ensure the file gets cleaned up at the end of the VW session, if I do
> not explicitly delete it before then
>
> Just thought I'd check if anyone has already implemented this...
>
> Thanks,
>
> Dave
>
> [hidden email] wrote:
>> Dave Stevenson <[hidden email]> wrote:
>>
>>
>> | Anyone have cross-platform api for creating and deleting temp files?
>>
>> e.g.
>>
>>     ('vw', OSHandle currentProcessID printString, '.tmp') asFilename
>>
>> will give you a filename unique to the VM process on all platforms.
>> ---
>> Eliot Miranda                 ,,,^..^,,,                
>> mailto:[hidden email]
>> VisualWorks Engineering, Cincom  Smalltalk: scene not herd  Tel +1 408
>> 216 4581
>> 3350 Scott Blvd, Bldg 36 Suite B, Santa Clara, CA 95054 USA Fax +1 408
>> 216 4500
>>
>>
>>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: temp file api

eliot-2
In reply to this post by Dave Stevenson-2
Dave Stevenson <[hidden email]> wrote:

The below is far more tnan you need.  On Unix its fine to assume /tmp, unless you've a very large file and ten you might choose /var/tmp if it exists.  On Windows I think you can get asway with CEnvironment getenv: TEMP'.  That leaves MacOS 8/9.  Any one know if there's an official temp dir thereon?

So ignoring MacOS 8/9 for the momement something like

        OSHandle currentOS == #unix
                ifTrue: [(dir := '/var/tmp' asFilename) exists and: [dir isDirectory]) ifFalse:
                                        [dir := '/tmp' asFilename]]
                ifFalse:
                        [OSHandle currentOS == #win32 ifTrue:
                                [dir := CEnvironment userEnvironment at: 'TEMP' ifAbsent: '.']].
        dir isNil ifTrue:
                [dir := Filename defaultDirectory]

will always give you a directory which most of the time is going to be a sanctioned temp directory.

You could bundle this up into a convenience API on e.g. SystemSupport and put it in an AR.

| For example, for Win I found the following example (I expect there are
| other examples for linux/unix), but I'm not much of a C guy, so I'm
| looking for Smalltalk code to access this kind of api on Win and other
| plats:

| FUNCTION GetTempWorkFileName (szPrefix AS ASCIIZ * 4, szTempFile AS
| ASCIIZ * %MAX_PATH) AS LONG
|     ' returns: 0 = success, the temp file name is in szTempFile (fully
| qualified)
|     '        other = System error number
|     LOCAL szTempPrefix AS ASCIIZ * 4   ' 3 + null
|     LOCAL szTempPath   AS ASCIIZ * %MAX_PATH
|     LOCAL Stat AS LONG
|     szTempPrefix = szPrefix     ' not that I care, but...
|     Stat =  GetTempPath(SIZEOF(szTempPath), szTempPath)
|     IF ISTRUE Stat THEN    ' GetTempPathSucceeded
|         Stat = GetTempFileName (szTempPath, szTempPrefix, 0&, szTempFile)
|     END IF
|     IF ISTRUE Stat THEN  ' function succeeded, return zero
|        FUNCTION = 0
|     ELSE
|        FUNCTION = GetLastError  ' GetTempFileName returns non-zero on
| success
|     END IF
| END FUNCTION

| Dave

| Dave Stevenson wrote:
| > Switching lists - I should have sent this to vwnc initially. I'm looking
| > for cross-platform api for creating temp files.
| >
| > Yes, the process id seems like a reasonable component for generating a
| > default temp file name. But don't at least some OS's provide api for
| > temp files already? Some advantages I'm hoping for:
| >
| > 1) automatically select an appropriate (and writable) directory
| >
| > 2) relieve me from having to worry about whether my "default" temp file
| > name already exists
| >
| > 3) ensure the file gets cleaned up at the end of the VW session, if I do
| > not explicitly delete it before then
| >
| > Just thought I'd check if anyone has already implemented this...
| >
| > Thanks,
| >
| > Dave
| >
| > [hidden email] wrote:
| >> Dave Stevenson <[hidden email]> wrote:
| >>
| >>
| >> | Anyone have cross-platform api for creating and deleting temp files?
| >>
| >> e.g.
| >>
| >>     ('vw', OSHandle currentProcessID printString, '.tmp') asFilename
| >>
| >> will give you a filename unique to the VM process on all platforms.
| >> ---
| >> Eliot Miranda                 ,,,^..^,,,
| >> mailto:[hidden email]
| >> VisualWorks Engineering, Cincom  Smalltalk: scene not herd  Tel +1 408
| >> 216 4581
| >> 3350 Scott Blvd, Bldg 36 Suite B, Santa Clara, CA 95054 USA Fax +1 408
| >> 216 4500
| >>
| >>
| >>
| >
| >
---
Eliot Miranda                 ,,,^..^,,,                mailto:[hidden email]
VisualWorks Engineering, Cincom  Smalltalk: scene not herd  Tel +1 408 216 4581
3350 Scott Blvd, Bldg 36 Suite B, Santa Clara, CA 95054 USA Fax +1 408 216 4500


Reply | Threaded
Open this post in threaded view
|

Re: temp file api

Mark Pirogovsky-3
On Win32  you should use something like following in the  Win32SystemSupport

class methods >>
!===============================!
getTempPath
"Search order :
The path specified by the TMP environment variable.
The path specified by the TEMP environment variable.
The path specified by the USERPROFILE environment variable.
The Windows directory.

Windows Me/98/95:  If TMP and TEMP are not set to a valid path,
GetTempPath uses the current directory.

Note that the function does not verify that the path exists.
"
"self getTempPath"

        | shortPath xif resultLength longName |
        ^
        [xif := self new.
        shortPath := xif LPTSTR gcMalloc: 2048.
        resultLength := xif GetTempPath: 2048 into: shortPath.
        longName := shortPath copyCStringFromHeap.
        resultLength = 0 ifTrue: [''] ifFalse: [longName]]
                        on: Error
                        do: [:ex | ex returnWith: '']

        "@__markp March 29, 2005 12:21:15 pm"
        "@__markp March 29, 2005 12:23:49 pm"

!=====================================================!
getTempFilenameInDir: tempDirPath prefix: aPrefix
        "self
                getTempFilenameInDir: '\temp\'
                prefix: 'tmp'
                unique: false"

        ^self
                getTempFilenameInDir: tempDirPath
                prefix: aPrefix
                unique: true

        "@__markp March 29, 2005 12:51:01 pm"

!===========================================================!
getTempFilenameInDir: tempDirPath prefix: aPrefix unique:uniqueBool
        | shortPath xif resultLength longName |
        ^
        [xif := self new.
        shortPath := xif LPTSTR gcMalloc: 2048.
        resultLength := xif
                                GetTempFileName: tempDirPath
                                prefix: aPrefix
                                unique: (uniqueBool ifTrue:[0]ifFalse:[1])
                                into: shortPath.
        longName := shortPath copyCStringFromHeap.
        resultLength = 0 ifTrue: [nil] ifFalse: [longName]]
                        on: Error
                        do: [:ex | ex returnWith: nil]

        "@__markp March 29, 2005 12:42:44 pm"
!===========================================================!


instance >>
!===============================!

GetTempPath: nBufferLength into: lpBuffer
        <C: DWORD _wincall GetTempPathA(
    DWORD nBufferLength,
  LPTSTR lpBuffer)>
        ^self externalAccessFailedWith: _errorCode

        "@__markp March 29, 2005 11:43:00 am"

!================================!

GetTempFileName: lpPathName prefix: lpPrefixString unique: uUnique into:
lpTempFileName
        <C:
UINT _wincall GetTempFileNameA(
   LPCTSTR lpPathName,
   LPCTSTR lpPrefixString,
   UINT uUnique,
   LPTSTR lpTempFileName
) >
        ^self externalAccessFailedWith: _errorCode

!===================================!

HTH
Mark Pirogovsky

[hidden email] wrote:

> Dave Stevenson <[hidden email]> wrote:
>
> The below is far more tnan you need.  On Unix its fine to assume /tmp, unless you've a very large file and ten you might choose /var/tmp if it exists.  On Windows I think you can get asway with CEnvironment getenv: TEMP'.  That leaves MacOS 8/9.  Any one know if there's an official temp dir thereon?
>
> So ignoring MacOS 8/9 for the momement something like
>
> OSHandle currentOS == #unix
> ifTrue: [(dir := '/var/tmp' asFilename) exists and: [dir isDirectory]) ifFalse:
> [dir := '/tmp' asFilename]]
> ifFalse:
> [OSHandle currentOS == #win32 ifTrue:
> [dir := CEnvironment userEnvironment at: 'TEMP' ifAbsent: '.']].
> dir isNil ifTrue:
> [dir := Filename defaultDirectory]
>
> will always give you a directory which most of the time is going to be a sanctioned temp directory.
>
> You could bundle this up into a convenience API on e.g. SystemSupport and put it in an AR.
>
> | For example, for Win I found the following example (I expect there are
> | other examples for linux/unix), but I'm not much of a C guy, so I'm
> | looking for Smalltalk code to access this kind of api on Win and other
> | plats:
>
> | FUNCTION GetTempWorkFileName (szPrefix AS ASCIIZ * 4, szTempFile AS
> | ASCIIZ * %MAX_PATH) AS LONG
> |     ' returns: 0 = success, the temp file name is in szTempFile (fully
> | qualified)
> |     '        other = System error number
> |     LOCAL szTempPrefix AS ASCIIZ * 4   ' 3 + null
> |     LOCAL szTempPath   AS ASCIIZ * %MAX_PATH
> |     LOCAL Stat AS LONG
> |     szTempPrefix = szPrefix     ' not that I care, but...
> |     Stat =  GetTempPath(SIZEOF(szTempPath), szTempPath)
> |     IF ISTRUE Stat THEN    ' GetTempPathSucceeded
> |         Stat = GetTempFileName (szTempPath, szTempPrefix, 0&, szTempFile)
> |     END IF
> |     IF ISTRUE Stat THEN  ' function succeeded, return zero
> |        FUNCTION = 0
> |     ELSE
> |        FUNCTION = GetLastError  ' GetTempFileName returns non-zero on
> | success
> |     END IF
> | END FUNCTION
>
> | Dave
>
> | Dave Stevenson wrote:
> | > Switching lists - I should have sent this to vwnc initially. I'm looking
> | > for cross-platform api for creating temp files.
> | >
> | > Yes, the process id seems like a reasonable component for generating a
> | > default temp file name. But don't at least some OS's provide api for
> | > temp files already? Some advantages I'm hoping for:
> | >
> | > 1) automatically select an appropriate (and writable) directory
> | >
> | > 2) relieve me from having to worry about whether my "default" temp file
> | > name already exists
> | >
> | > 3) ensure the file gets cleaned up at the end of the VW session, if I do
> | > not explicitly delete it before then
> | >
> | > Just thought I'd check if anyone has already implemented this...
> | >
> | > Thanks,
> | >
> | > Dave
> | >
> | > [hidden email] wrote:
> | >> Dave Stevenson <[hidden email]> wrote:
> | >>
> | >>
> | >> | Anyone have cross-platform api for creating and deleting temp files?
> | >>
> | >> e.g.
> | >>
> | >>     ('vw', OSHandle currentProcessID printString, '.tmp') asFilename
> | >>
> | >> will give you a filename unique to the VM process on all platforms.
> | >> ---
> | >> Eliot Miranda                 ,,,^..^,,,                
> | >> mailto:[hidden email]
> | >> VisualWorks Engineering, Cincom  Smalltalk: scene not herd  Tel +1 408
> | >> 216 4581
> | >> 3350 Scott Blvd, Bldg 36 Suite B, Santa Clara, CA 95054 USA Fax +1 408
> | >> 216 4500
> | >>
> | >>
> | >>
> | >
> | >
> ---
> Eliot Miranda                 ,,,^..^,,,                mailto:[hidden email]
> VisualWorks Engineering, Cincom  Smalltalk: scene not herd  Tel +1 408 216 4581
> 3350 Scott Blvd, Bldg 36 Suite B, Santa Clara, CA 95054 USA Fax +1 408 216 4500
>
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: temp file api

Travis Griggs-3
In reply to this post by eliot-2

On Nov 29, 2006, at 17:23, [hidden email] wrote:

The below is far more tnan you need.  On Unix its fine to assume /tmp, unless you've a very large file and ten you might choose /var/tmp if it exists.  On Windows I think you can get asway with CEnvironment getenv: TEMP'.  That leaves MacOS 8/9.  Any one know if there's an official temp dir thereon?


So ignoring MacOS 8/9 for the momement something like


OSHandle currentOS == #unix

ifTrue: [(dir := '/var/tmp' asFilename) exists and: [dir isDirectory]) ifFalse:

[dir := '/tmp' asFilename]]

ifFalse:

[OSHandle currentOS == #win32 ifTrue:

[dir := CEnvironment userEnvironment at: 'TEMP' ifAbsent: '.']].

dir isNil ifTrue:

[dir := Filename defaultDirectory]


will always give you a directory which most of the time is going to be a sanctioned temp directory.


You could bundle this up into a convenience API on e.g. SystemSupport and put it in an AR.


May I suggest that you implement it something along the lines:

Filename>>tempDirectory
^self concreteClass getTempDirectory

Filename>>getTempDirectory
^self defaultDirectory

PCFilename>>getTempDirectory
^(CEnvironment userEnvironment at: 'TEMP' ifAbsent: ['.']) asFilename

UnixFilename>>getTempDirectory
tmp := '/var/tmp' asFilename.
(tmp exists and: [tmp isDirectory and: [tmp isWritable]]) ifTrue: [^tmp].
^'/tmp' asDirectory

--
Travis Griggs
Objologist
"It’s actually much easier to get around on ice than it is on dry land—if you use skates." - Paul Graham


Reply | Threaded
Open this post in threaded view
|

AW: temp file api

Mülheims, Klaus
In reply to this post by Dave Stevenson-2
Hi,
 
on Windows platform, it ist very easy:
 
'$(TEMP)' asLogicalFileSpecification asFilename
 
gives you a Filename instance to the directory. Perhaps, it works on Unix and Max too.
 
Greetings
 
Klaus
 
Collogia AG
Ubierring 11
 
50678 Köln
Germany
+49 221 336080
http://www.collogia.de
 


Von: Travis Griggs [mailto:[hidden email]]
Gesendet: Donnerstag, 30. November 2006 16:41
An: VW NC
Betreff: Re: temp file api


On Nov 29, 2006, at 17:23, [hidden email] wrote:

The below is far more tnan you need.  On Unix its fine to assume /tmp, unless you've a very large file and ten you might choose /var/tmp if it exists.  On Windows I think you can get asway with CEnvironment getenv: TEMP'.  That leaves MacOS 8/9.  Any one know if there's an official temp dir thereon?


So ignoring MacOS 8/9 for the momement something like


OSHandle currentOS == #unix

ifTrue: [(dir := '/var/tmp' asFilename) exists and: [dir isDirectory]) ifFalse:

[dir := '/tmp' asFilename]]

ifFalse:

[OSHandle currentOS == #win32 ifTrue:

[dir := CEnvironment userEnvironment at: 'TEMP' ifAbsent: '.']].

dir isNil ifTrue:

[dir := Filename defaultDirectory]


will always give you a directory which most of the time is going to be a sanctioned temp directory.


You could bundle this up into a convenience API on e.g. SystemSupport and put it in an AR.


May I suggest that you implement it something along the lines:

Filename>>tempDirectory
^self concreteClass getTempDirectory

Filename>>getTempDirectory
^self defaultDirectory

PCFilename>>getTempDirectory
^(CEnvironment userEnvironment at: 'TEMP' ifAbsent: ['.']) asFilename

UnixFilename>>getTempDirectory
tmp := '/var/tmp' asFilename.
(tmp exists and: [tmp isDirectory and: [tmp isWritable]]) ifTrue: [^tmp].
^'/tmp' asDirectory

--
Travis Griggs
Objologist
"It’s actually much easier to get around on ice than it is on dry land—if you use skates." - Paul Graham



Diese Nachricht ist vertraulich. Wenn Sie nicht der beabsichtigte Empfänger sind, informieren Sie bitte den Absender. Das unbefugte Kopieren oder Weiterleiten ist nicht gestattet. Wegen der Manipulierbarkeit von E-Mails übernehmen wir keine Haftung für den Inhalt.