From 570de6d0be5612acdc86f6dcfbf1962dc9604667 Mon Sep 17 00:00:00 2001
From: "tanguy.cavagna" <tanguy.cavagna@etu.hesge.ch>
Date: Mon, 27 Jun 2022 18:49:15 +0200
Subject: [PATCH] Added time attributes on copy

---
 ultra-cp/ultra-cp.c | 11 +++++++++++
 ultra-cp/ultra-cp.h |  1 +
 2 files changed, 12 insertions(+)

diff --git a/ultra-cp/ultra-cp.c b/ultra-cp/ultra-cp.c
index d8a31d8..48e8603 100644
--- a/ultra-cp/ultra-cp.c
+++ b/ultra-cp/ultra-cp.c
@@ -27,6 +27,11 @@ int get_int_permissions(const char *path) {
  */
 void copy_entry(struct dirent *entry, const char *source,
                 const char *destination) {
+    struct stat entry_stat;
+
+    if (stat(source, &entry_stat) < 0)
+        return;
+
     // Entry is a file, copy it
     if (entry->d_type == DT_REG) {
         FILE *src_file, *dst_file;
@@ -47,6 +52,12 @@ void copy_entry(struct dirent *entry, const char *source,
     // Go deeper if directory
     if (entry->d_type == DT_DIR)
         mkdir(destination, get_int_permissions(source));
+
+    struct utimbuf new_time;
+    new_time.actime = entry_stat.st_atim.tv_sec;
+    new_time.modtime = entry_stat.st_mtim.tv_sec;
+
+    utime(destination, &new_time);
 }
 
 /**
diff --git a/ultra-cp/ultra-cp.h b/ultra-cp/ultra-cp.h
index 8f3878a..28e2c44 100644
--- a/ultra-cp/ultra-cp.h
+++ b/ultra-cp/ultra-cp.h
@@ -11,6 +11,7 @@
 #include <sys/types.h>
 #include <time.h>
 #include <unistd.h>
+#include <utime.h>
 
 /**
  * @brief Get the given file permissions
-- 
GitLab