tags:

views:

428

answers:

2

we need to change the default SYSTEM temp folder for our multiplatform application.

The systems default call for getting the SYSTEM temp folder should return the folder we have specified. On MS-Windows this is GetTempPath(). On MacOS the function is called NSTemporaryDirectory() I think.

We need to do this because we are running multiple instances of our application at the same time. There are some 3rd party libs which are using non-unique temporary filenames stored in the SYSTEMs temp folder.

For Microsoft Windows and for Unix platforms we already have a solution:

   Microsoft Windows:

        setenv("TMP", myOwnTempFolder);
        tmpFolderToUse=GetTempPath(); // use WinOS API call


   Unix:

         setenv("TMPDIR", myOwnTempFolder);
         tmpFolderToUse = getenv("TMPDIR");

but this doesn't work for MacOS(X).

   MacOS:

         setenv("TMPDIR", myOwnTempFolder);
         tmpFolderToUse = NSTemporaryDirectory(); // use MacOS API call

The call to NSTemporaryDirectory() always returns the default path afterwards (as without setting a different folder).

I have tried to invoke setenv("...") with TMPDIR, TEMP, TEMPDIR, and TMP - but no luck on MacOSX.

For clarification: here a multiple instance pseudo-code example as it currently implemented for the Windows OS flavor of our application:

instance1:

tmp=GetTempPath(); // -> 'C:\User\testing\temp'
uuid=getUUID(); // -> 'd7c5df40-d48d-11de-8a39-0800200c9a66'
setenv("TMP", tmp + uuid);
tmp=GetTempPath(); // --> 'C:\User\testing\temp\d7c5df40-d48d-11de-8a39-0800200c9a66'

instance2:

tmp=GetTempPath(); // -> 'C:\User\testing\temp'
uuid=getUUID(); // -> '435aeb10-d48e-11de-8a39-0800200c9a66'
setenv("TMP", tmp + uuid);
tmp=GetTempPath(); // --> 'C:\User\testing\temp\435aeb10-d48e-11de-8a39-0800200c9a66'

Any advice on how to achieve the same behavior on MacOS would be very apricated.

+2  A: 

NSTemporaryDirectory() uses confstr(_CS_DARWIN_USER_TEMP_DIR), not $TMPDIR. I don't know of an API to set confstr(3)s, so I think you'll need to override either NSTemporaryDirectory or confstr$UNIX2003 with DYLD_INSERT_LIBRARIES or a similar mechanism.

But really, this is a tremendous hack; your application should not assume it is the only instance running in the temporary directory if this is not the case. It should do its own uniquing with mkdtemp(3) or similar.

Nicholas Riley
You are right. Hacking with confstr or with DYLD_INSERT_LIBRARIES is not an option.So for MacOS I will try a workaround for getting the tmp folder: return the value of TMPDIR if it is set, otherwise return the value as provided by NSTemporaryDirectory. But this still has the disadvantage that the used 3rd party libs will not make use of TMPDIR.
... we have implemented this now. As expected this approach is working well for our own application/our own libraries. But not for the 3rd party libs used by our application.
+2  A: 

Sounds to me like the problem is in your libraries...

Dave DeLong
No! It works well with our own libraries, but it doesn't work with the 3rd party libs we are using.