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