Make user.c independent of command line options.
[adu.git] / create.c
index 1ce2e54..2925d57 100644 (file)
--- a/create.c
+++ b/create.c
@@ -4,51 +4,21 @@
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
-/** \file create.c The create mode of adu. */
+/** \file create.c \brief The create mode of adu. */
 
 #include <dirent.h> /* readdir() */
+#include "format.h"
 #include "adu.h"
 #include "gcc-compat.h"
 #include "cmdline.h"
 #include "fd.h"
 #include "string.h"
 #include "error.h"
-#include "portable_io.h"
+#include "user.h"
 
 /* Id of the device containing the base dir. */
 static dev_t device_id;
 
-static int write_uid(struct user_info *ui, void *data)
-{
-       char **p = data;
-
-       write_u32(*p, ui->uid);
-       *p += sizeof(uint32_t);
-       return 1;
-}
-
-static int write_uid_list(void)
-{
-       char *buf, *p, *filename;
-       uint32_t count = 0;
-       struct user_info *ui;
-       size_t size = num_uids * sizeof(uint32_t);
-       int ret;
-
-       if (!num_uids)
-               return 0;
-       buf = p = adu_malloc(size);
-       ret = for_each_admissible_user(write_uid, &p);
-       if (ret < 0)
-               goto out;
-       filename = get_uid_list_name();
-       ret = adu_write_file(filename, buf, size);
-       free(filename);
-out:
-       free(buf);
-       return ret;
-}
-
 static int add_directory(char *dirname, uint64_t *dir_num, uint64_t *parent_dir_num,
                uint64_t *dir_size, uint64_t *dir_files)
 {
@@ -88,9 +58,7 @@ static int update_user_row(struct osl_table *t, uint64_t dir_num,
                objects[UT_BYTES].size = sizeof(*add);
                objects[UT_FILES].data = &num_files;
                objects[UT_FILES].size = sizeof(num_files);
-               INFO_LOG("######################### ret: %d\n", ret);
                ret = osl(osl_add_row(t, objects));
-               INFO_LOG("######################### ret: %d\n", ret);
                return ret;
        } else { /* add size and increment file count */
                uint64_t num;
@@ -117,10 +85,13 @@ static int scan_dir(char *dirname, uint64_t *parent_dir_num)
        struct dirent *entry;
        int ret, cwd_fd, ret2;
        uint64_t dir_size = 0, dir_files = 0;
-       uint64_t this_dir_num = ++num_dirs;
+       /* dir count. */
+       static uint64_t current_dir_num;
+
+       uint64_t this_dir_num = ++current_dir_num;
 
        check_signals();
-       DEBUG_LOG("----------------- %llu: %s\n", (long long unsigned)num_dirs, dirname);
+       DEBUG_LOG("----------------- %llu: %s\n", (long long unsigned)current_dir_num, dirname);
        ret = adu_opendir(dirname, &dir, &cwd_fd);
        if (ret < 0) {
                if (ret != -ERRNO_TO_ERROR(EACCES))
@@ -130,7 +101,7 @@ static int scan_dir(char *dirname, uint64_t *parent_dir_num)
        }
        while ((entry = readdir(dir))) {
                mode_t m;
-               struct stat s;
+               struct stat64 s;
                uint32_t uid;
                uint64_t size;
                struct user_info *ui;
@@ -139,9 +110,9 @@ static int scan_dir(char *dirname, uint64_t *parent_dir_num)
                        continue;
                if (!strcmp(entry->d_name, ".."))
                        continue;
-               if (lstat(entry->d_name, &s) == -1) {
-                       WARNING_LOG("lstat error for %s/%s\n", dirname,
-                               entry->d_name);
+               if (lstat64(entry->d_name, &s) == -1) {
+                       WARNING_LOG("lstat64 error for %s/%s (%s)\n",
+                               dirname, entry->d_name, strerror(errno));
                        continue;
                }
                m = s.st_mode;
@@ -158,15 +129,11 @@ static int scan_dir(char *dirname, uint64_t *parent_dir_num)
                /* regular file */
                size = s.st_size;
                dir_size += size;
-               num_bytes += size;
                dir_files++;
-               num_files++;
                uid = s.st_uid;
-               ret = search_uid(uid, CREATE_USER_TABLE | OPEN_USER_TABLE, &ui);
+               ret = create_user_table(conf.database_dir_arg, uid, &ui);
                if (ret < 0)
                        goto out;
-               ui->bytes += size;
-               ui->files++;
                ret = update_user_row(ui->table, this_dir_num, &size);
                if (ret < 0)
                        goto out;
@@ -182,7 +149,12 @@ out:
        return ret;
 }
 
-int com_create()
+/**
+ * The main function of the create mode.
+ *
+ * \return Standard.
+ */
+int com_create(void)
 {
        uint64_t zero = 0ULL;
        int ret;
@@ -201,8 +173,7 @@ int com_create()
        ret = scan_dir(conf.base_dir_arg, &zero);
        if (ret < 0)
                goto out;
-       ret = write_uid_list();
+       ret = write_uid_file(conf.database_dir_arg);
 out:
-       close_all_tables();
        return ret;
 }