commit 511a516b74d9f5e498a5dc2de97b0e488df51088 from: Stefan Sperling date: Sat May 19 14:57:12 2018 UTC move got_opentmp* functions to public API; used from tog commit - 11528a829855823153f2aaa9438bdf400a2995d7 commit + 511a516b74d9f5e498a5dc2de97b0e488df51088 blob - 0044a48fd54842d1cf8fc7bcc9f7c0144bf4be4a blob + 810f98c5f9db96b76e1d0493976cc452b00b373e --- got/Makefile +++ 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 blob - /dev/null blob + 1820266f93639ec63112a3c7606a8f613e897296 (mode 644) --- /dev/null +++ include/got_opentemp.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2018 Stefan Sperling + * + * 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 *); blob - 85c538615c4936d03916032ae8518959958e942f blob + c9ec771e35f0f82b5f4685972ff2bcb68251d934 --- lib/diff.c +++ 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" blob - d1ceebbeaf4f607651a7bc7f2fcec650e1c6b04a blob + 7fcd5f62f6c3cc3179fed12218f5e122f8ffde3b --- lib/got_lib_path.h +++ 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); blob - c3822193b294b2380efac47729fd22101b9c0157 blob + 45d21d4d24881d81b51e3afb2be011038e6c143f --- lib/object.c +++ 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" blob - 5ae9420580cc4f8445c55cd7f6fdc5d834dacb2f blob + 6ced42c00804dbc2dce4ae8c33debdd3c8a80563 --- lib/pack.c +++ 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" blob - /dev/null blob + 90af269f16900ad7f3484481718bef2f88c6ed82 (mode 644) --- /dev/null +++ lib/opentemp.c @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2018 Stefan Sperling + * + * 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 +#include +#include +#include +#include + +#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; +} blob - 659eaea0299a501e44b05d9f6f3dfc0e603a0da5 blob + 30be0f88300d852efbfbef730e09f38d66a99b56 --- lib/path.c +++ 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; -} blob - 4fe01db2011a09de7575dbead225e8a5cdd61e41 blob + 176d82f3d2de6c76d2ed803b1e3c8857aa627a9d --- lib/worktree.c +++ 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" blob - 20d651390087f6b818b5116871c4e3c158eefc8c blob + 3c421aacff588dc89a664f8b861ef2f60a071b52 --- regress/delta/Makefile +++ 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 blob - b4272d32123c83c66b97ea8db4c10b5aa9bbb9da blob + f8b0bfa821086e80656b9e1a57f229ff653befee --- regress/delta/delta_test.c +++ regress/delta/delta_test.c @@ -23,6 +23,7 @@ #include #include "got_error.h" +#include "got_opentemp.h" #include "got_lib_delta.h" #include "got_lib_path.h" blob - 605897bc12d293b738391522d9efbee802cb54c8 blob + 18bd5277937e56ca6c47c5538db76068e10d5685 --- regress/repository/Makefile +++ 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 blob - 67c32399cf6d5ff610bbb6cfae01015915a78850 blob + 6c9a268b1c6ce21ed2cc089da6a3914ec9103d33 --- regress/repository/repository_test.c +++ 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" blob - 7d7df3bd9b3f8e19311418e163aa4904b3ab7239 blob + bdce9e6ef00763c45f688ceec5563b895cf56dd6 --- regress/worktree/Makefile +++ 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 blob - eb723d0e2017c280c82d33e528302983a90378ab blob + a1d1e49fe8037949908f248557e97cfb381a9757 --- regress/worktree/worktree_test.c +++ 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" blob - 965c0cd925a617228dc60041904604c3fb359924 blob + 4fd5ba17802dd70e7c1323e8e2676299a6f2573b --- tog/Makefile +++ 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 blob - 1eeab3c01b4b597a967bbbbd27de4658259cedac blob + db5f5fc1ee2a0a8f2bc288df7d3b5a2b6178638a --- tog/tog.c +++ 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)) @@ -86,40 +87,7 @@ static struct tog_diff_view { WINDOW *window; 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_obj 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();