[PATCH weston 2/3] file-util: allow specifying path separately in file_create_dated()

Aleksander Morgado aleksander at aleksander.es
Tue Jan 23 00:05:21 UTC 2018


Instead of assuming the file prefix contains the path and filename
prefix, give these two items separately.

A NULL or empty string path may still be given to refer to the current
directory.

Signed-off-by: Aleksander Morgado <aleksander at aleksander.es>

diff --git a/libweston/timeline.c b/libweston/timeline.c
index 8234c27c..f5a39cba 100644
--- a/libweston/timeline.c
+++ b/libweston/timeline.c
@@ -53,7 +53,7 @@ weston_timeline_do_open(void)
 	const char *suffix = ".log";
 	char fname[1000];

-	timeline_.file = file_create_dated(prefix, suffix,
+	timeline_.file = file_create_dated(NULL, prefix, suffix,
 					   fname, sizeof(fname));
 	if (!timeline_.file) {
 		const char *msg;
diff --git a/shared/file-util.c b/shared/file-util.c
index 979a3811..98be1785 100644
--- a/shared/file-util.c
+++ b/shared/file-util.c
@@ -65,14 +65,15 @@ create_file_excl(const char *fname)

 /** Create a unique file with date and time in the name
  *
- * \param path_prefix Path and file name prefix.
+ * \param path File path
+ * \param prefix File name prefix.
  * \param suffix File name suffix.
  * \param name_out[out] Buffer for the resulting file name.
  * \param name_len Number of bytes usable in name_out.
  * \return stdio FILE pointer, or NULL on failure.
  *
  * Create and open a new file with the name concatenated from
- * path_prefix, date and time, and suffix. If a file with this name
+ * path/prefix, date and time, and suffix. If a file with this name
  * already exists, an counter number is added to the end of the
  * date and time sub-string. The counter is increased until a free file
  * name is found.
@@ -81,19 +82,23 @@ create_file_excl(const char *fname)
  * On failure, the contents of name_out are undefined and errno is set.
  */
 FILE *
-file_create_dated(const char *path_prefix, const char *suffix,
+file_create_dated(const char *path, const char *prefix, const char *suffix,
 		  char *name_out, size_t name_len)
 {
 	char timestr[128];
 	int ret;
 	int fd;
 	int cnt = 0;
+	int with_path;
+
+	with_path = path && path[0];

 	if (current_time_str(timestr, sizeof(timestr), "%F_%H-%M-%S") < 0)
 		return NULL;

-	ret = snprintf(name_out, name_len, "%s%s%s",
-		       path_prefix, timestr, suffix);
+	ret = snprintf(name_out, name_len, "%s%s%s%s%s",
+		       with_path ? path : "", with_path ? "/" : "",
+		       prefix, timestr, suffix);
 	if (ret < 0 || (size_t)ret >= name_len) {
 		errno = ENOBUFS;
 		return NULL;
@@ -104,8 +109,9 @@ file_create_dated(const char *path_prefix, const char *suffix,
 	while (fd == -1 && errno == EEXIST) {
 		cnt++;

-		ret = snprintf(name_out, name_len, "%s%s-%d%s",
-			       path_prefix, timestr, cnt, suffix);
+		ret = snprintf(name_out, name_len, "%s%s%s%s-%d%s",
+			       with_path ? path : "", with_path ? "/" : "",
+			       prefix, timestr, cnt, suffix);
 		if (ret < 0 || (size_t)ret >= name_len) {
 			errno = ENOBUFS;
 			return NULL;
diff --git a/shared/file-util.h b/shared/file-util.h
index f639c446..e272dde7 100644
--- a/shared/file-util.h
+++ b/shared/file-util.h
@@ -33,7 +33,7 @@ extern "C" {
 #include <stdio.h>

 FILE *
-file_create_dated(const char *path_prefix, const char *suffix,
+file_create_dated(const char *path, const char *prefix, const char *suffix,
 		  char *name_out, size_t name_len);

 #ifdef  __cplusplus
diff --git a/tests/surface-screenshot.c b/tests/surface-screenshot.c
index f5199371..908022de 100644
--- a/tests/surface-screenshot.c
+++ b/tests/surface-screenshot.c
@@ -185,7 +185,7 @@ trigger_binding(struct weston_keyboard *keyboard, const struct timespec *time,

 	unpremultiply_and_swap_a8b8g8r8_to_PAMrgba(pixels, sz);

-	fp = file_create_dated(prefix, suffix, fname, sizeof(fname));
+	fp = file_create_dated(NULL, prefix, suffix, fname, sizeof(fname));
 	if (!fp) {
 		const char *msg;

--
2.15.1


More information about the wayland-devel mailing list