bug-parted
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[patch] ped_device_from_store


From: Neal H Walfield
Subject: [patch] ped_device_from_store
Date: Tue, 21 Aug 2001 01:23:23 +0200
User-agent: Mutt/1.3.18i

Here is an implementation of ped_device_new_from_store.  I have drawn
the following conclusions, which I find quite reasonable:

        o ped_device_{open,close} drop local resources, i.e. those that
          can be easily recreated given a few parameters.  For instance,
          we can easily get another file descriptor if we have the path
          name.
        
        o ped_device_{new,destroy}: Drop all resources owned by
          libparted.  This frees everything that libparted owns including
          the buffer holding the filename.  We cannot reopen this
          without the help of parted (i.e. the user of libparted).

Correct me if I am wrong.  And if you think that these are right, please
formalize them in doc/API.

Here is a change log:

2001-08-21  Neal H Walfield  <address@hidden>

        * include/parted/device_gnu.h (_GNUSpecific): New member,
        consume.
        (ped_device_new_from_store): Prototype it.
        * libparted/device_gnu.c (_init_device): General PedDevice
        initializer ripped from _arch_device_new.
        (_arch_device_new): Use _init_device.

        (_done_device): General PedDevice destructor, ripped from
        _arch_device_destory.
        (_arch_device_destory): Reimplemented using _done_device.

        (_arch_device_open): Functionality moved from here . . .
        (_arch_device_new): . . . here.

        (ped_device_new_from_store): New function.

        (_arch_device_close): Do not close the store here.
        (_arch_device_destory): Close it here if ARCH_SPECIFIC->consume
        is true.


diff -uprN parted-1.5.4-pre2.orig/include/parted/device_gnu.h 
parted-1.5.4-pre2/include/parted/device_gnu.h
--- parted-1.5.4-pre2.orig/include/parted/device_gnu.h  Sun Jul 29 02:38:43 2001
+++ parted-1.5.4-pre2/include/parted/device_gnu.h       Mon Aug 20 23:35:16 2001
@@ -28,7 +28,13 @@ typedef      struct _GNUSpecific     GNUSpecific;
 
 struct _GNUSpecific {
        struct store*   store;
+       int consume;
 };
+
+/* Initialize a PedDevice useing SOURCE.  The device store will be destoyed
+   when ped_device_destory is called if CONSUME is true.  This device is
+   not registered in Parted's list of devices.  */
+PedDevice* ped_device_new_from_store (struct store *source, int consume);
 
 #endif /* PED_DEVICE_GNU_H_INCLUDED */
 
diff -uprN parted-1.5.4-pre2.orig/libparted/device_gnu.c 
parted-1.5.4-pre2/libparted/device_gnu.c
--- parted-1.5.4-pre2.orig/libparted/device_gnu.c       Mon Aug 20 06:24:46 2001
+++ parted-1.5.4-pre2/libparted/device_gnu.c    Tue Aug 21 00:49:45 2001
@@ -135,12 +135,25 @@ error:
        return 0;
 }
 
-PedDevice*
-_arch_device_new (const char* path)
+static void
+_flush_cache (PedDevice* dev)
 {
-       PedDevice*      dev;
+       GNUSpecific*    arch_specific = GNU_SPECIFIC (dev);
 
-       PED_ASSERT (path != NULL, return NULL);
+       if (dev->read_only)
+               return;
+
+       /* Wait for a complete sync to finish.  */
+       file_sync (arch_specific->store->source, 1, 0);
+}
+
+/* Initialize by allocating memory and filling in a few defaults, a
+   PedDevice structure.  */
+static PedDevice*
+_init_device (const char *path)
+{
+       PedDevice *dev;
+       GNUSpecific*    arch_specific;
 
        dev = (PedDevice*) ped_malloc (sizeof (PedDevice));
        if (!dev)
@@ -164,9 +177,6 @@ _arch_device_new (const char* path)
        dev->geom_known = 1;
        dev->geom_already_guessed = 0;
 
-       if (!init_file (dev))
-               goto error_free_dev;
-
        return dev;
 
 error_free_arch_specific:
@@ -179,14 +189,6 @@ error:
        return NULL;
 }
 
