2009-03-04 04:28:35 +01:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1990, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Chris Torek.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
2012-09-25 02:55:15 +02:00
|
|
|
#include <signal.h>
|
2009-03-04 04:28:35 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2012-09-25 02:55:15 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2013-02-12 17:40:24 +01:00
|
|
|
#include "private/ErrnoRestorer.h"
|
2017-10-07 01:58:36 +02:00
|
|
|
#include "private/ScopedSignalBlocker.h"
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2012-09-25 02:55:15 +02:00
|
|
|
static FILE* __tmpfile_dir(const char* tmp_dir) {
|
2018-08-03 02:31:13 +02:00
|
|
|
char* path = nullptr;
|
2014-05-13 19:14:22 +02:00
|
|
|
if (asprintf(&path, "%s/tmp.XXXXXXXXXX", tmp_dir) == -1) {
|
2018-08-03 02:31:13 +02:00
|
|
|
return nullptr;
|
2012-09-25 02:55:15 +02:00
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2012-09-25 02:55:15 +02:00
|
|
|
int fd;
|
|
|
|
{
|
|
|
|
ScopedSignalBlocker ssb;
|
2014-05-13 19:14:22 +02:00
|
|
|
fd = mkstemp(path);
|
2012-09-25 02:55:15 +02:00
|
|
|
if (fd == -1) {
|
2014-05-13 19:14:22 +02:00
|
|
|
free(path);
|
2018-08-03 02:31:13 +02:00
|
|
|
return nullptr;
|
2012-09-25 02:55:15 +02:00
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2012-09-25 02:55:15 +02:00
|
|
|
// Unlink the file now so that it's removed when closed.
|
2014-05-13 19:14:22 +02:00
|
|
|
unlink(path);
|
|
|
|
free(path);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2012-09-25 02:55:15 +02:00
|
|
|
// Can we still use the file now it's unlinked?
|
|
|
|
// File systems without hard link support won't have the usual Unix semantics.
|
|
|
|
struct stat sb;
|
|
|
|
int rc = fstat(fd, &sb);
|
|
|
|
if (rc == -1) {
|
2013-02-12 17:40:24 +01:00
|
|
|
ErrnoRestorer errno_restorer;
|
2012-09-25 02:55:15 +02:00
|
|
|
close(fd);
|
2018-08-03 02:31:13 +02:00
|
|
|
return nullptr;
|
2012-09-25 02:55:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Turn the file descriptor into a FILE*.
|
|
|
|
FILE* fp = fdopen(fd, "w+");
|
2018-08-03 02:31:13 +02:00
|
|
|
if (fp != nullptr) {
|
2012-09-25 02:55:15 +02:00
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Failure. Clean up. We already unlinked, so we just need to close.
|
2013-02-12 17:40:24 +01:00
|
|
|
ErrnoRestorer errno_restorer;
|
2012-09-25 02:55:15 +02:00
|
|
|
close(fd);
|
2018-08-03 02:31:13 +02:00
|
|
|
return nullptr;
|
2012-09-25 02:55:15 +02:00
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2012-09-25 02:55:15 +02:00
|
|
|
FILE* tmpfile() {
|
|
|
|
// TODO: get this app's temporary directory from the framework ("/data/data/app/cache").
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2012-09-25 02:55:15 +02:00
|
|
|
// $EXTERNAL_STORAGE turns out not to be very useful because it doesn't support hard links.
|
|
|
|
// This means we can't do the usual trick of calling unlink before handing the file back.
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2012-09-25 02:55:15 +02:00
|
|
|
FILE* fp = __tmpfile_dir("/data/local/tmp");
|
2018-08-03 02:31:13 +02:00
|
|
|
if (fp == nullptr) {
|
2012-09-25 02:55:15 +02:00
|
|
|
// P_tmpdir is "/tmp/", but POSIX explicitly says that tmpdir(3) should try P_tmpdir before
|
|
|
|
// giving up. This is potentially useful for bionic on the host anyway.
|
|
|
|
fp = __tmpfile_dir(P_tmpdir);
|
|
|
|
}
|
|
|
|
return fp;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
2016-02-03 20:24:28 +01:00
|
|
|
__strong_alias(tmpfile64, tmpfile);
|