Move common 'captive-install-*' parts to static 'libcaptive-install'.
authorshort <>
Wed, 3 Sep 2003 14:38:07 +0000 (14:38 +0000)
committershort <>
Wed, 3 Sep 2003 14:38:07 +0000 (14:38 +0000)
src/install/fstab/Makefile.am
src/install/fstab/main.c
src/install/libcaptive-install/Makefile.am [new file with mode: 0644]
src/install/libcaptive-install/proc_partitions.c [new file with mode: 0644]
src/install/libcaptive-install/proc_partitions.h [new file with mode: 0644]

index da72a87..e3656ea 100644 (file)
@@ -20,7 +20,8 @@ include $(top_srcdir)/Makefile-head.am
 
 captive_install_fstab_SOURCES= \
                main.c
-captive_install_fstab_LDADD=$(LIBNTFS_LIBS) $(POPT_LIBS)
+captive_install_fstab_LDADD=../libcaptive-install/libcaptive-install.a
+captive_install_fstab_LDADD+=$(LIBNTFS_LIBS) $(POPT_LIBS)
 
 if HAVE_LIBNTFS
 captive_install_fstab_cond=captive-install-fstab
index 4563fe3..76ddbdf 100644 (file)
@@ -36,6 +36,7 @@
 #include <sys/stat.h>
 #include <errno.h>
 #include <unistd.h>
+#include "../libcaptive-install/proc_partitions.h"
 
 #include <captive/macros.h>
 
@@ -82,66 +83,6 @@ static const struct poptOption popt_table[]={
                };
 
 
-static void devices_hash_key_destroy_func(gchar *device)
-{
-       g_return_if_fail(device!=NULL);
-
-       g_free(device);
-}
-
-static void devices_hash_value_destroy_func(gchar *vol_name)
-{
-       g_return_if_fail(vol_name!=NULL);
-
-       g_free(vol_name);
-}
-
-
-static GHashTable *devices_hash_get(void)
-{
-FILE *fpartitions;
-gchar line[LINE_MAX];
-gint lineno;
-/* map: (gchar *)device -> (gchar *)vol_name */
-GHashTable *devices_hash;
-
-       devices_hash=g_hash_table_new_full(g_str_hash,g_str_equal,
-                       (GDestroyNotify)devices_hash_key_destroy_func,
-                       (GDestroyNotify)devices_hash_value_destroy_func);
-
-       if (!(fpartitions=fopen(FILENAME_PROC_PARTITIONS,"r")))
-               g_error(_("Cannot open \"%s\": %m"),FILENAME_PROC_PARTITIONS);
-       lineno=0;
-       while (fgets(line,sizeof(line),fpartitions)) {
-unsigned major,minor;
-unsigned long long blocks;
-char device[strlen("/dev/")+100];
-ntfs_volume *volume;
-
-               lineno++;
-               if (lineno<=2)
-                       continue;
-               if (4!=sscanf(line,"%u%u%llu%100s",&major,&minor,&blocks,device+strlen("/dev/")))
-                       g_error(_("Error parsing line of \"%s\": %s"),FILENAME_PROC_PARTITIONS,line);
-               memcpy(device,"/dev/",strlen("/dev/"));
-               if (!(volume=ntfs_mount(device,MS_RDONLY))) {
-                       if (optarg_verbose)
-                               g_message(_("not ntfs: %s"),device);
-                       continue;
-                       }
-               if (optarg_verbose)
-                       g_message(_("FOUND ntfs: %s\t%s"),device,volume->vol_name);
-               g_hash_table_insert(devices_hash,g_strdup(device),g_strdup(volume->vol_name));
-               if (ntfs_umount(volume,
-                               TRUE))  /* force; close even if it would mean data loss */
-                       g_warning(_("Error unmounting volume: %s"),device);
-               }
-       if (fclose(fpartitions))
-               g_error(_("Cannot close \"%s\": %m"),FILENAME_PROC_PARTITIONS);
-       return devices_hash;
-}
-
-
 /* map: (gchar *)dir -> (gpointer)!NULL */
 static GHashTable *dirs_used_hash;
 
