summaryrefslogtreecommitdiff
path: root/usr.bin/rsync/mktemp.c
diff options
context:
space:
mode:
authorSebastian Benoit <benno@cvs.openbsd.org>2019-02-18 22:47:35 +0000
committerSebastian Benoit <benno@cvs.openbsd.org>2019-02-18 22:47:35 +0000
commit2f42213ec04aa78b5d93fb5d401b5976e9fa06f1 (patch)
treedfdaa1bcacef4b596c963575403c3736797938d2 /usr.bin/rsync/mktemp.c
parent9bb0da04d46a25150c43281f9df758c060567427 (diff)
new attempt to sync with kristaps up to Sun Feb 17 2019
339cf5998c0c022623cd68de50722b6c14543952 Push "error trail" further into code. baf58ce5fe1bc6ce431b3b0ac8264b83ae8c7d02 Document all arguments. Add common -av usage. Remove bits about not supporting anything but files/dirs. 821a811a8c80e52fb56b241fc65a16cae1b4fb2c Disambiguate as prodded by deraadt@ 6c4475b8f226e9031ec0ec1b3f14f7d347132c87 Add -h to usage string 4d344ae6156873b44c95de0c1ed629e637c2d7ab Clarify error message language, use service name instead of port, specify that the socket is SOCK_STREAM. From deraadt@. Tweaked for lowercase messages. f3ec049e76257fc96bcdc872f1d3b967b98f3eb6 In consideration to benno@'s comments, let the mktemp functions propogate an errno handled by the caller. Also keep the original line lengths. While in mktemp.c, make some defines into an enum. e116c2bd00e634b56e4276120135915ceaa31cf2 Put the FSM of the sender into its own function. Put dry_run ack and end of phase ack into the send buffer too, further reducing the possibility of deadlock. c7745aa4c7394ca89d841f8ee76782256d694340 Make the sender write loop be fully non-blocking. This frees us of deadlocking the protocol because the sender will always be able to pull down data. 93c7b4843e80aeac2ec6ae6ffc395df4deaf4a31 Remove "yoda" notation to be more in tune with OpenBSD. Most found by deraadt@.
Diffstat (limited to 'usr.bin/rsync/mktemp.c')
-rw-r--r--usr.bin/rsync/mktemp.c121
1 files changed, 76 insertions, 45 deletions
diff --git a/usr.bin/rsync/mktemp.c b/usr.bin/rsync/mktemp.c
index 15a3a46e824..870b252ff74 100644
--- a/usr.bin/rsync/mktemp.c
+++ b/usr.bin/rsync/mktemp.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: mktemp.c,v 1.6 2019/02/18 21:55:27 benno Exp $ */
+/* $OpenBSD: mktemp.c,v 1.7 2019/02/18 22:47:34 benno Exp $ */
/*
* Copyright (c) 1996-1998, 2008 Theo de Raadt
* Copyright (c) 1997, 2008-2009 Todd C. Miller
@@ -21,45 +21,67 @@
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
+
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
+#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
-#define MKTEMP_NAME 0
-#define MKTEMP_FILE 1
-#define MKTEMP_DIR 2
-#define MKTEMP_LINK 3
-#define MKTEMP_FIFO 4
-#define MKTEMP_NOD 5
-#define MKTEMP_SOCK 6
+#include "extern.h"
+
+/*
+ * The type of temporary files we can create.
+ */
+enum tmpmode {
+ MKTEMP_NAME,
+ MKTEMP_FILE,
+ MKTEMP_DIR,
+ MKTEMP_LINK,
+ MKTEMP_FIFO,
+ MKTEMP_NOD,
+ MKTEMP_SOCK
+};
+/*
+ * Characters we'll use for replacement in the template string.
+ */
#define TEMPCHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
#define NUM_CHARS (sizeof(TEMPCHARS) - 1)
+
+/*
+ * The number of template replacement values (foo.XXXXXX = 6) that we
+ * require as a minimum for the filename.
+ */
#define MIN_X 6
+/*
+ * The only flags we'll accept for creation of the temporary file.
+ */
#define MKOTEMP_FLAGS (O_APPEND | O_CLOEXEC | O_DSYNC | O_RSYNC | O_SYNC)
#ifndef nitems
#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
#endif
-/* adapted from libc/stdio/mktemp.c */
+/*
+ * Adapted from libc/stdio/mktemp.c.
+ */
static int
-mktemp_internalat(int pfd, char *path, int slen, int mode, int flags,
- const char *link, mode_t dev_type, dev_t dev)
+mktemp_internalat(int pfd, char *path, int slen, enum tmpmode mode,
+ int flags, const char *link, mode_t dev_type, dev_t dev)
{
- char *start, *cp, *ep;
- const char tempchars[] = TEMPCHARS;
- unsigned int tries;
- struct stat sb;
+ char *start, *cp, *ep;
+ const char tempchars[] = TEMPCHARS;
+ unsigned int tries;
+ struct stat sb;
struct sockaddr_un sun;
- size_t len;
- int fd, saved_errno;
+ size_t len;
+ int fd, saved_errno;
len = strlen(path);
if (len < MIN_X || slen < 0 || (size_t)slen > len - MIN_X) {
@@ -69,7 +91,8 @@ mktemp_internalat(int pfd, char *path, int slen, int mode, int flags,
ep = path + len - slen;
for (start = ep; start > path && start[-1] == 'X'; start--)
- ;
+ /* continue */ ;
+
if (ep - start < MIN_X) {
errno = EINVAL;
return(-1);
@@ -183,69 +206,73 @@ mktemp_internalat(int pfd, char *path, int slen, int mode, int flags,
* A combination of mkstemp(3) and openat(2).
* On success returns a file descriptor and trailing Xs are overwritten in
* path to create a unique file name.
- * Returns -1 on failure.
+ * Returns -1 on failure and sets errno.
*/
int
mkstempat(int fd, char *path)
{
- return(mktemp_internalat(fd, path, 0, MKTEMP_FILE, 0, NULL, 0, 0));
+ return mktemp_internalat(fd, path, 0, MKTEMP_FILE, 0, NULL, 0, 0);
}
/*
* A combination of mkstemp(3) and symlinkat(2).
* On success returns path with trailing Xs overwritten to create a unique
* file name.
- * Returns NULL on failure.
+ * Returns NULL on failure and sets errno.
*/
-char*
+char *
mkstemplinkat(char *link, int fd, char *path)
{
+
if (mktemp_internalat(fd, path, 0, MKTEMP_LINK, 0, link, 0, 0) == -1)
- return(NULL);
- return(path);
+ return NULL;
+ return path;
}
/*
* A combination of mkstemp(3) and mkfifoat(2).
* On success returns path with trailing Xs overwritten to create a unique
* file name.
- * Returns NULL on failure.
+ * Returns NULL on failure and sets errno.
*/
-char*
+char *
mkstempfifoat(int fd, char *path)
{
+
if (mktemp_internalat(fd, path, 0, MKTEMP_FIFO, 0, NULL, 0, 0) == -1)
- return(NULL);
- return(path);
+ return NULL;
+ return path;
}
/*
* A combination of mkstemp(3) and mknodat(2).
* On success returns path with trailing Xs overwritten to create a unique
* file name.
- * Returns NULL on failure.
+ * Returns NULL on failure and sets errno.
*/
-char*
+char *
mkstempnodat(int fd, char *path, mode_t mode, dev_t dev)
{
- if (mktemp_internalat(fd, path, 0, MKTEMP_NOD, 0, NULL, mode, dev) ==
- -1)
- return(NULL);
- return(path);
+
+ if (mktemp_internalat(fd, path, 0,
+ MKTEMP_NOD, 0, NULL, mode, dev) == -1)
+ return NULL;
+ return path;
}
/*
* A combination of mkstemp(3) and bind(2) on a unix domain socket.
* On success returns path with trailing Xs overwritten to create a unique
* file name.
- * Returns NULL on failure.
+ * Returns NULL on failure and sets errno.
*/
-char*
+char *
mkstempsock(const char *root, char *path)
{
+
if (mktemp_internalat(0, path, 0, MKTEMP_SOCK, 0, root, 0, 0) == -1)
- return(NULL);
- return(path);
+ return NULL;
+ return path;
}
/*
@@ -256,19 +283,23 @@ mkstempsock(const char *root, char *path)
* (excluding the final '\0').
*/
int
-mktemplate(char **ret, const char *path, int recursive)
+mktemplate(struct sess *sess, char **ret, const char *path, int recursive)
{
int n, dirlen;
const char *cp;
if (recursive && (cp = strrchr(path, '/')) != NULL) {
dirlen = cp - path;
- if ((n = asprintf(ret, "%.*s/.%s.XXXXXXXXXX", dirlen, path,
- path + dirlen + 1)) == -1)
- *ret = NULL;
- } else {
- if ((n = asprintf(ret, ".%s.XXXXXXXXXX", path)) == -1)
+ n = asprintf(ret, "%.*s/.%s.XXXXXXXXXX",
+ dirlen, path, path + dirlen + 1);
+ if (n < 0) {
+ ERR(sess, "asprintf");
*ret = NULL;
+ }
+ } else if ((n = asprintf(ret, ".%s.XXXXXXXXXX", path)) < 0) {
+ ERR(sess, "asprintf");
+ *ret = NULL;
}
- return(n);
+
+ return n;
}