Blame


1 511a516b 2018-05-19 stsp /*
2 511a516b 2018-05-19 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 511a516b 2018-05-19 stsp *
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.
7 511a516b 2018-05-19 stsp *
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.
15 511a516b 2018-05-19 stsp */
16 511a516b 2018-05-19 stsp
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>
22 511a516b 2018-05-19 stsp
23 511a516b 2018-05-19 stsp #include "got_opentemp.h"
24 511a516b 2018-05-19 stsp #include "got_error.h"
25 511a516b 2018-05-19 stsp
26 511a516b 2018-05-19 stsp int
27 511a516b 2018-05-19 stsp got_opentempfd(void)
28 511a516b 2018-05-19 stsp {
29 511a516b 2018-05-19 stsp char name[PATH_MAX];
30 511a516b 2018-05-19 stsp int fd;
31 511a516b 2018-05-19 stsp
32 ea2819be 2023-05-29 op if (strlcpy(name, GOT_TMPDIR_STR "/got.XXXXXXXXXX", sizeof(name))
33 bb63914a 2020-02-17 stsp >= sizeof(name))
34 511a516b 2018-05-19 stsp return -1;
35 511a516b 2018-05-19 stsp
36 511a516b 2018-05-19 stsp fd = mkstemp(name);
37 21017333 2022-10-15 stsp if (fd != -1) {
38 21017333 2022-10-15 stsp if (unlink(name) == -1) {
39 21017333 2022-10-15 stsp close(fd);
40 21017333 2022-10-15 stsp return -1;
41 21017333 2022-10-15 stsp }
42 21017333 2022-10-15 stsp }
43 511a516b 2018-05-19 stsp return fd;
44 511a516b 2018-05-19 stsp }
45 511a516b 2018-05-19 stsp
46 511a516b 2018-05-19 stsp FILE *
47 511a516b 2018-05-19 stsp got_opentemp(void)
48 511a516b 2018-05-19 stsp {
49 511a516b 2018-05-19 stsp int fd;
50 511a516b 2018-05-19 stsp FILE *f;
51 511a516b 2018-05-19 stsp
52 511a516b 2018-05-19 stsp fd = got_opentempfd();
53 511a516b 2018-05-19 stsp if (fd < 0)
54 511a516b 2018-05-19 stsp return NULL;
55 511a516b 2018-05-19 stsp
56 511a516b 2018-05-19 stsp f = fdopen(fd, "w+");
57 511a516b 2018-05-19 stsp if (f == NULL) {
58 511a516b 2018-05-19 stsp close(fd);
59 511a516b 2018-05-19 stsp return NULL;
60 511a516b 2018-05-19 stsp }
61 511a516b 2018-05-19 stsp
62 511a516b 2018-05-19 stsp return f;
63 511a516b 2018-05-19 stsp }
64 511a516b 2018-05-19 stsp
65 511a516b 2018-05-19 stsp const struct got_error *
66 b90054ed 2022-11-01 stsp got_opentemp_named(char **path, FILE **outfile, const char *basepath,
67 b90054ed 2022-11-01 stsp const char *suffix)
68 511a516b 2018-05-19 stsp {
69 511a516b 2018-05-19 stsp const struct got_error *err = NULL;
70 511a516b 2018-05-19 stsp int fd;
71 511a516b 2018-05-19 stsp
72 0c92744a 2018-09-20 stsp *outfile = NULL;
73 0c92744a 2018-09-20 stsp
74 ea2819be 2023-05-29 op if (asprintf(path, "%s-XXXXXXXXXX%s", basepath, suffix) == -1) {
75 511a516b 2018-05-19 stsp *path = NULL;
76 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
77 511a516b 2018-05-19 stsp }
78 511a516b 2018-05-19 stsp
79 b90054ed 2022-11-01 stsp fd = mkstemps(*path, strlen(suffix));
80 511a516b 2018-05-19 stsp if (fd == -1) {
81 b90054ed 2022-11-01 stsp err = got_error_from_errno2("mkstemps", *path);
82 511a516b 2018-05-19 stsp free(*path);
83 511a516b 2018-05-19 stsp *path = NULL;
84 511a516b 2018-05-19 stsp return err;
85 511a516b 2018-05-19 stsp }
86 511a516b 2018-05-19 stsp
87 511a516b 2018-05-19 stsp *outfile = fdopen(fd, "w+");
88 511a516b 2018-05-19 stsp if (*outfile == NULL) {
89 0d0c539e 2019-05-22 jcs err = got_error_from_errno2("fdopen", *path);
90 511a516b 2018-05-19 stsp free(*path);
91 511a516b 2018-05-19 stsp *path = NULL;
92 511a516b 2018-05-19 stsp }
93 511a516b 2018-05-19 stsp
94 511a516b 2018-05-19 stsp return err;
95 511a516b 2018-05-19 stsp }
96 507dc3bb 2018-12-29 stsp
97 507dc3bb 2018-12-29 stsp const struct got_error *
98 b90054ed 2022-11-01 stsp got_opentemp_named_fd(char **path, int *outfd, const char *basepath,
99 b90054ed 2022-11-01 stsp const char *suffix)
100 507dc3bb 2018-12-29 stsp {
101 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
102 507dc3bb 2018-12-29 stsp int fd;
103 507dc3bb 2018-12-29 stsp
104 507dc3bb 2018-12-29 stsp *outfd = -1;
105 507dc3bb 2018-12-29 stsp
106 ea2819be 2023-05-29 op if (asprintf(path, "%s-XXXXXXXXXX%s", basepath, suffix) == -1) {
107 507dc3bb 2018-12-29 stsp *path = NULL;
108 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
109 507dc3bb 2018-12-29 stsp }
110 507dc3bb 2018-12-29 stsp
111 b90054ed 2022-11-01 stsp fd = mkstemps(*path, strlen(suffix));
112 507dc3bb 2018-12-29 stsp if (fd == -1) {
113 638f9024 2019-05-13 stsp err = got_error_from_errno("mkstemp");
114 507dc3bb 2018-12-29 stsp free(*path);
115 507dc3bb 2018-12-29 stsp *path = NULL;
116 507dc3bb 2018-12-29 stsp return err;
117 507dc3bb 2018-12-29 stsp }
118 507dc3bb 2018-12-29 stsp
119 507dc3bb 2018-12-29 stsp *outfd = fd;
120 507dc3bb 2018-12-29 stsp return err;
121 507dc3bb 2018-12-29 stsp }
122 a558dd1b 2022-06-08 stsp
123 a558dd1b 2022-06-08 stsp const struct got_error *
124 a558dd1b 2022-06-08 stsp got_opentemp_truncate(FILE *f)
125 a558dd1b 2022-06-08 stsp {
126 a558dd1b 2022-06-08 stsp if (fpurge(f) == EOF)
127 a558dd1b 2022-06-08 stsp return got_error_from_errno("fpurge");
128 a558dd1b 2022-06-08 stsp if (ftruncate(fileno(f), 0L) == -1)
129 a558dd1b 2022-06-08 stsp return got_error_from_errno("ftruncate");
130 a558dd1b 2022-06-08 stsp if (fseeko(f, 0L, SEEK_SET) == -1)
131 a558dd1b 2022-06-08 stsp return got_error_from_errno("fseeko");
132 a558dd1b 2022-06-08 stsp return NULL;
133 a558dd1b 2022-06-08 stsp }