From 85e52ccd4c8aada6660d171e91c6c603f40606bb Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Thu, 16 May 2024 23:52:07 +0200
Subject: Move some FS functions from io.[ch] to pathname.[ch].

---
 src/analysis/storage/storage.c |   1 +
 src/common/Makefile.am         |   1 +
 src/common/io.c                | 177 ----------------------------------------
 src/common/io.h                |   9 ---
 src/common/pathname.c          | 179 +++++++++++++++++++++++++++++++++++++++++
 src/common/pathname.h          |  11 +++
 src/core/core.c                |   2 +-
 7 files changed, 193 insertions(+), 187 deletions(-)

diff --git a/src/analysis/storage/storage.c b/src/analysis/storage/storage.c
index c641a52..610a0f6 100644
--- a/src/analysis/storage/storage.c
+++ b/src/analysis/storage/storage.c
@@ -35,6 +35,7 @@
 #include "../db/misc/rlestr.h"
 #include "../../common/io.h"
 #include "../../common/leb128.h"
+#include "../../common/pathname.h"
 #include "../../core/logs.h"
 
 
diff --git a/src/common/Makefile.am b/src/common/Makefile.am
index 8366376..7da1a13 100644
--- a/src/common/Makefile.am
+++ b/src/common/Makefile.am
@@ -58,6 +58,7 @@ libcommon4_la_SOURCES =						\
 	compiler.h								\
 	environment.h environment.c				\
 	extstr.h extstr.c						\
+	io.h io.c								\
 	pathname.h pathname.c					\
 	sort.h sort.c							\
 	xdg.h xdg.c
diff --git a/src/common/io.c b/src/common/io.c
index a710721..3208a2a 100644
--- a/src/common/io.c
+++ b/src/common/io.c
@@ -25,21 +25,15 @@
 
 
 #include <errno.h>
-#include <fcntl.h>
-#include <libgen.h>
 #include <malloc.h>
 #include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include <sys/socket.h>
-#include <sys/stat.h>
 #include <sys/un.h>
 
 
 #include "../core/logs.h"
-#include "../core/params.h"
 
 
 
@@ -290,174 +284,3 @@ bool safe_send(int sockfd, const void *buf, size_t len, int flags)
     return (remaining == 0);
 
 }
