diff options
author | bigbiff bigbiff <bigbiff@teamw.in> | 2013-02-23 02:55:50 +0100 |
---|---|---|
committer | bigbiff bigbiff <bigbiff@teamw.in> | 2013-02-25 15:06:46 +0100 |
commit | e60683a0d553b6488c564863f4e48954944fb0f8 (patch) | |
tree | 9364f97cb88b7c1359f5f06dfb32150a78168c31 /libblkid/fileutils.c | |
parent | Fix building of updater for 4.2 environment (diff) | |
download | android_bootable_recovery-e60683a0d553b6488c564863f4e48954944fb0f8.tar android_bootable_recovery-e60683a0d553b6488c564863f4e48954944fb0f8.tar.gz android_bootable_recovery-e60683a0d553b6488c564863f4e48954944fb0f8.tar.bz2 android_bootable_recovery-e60683a0d553b6488c564863f4e48954944fb0f8.tar.lz android_bootable_recovery-e60683a0d553b6488c564863f4e48954944fb0f8.tar.xz android_bootable_recovery-e60683a0d553b6488c564863f4e48954944fb0f8.tar.zst android_bootable_recovery-e60683a0d553b6488c564863f4e48954944fb0f8.zip |
Diffstat (limited to 'libblkid/fileutils.c')
-rw-r--r-- | libblkid/fileutils.c | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/libblkid/fileutils.c b/libblkid/fileutils.c new file mode 100644 index 000000000..ebfb12857 --- /dev/null +++ b/libblkid/fileutils.c @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2012 Sami Kerola <kerolasa@iki.fi> + */ + +#include <stdio.h> +#include <stdlib.h> +#include <sys/stat.h> +#include <unistd.h> +#include <sys/time.h> +#include <sys/resource.h> + +#include "c.h" +#include "fileutils.h" +#include "pathnames.h" +#include "xalloc.h" + +#define _PATH_TMP "/tmp" +/* Create open temporary file in safe way. Please notice that the + * file permissions are -rw------- by default. */ +int xmkstemp(char **tmpname, char *dir) +{ + char *localtmp; + char *tmpenv; + mode_t old_mode; + int fd; + + /* Some use cases must be capable of being moved atomically + * with rename(2), which is the reason why dir is here. */ + if (dir != NULL) + tmpenv = dir; + else + tmpenv = getenv("TMPDIR"); + + if (tmpenv) + xasprintf(&localtmp, "%s/%s.XXXXXX", tmpenv, + program_invocation_short_name); + else + xasprintf(&localtmp, "%s/%s.XXXXXX", _PATH_TMP, + program_invocation_short_name); + old_mode = umask(077); + fd = mkstemp(localtmp); + umask(old_mode); + if (fd == -1) { + free(localtmp); + localtmp = NULL; + } + *tmpname = localtmp; + return fd; +} + +/* + * portable getdtablesize() + */ +int get_fd_tabsize(void) +{ + int m; + +#if defined(HAVE_GETDTABLESIZE) + m = getdtablesize(); +#elif defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE) + struct rlimit rl; + + getrlimit(RLIMIT_NOFILE, &rl); + m = rl.rlim_cur; +#elif defined(HAVE_SYSCONF) && defined(_SC_OPEN_MAX) + m = sysconf(_SC_OPEN_MAX); +#else + m = OPEN_MAX; +#endif + return m; +} + +#ifdef TEST_PROGRAM +int main(void) +{ + FILE *f; + char *tmpname; + f = xfmkstemp(&tmpname, NULL); + unlink(tmpname); + free(tmpname); + fclose(f); + return EXIT_FAILURE; +} +#endif |