Signed-off-by: Stanislav Kholmanskikh <[email protected]>
---
include/safe_file_ops.h | 21 +++++++++++++++++++++
lib/safe_file_ops.c | 41 +++++++++++++++++++++++++++++++++++++++++
2 files changed, 62 insertions(+), 0 deletions(-)
diff --git a/include/safe_file_ops.h b/include/safe_file_ops.h
index 7ff5baa..77ad594 100644
--- a/include/safe_file_ops.h
+++ b/include/safe_file_ops.h
@@ -72,4 +72,25 @@ void safe_cp(const char *file, const int lineno,
#define SAFE_CP(cleanup_fn, src, dst) \
safe_cp(__FILE__, __LINE__, (cleanup_fn), (src), (dst))
+/*
+ * Safe function to touch a file.
+ *
+ * If the file (pathname) does not exist It will be created with
+ * the specified permission (mode) and the access/modification times (times).
+ *
+ * If mode is 0 then the file is created with (0666 & ~umask)
+ * permission or (if the file exists) the permission is not changed.
+ *
+ * times is a timespec[2] (as for utimensat(2)). If times is NULL then
+ * the access/modification times of the file is set to the current time.
+ */
+void safe_touch(const char *file, const int lineno,
+ void (*cleanup_fn)(void),
+ const char *pathname,
+ mode_t mode, const struct timespec times[2]);
+
+#define SAFE_TOUCH(cleanup_fn, pathname, mode, times) \
+ safe_touch(__FILE__, __LINE__, (cleanup_fn), \
+ (pathname), (mode), (times))
+
#endif /* SAFE_FILE_OPS */
diff --git a/lib/safe_file_ops.c b/lib/safe_file_ops.c
index b602345..8cfd264 100644
--- a/lib/safe_file_ops.c
+++ b/lib/safe_file_ops.c
@@ -23,6 +23,9 @@
#include <stdarg.h>
#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
#include "safe_file_ops.h"
@@ -152,3 +155,41 @@ void safe_cp(const char *file, const int lineno,
src, dst, file, lineno);
}
}
+
+void safe_touch(const char *file, const int lineno,
+ void (*cleanup_fn)(void),
+ const char *pathname,
+ mode_t mode, const struct timespec times[2])
+{
+ int ret;
+ mode_t defmode;
+
+ defmode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
+
+ ret = open(pathname, O_CREAT | O_WRONLY, defmode);
+ if (ret == -1)
+ tst_brkm(TBROK | TERRNO, cleanup_fn,
+ "Failed to open file '%s' at %s:%d",
+ pathname, file, lineno);
+
+ ret = close(ret);
+ if (ret == -1)
+ tst_brkm(TBROK | TERRNO, cleanup_fn,
+ "Failed to close file '%s' at %s:%d",
+ pathname, file, lineno);
+
+ if (mode != 0) {
+ ret = chmod(pathname, mode);
+ if (ret == -1)
+ tst_brkm(TBROK | TERRNO, cleanup_fn,
+ "Failed to chmod file '%s' at %s:%d",
+ pathname, file, lineno);
+ }
+
+ ret = utimensat(AT_FDCWD, pathname, times, 0);
+ if (ret == -1)
+ tst_brkm(TBROK | TERRNO, cleanup_fn,
+ "Failed to do utimensat() on file '%s' at %s:%d",
+ pathname, file, lineno);
+}
+
--
1.7.1
------------------------------------------------------------------------------
November Webinars for C, C++, Fortran Developers
Accelerate application performance with scalable programming models. Explore
techniques for threading, error checking, porting, and tuning. Get the most
from the latest Intel processors and coprocessors. See abstracts and register
http://pubads.g.doubleclick.net/gampad/clk?id=60136231&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list