GetTemporaryDirectory()

Mac OSX specific forum
mikejs
Enthusiast
Enthusiast
Posts: 176
Joined: Thu Oct 21, 2010 9:46 pm

GetTemporaryDirectory()

Post by mikejs »

Probably another daft Mac question...

On Windows, GetTemporaryDirectory() gives me something like C:\Users\[username]\Appdata\Local\Temp. i.e. a temporary folder that is specific to the user who's running the program.

On OS X, I get /Tmp. Which is a temporary directory, and is writeable to anyone, but is not specific to the user. This leads to rights problems, in that files created in /Tmp by one user are not modifiable by another, assuming neither is an admin of the system. This means that, unlike on Windows, I have to make sure that any files I create in /Tmp have unique names to avoid clashing with files created by other users. This gets fiddly very quickly. Deleting temp files after use is one solution, but not entirely reliable, as if the program crashes, it may leave files behind that can't be cleaned up by any other user.

Is this because the Mac doesn't have the same concept of temporary files being per-user, and avoiding file clashes is just a fact of life? Or is there some other way to obtain a per-user temp file location on a Mac?
Fred
Administrator
Administrator
Posts: 18499
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: GetTemporaryDirectory()

Post by Fred »

You can still create your own 'temp' dir in the user home directory (GetHomeDirectory()).
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: GetTemporaryDirectory()

Post by PB »

> Deleting temp files after use is one solution, but not entirely reliable,
> as if the program crashes, it may leave files behind

That's the programmer's job to fix. Example: when the app launches,
it checks for existing non-deleted files from its last session. If found,
delete them; then proceed with the app's operation. ;)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
mikejs
Enthusiast
Enthusiast
Posts: 176
Joined: Thu Oct 21, 2010 9:46 pm

Re: GetTemporaryDirectory()

Post by mikejs »

Fred wrote:You can still create your own 'temp' dir in the user home directory (GetHomeDirectory()).
Yes, but that gets a bit messy if you want cross platform code, as that won't do quite the right thing on Windows. Still, should be possible to have a library function that does what's needed.
Fred
Administrator
Administrator
Posts: 18499
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: GetTemporaryDirectory()

Post by Fred »

It does the right thing, we won't create a temp dir in every home directory on Linux/OSX, it's not the way it should work. You have to do an unique file routine, as even Windows another application run by the same user could create a file with the same name as yours in the User temp dir.
mikejs
Enthusiast
Enthusiast
Posts: 176
Joined: Thu Oct 21, 2010 9:46 pm

Re: GetTemporaryDirectory()

Post by mikejs »

PB wrote:> Deleting temp files after use is one solution, but not entirely reliable,
> as if the program crashes, it may leave files behind

That's the programmer's job to fix. Example: when the app launches,
it checks for existing non-deleted files from its last session. If found,
delete them; then proceed with the app's operation. ;)
In this case, the next time the app is run, it is very likely to be running as a different user who has no rights to delete other user's files from /Tmp. No reason you'd know that, because I didn't mention it :) , but this is stuff for student lab machines which usually have very high user turnover. To a first approximation, no user uses the same machine twice. Well, sometimes they do, but it's best not to rely on it.

But Fred's answer covers it I think - don't create files in a common temp area in the first place. If I keep entirely within a user-specific location, then I can tidy up on startup and shutdown as you describe.
LuCiFeR[SD]
666
666
Posts: 1033
Joined: Mon Sep 01, 2003 2:33 pm

Re: GetTemporaryDirectory()

Post by LuCiFeR[SD] »

Actually, Ignore my shit :).... removes pointless stuff.
Last edited by LuCiFeR[SD] on Tue Aug 20, 2013 2:50 pm, edited 1 time in total.
mikejs
Enthusiast
Enthusiast
Posts: 176
Joined: Thu Oct 21, 2010 9:46 pm

Re: GetTemporaryDirectory()

Post by mikejs »

