2 511a516b 2018-05-19 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 511a516b 2018-05-19 stsp * Permission to use, copy, modify, and distribute this software for any
5 511a516b 2018-05-19 stsp * purpose with or without fee is hereby granted, provided that the above
6 511a516b 2018-05-19 stsp * copyright notice and this permission notice appear in all copies.
8 511a516b 2018-05-19 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 511a516b 2018-05-19 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 511a516b 2018-05-19 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 511a516b 2018-05-19 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 511a516b 2018-05-19 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 511a516b 2018-05-19 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 511a516b 2018-05-19 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 511a516b 2018-05-19 stsp #include <limits.h>
18 511a516b 2018-05-19 stsp #include <stdlib.h>
19 511a516b 2018-05-19 stsp #include <unistd.h>
20 511a516b 2018-05-19 stsp #include <string.h>
21 511a516b 2018-05-19 stsp #include <stdio.h>
23 511a516b 2018-05-19 stsp #include "got_opentemp.h"
24 511a516b 2018-05-19 stsp #include "got_error.h"
27 511a516b 2018-05-19 stsp got_opentempfd(void)
29 511a516b 2018-05-19 stsp char name[PATH_MAX];
32 bb63914a 2020-02-17 stsp if (strlcpy(name, GOT_TMPDIR_STR "/got.XXXXXXXX", sizeof(name))
33 bb63914a 2020-02-17 stsp >= sizeof(name))
36 511a516b 2018-05-19 stsp fd = mkstemp(name);
37 e1e77cc4 2020-03-17 stsp if (fd != -1)
38 e1e77cc4 2020-03-17 stsp unlink(name);
43 511a516b 2018-05-19 stsp got_opentemp(void)
48 511a516b 2018-05-19 stsp fd = got_opentempfd();
50 511a516b 2018-05-19 stsp return NULL;
52 511a516b 2018-05-19 stsp f = fdopen(fd, "w+");
53 511a516b 2018-05-19 stsp if (f == NULL) {
55 511a516b 2018-05-19 stsp return NULL;
61 511a516b 2018-05-19 stsp const struct got_error *
62 511a516b 2018-05-19 stsp got_opentemp_named(char **path, FILE **outfile, const char *basepath)
64 511a516b 2018-05-19 stsp const struct got_error *err = NULL;
67 0c92744a 2018-09-20 stsp *outfile = NULL;
69 511a516b 2018-05-19 stsp if (asprintf(path, "%s-XXXXXX", basepath) == -1) {
70 511a516b 2018-05-19 stsp *path = NULL;
71 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
74 511a516b 2018-05-19 stsp fd = mkstemp(*path);
75 511a516b 2018-05-19 stsp if (fd == -1) {
76 0d0c539e 2019-05-22 jcs err = got_error_from_errno2("mkstemp", *path);
77 511a516b 2018-05-19 stsp free(*path);
78 511a516b 2018-05-19 stsp *path = NULL;
82 511a516b 2018-05-19 stsp *outfile = fdopen(fd, "w+");
83 511a516b 2018-05-19 stsp if (*outfile == NULL) {
84 0d0c539e 2019-05-22 jcs err = got_error_from_errno2("fdopen", *path);
85 511a516b 2018-05-19 stsp free(*path);
86 511a516b 2018-05-19 stsp *path = NULL;
92 507dc3bb 2018-12-29 stsp const struct got_error *
93 507dc3bb 2018-12-29 stsp got_opentemp_named_fd(char **path, int *outfd, const char *basepath)
95 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
98 507dc3bb 2018-12-29 stsp *outfd = -1;
100 507dc3bb 2018-12-29 stsp if (asprintf(path, "%s-XXXXXX", basepath) == -1) {
101 507dc3bb 2018-12-29 stsp *path = NULL;
102 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
105 507dc3bb 2018-12-29 stsp fd = mkstemp(*path);
106 507dc3bb 2018-12-29 stsp if (fd == -1) {
107 638f9024 2019-05-13 stsp err = got_error_from_errno("mkstemp");
108 507dc3bb 2018-12-29 stsp free(*path);
109 507dc3bb 2018-12-29 stsp *path = NULL;
110 507dc3bb 2018-12-29 stsp return err;
113 507dc3bb 2018-12-29 stsp *outfd = fd;
114 507dc3bb 2018-12-29 stsp return err;