Commits

jjacky committed 08f0888

when creating local copy of dbs, it would fail if there were folders (in sync/); fixed (only copy files now)
added basic command-line parser, and option -d/--debug to enable debug mode

Comments (0)

Files changed (5)

 }
 
 static gboolean
-create_local_db (const gchar *dbpath, gchar **newpath, GError **error)
+create_local_db (const gchar *_dbpath, gchar **newpath, GError **error)
 {
     gchar    buf[MAX_PATH];
     gchar    buf2[MAX_PATH];
+    gchar   *dbpath;
+    size_t   l;
     gchar   *folder;
     GDir    *dir;
     
     }
     debug ("created tmp folder %s", folder);
     
+    /* dbpath will not be slash-terminated */
+    dbpath = strdup (_dbpath);
+    l = strlen (dbpath) - 1;
+    if (dbpath[l] == '/')
+    {
+        dbpath[l] = '\0';
+    }
+    
     /* symlink local */
     snprintf (buf, MAX_PATH - 1, "%s/local", dbpath);
     snprintf (buf2, MAX_PATH - 1, "%s/local", folder);
     while ((file = g_dir_read_name (dir)))
     {
         snprintf (buf, MAX_PATH - 1, "%s/sync/%s", dbpath, file);
-        snprintf (buf2, MAX_PATH - 1, "%s/sync/%s", folder, file);
-        if (!copy_file (buf, buf2))
+        /* stat so we copy files only. also, we need to preserve modified date,
+         * used to determine if DBs are up to date or not by libalpm */
+        if (0 == stat (buf, &filestat))
         {
-            g_set_error (error, KALU_ERROR, 1, "Copy failed for %s", buf);
-            g_dir_close (dir);
-            goto error;
+            if (S_ISREG (filestat.st_mode))
+            {
+                snprintf (buf2, MAX_PATH - 1, "%s/sync/%s", folder, file);
+                if (!copy_file (buf, buf2))
+                {
+                    g_set_error (error, KALU_ERROR, 1, "Copy failed for %s", buf);
+                    g_dir_close (dir);
+                    goto error;
+                }
+                /* preserve time */
+                times.actime = filestat.st_atime;
+                times.modtime = filestat.st_mtime;
+                if (0 != utime (buf2, &times))
+                {
+                    /* sucks, but no fail, we'll just have to download this db */
+                    debug ("Unable to change time of %s", buf2);
+                }
+                else
+                {
+                    debug ("updated time for %s", buf2);
+                }
+            }
+            else
+            {
+                debug ("ignoring non-regular file: %s", buf);
+            }
         }
-        /* we need to preserve modified date, used to determine if DBs are
-         * up to date or not by libalpm */
-        if (0 != stat (buf, &filestat))
+        else
         {
             g_set_error (error, KALU_ERROR, 1, "Unable to stat %s\n", buf);
             g_dir_close (dir);
             goto error;
         }
-        else
-        {
-            times.actime = filestat.st_atime;
-            times.modtime = filestat.st_mtime;
-            if (0 != utime (buf2, &times))
-            {
-                /* sucks, but no fail, we'll just have to download this db */
-                debug ("Unable to change time of %s", buf2);
-            }
-            else
-            {
-                debug ("updated time for %s", buf2);
-            }
-        }
     }
     g_dir_close (dir);
+    free (dbpath);
     
     *newpath = folder;
     return TRUE;
 
 error:
+    free (dbpath);
     g_free (folder);
     return FALSE;
 }
 
 #define _UNUSED_            __attribute__ ((unused)) 
 
-#define KALU_VERSION       "0.0.2"
+#define KALU_VERSION       "0.0.3"
+#define KALU_TAG            "Keeping Arch Linux Up-to-date"
 
 #define MAX_PATH            255
 
                 p = NULL;                                                   \
             } while(0)
 