@@ -153,7 +94,8 @@ static void dirs_used_hash_key_destroy_func(gchar *dir)
 }
 
 
-static void mntent_add_devices_hash_entry(const gchar *device,const gchar *vol_name,FILE *mntentfilew /* user_data */)
+static void mntent_add_proc_partitions_ntfs_hash_entry
+               (const gchar *device,const gchar *vol_name,FILE *mntentfilew /* user_data */)
 {
 struct mntent mntent_local;
 gint dir_count;
@@ -247,12 +189,12 @@ gboolean modified=FALSE;
        switch (optarg_mode) {
 
                case OPTARG_MODE_ADD: {
-GHashTable *devices_hash;
+GHashTable *proc_partitions_ntfs_hash;
 struct mntent *mntent;
 
-                       devices_hash=devices_hash_get();
+                       proc_partitions_ntfs_hash=proc_partitions_ntfs_hash_get(optarg_verbose);
                        while ((mntent=getmntent(mntentfiler))) {
-                               if (!g_hash_table_lookup(devices_hash,mntent->mnt_fsname)) {
+                               if (!g_hash_table_lookup(proc_partitions_ntfs_hash,mntent->mnt_fsname)) {
                                        if (!strcmp(mntent->mnt_type,"captive-ntfs")
                                                        && !strncmp(mntent->mnt_fsname,"/dev/",strlen("/dev/"))) {
                                                g_warning(_("Dropping no-longer valid captive filesystem mntent from \"%s\" of device: %s"),
@@ -271,12 +213,12 @@ struct mntent *mntent;
                                        modified=TRUE;
                                        }
                                }
-                       if (g_hash_table_size(devices_hash))
+                       if (g_hash_table_size(proc_partitions_ntfs_hash))
                                modified=TRUE;
-                       g_hash_table_foreach(devices_hash,
-                                       (GHFunc)mntent_add_devices_hash_entry,  /* func */
+                       g_hash_table_foreach(proc_partitions_ntfs_hash,
+                                       (GHFunc)mntent_add_proc_partitions_ntfs_hash_entry,     /* func */
                                        mntentfilew);   /* user_data */
-                       g_hash_table_destroy(devices_hash);
+                       g_hash_table_destroy(proc_partitions_ntfs_hash);
                        } break;
 
                case OPTARG_MODE_REMOVE: {
diff --git a/src/install/libcaptive-install/Makefile.am b/src/install/libcaptive-install/Makefile.am
new file mode 100644 (file)
index 0000000..89b186f
--- /dev/null
@@ -0,0 +1,28 @@
+# $Id$
+# automake source for the libcaptive-install Makefile 
+# Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+# 
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; exactly version 2 of June 1991 is required
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+
+include $(top_srcdir)/Makefile-head.am
+
+libcaptive_install_a_SOURCES= \
+               proc_partitions.c \
+               proc_partitions.h
+
+if HAVE_LIBNTFS
+libcaptive_install_a_cond=libcaptive-install.a
+endif
+noinst_LIBRARIES=$(libcaptive_install_a_cond)
diff --git a/src/install/libcaptive-install/proc_partitions.c b/src/install/libcaptive-install/proc_partitions.c
new file mode 100644 (file)
index 0000000..59a9a80
--- /dev/null
@@ -0,0 +1,107 @@
+/* $Id$
+ * /proc/partitions file reader for installers of captive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; exactly version 2 of June 1991 is required
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+
+#include "config.h"
+
+#undef FALSE
+#undef TRUE
+#include <ntfs/types.h>        /* for 'FALSE'/'TRUE' libntfs definition */
+#define FALSE FALSE
+#define TRUE TRUE
+
+#include "proc_partitions.h"   /* self */
+#include <glib/gmessages.h>
+#include <popt.h>
+#include <locale.h>
+#include <stdlib.h>
+#include <string.h>
+#include <mntent.h>
+#include <glib/ghash.h>
+#include <glib/gstrfuncs.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include <captive/macros.h>
+
+#include <ntfs/volume.h>
+
+
+/* Config: */
+#define FILENAME_PROC_PARTITIONS  "/proc/partitions"
+
+
+static void proc_partitions_ntfs_hash_key_destroy_func(gchar *device)
+{
+       g_return_if_fail(device!=NULL);
+
+       g_free(device);
+}
+
+static void proc_partitions_ntfs_hash_value_destroy_func(gchar *vol_name)
+{
+       g_return_if_fail(vol_name!=NULL);
+
+       g_free(vol_name);
+}
+
+
+GHashTable *proc_partitions_ntfs_hash_get(gboolean msg_verbose)
+{
+FILE *fpartitions;
+gchar line[LINE_MAX];
+gint lineno;
+/* map: (gchar *)device -> (gchar *)vol_name */
+GHashTable *proc_partitions_ntfs_hash;
+
+       proc_partitions_ntfs_hash=g_hash_table_new_full(g_str_hash,g_str_equal,
+                       (GDestroyNotify)proc_partitions_ntfs_hash_key_destroy_func,
+                       (GDestroyNotify)proc_partitions_ntfs_hash_value_destroy_func);
+
+       if (!(fpartitions=fopen(FILENAME_PROC_PARTITIONS,"r")))
+               g_error(_("Cannot open \"%s\": %m"),FILENAME_PROC_PARTITIONS);
+       lineno=0;
+       while (fgets(line,sizeof(line),fpartitions)) {
+unsigned major,minor;
+unsigned long long blocks;
+char device[strlen("/dev/")+100];
+ntfs_volume *volume;
+
+               lineno++;
+               if (lineno<=2)
+                       continue;
+               if (4!=sscanf(line,"%u%u%llu%100s",&major,&minor,&blocks,device+strlen("/dev/")))
+                       g_error(_("Error parsing line of \"%s\": %s"),FILENAME_PROC_PARTITIONS,line);
+               memcpy(device,"/dev/",strlen("/dev/"));
+               if (!(volume=ntfs_mount(device,MS_RDONLY))) {
+                       if (msg_verbose)
+                               g_message(_("not ntfs: %s"),device);
+                       continue;
+                       }
+               if (msg_verbose)
+                       g_message(_("FOUND ntfs: %s\t%s"),device,volume->vol_name);
+               g_hash_table_insert(proc_partitions_ntfs_hash,g_strdup(device),g_strdup(volume->vol_name));
+               if (ntfs_umount(volume,
+                               TRUE))  /* force; close even if it would mean data loss */
+                       g_warning(_("Error unmounting volume: %s"),device);
+               }
+       if (fclose(fpartitions))
+               g_error(_("Cannot close \"%s\": %m"),FILENAME_PROC_PARTITIONS);
+       return proc_partitions_ntfs_hash;
+}
diff --git a/src/install/libcaptive-install/proc_partitions.h b/src/install/libcaptive-install/proc_partitions.h
new file mode 100644 (file)
index 0000000..63e1daf
--- /dev/null
@@ -0,0 +1,36 @@
+/* $Id$
+ * Include file for /proc/partitions file reader for installers of captive
+ * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; exactly version 2 of June 1991 is required
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+
+#ifndef _CAPTIVE_PROC_PARTITIONS_USECOUNT_H
+#define _CAPTIVE_PROC_PARTITIONS_USECOUNT_H 1
+
+
+#include <glib/gtypes.h>
+#include <glib/ghash.h>
+
+
+G_BEGIN_DECLS
+
+/* map: (gchar *)device -> (gchar *)vol_name */
+GHashTable *proc_partitions_ntfs_hash_get(gboolean msg_verbose);
+
+G_END_DECLS
+
+
+#endif /* _CAPTIVE_PROC_PARTITIONS_USECOUNT_H */