Fred wrote:It does the right thing, we won't create a temp dir in every home directory on Linux/OSX, it's not the way it should work. You have to do an unique file routine, as even Windows another application run by the same user could create a file with the same name as yours in the User temp dir.
Appdata\Local is slightly special in that it does not roam. So putting temp files in there doesn't result in slower logon/logoff times, or clutter up the user's network filestore if they have a roaming profile.

Just creating a folder under GetHomeDirectory() won't be excluded from roaming in that way (unless you fiddle with the registry key that controls roaming exclusions), but it's easier just to call GetTemporaryDirectory() if the platform is windows.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3944
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: GetTemporaryDirectory()

Post by wilbert »

On OSX the code below should give the temporary directory for the current user if such a directory exists.

Code: Select all

ImportC ""
  NSTemporaryDirectory()
EndImport

TempDir.s = PeekS(CocoaMessage(0, NSTemporaryDirectory(), "UTF8String"), -1, #PB_UTF8)

Debug TempDir
Windows (x64)
Raspberry Pi OS (Arm64)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: GetTemporaryDirectory()

Post by PB »

> the next time the app is run, it is very likely to be running as a
> different user who has no rights to delete other user's files from
> /Tmp

Yes, but surely the app could create its own temp folder in some
location available for all users? Or doesn't the Mac work like that?
Sorry, I'm a Windows-only programmer, which I didn't mention.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
mikejs
Enthusiast
Enthusiast
Posts: 176
Joined: Thu Oct 21, 2010 9:46 pm

Re: GetTemporaryDirectory()

Post by mikejs »

PB wrote:> the next time the app is run, it is very likely to be running as a
> different user who has no rights to delete other user's files from
> /Tmp

Yes, but surely the app could create its own temp folder in some
location available for all users? Or doesn't the Mac work like that?
Sorry, I'm a Windows-only programmer, which I didn't mention.
As was I until a couple of weeks ago...

It seems the Mac doesn't work like that. Any user can create files in /Tmp, but any files they create seem to be only deletable or modifyable by them or an admin user. Something to do with file ownership, I think. Either way, a non-admin user can't do anything to files created in /Tmp by someone else. Using a tmp folder inside their home directory gets around it.
LuCiFeR[SD]
666
666
Posts: 1033
Joined: Mon Sep 01, 2003 2:33 pm

Re: GetTemporaryDirectory()

Post by LuCiFeR[SD] »

could you not use something like this? It creates a directory With the username as a prefix. I didn't really intend to go so in depth with this code... might be of no use whatsoever? worth a shot :)

Code: Select all

EnableExplicit
Define.s TempPath

TempPath=GetTemporaryDirectory()+UserName()+"_TestProgram"

Debug TempPath

Declare DirectoryExists(TempPath.s)
Declare CreateTempDir(TempPath.s)
Declare RemoveTempDir(TempPath.s)

CreateTempDir(TempPath)
RemoveTempDir(TempPath)


;- Procedures
End ;  just here to stop anything beyond this point from being accidentally executed!
Procedure DirectoryExists(TempPath.s)
  Select FileSize(TempPath)
    Case -2
      Debug "Procedure returning #True as directory exists!"
      ProcedureReturn #True
      
  EndSelect
  Debug "Procedure returning #False as directory does not exist!"
  ProcedureReturn #False
EndProcedure

Procedure CreateTempDir(TempPath.s)
  If Not DirectoryExists(TempPath)
    If CreateDirectory(TempPath)
      Debug "Directory Created!"
    Else
      Debug "Failed to create directory :("
    EndIf
  Else
    Debug "Directory Exists... No need to create"
  EndIf
EndProcedure

Procedure RemoveTempDir(TempPath.s)
  If DirectoryExists(TempPath)
    
    If DeleteDirectory(TempPath,"",#PB_FileSystem_Recursive)
      Debug "Directory Deleted!"
    Else
      Debug "Failed to delete directory!"
    EndIf
    
  Else
    Debug "Directory doesn't exist"
  EndIf
EndProcedure
Post Reply