mizucp: count number of copied bytes/files
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Wed, 10 Nov 2021 10:30:50 +0000 (11:30 +0100)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Wed, 10 Nov 2021 10:30:50 +0000 (11:30 +0100)
mizucp/atomic.h [new file with mode: 0644]
mizucp/main.c
mizucp/main.h
mizucp/vfs.c [new file with mode: 0644]
mizucp/vfs.h [new file with mode: 0644]

diff --git a/mizucp/atomic.h b/mizucp/atomic.h
new file mode 100644 (file)
index 0000000..f624e7e
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2021 Olaf Wintermann
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a 
+ * copy of this software and associated documentation files (the "Software"), 
+ * to deal in the Software without restriction, including without limitation 
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
+ * and/or sell copies of the Software, and to permit persons to whom the 
+ * Software is furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in 
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef MZCP_ATOMIC_H
+#define MZCP_ATOMIC_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#if defined(__clang__) || defined(__GNUC__)
+    
+#define mz_atomic_inc64(intptr) __sync_fetch_and_add(intptr, 1)
+#define mz_atomic_dec64(intptr) __sync_fetch_and_sub(intptr, 1)
+#define mz_atomic_add64(intptr, val) __sync_fetch_and_add(intptr, val)
+    
+#elif defined(__sun)
+    
+#include <atomic.h>
+
+#define mz_atomic_inc64(intptr) atomic_inc_64_nv(intptr)
+#define mz_atomic_dec64(intptr) atomic_dec_64_nv(intptr)
+#define mz_atomic_add64(intptr, val) atomic_add_64_nv(intptr, val)
+    
+#endif
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* MZCP_ATOMIC_H */
+
index 77b23ea..f590dc7 100644 (file)
 #include "main.h"
 
 #include "srvctrl.h"
+#include "atomic.h"
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <inttypes.h>
 #include <string.h>
 #include <unistd.h>
 #include <signal.h>
@@ -61,6 +63,13 @@ static MZQueue         *queue_end;
 
 static int scan_complete;
 
+
+static uint64_t stat_num_files;
+static uint64_t stat_copied_files;
+static uint64_t stat_error_files;
+static uint64_t stat_total_size;
+static uint64_t stat_copied_size;
+
 int main(int argc, char** argv) { 
     int ret = 1;
     
@@ -298,6 +307,7 @@ void* scan_run(void *data) {
             SrcFile *f = calloc(1, sizeof(SrcFile));
             f->path = util_concat_path(elm->path, name);
             f->isdir = S_ISDIR(s.st_mode);
+            f->size = s.st_size;
             f->mode = s.st_mode;
             f->depends_on = elm;
             
@@ -305,6 +315,10 @@ void* scan_run(void *data) {
                 // TODO: error?
                 fprintf(stderr, "enqueue failed\n");
                 break;
+            } else {
+                mz_atomic_inc64(&stat_num_files);
+                int64_t sz = s.st_size;
+                mz_atomic_add64(&stat_total_size, sz);
             }
             
             // put dir on stack
@@ -510,9 +524,15 @@ int mz_copy_file(CPSettings *settings, SrcFile *file, const char *from, const ch
         return 1;
     }
     
+    int64_t copied = 0;
     ssize_t r;
     while((r = read(fin, buffer, bufsize)) > 0) {
-        if(write(fout, buffer, r) != r) {
+        ssize_t w = write(fout, buffer, r);
+        if(w > 0) {
+            mz_atomic_add64(&stat_copied_size, w);
+            copied += w;
+        }
+        if(w != r) {
             ret = 1;
             break;
         }
@@ -521,7 +541,23 @@ int mz_copy_file(CPSettings *settings, SrcFile *file, const char *from, const ch
     close(fin);
     close(fout);
     
-    file_set_status(file, !ret ? 1 : -1);
+    if(!ret) {
+        file_set_status(file, 1);
+        mz_atomic_inc64(&stat_copied_files);
+        if(copied != file->size) {
+            // size changed after scan -> readjust total size
+            int64_t filesz_diff = copied - file->size;
+            mz_atomic_add64(&stat_total_size, filesz_diff);
+        }
+    } else {
+        file_set_status(file, -1);
+        mz_atomic_inc64(&stat_error_files);
+        if(copied != file->size) {
+            // count the full file size as copied, although we had an error
+            int64_t filesz_diff = file->size - copied;
+            mz_atomic_add64(&stat_copied_size, filesz_diff);
+        }
+    }
     
     return ret;
 }
index 956ecab..e5537e5 100644 (file)
@@ -91,6 +91,11 @@ struct SrcFile {
     mode_t mode;
     
     /*
+     * file size
+     */
+    uint64_t size;
+    
+    /*
      * processing this file depends on another file (directory)
      */
     SrcFile *depends_on;
diff --git a/mizucp/vfs.c b/mizucp/vfs.c
new file mode 100644 (file)
index 0000000..3a3b199
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * Copyright 2019 Olaf Wintermann
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a 
+ * copy of this software and associated documentation files (the "Software"), 
+ * to deal in the Software without restriction, including without limitation 
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
+ * and/or sell copies of the Software, and to permit persons to whom the 
+ * Software is furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in 
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "vfs.h"
diff --git a/mizucp/vfs.h b/mizucp/vfs.h
new file mode 100644 (file)
index 0000000..7e09c30
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2019 Olaf Wintermann
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a 
+ * copy of this software and associated documentation files (the "Software"), 
+ * to deal in the Software without restriction, including without limitation 
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
+ * and/or sell copies of the Software, and to permit persons to whom the 
+ * Software is furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in 
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef VFS_H
+#define VFS_H
+
+#include <stdlib.h>
+#include <unistd.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* VFS_H */
+