add query-files
authorKlaus Kaempf <kkaempf@suse.de>
Fri, 3 Feb 2006 20:55:07 +0000 (20:55 +0000)
committerKlaus Kaempf <kkaempf@suse.de>
Fri, 3 Feb 2006 20:55:07 +0000 (20:55 +0000)
zmd/backend/Makefile.am
zmd/backend/query-files.cc [new file with mode: 0644]
zmd/backend/resolvable-writer.h

index d0e920d..548ac49 100644 (file)
@@ -15,3 +15,10 @@ query_system_SOURCES = query-system.cc $(RESOLVABLE_WRITER)
 query_system_CFLAGS = $(PKG_MANAGEMENT_CFLAGS) $(SQLITE_CFLAGS)
 query_system_LDADD = $(PKG_MANAGEMENT_LIBS) $(SQLITE_LIBS)
 
+query_filesdir = $(libdir)/zmd
+query_files_PROGRAMS = query-files
+query_files_SOURCES = query-files.cc $(RESOLVABLE_WRITER)
+query_files_CFLAGS = $(PKG_MANAGEMENT_CFLAGS) $(SQLITE_CFLAGS)
+query_files_LDADD = $(PKG_MANAGEMENT_LIBS) $(SQLITE_LIBS)
+
+
diff --git a/zmd/backend/query-files.cc b/zmd/backend/query-files.cc
new file mode 100644 (file)
index 0000000..4f9b00f
--- /dev/null
@@ -0,0 +1,171 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
+
+#include <list>
+
+#include "zypp/ZYpp.h"
+#include "zypp/ZYppFactory.h"
+#include "zypp/base/Logger.h"
+#include "zypp/base/Exception.h"
+
+#include "zypp/Target.h"
+
+using std::endl;
+using namespace zypp;
+
+#include <sqlite3.h>
+#include <cstring>
+
+#include <sys/stat.h>
+
+#include "resolvable-writer.h"
+
+static ResolvableList
+query_file (Target_Ptr target, const char *path)
+{
+    ResolvableList resolvables;
+
+    Resolvable::constPtr resolvable = target->whoOwnsFile (path);
+    if (resolvable != NULL) {
+       resolvables.push_back (resolvable);
+    }
+
+    return resolvables;
+}
+
+static char *
+extract_value (char *token)
+{
+    char *eq, *value;
+
+    eq = strchr (token, '=');
+
+    if (!eq)
+       return NULL;
+
+    while (*(++eq) == ' ');    /* Move past the equals sign and strip leading whitespace */
+
+    return strdup (value);
+}
+
+static void
+parse_query (const char *query, bool *recursive)
+{
+#if 0
+    char **tokens, **t;
+
+    tokens = g_strsplit (query, ";", 0);
+
+    for (t = tokens; t && *t; t++) {
+       if (g_strncasecmp (*t, "recursive", 9) == 0) {
+           char *tmp = extract_value (*t);
+
+           if (atoi (tmp))
+               *recursive = TRUE;
+
+           free (tmp);
+       }
+
+       /* Ignore unknown parts */
+    }
+
+    g_strfreev (tokens);
+#endif
+}
+
+static ResolvableList
+query_directory (Target_Ptr target, const char *path, bool recursive)
+{
+    ResolvableList resolvables;
+#warning Unclear semantics
+#if 0
+    rc_extract_packages_from_directory (path, channel,
+                                       rc_packman_get_global (),
+                                       recursive,
+                                       prepend_package,
+                                       &packages);
+#endif
+    return resolvables;
+}
+
+//----------------------------------------------------------------------------
+
+static ResolvableList
+query (Target_Ptr target, const char *uri, const char *channel_id)
+{
+    char *query_part;
+    char *path = NULL;
+    ResolvableList resolvables;
+
+    /* The magic 7 is strlen ("file://") */
+
+    if (strncasecmp (uri, "file://", 7) != 0) {
+       ERR << "Invalid uri '" << uri << "'" << endl;
+       return resolvables;
+    }
+
+    /* Find the path. The "+ 7" part moves past "file://" */
+    query_part = strchr (uri + 7, '?');
+
+    if (query_part)
+       path = strndup (uri + 7, query_part - uri - 7);
+    else
+       path = strdup (uri + 7);
+
+#if 0
+    channel = rc_channel_new (channel_id != NULL ? channel_id : "@local",
+                             "foo", "foo", "foo");
+#endif
+
+    struct stat buf;
+
+    int err = stat (path, &buf);
+
+    if (err != 0) {
+       ERR << "Invalid path '" << path << "'" << endl;
+    }
+    else if (S_ISREG(buf.st_mode)) {
+       /* Single file */
+       resolvables = query_file (target, path);
+    } else if (S_ISDIR(buf.st_mode)) {
+       /* Directory */
+       bool recursive = false;
+
+       if (query_part)
+           /* + 1 to move past the "?" */
+           parse_query (query_part + 1, &recursive);
+
+       resolvables = query_directory (target, path, recursive);
+    }
+
+    return resolvables;
+}
+
+//----------------------------------------------------------------------------
+
+int
+main (int argc, char **argv)
+{
+    ZYppFactory zf;
+    ZYpp::Ptr God = zf.letsTest();
+
+    try {
+       God->initTarget("/");
+    }
+    catch (Exception & excpt_r) {
+       ZYPP_CAUGHT (excpt_r);
+       ERR << "Couldn't access the packaging system" << endl;
+       return 1;
+    }
+
+    if (argc < 3 || argc > 4) {
+       ERR << "usage: " << argv[0] << " <database> <uri> [catalog id]" << endl;
+       return 1;
+    }
+
+    ResolvableList resolvables = query (God->target(), argv[2], argc == 4 ? argv[3] : NULL);
+    if (!resolvables.empty()) {
+       write_resolvables_to_db (argv[1], resolvables, true);
+    }
+
+    return 0;
+}
index 30e62c6..ddefd5f 100644 (file)
@@ -5,8 +5,7 @@
 #include "zypp/ResStore.h"
 #include "zypp/Resolvable.h"
 
-typedef std::list<const zypp::Resolvable::constPtr> ResolvableList;
+typedef std::list<zypp::Resolvable::constPtr> ResolvableList;
 
 void write_store_to_db (const std::string & db_file, const zypp::ResStore & resolvables, bool is_installed);
 void write_resolvables_to_db (const std::string & db_file, const ResolvableList & resolvables, bool is_installed);
-