-#define debug(...)     
-#define _debug(...)     do {                                         \
-                                fprintf (stdout, "[DEBUG] ");       \
-                                fprintf (stdout, __VA_ARGS__);      \
-                                fprintf (stdout, "\n");             \
-                          } while (0)
-
 typedef enum {
     UPGRADE_NO_ACTION = 0,
     UPGRADE_ACTION_KALU,
 } templates_t;
 
 typedef struct _config_t {
+    gboolean         is_debug;
     unsigned int     verbose;
     unsigned int     verbose_watched;
     unsigned int     verbose_aur;
 /* global variable */
 extern config_t *config;
 
+void
+debug (const char *fmt, ...);
 
 void free_package (kalu_package_t *package);
 void free_watched_package (watched_package_t *w_pkg);
 
 /* C */
 #include <string.h>
+#include <time.h> /* for debug() */
 
 /* gtk */
 #include <gtk/gtk.h>
     }
 }
 
+void
+debug (const char *fmt, ...)
+{
+    va_list    args;
+    time_t     now;
+    struct tm *ptr;
+    char       buf[10];
+    
+    if (!config->is_debug)
+    {
+        return;
+    }
+    
+    now = time (NULL);
+    ptr = localtime (&now);
+    strftime (buf, 10, "%H:%M:%S", ptr);
+    fprintf (stdout, "[%s] ", buf);
+    
+    va_start (args, fmt);
+    vfprintf (stdout, fmt, args);
+    va_end (args);
+    
+    fprintf (stdout, "\n");
+}
+
 int
 main (int argc, char *argv[])
 {
     gtk_init (&argc, &argv);
     config = calloc (1, sizeof(*config));
     
+    /* parse command line -- very basic stuff */
+    if (argc > 1)
+    {
+        if (strcmp (argv[1], "-h") == 0 || strcmp (argv[1], "--help") == 0)
+        {
+            printf ("kalu - " KALU_TAG " v" KALU_VERSION "\n\n");
+            printf (" -h, --help        Show this help screen and exit\n");
+            printf (" -V, --version     Show version information and exit\n");
+            printf (" -d, --debug       Enable debug mode\n");
+            printf ("\nFor more, please refer to the man page: man kalu\n");
+            return 0;
+        }
+        else if (strcmp (argv[1], "-V") == 0 || strcmp (argv[1], "--version") == 0)
+        {
+            printf ("kalu - " KALU_TAG " v" KALU_VERSION "\n");
+            printf ("Copyright (C) 2012 Olivier Brunel\n");
+            printf ("License GPLv3+: GNU GPL version 3 or later"
+                    " <http://gnu.org/licenses/gpl.html>\n");
+            printf ("This is free software: you are free to change and redistribute it.\n");
+            printf ("There is NO WARRANTY, to the extent permitted by law.\n");
+            return 0;
+        }
+        else if (strcmp (argv[1], "-d") == 0 || strcmp (argv[1], "--debug") == 0)
+        {
+            config->is_debug = TRUE;
+            debug ("debug mode enabled");
+        }
+    }
+    
     /* defaults -- undefined "sub"templates will use the corresponding main ones */
     /* (e.g. tpl_sep_watched_verbose defaults to tpl_sep_verbose) */
     config->pacmanconf = strdup ("/etc/pacman.conf");
     gchar question[255];
     snprintf (question, 255,
         "There are %d providers available for %s.\nPlease select the one to install :",
-        alpm_list_count (providers), pkg);
+        (int) alpm_list_count (providers), pkg);
     add_log (LOGTYPE_INFO, "%s\n", question);
     
     /* dialog */
     add_db_t *add_db = calloc (1, sizeof (*add_db));
     add_db->pac_conf = pac_conf;
     add_db->pctg = 0.42;
-    add_db->inc = 0.58 / alpm_list_count (pac_conf->databases);
+    add_db->inc = 0.58 / (int) alpm_list_count (pac_conf->databases);
     add_db->i = NULL;
     
     updater_add_db_cb (kupdater, NULL, add_db);
 gboolean
 ensure_path (char *path)
 {
-    char *s, *p, org;
+    char *s, *p;
     struct stat stat_info;
     
     p = path + 1; /* skip the root */
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.