-
-
-/******************************************************************************
-*                                                                             *
-*  Paramètres  : path = chemin d'accès à valider.                             *
-*                                                                             *
-*  Description : S'assure qu'un chemin donné existe dans le système.          *
-*                                                                             *
-*  Retour      : 0 si le chemin est actuellement présent, -1 sinon.           *
-*                                                                             *
-*  Remarques   : -                                                            *
-*                                                                             *
-******************************************************************************/
-
-int ensure_path_exists(const char *path)
-{
-    int result;                             /* Bilan de l'assurance        */
-    char *copy;                             /* Chemin libérable            */
-    char *tmp;                              /* Chemin altérable            */
-
-    copy = strdup(path);
-    tmp = dirname(copy);
-
-    result = access(tmp, W_OK | X_OK);
-
-    if (result != 0)
-    {
-        result = ensure_path_exists(tmp);
-
-        if (result == 0)
-            result = mkdir(tmp, 0700);
-
-    }
-
-    free(copy);
-
-    return result;
-
-}
-
-
-/******************************************************************************
-*                                                                             *
-*  Paramètres  : prefix   = préfixe du nom du fichier temporaire à créer.     *
-*                suffix   = éventuel suffixe à coller au nom de fichier.      *
-*                filename = chemin d'accès complet au nouveau fichier. [OUT]  *
-*                                                                             *
-*  Description : Met en place un fichier temporaire.                          *
-*                                                                             *
-*  Retour      : Flux ouvert en lecture et écriture, ou -1 en cas d'erreur.   *
-*                                                                             *
-*  Remarques   : -                                                            *
-*                                                                             *
-******************************************************************************/
-
-int make_tmp_file(const char *prefix, const char *suffix, char **filename)
-{
-    int result;                             /* Flux ou code à retourner    */
-    const char *tmpdir;                     /* Répertoire d'accueil        */
-    bool status;                            /* Bilan d'un consultation     */
-    size_t slen;                            /* Taille du suffixe           */
-
-    status = g_generic_config_get_value(get_main_configuration(), MPK_TMPDIR, &tmpdir);
-    if (!status) return -1;
-
-    slen = strlen(suffix);
-
-    if (slen > 0)
-        asprintf(filename, "%s" G_DIR_SEPARATOR_S "%s-%d.XXXXXX.%s", tmpdir, prefix, getpid(), suffix);
-    else
-        asprintf(filename, "%s" G_DIR_SEPARATOR_S "%s-%d.XXXXXX", tmpdir, prefix, getpid());
-
-    result = ensure_path_exists(*filename);
-
-    if (result == 0)
-    {
-        if (slen > 0)
-            result = mkstemps(*filename, slen + 1);
-        else
-            result = mkstemp(*filename);
-
-        if (result == -1) perror("mkstemp");
-
-    }
-
-    if (result == -1)
-    {
-        free(*filename);
-        *filename = NULL;
-    }
-
-    return result;
-
-}
-
-
-/******************************************************************************
-*                                                                             *
-*  Paramètres  : dest = fichier de destination de la copie.                   *
-*                src  = fichier source à copier.                              *
-*                                                                             *
-*  Description : Copie un fichier.                                            *
-*                                                                             *
-*  Retour      : Bilan de l'opération.                                        *
-*                                                                             *
-*  Remarques   : -                                                            *
-*                                                                             *
-******************************************************************************/
-
-bool copy_file(const char *dest, const char *src)
-{
-    bool result;                            /* Bilan à retourner           */
-    int fd;                                 /* Descripteur du fichier      */
-    struct stat info;                       /* Informations sur le fichier */
-    int ret;                                /* Bilan d'un appel            */
-    void *data;                             /* Quantité de données traitées*/
-    bool status;                            /* Bilan de la lecture         */
-
-    result = false;
-
-    /* Côté source */
-
-    fd = open(src, O_RDONLY);
-    if (fd == -1)
-    {
-        LOG_ERROR_N("open");
-        goto exit;
-    }
-
-    ret = fstat(fd, &info);
-    if (ret == -1)
-    {
-        LOG_ERROR_N("fstat");
-        goto done;
-    }
-
-    data = malloc(info.st_size);
-
-    status = safe_read(fd, data, info.st_size);
-    if (!status) goto clean;
-
-    close(fd);
-
-    /* Côté destination */
-
-    fd = open(dest, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
-    if (fd == -1)
-    {
-        LOG_ERROR_N("open");
-        free(data);
-        goto exit;
-    }
-
-    status = safe_write(fd, data, info.st_size);
-    if (!status) goto clean;
-
-    result = true;
-
- clean:
-
-    free(data);
-
- done:
-
-    close(fd);
-
- exit:
-
-    return result;
-
-}
diff --git a/src/common/io.h b/src/common/io.h
index b1d4035..82fb41e 100644
--- a/src/common/io.h
+++ b/src/common/io.h
@@ -45,15 +45,6 @@ bool safe_recv(int, void *, size_t, int);
 /* Envoie des données au travers un flux réseau. */
 bool safe_send(int, const void *, size_t, int);
 
-/* S'assure qu'un chemin donné existe dans le système. */
-int ensure_path_exists(const char *);
-
-/* Met en place un fichier temporaire. */
-int make_tmp_file(const char *, const char *, char **);
-
-/* Copie un fichier. */
-bool copy_file(const char *, const char *);
-
 
 
 #endif  /* _COMMON_IO_H */
diff --git a/src/common/pathname.c b/src/common/pathname.c
index d6355ed..dd3ec9e 100644
--- a/src/common/pathname.c
+++ b/src/common/pathname.c
@@ -25,13 +25,21 @@
 
 
 #include <assert.h>
+#include <fcntl.h>
 #include <glib.h>
+#include <libgen.h>
 #include <malloc.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 #include <sys/stat.h>
 
 
 #include "extstr.h"
+#include "io.h"
+#include "../core/logs.h"
+//#include "../core/params.h"   // TODO : config
 
 
 
@@ -254,3 +262,174 @@ bool mkpath(const char *path)
     return (ret == 0);
 
 }
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : path = chemin d'accès à valider.                             *
+*                                                                             *
+*  Description : S'assure qu'un chemin donné existe dans le système.          *
+*                                                                             *
+*  Retour      : 0 si le chemin est actuellement présent, -1 sinon.           *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+int ensure_path_exists(const char *path)
+{
+    int result;                             /* Bilan de l'assurance        */
+    char *copy;                             /* Chemin libérable            */
+    char *tmp;                              /* Chemin altérable            */
+
+    copy = strdup(path);
+    tmp = dirname(copy);
+
+    result = access(tmp, W_OK | X_OK);
+
+    if (result != 0)
+    {
+        result = ensure_path_exists(tmp);
+
+        if (result == 0)
+            result = mkdir(tmp, 0700);
+
+    }
+
+    free(copy);
+
+    return result;
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : prefix   = préfixe du nom du fichier temporaire à créer.     *
+*                suffix   = éventuel suffixe à coller au nom de fichier.      *
+*                filename = chemin d'accès complet au nouveau fichier. [OUT]  *
+*                                                                             *
+*  Description : Met en place un fichier temporaire.                          *
+*                                                                             *
+*  Retour      : Flux ouvert en lecture et écriture, ou -1 en cas d'erreur.   *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+#if 0 // TODO
+int make_tmp_file(const char *prefix, const char *suffix, char **filename)
+{
+    int result;                             /* Flux ou code à retourner    */
+    const char *tmpdir;                     /* Répertoire d'accueil        */
+    bool status;                            /* Bilan d'un consultation     */
+    size_t slen;                            /* Taille du suffixe           */
+
+    status = g_generic_config_get_value(get_main_configuration(), MPK_TMPDIR, &tmpdir);
+    if (!status) return -1;
+
+    slen = strlen(suffix);
+
+    if (slen > 0)
+        asprintf(filename, "%s" G_DIR_SEPARATOR_S "%s-%d.XXXXXX.%s", tmpdir, prefix, getpid(), suffix);
+    else
+        asprintf(filename, "%s" G_DIR_SEPARATOR_S "%s-%d.XXXXXX", tmpdir, prefix, getpid());
+
+    result = ensure_path_exists(*filename);
+
+    if (result == 0)
+    {
+        if (slen > 0)
+            result = mkstemps(*filename, slen + 1);
+        else
+            result = mkstemp(*filename);
+
+        if (result == -1) perror("mkstemp");
+
+    }
+
+    if (result == -1)
+    {
+        free(*filename);
+        *filename = NULL;
+    }
+
+    return result;
+
+}
+#endif
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : dest = fichier de destination de la copie.                   *
+*                src  = fichier source à copier.                              *
+*                                                                             *
+*  Description : Copie un fichier.                                            *
+*                                                                             *
+*  Retour      : Bilan de l'opération.                                        *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+bool copy_file(const char *dest, const char *src)
+{
+    bool result;                            /* Bilan à retourner           */
+    int fd;                                 /* Descripteur du fichier      */
+    struct stat info;                       /* Informations sur le fichier */
+    int ret;                                /* Bilan d'un appel            */
+    void *data;                             /* Quantité de données traitées*/
+    bool status;                            /* Bilan de la lecture         */
+
+    result = false;
+
+    /* Côté source */
+
+    fd = open(src, O_RDONLY);
+    if (fd == -1)
+    {
+        LOG_ERROR_N("open");
+        goto exit;
+    }
+
+    ret = fstat(fd, &info);
+    if (ret == -1)
+    {
+        LOG_ERROR_N("fstat");
+        goto done;
+    }
+
+    data = malloc(info.st_size);
+
+    status = safe_read(fd, data, info.st_size);
+    if (!status) goto clean;
+
+    close(fd);
+
+    /* Côté destination */
+
+    fd = open(dest, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
+    if (fd == -1)
+    {
+        LOG_ERROR_N("open");
+        free(data);
+        goto exit;
+    }
+
+    status = safe_write(fd, data, info.st_size);
+    if (!status) goto clean;
+
+    result = true;
+
+ clean:
+
+    free(data);
+
+ done:
+
+    close(fd);
+
+ exit:
+
+    return result;
+
+}
diff --git a/src/common/pathname.h b/src/common/pathname.h
index f7b679e..1b6624c 100644
--- a/src/common/pathname.h
+++ b/src/common/pathname.h
@@ -38,6 +38,17 @@ char *build_absolute_filename(const char *, const char *);
 /* S'assure que le chemin fourni est bien en place. */
 bool mkpath(const char *);
 
+/* S'assure qu'un chemin donné existe dans le système. */
+int ensure_path_exists(const char *);
+
+/* Met en place un fichier temporaire. */
+#if 0 // TODO
+int make_tmp_file(const char *, const char *, char **);
+#endif
+
+/* Copie un fichier. */
+bool copy_file(const char *, const char *);
+
 
 
 #endif  /* _COMMON_PATHNAME_H */
diff --git a/src/core/core.c b/src/core/core.c
index 636e41e..addd58c 100644
--- a/src/core/core.c
+++ b/src/core/core.c
@@ -41,7 +41,7 @@
 #ifdef INCLUDE_MAGIC_SUPPORT
 #   include "../analysis/scan/items/magic/cookie.h"
 #endif
-#include "../common/io.h"
+#include "../common/pathname.h"
 #include "../common/xdg.h"
 #include "../glibext/linesegment.h"
 
-- 
cgit v0.11.2-87-g4458