开发者

MacOS - howto change the SYSTEM temp folder (programmatically)?

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, 开发者_JS百科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'

How do we achieve the same behavior on MacOS?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