Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F178115181
D59380.1796008577.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
6 KB
Referenced Files
None
Subscribers
None
D59380.1796008577.diff
View Options
diff --git a/usr.bin/xinstall/xinstall.c b/usr.bin/xinstall/xinstall.c
--- a/usr.bin/xinstall/xinstall.c
+++ b/usr.bin/xinstall/xinstall.c
@@ -96,6 +96,8 @@
#define HAVE_STRUCT_STAT_ST_FLAGS 0
#endif
+#define METALOG_MEM_MAX 4096
+
#define LN_ABSOLUTE 0x01
#define LN_RELATIVE 0x02
#define LN_HARD 0x04
@@ -141,7 +143,10 @@
static bool haveopt_f, haveopt_g, haveopt_m, haveopt_o;
static int linkmode;
static mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
-static FILE *metafp;
+static int metafd = -1;
+static FILE *memfp;
+static char *membuf;
+static size_t memsz;
static const char *group, *owner;
static const char *suffix = BACKUP_SUFFIX;
static char *destdir, *digest, *fflags, *metafile, *tags;
@@ -160,6 +165,7 @@
static void makelink(const char *, const char *, const struct stat *);
static void install(const char *, const char *, u_long, u_int);
static void install_dir(char *);
+static void metadata_flush(void);
static void metadata_log(const char *, const char *, struct timespec *,
const char *, const char *, off_t);
static int parseid(const char *, id_t *);
@@ -172,7 +178,7 @@
struct stat from_sb, to_sb;
mode_t *set;
u_long fset;
- int ch, no_target;
+ int ch, no_target, o_flags;
u_int iflags;
char *p;
const char *to_name;
@@ -365,8 +371,21 @@
}
if (metafile != NULL) {
- if ((metafp = fopen(metafile, "a")) == NULL)
+ /*
+ * Append to metalog. Log lines are first buffered in a memory
+ * stream in order to reduce advisory lock calls. The metalog
+ * usually already exists, so first try opening without
+ * O_CREAT.
+ */
+ o_flags = O_WRONLY | O_APPEND;
+ metafd = open(metafile, o_flags);
+ if (metafd < 0 && errno == ENOENT)
+ metafd = open(metafile, o_flags | O_CREAT, S_IRUSR |
+ S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
+ if (metafd < 0)
warn("open %s", metafile);
+ else if (atexit(metadata_flush) != 0)
+ err(EX_SOFTWARE, "atexit");
} else
digesttype = DIGEST_NONE;
@@ -1405,11 +1424,61 @@
metadata_log(path, "dir", NULL, NULL, NULL, 0);
}
+static void
+metadata_flush(void)
+{
+ struct flock metalog_lock;
+ char *p;
+ ssize_t n;
+
+ if (memfp == NULL)
+ return;
+
+ fclose(memfp);
+ memfp = NULL;
+ if (memsz == 0)
+ goto clean;
+
+ /* Lock log file. */
+ metalog_lock.l_start = 0;
+ metalog_lock.l_len = 0;
+ metalog_lock.l_whence = SEEK_SET;
+ metalog_lock.l_type = F_WRLCK;
+ if (fcntl(metafd, F_SETLKW, &metalog_lock) == -1) {
+ warn("can't lock %s", metafile);
+ goto clean;
+ }
+
+ /* Flush the memory buffer to the metalog. */
+ p = membuf;
+ while (memsz > 0) {
+ n = write(metafd, p, memsz);
+ if (n < 0) {
+ if (errno == EINTR)
+ continue;
+ warn("writing %s", metafile);
+ break;
+ }
+ memsz -= n;
+ p += n;
+ }
+
+ /* Unlock log file. */
+ metalog_lock.l_type = F_UNLCK;
+ if (fcntl(metafd, F_SETLKW, &metalog_lock) == -1)
+ warn("can't unlock %s", metafile);
+
+ clean:
+ free(membuf);
+ membuf = NULL;
+ memsz = 0;
+}
+
/*
* metadata_log --
- * if metafp is not NULL, output mtree(8) full path name and settings to
- * metafp, to allow permissions to be set correctly by other tools,
- * or to allow integrity checks to be performed.
+ * if we have a metalog, output mtree(8) full path name and settings to
+ * it, to allow permissions to be set correctly by other tools, or to
+ * allow integrity checks to be performed.
*/
static void
metadata_log(const char *path, const char *type, struct timespec *ts,
@@ -1419,11 +1488,20 @@
const char *p;
char *buf;
size_t buflen, destlen;
- struct flock metalog_lock;
id_t id;
- if (metafp == NULL)
+ if (metafd < 0)
return;
+
+ /* Lazy open of memory buffer. */
+ if (memfp == NULL) {
+ memfp = open_memstream(&membuf, &memsz);
+ if (memfp == NULL) {
+ warn("buffering for %s", metafile);
+ return;
+ }
+ }
+
/* Buffer for strsnvis(3), used for both path and slink. */
buflen = strlen(path);
if (slink && strlen(slink) > buflen)
@@ -1434,17 +1512,6 @@
return;
}
- /* Lock log file. */
- metalog_lock.l_start = 0;
- metalog_lock.l_len = 0;
- metalog_lock.l_whence = SEEK_SET;
- metalog_lock.l_type = F_WRLCK;
- if (fcntl(fileno(metafp), F_SETLKW, &metalog_lock) == -1) {
- warn("can't lock %s", metafile);
- free(buf);
- return;
- }
-
/* Remove destdir. */
p = path;
if (destdir) {
@@ -1458,44 +1525,43 @@
strsnvis(buf, buflen, p, VIS_OCTAL, extra);
p = buf;
/* Print details. */
- fprintf(metafp, ".%s%s type=%s", *p ? "/" : "", p, type);
+ fprintf(memfp, ".%s%s type=%s", *p ? "/" : "", p, type);
if (owner) {
if (parseid(owner, &id))
- fprintf(metafp, " uid=%jd", (intmax_t)id);
+ fprintf(memfp, " uid=%jd", (intmax_t)id);
else
- fprintf(metafp, " uname=%s", owner);
+ fprintf(memfp, " uname=%s", owner);
}
if (group) {
if (parseid(group, &id))
- fprintf(metafp, " gid=%jd", (intmax_t)id);
+ fprintf(memfp, " gid=%jd", (intmax_t)id);
else
- fprintf(metafp, " gname=%s", group);
+ fprintf(memfp, " gname=%s", group);
}
- fprintf(metafp, " mode=%#o", mode);
+ fprintf(memfp, " mode=%#o", mode);
if (slink) {
strsnvis(buf, buflen, slink, VIS_CSTYLE, extra);
- fprintf(metafp, " link=%s", buf);
+ fprintf(memfp, " link=%s", buf);
}
if (*type == 'f') /* type=file */
- fprintf(metafp, " size=%lld", (long long)size);
+ fprintf(memfp, " size=%lld", (long long)size);
if (ts != NULL && dopreserve)
- fprintf(metafp, " time=%lld.%09ld",
+ fprintf(memfp, " time=%lld.%09ld",
(long long)ts[1].tv_sec, ts[1].tv_nsec);
if (digestresult && digest != NULL)
- fprintf(metafp, " %s=%s", digest, digestresult);
+ fprintf(memfp, " %s=%s", digest, digestresult);
if (fflags != NULL)
- fprintf(metafp, " flags=%s", fflags);
+ fprintf(memfp, " flags=%s", fflags);
if (tags != NULL)
- fprintf(metafp, " tags=%s", tags);
- fputc('\n', metafp);
- /* Flush line. */
- fflush(metafp);
+ fprintf(memfp, " tags=%s", tags);
+ fputc('\n', memfp);
- /* Unlock log file. */
- metalog_lock.l_type = F_UNLCK;
- if (fcntl(fileno(metafp), F_SETLKW, &metalog_lock) == -1)
- warn("can't unlock %s", metafile);
free(buf);
+
+ /* Flush memory buffer if it's getting large. */
+ fflush(memfp);
+ if (memsz >= METALOG_MEM_MAX)
+ metadata_flush();
}
/*
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Nov 30, 3:16 AM (19 h, 25 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
38284442
Default Alt Text
D59380.1796008577.diff (6 KB)
Attached To
Mode
D59380: install: amortize metalog locking/flushing with a memory buffer
Attached
Detach File
Event Timeline