-void
-_arch_device_destroy (PedDevice* dev)
-{
-       ped_free (dev->arch_specific);
-       ped_free (dev->path);
-       ped_free (dev);
-}
-
 static int
 _kernel_reread_part_table (PedDevice* dev)
 {
@@ -194,28 +196,47 @@ _kernel_reread_part_table (PedDevice* de
        return 1;
 }
 
+/* Free the memory associated with a PedDevice structure.  */
 static void
-_flush_cache (PedDevice* dev)
+_done_device (PedDevice *dev)
+{
+        ped_free (dev->arch_specific);
+       ped_free (dev->path);
+       ped_free (dev);
+}
+
+/* Release all resources that libparted owns in DEV.  */
+void
+_arch_device_destroy (PedDevice* dev)
 {
        GNUSpecific*    arch_specific = GNU_SPECIFIC (dev);
 
-       if (dev->read_only)
-               return;
+       if (arch_specific->consume)
+               store_free (arch_specific->store);
 
-       /* Wait for a complete sync to finish.  */
-       file_sync (arch_specific->store->source, 1, 0);
+       _done_device (dev);
 }
 
-int
-_arch_device_open (PedDevice* dev)
+PedDevice*
+_arch_device_new (const char* path)
 {
-       GNUSpecific*    arch_specific = GNU_SPECIFIC (dev);
-       error_t err, rw_err;
+       PedDevice*      dev;
+       GNUSpecific*    arch_specific;
+       error_t err;
+
+       PED_ASSERT (path != NULL, return NULL);
 
-retry:
+       dev = _init_device (path);
+       if (!dev)
+               return NULL;
+
+       arch_specific = GNU_SPECIFIC (dev);
+       arch_specific->consume = 1;
+
+ retry_open:
        err = store_typed_open (dev->path, 0, NULL, &arch_specific->store);
        if (err) {
-               rw_err = err;
+               error_t rw_err = err;
 
                err = store_typed_open (dev->path, STORE_READONLY, NULL,
                                        &arch_specific->store);
@@ -226,9 +247,9 @@ retry:
                                _("Error opening %s: %s"),
                                dev->path, strerror (err))
                                        != PED_EXCEPTION_RETRY) {
-                               return 0;
+                               return NULL;
                        } else
-                               goto retry;
+                               goto retry_open;
                } else {
                        ped_exception_throw (
                                PED_EXCEPTION_WARNING,
@@ -244,7 +265,44 @@ retry:
 
        _flush_cache (dev);
 
-       return 1;
+       if (!init_file (dev)) {
+               _arch_device_destroy(dev);
+               return NULL;
+       }
+
+       return dev;
+}
+
+PedDevice*
+ped_device_new_from_store (struct store *source, int consume)
+{
+        PedDevice*      dev;
+       GNUSpecific*    arch_specific;
+
+       PED_ASSERT (source != NULL, return NULL);
+
+       dev = _init_device (source->name ?: "(unknown)");
+       if (!dev)
+               return NULL;
+
+       arch_specific = GNU_SPECIFIC (dev);
+       arch_specific->store = source;
+       arch_specific->consume = consume;
+
+       dev->read_only = source->flags & (STORE_READONLY|STORE_HARD_READONLY);
+
+       if (!init_file (dev)) {
+               _done_device (dev);
+               return NULL;
+       }
+
+       return dev;
+}
+
+int
+_arch_device_open (PedDevice* dev)
+{
+        return 1;
 }
 
 int
@@ -264,8 +322,6 @@ _arch_device_close (PedDevice* dev)
                if (_kernel_reread_part_table (dev))
                        dev->dirty = 0;
        }
-
-       store_free (arch_specific->store);
 
 #if 0
        if (dev->dirty && dev->boot_dirty && dev->type != PED_DEVICE_FILE) {

Attachment: pgpgr6NPkFGPg.pgp
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]