summaryrefslogtreecommitdiff
path: root/src/shared/util.c
diff options
context:
space:
mode:
authorTom Gundersen <teg@jklm.no>2015-03-18 19:38:08 -0400
committerAnthony G. Basile <blueness@gentoo.org>2015-03-18 19:56:01 -0400
commit5c3965d1a89539ad6042bbfdcc844fc9ed8aa379 (patch)
tree07ea294763228c5a0ae10b16568ca65b48811a65 /src/shared/util.c
parentced0d2419e3fc88e29f8076363deb4214b87f0f2 (diff)
libudev: udev_device_add_property - implicitly mark properties for saving to db
Note: We also ported touch() and touch_file() from upstream. -AGB. Signed-off-by: Anthony G. Basile <blueness@gentoo.org>
Diffstat (limited to 'src/shared/util.c')
-rw-r--r--src/shared/util.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/shared/util.c b/src/shared/util.c
index f68b687006..d1fd3c80f3 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -1001,6 +1001,49 @@ int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) {
return 0;
}
+int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode) {
+ _cleanup_close_ int fd;
+ int r;
+
+ assert(path);
+
+ if (parents)
+ mkdir_parents(path, 0755);
+
+ fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, mode > 0 ? mode : 0644);
+ if (fd < 0)
+ return -errno;
+
+ if (mode > 0) {
+ r = fchmod(fd, mode);
+ if (r < 0)
+ return -errno;
+ }
+
+ if (uid != UID_INVALID || gid != GID_INVALID) {
+ r = fchown(fd, uid, gid);
+ if (r < 0)
+ return -errno;
+ }
+
+ if (stamp != USEC_INFINITY) {
+ struct timespec ts[2];
+
+ timespec_store(&ts[0], stamp);
+ ts[1] = ts[0];
+ r = futimens(fd, ts);
+ } else
+ r = futimens(fd, NULL);
+ if (r < 0)
+ return -errno;
+
+ return 0;
+}
+
+int touch(const char *path) {
+ return touch_file(path, false, USEC_INFINITY, UID_INVALID, GID_INVALID, 0);
+}
+
bool null_or_empty(struct stat *st) {
assert(st);