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