move got_opentmp* functions to public API; used from tog
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
diff --git a/got/Makefile b/got/Makefile
index 0044a48..810f98c 100644
--- a/got/Makefile
+++ b/got/Makefile
@@ -2,8 +2,8 @@
PROG= got
SRCS= got.c delta.c diff.c diffreg.c error.c fileindex.c object.c \
- path.c pack.c privsep.c reference.c repository.c sha1.c \
- worktree.c zbuf.c
+ opentemp.c path.c pack.c privsep.c reference.c repository.c \
+ sha1.c worktree.c zbuf.c
CPPFLAGS = -I${.CURDIR}/../include -I${.CURDIR}/../lib
LDADD = -lutil -lz
diff --git a/include/got_opentemp.h b/include/got_opentemp.h
new file mode 100644
index 0000000..1820266
--- /dev/null
+++ b/include/got_opentemp.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/* Utilities for opening temporary files. */
+
+/* Open a file descriptor to a new temporary file for writing.
+ * The file is not visible in the filesystem. */
+int got_opentempfd(void);
+
+/* Open a new temporary file for writing.
+ * The file is not visible in the filesystem. */
+FILE *got_opentemp(void);
+
+/* Open a new temporary file for writing.
+ * The file is visible in the filesystem. */
+const struct got_error *got_opentemp_named(char **, FILE **, const char *);
diff --git a/lib/diff.c b/lib/diff.c
index 85c5386..c9ec771 100644
--- a/lib/diff.c
+++ b/lib/diff.c
@@ -28,6 +28,7 @@
#include "got_object.h"
#include "got_error.h"
#include "got_diff.h"
+#include "got_opentemp.h"
#include "got_lib_diff.h"
#include "got_lib_path.h"
diff --git a/lib/got_lib_path.h b/lib/got_lib_path.h
index d1ceebb..7fcd5f6 100644
--- a/lib/got_lib_path.h
+++ b/lib/got_lib_path.h
@@ -34,18 +34,6 @@ char *got_path_get_absolute(const char *);
*/
char *got_path_normalize(const char *);
-/* Open a file descriptor to a new temporary file for writing.
- * The file is not visible in the filesystem. */
-int got_opentempfd(void);
-
-/* Open a new temporary file for writing.
- * The file is not visible in the filesystem. */
-FILE *got_opentemp(void);
-
-/* Open a new temporary file for writing.
- * The file is visible in the filesystem. */
-const struct got_error *got_opentemp_named(char **, FILE **, const char *);
-
/* Count the number of path segments separated by '/'. */
const struct got_error *
got_path_segment_count(int *count, const char *path);
diff --git a/lib/object.c b/lib/object.c
index c382219..45d21d4 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -36,6 +36,7 @@
#include "got_error.h"
#include "got_object.h"
#include "got_repository.h"
+#include "got_opentemp.h"
#include "got_lib_sha1.h"
#include "got_lib_delta.h"
diff --git a/lib/opentemp.c b/lib/opentemp.c
new file mode 100644
index 0000000..90af269
--- /dev/null
+++ b/lib/opentemp.c
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <limits.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdio.h>
+
+#include "got_opentemp.h"
+#include "got_error.h"
+
+int
+got_opentempfd(void)
+{
+ char name[PATH_MAX];
+ int fd;
+
+ if (strlcpy(name, "/tmp/got.XXXXXXXX", sizeof(name)) >= sizeof(name))
+ return -1;
+
+ fd = mkstemp(name);
+ unlink(name);
+ return fd;
+}
+
+FILE *
+got_opentemp(void)
+{
+ int fd;
+ FILE *f;
+
+ fd = got_opentempfd();
+ if (fd < 0)
+ return NULL;
+
+ f = fdopen(fd, "w+");
+ if (f == NULL) {
+ close(fd);
+ return NULL;
+ }
+
+ return f;
+}
+
+const struct got_error *
+got_opentemp_named(char **path, FILE **outfile, const char *basepath)
+{
+ const struct got_error *err = NULL;
+ int fd;
+
+ if (asprintf(path, "%s-XXXXXX", basepath) == -1) {
+ *path = NULL;
+ return got_error_from_errno();
+ }
+
+ fd = mkstemp(*path);
+ if (fd == -1) {
+ err = got_error_from_errno();
+ free(*path);
+ *path = NULL;
+ return err;
+ }
+
+ *outfile = fdopen(fd, "w+");
+ if (*outfile == NULL) {
+ err = got_error_from_errno();
+ free(*path);
+ *path = NULL;
+ }
+
+ return err;
+}
diff --git a/lib/pack.c b/lib/pack.c
index 5ae9420..6ced42c 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -33,6 +33,7 @@
#include "got_error.h"
#include "got_object.h"
#include "got_repository.h"
+#include "got_opentemp.h"
#include "got_lib_sha1.h"
#include "got_lib_pack.h"
diff --git a/lib/path.c b/lib/path.c
index 659eaea..30be0f8 100644
--- a/lib/path.c
+++ b/lib/path.c
@@ -81,65 +81,3 @@ got_path_segment_count(int *count, const char *path)
return NULL;
}
-
-int
-got_opentempfd(void)
-{
- char name[PATH_MAX];
- int fd;
-
- if (strlcpy(name, "/tmp/got.XXXXXXXX", sizeof(name)) >= sizeof(name))
- return -1;
-
- fd = mkstemp(name);
- unlink(name);
- return fd;
-}
-
-FILE *
-got_opentemp(void)
-{
- int fd;
- FILE *f;
-
- fd = got_opentempfd();
- if (fd < 0)
- return NULL;
-
- f = fdopen(fd, "w+");
- if (f == NULL) {
- close(fd);
- return NULL;
- }
-
- return f;
-}
-
-const struct got_error *
-got_opentemp_named(char **path, FILE **outfile, const char *basepath)
-{
- const struct got_error *err = NULL;
- int fd;
-
- if (asprintf(path, "%s-XXXXXX", basepath) == -1) {
- *path = NULL;
- return got_error_from_errno();
- }
-
- fd = mkstemp(*path);
- if (fd == -1) {
- err = got_error_from_errno();
- free(*path);
- *path = NULL;
- return err;
- }
-
- *outfile = fdopen(fd, "w+");
- if (*outfile == NULL) {
- err = got_error_from_errno();
- free(*path);
- *path = NULL;
- }
-
- return err;
-}
diff --git a/lib/worktree.c b/lib/worktree.c
index 4fe01db..176d82f 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -33,6 +33,7 @@
#include "got_reference.h"
#include "got_object.h"
#include "got_worktree.h"
+#include "got_opentemp.h"
#include "got_lib_worktree.h"
#include "got_lib_path.h"
diff --git a/regress/delta/Makefile b/regress/delta/Makefile
index 20d6513..3c421aa 100644
--- a/regress/delta/Makefile
+++ b/regress/delta/Makefile
@@ -1,7 +1,7 @@
.PATH:${.CURDIR}/../../lib
PROG = delta_test
-SRCS = delta.c error.c path.c zbuf.c delta_test.c
+SRCS = delta.c error.c opentemp.c path.c zbuf.c delta_test.c
CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib
LDADD = -lz
diff --git a/regress/delta/delta_test.c b/regress/delta/delta_test.c
index b4272d3..f8b0bfa 100644
--- a/regress/delta/delta_test.c
+++ b/regress/delta/delta_test.c
@@ -23,6 +23,7 @@
#include <unistd.h>
#include "got_error.h"
+#include "got_opentemp.h"
#include "got_lib_delta.h"
#include "got_lib_path.h"
diff --git a/regress/repository/Makefile b/regress/repository/Makefile
index 605897b..18bd527 100644
--- a/regress/repository/Makefile
+++ b/regress/repository/Makefile
@@ -1,9 +1,9 @@
.PATH:${.CURDIR}/../../lib
PROG = repository_test
-SRCS = path.c repository.c error.c reference.c object.c sha1.c diff.c \
- diffreg.c pack.c privsep.c delta.c fileindex.c worktree.c \
- zbuf.c repository_test.c
+SRCS = path.c repository.c error.c reference.c object.c opentemp.c \
+ sha1.c diff.c diffreg.c pack.c privsep.c delta.c fileindex.c \
+ worktree.c zbuf.c repository_test.c
CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib
LDADD = -lutil -lz
diff --git a/regress/repository/repository_test.c b/regress/repository/repository_test.c
index 67c3239..6c9a268 100644
--- a/regress/repository/repository_test.c
+++ b/regress/repository/repository_test.c
@@ -31,6 +31,7 @@
#include "got_reference.h"
#include "got_repository.h"
#include "got_diff.h"
+#include "got_opentemp.h"
#include "got_lib_path.h"
diff --git a/regress/worktree/Makefile b/regress/worktree/Makefile
index 7d7df3b..bdce9e6 100644
--- a/regress/worktree/Makefile
+++ b/regress/worktree/Makefile
@@ -1,8 +1,9 @@
.PATH:${.CURDIR}/../../lib
PROG = worktree_test
-SRCS = worktree.c repository.c object.c path.c error.c reference.c sha1.c \
- pack.c privsep.c delta.c zbuf.c fileindex.c worktree_test.c
+SRCS = worktree.c repository.c object.c opentemp.c path.c error.c \
+ reference.c sha1.c pack.c privsep.c delta.c zbuf.c \
+ fileindex.c worktree_test.c
CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib
LDADD = -lutil -lz
diff --git a/regress/worktree/worktree_test.c b/regress/worktree/worktree_test.c
index eb723d0..a1d1e49 100644
--- a/regress/worktree/worktree_test.c
+++ b/regress/worktree/worktree_test.c
@@ -34,6 +34,7 @@
#include "got_reference.h"
#include "got_repository.h"
#include "got_worktree.h"
+#include "got_opentemp.h"
#include "got_lib_worktree.h"
#include "got_lib_path.h"
diff --git a/tog/Makefile b/tog/Makefile
index 965c0cd..4fd5ba1 100644
--- a/tog/Makefile
+++ b/tog/Makefile
@@ -2,8 +2,8 @@
PROG= tog
SRCS= tog.c delta.c diff.c diffreg.c error.c fileindex.c object.c \
- path.c pack.c privsep.c reference.c repository.c sha1.c \
- worktree.c zbuf.c
+ opentemp.c path.c pack.c privsep.c reference.c repository.c \
+ sha1.c worktree.c zbuf.c
CPPFLAGS = -I${.CURDIR}/../include -I${.CURDIR}/../lib
LDADD = -lpanel -lcurses -lutil -lz
diff --git a/tog/tog.c b/tog/tog.c
index 1eeab3c..db5f5fc 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -34,6 +34,7 @@
#include "got_reference.h"
#include "got_repository.h"
#include "got_diff.h"
+#include "got_opentemp.h"
#ifndef MIN
#define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
@@ -87,39 +88,6 @@ static struct tog_diff_view {
PANEL *panel;
} tog_diff_view;
-int
-tog_opentempfd(void)
-{
- char name[PATH_MAX];
- int fd;
-
- if (strlcpy(name, "/tmp/tog.XXXXXXXX", sizeof(name)) >= sizeof(name))
- return -1;
-
- fd = mkstemp(name);
- unlink(name);
- return fd;
-}
-
-FILE *
-tog_opentemp(void)
-{
- int fd;
- FILE *f;
-
- fd = tog_opentempfd();
- if (fd < 0)
- return NULL;
-
- f = fdopen(fd, "w+");
- if (f == NULL) {
- close(fd);
- return NULL;
- }
-
- return f;
-}
-
__dead void
usage_log(void)
{
@@ -832,7 +800,7 @@ show_diff_view(struct got_object *obj1, struct got_object *obj2,
if (got_object_get_type(obj1) != got_object_get_type(obj2))
return got_error(GOT_ERR_OBJ_TYPE);
- f = tog_opentemp();
+ f = got_opentemp();
if (f == NULL)
return got_error_from_errno();