Whamcloud - gitweb
Roto-till and clean up libblkid API.
authorTheodore Ts'o <tytso@mit.edu>
Mon, 27 Jan 2003 06:09:24 +0000 (01:09 -0500)
committerTheodore Ts'o <tytso@mit.edu>
Mon, 27 Jan 2003 06:09:24 +0000 (01:09 -0500)
read.c (parse_tag): Do not return that blkid_tag when parsing
the blkid.tag file.

resolve.c (blkid_get_token, blkid_get_tagname_devname): Fold in
code from removed functions

tag.c (blkid_create_tag): Don't return the newly tag strcture
any more, as it's not needed.
(blkid_find_tag_cache, blkid_get_tag_cache,
 blkid_token_to_tag, blkid_find_tv_tags): Remove these
functions, as they are either only used once or have
interfaces that should be deprecated.
(blkid_find_tag_dev, blkid_find_head_cache): Replace use
of blkid_tag with one or two const char * type/value
arguments.
(blkid_find_dev_with_tag): Fold in code from removed functions

probe.h, probe.c: Use a more sophisticated set of byte-swapping
routines which are more compact and efficient.  Drop
calculation of bid_free, since it's unnecessary.  Avoid

save.c (blkid_save_cache): Set the permissions on the blkid
cache file to 644.

lib/blkid/ChangeLog
lib/blkid/blkidP.h
lib/blkid/dev.c
lib/blkid/libblkid.3.in
lib/blkid/probe.c
lib/blkid/probe.h
lib/blkid/read.c
lib/blkid/resolve.c
lib/blkid/save.c
lib/blkid/tag.c

index 8722571..827a685 100644 (file)
@@ -1,3 +1,29 @@
+2003-01-27  Theodore Ts'o  <tytso@mit.edu>
+
+       * read.c (parse_tag): Do not return that blkid_tag when parsing
+               the blkid.tag file.
+
+       * resolve.c (blkid_get_token, blkid_get_tagname_devname): Fold in
+               code from removed functions
+
+       * tag.c (blkid_create_tag): Don't return the newly tag strcture
+               any more, as it's not needed.
+               (blkid_find_tag_cache, blkid_get_tag_cache,
+                blkid_token_to_tag, blkid_find_tv_tags): Remove these 
+               functions, as they are either only used once or have
+               interfaces that should be deprecated.
+               (blkid_find_tag_dev, blkid_find_head_cache): Replace use
+               of blkid_tag with one or two const char * type/value
+               arguments.
+               (blkid_find_dev_with_tag): Fold in code from removed functions
+
+       * probe.h, probe.c: Use a more sophisticated set of byte-swapping
+               routines which are more compact and efficient.  Drop
+               calculation of bid_free, since it's unnecessary.  Avoid 
+
+       * save.c (blkid_save_cache): Set the permissions on the blkid
+               cache file to 644.
+       
 2003-01-25  Theodore Ts'o  <tytso@mit.edu>
 
        * cache.c, dev.c, devname.c, devno.c, getsize.c, llseek.c, 
index 4bf6f33..6b30061 100644 (file)
@@ -36,7 +36,6 @@ struct blkid_struct_dev
        char                    *bid_name;      /* Device inode pathname */
        char                    *bid_type;      /* Preferred device TYPE */
        blkid_loff_t            bid_size;       /* Filesystem size in bytes */
-       blkid_loff_t            bid_free;       /* Filesystem free in bytes */
        blkid_loff_t            bid_devsize;    /* Device size in bytes */
        dev_t                   bid_devno;      /* Device major/minor number */
        time_t                  bid_time;       /* Last update time of device */
@@ -114,6 +113,10 @@ extern const char *devdirs[];
 #define BLKID_ERR_PARAM        22
 #define BLKID_ERR_BIG  27
 
+#if defined(TEST_PROGRAM)
+#define DEBUG
+#endif
+
 #ifdef DEBUG
 #define DEBUG_CACHE
 #define DEBUG_DUMP
@@ -160,7 +163,7 @@ static inline void DEB_DUMP_DEV(blkid_dev dev)
        printf("  dev: flags = 0x%08X\n", dev->bid_flags);
 
        list_for_each(p, &dev->bid_tags) {
-               blkid_tag tag = list_entry(p, struct blkid_stuct_tag, bit_tags);
+               blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
                DEB_DUMP_TAG(tag);
        }
        printf("\n");
@@ -232,15 +235,11 @@ extern blkid_dev blkid_get_devno(blkid_cache cache, dev_t devno);
  */
 extern blkid_tag blkid_new_tag(void);
 extern void blkid_free_tag(blkid_tag tag);
-extern int blkid_create_tag(blkid_dev dev, blkid_tag *tag,
-                           const char *name, const char *value,
-                           const int vlength);
-extern blkid_tag blkid_token_to_tag(const char *token);
-extern blkid_tag blkid_find_tv_tags(blkid_tag head, const char *value);
-extern blkid_tag blkid_find_tag_dev(blkid_dev dev, blkid_tag tag);
-extern blkid_tag blkid_find_head_cache(blkid_cache cache, blkid_tag tag);
-extern blkid_tag blkid_find_tag_cache(blkid_cache cache, blkid_tag tag);
-extern blkid_tag blkid_get_tag_cache(blkid_cache cache, blkid_tag tag);
+extern int blkid_create_tag(blkid_dev dev, const char *name,
+                           const char *value, const int vlength);
+extern blkid_tag blkid_find_tag_dev(blkid_dev dev, const char *type,
+                                   const char *value);
+extern blkid_tag blkid_find_head_cache(blkid_cache cache, const char *type);
 
 /*
  * Functions to create and find a specific tag type: dev.c
index afba909..2075d45 100644 (file)
@@ -100,7 +100,7 @@ static int add_tag_to_cache(blkid_cache cache, blkid_tag tag)
 
        DBG(printf("    adding tag %s=%s to cache\n", tag->bit_name, tag->bit_val));
 
-       if (!(head = blkid_find_head_cache(cache, tag))) {
+       if (!(head = blkid_find_head_cache(cache, tag->bit_name))) {
                head = blkid_new_tag();
                if (!head)
                        return -BLKID_ERR_MEM;
index 23938d1..98f487f 100644 (file)
@@ -86,11 +86,8 @@ http://www.gnu.org/licenses/licenses.html#LGPL
 .BR blkid_get_devname (3),
 .BR blkid_get_devno (3),
 .BR blkid_probe_all (3),
-.BR blkid_token_to_tag (3),
 .BR blkid_free_dev (3),
 .BR blkid_find_tag_dev (3),
-.BR blkid_find_tag_cache (3),
-.BR blkid_get_tag_cache (3),
 .BR blkid_get_tagname_devname (3),
 .BR blkid_get_token (3),
 .BR blkid.tab (7)
index 5533d89..9969a97 100644 (file)
@@ -63,7 +63,7 @@ static int probe_default(int fd, blkid_dev *dev_p, const char *devname,
 
        offset = (blkid_loff_t)id->bim_kboff << 10;
        if (id->bim_kboff < 0)
-               offset = (size & ~((blkid_loff_t)id->bim_align - 1)) + offset;
+               offset += (size & ~((blkid_loff_t)(id->bim_align - 1)));
 
        if (blkid_llseek(fd, offset, 0) < 0 ||
            read(fd, buf, id->bim_kbsize << 10) != id->bim_kbsize << 10)
@@ -91,7 +91,7 @@ static int probe_default(int fd, blkid_dev *dev_p, const char *devname,
        dev->bid_flags |= BLKID_BID_FL_VERIFIED;
 
        if (id->bim_type)
-               blkid_create_tag(dev, NULL, "TYPE", id->bim_type,
+               blkid_create_tag(dev, "TYPE", id->bim_type,
                                 strlen(id->bim_type));
 
        DBG(printf("%s: devno 0x%04Lx, type %s\n", devname,
@@ -117,15 +117,15 @@ static int probe_ext2(int fd, blkid_dev *dev_p, const char *devname,
        es = (struct ext2_super_block *)buf;
 
        DBG(printf("size = %Ld, ext2_sb.compat = %08X:%08X:%08X\n", size,
-                  le32_to_cpu(es->s_feature_compat),
-                  le32_to_cpu(es->s_feature_incompat),
-                  le32_to_cpu(es->s_feature_ro_compat)));
+                  blkid_le32(es->s_feature_compat),
+                  blkid_le32(es->s_feature_incompat),
+                  blkid_le32(es->s_feature_ro_compat)));
 
        /* Make sure we don't keep re-probing as ext2 for a journaled fs */
        if (!strcmp(id->bim_type, "ext2") &&
-           (le32_to_cpu(es->s_feature_compat) &
+           (blkid_le32(es->s_feature_compat) &
             EXT3_FEATURE_COMPAT_HAS_JOURNAL ||
-            le32_to_cpu(es->s_feature_incompat) &
+            blkid_le32(es->s_feature_incompat) &
             EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
                blkid_free_dev(dev);
                return -BLKID_ERR_PARAM;
@@ -134,22 +134,18 @@ static int probe_ext2(int fd, blkid_dev *dev_p, const char *devname,
        /* Don't set this until there is no chance of error */
        *dev_p = dev;
 
-       dev->bid_size = (blkid_loff_t)le32_to_cpu(es->s_blocks_count) <<
-               (le32_to_cpu(es->s_log_block_size) + 10);
-
-       /* This is a safe (minimum) number, as it ignores metadata usage. */
-       dev->bid_free = (blkid_loff_t)le32_to_cpu(es->s_free_blocks_count) <<
-               (le32_to_cpu(es->s_log_block_size) + 10);
+       dev->bid_size = (blkid_loff_t)blkid_le32(es->s_blocks_count) <<
+               (blkid_le32(es->s_log_block_size) + 10);
 
        if (strlen(es->s_volume_name)) {
-               blkid_create_tag(dev, NULL, "LABEL", es->s_volume_name,
+               blkid_create_tag(dev, "LABEL", es->s_volume_name,
                                 sizeof(es->s_volume_name));
        }
 
        if (!uuid_is_null(es->s_uuid)) {
                char uuid[37];
                uuid_unparse(es->s_uuid, uuid);
-               blkid_create_tag(dev, NULL, "UUID", uuid, sizeof(uuid));
+               blkid_create_tag(dev, "UUID", uuid, sizeof(uuid));
        }
 
        return 0;
@@ -168,7 +164,7 @@ static int probe_jbd(int fd, blkid_dev *dev_p, const char *devname,
 
        es = (struct ext2_super_block *)buf;
 
-       if (!(le32_to_cpu(es->s_feature_incompat) &
+       if (!(blkid_le32(es->s_feature_incompat) &
              EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
                blkid_free_dev(dev);
                return -BLKID_ERR_PARAM;
@@ -192,7 +188,7 @@ static int probe_ext3(int fd, blkid_dev *dev_p, const char *devname,
 
        es = (struct ext2_super_block *)buf;
 
-       if (!(le32_to_cpu(es->s_feature_compat) &
+       if (!(blkid_le32(es->s_feature_compat) &
              EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
                blkid_free_dev(dev);
                *dev_p = NULL;
@@ -201,9 +197,9 @@ static int probe_ext3(int fd, blkid_dev *dev_p, const char *devname,
        /* Don't set this until there is no chance of error */
        *dev_p = dev;
 
-       if (!(le32_to_cpu(es->s_feature_incompat) &
+       if (!(blkid_le32(es->s_feature_incompat) &
              EXT3_FEATURE_INCOMPAT_RECOVER)) {
-               blkid_create_tag(dev, NULL, "TYPE", "ext2", 4);
+               blkid_create_tag(dev, "TYPE", "ext2", 4);
                dev->bid_flags |= BLKID_BID_FL_MTYPE;
        }
 
@@ -242,14 +238,14 @@ static int probe_vfat(int fd, blkid_dev *dev_p, const char *devname,
                while (*end == ' ' && end >= vs->vs_label)
                        --end;
                if (end >= vs->vs_label)
-                       blkid_create_tag(dev, NULL, "LABEL", vs->vs_label,
+                       blkid_create_tag(dev, "LABEL", vs->vs_label,
                                         end - vs->vs_label + 1);
        }
 
        /* We can't just print them as %04X, because they are unaligned */
        sprintf(serno, "%02X%02X-%02X%02X", vs->vs_serno[3], vs->vs_serno[2],
                vs->vs_serno[1], vs->vs_serno[0]);
-       blkid_create_tag(dev, NULL, "UUID", serno, sizeof(serno));
+       blkid_create_tag(dev, "UUID", serno, sizeof(serno));
 
        return 0;
 }
@@ -286,14 +282,14 @@ static int probe_msdos(int fd, blkid_dev *dev_p, const char *devname,
                while (*end == ' ' && end >= ms->ms_label)
                        --end;
                if (end >= ms->ms_label)
-                       blkid_create_tag(dev, NULL, "LABEL", ms->ms_label,
+                       blkid_create_tag(dev, "LABEL", ms->ms_label,
                                         end - ms->ms_label + 1);
        }
 
        /* We can't just print them as %04X, because they are unaligned */
        sprintf(serno, "%02X%02X-%02X%02X", ms->ms_serno[3], ms->ms_serno[2],
                ms->ms_serno[1], ms->ms_serno[0]);
-       blkid_create_tag(dev, NULL, "UUID", serno, sizeof(serno));
+       blkid_create_tag(dev, "UUID", serno, sizeof(serno));
 
        return 0;
 }
@@ -314,19 +310,17 @@ static int probe_xfs(int fd, blkid_dev *dev_p, const char *devname,
        /* Don't set this until there is no chance of error */
        *dev_p = dev;
        /* If the filesystem size is larger than the device, this is bad */
-       dev->bid_size = be64_to_cpu(xs->xs_dblocks) *
-               be32_to_cpu(xs->xs_blocksize);
-       dev->bid_free = be64_to_cpu(xs->xs_fdblocks) *
-               be32_to_cpu(xs->xs_blocksize);
+       dev->bid_size = blkid_be64(xs->xs_dblocks) *
+               blkid_be32(xs->xs_blocksize);
 
        if (strlen(xs->xs_fname))
-               blkid_create_tag(dev, NULL, "LABEL", xs->xs_fname,
+               blkid_create_tag(dev, "LABEL", xs->xs_fname,
                                 sizeof(xs->xs_fname));
 
        if (!uuid_is_null(xs->xs_uuid)) {
                char uuid[37];
                uuid_unparse(xs->xs_uuid, uuid);
-               blkid_create_tag(dev, NULL, "UUID", uuid, sizeof(uuid));
+               blkid_create_tag(dev, "UUID", uuid, sizeof(uuid));
        }
        return 0;
 }
@@ -345,10 +339,10 @@ static int probe_reiserfs(int fd, blkid_dev *dev_p, const char *devname,
 
        rs = (struct reiserfs_super_block *)buf;
 
-       blocksize = le16_to_cpu(rs->rs_blocksize);
+       blocksize = blkid_le16(rs->rs_blocksize);
 
        /* If the superblock is inside the journal, we have the wrong one */
-       if (id->bim_kboff/(blocksize>>10) > le32_to_cpu(rs->rs_journal_block)) {
+       if (id->bim_kboff/(blocksize>>10) > blkid_le32(rs->rs_journal_block)) {
                blkid_free_dev(dev);
                return -BLKID_ERR_BIG;
        }
@@ -357,21 +351,20 @@ static int probe_reiserfs(int fd, blkid_dev *dev_p, const char *devname,
        *dev_p = dev;
 
        /* If the filesystem size is larger than the device, this is bad */
-       dev->bid_size = le32_to_cpu(rs->rs_blocks_count) * blocksize;
-       dev->bid_free = le32_to_cpu(rs->rs_free_blocks) * blocksize;
+       dev->bid_size = blkid_le32(rs->rs_blocks_count) * blocksize;
 
        /* LABEL/UUID are only valid for later versions of Reiserfs v3.6. */
        if (!strcmp(id->bim_magic, "ReIsEr2Fs") ||
            !strcmp(id->bim_magic, "ReIsEr3Fs")) {
                if (strlen(rs->rs_label)) {
-                       blkid_create_tag(dev, NULL, "LABEL", rs->rs_label,
+                       blkid_create_tag(dev, "LABEL", rs->rs_label,
                                         sizeof(rs->rs_label));
                }
 
                if (!uuid_is_null(rs->rs_uuid)) {
                        char uuid[37];
                        uuid_unparse(rs->rs_uuid, uuid);
-                       blkid_create_tag(dev, NULL, "UUID", uuid, sizeof(uuid));
+                       blkid_create_tag(dev, "UUID", uuid, sizeof(uuid));
                }
        }
 
@@ -421,7 +414,7 @@ static int probe_swap(int fd, blkid_dev *dev_p, const char *devname,
 
        /* A label can not exist on the old (128MB max) swap format */
        if (!strcmp(id->bim_magic, "SWAPSPACE2") && sh->sh_label[0]) {
-               blkid_create_tag(dev, NULL, "LABEL", sh->sh_label,
+               blkid_create_tag(dev, "LABEL", sh->sh_label,
                                 sizeof(sh->sh_label));
        }
 
@@ -455,7 +448,7 @@ static int probe_mdraid(int fd, blkid_dev *dev_p, const char *devname,
                memcpy(md_uuid + 4, &md->set_uuid1, 12);
 
                uuid_unparse(md_uuid, uuid);
-               blkid_create_tag(dev, NULL, "UUID", uuid, sizeof(uuid));
+               blkid_create_tag(dev, "UUID", uuid, sizeof(uuid));
        }
        return 0;
 }
@@ -473,7 +466,7 @@ static int probe_hfs(int fd, blkid_dev *dev_p, const char *devname,
 
        hfs = (struct hfs_super_block *)buf;
 
-       if (be32_to_cpu(hfs->h_blksize) != 512)
+       if (blkid_be32(hfs->h_blksize) != 512)
                return -BLKID_ERR_PARAM;
 
        /* Don't set this until there is no chance of error */
@@ -621,7 +614,7 @@ static struct blkid_magic *devname_to_magic(const char *devname, int fd,
                 * alignment requirements.
                 */
                if (id->bim_kboff < 0) {
-                       start = (size & ~((blkid_loff_t)id->bim_align - 1)) +
+                       start = (size & ~((blkid_loff_t)(id->bim_align - 1))) +
                                offset;
                        if (start < 0) /* Device too small for alignment */
                                continue;
index 8e22b19..23dd07f 100644 (file)
@@ -193,36 +193,85 @@ struct hfs_super_block {
        __u32   h_blksize;
 };
 
-#ifndef le32_to_cpu    /* Assume if one is defined, all are defined */
-#define X16_to_cpu(x) ((__u16)((((__u16)(x) & 0x00ffU) << 8) | \
-                              (((__u16)(x) & 0xff00U) >> 8)))
-#define X32_to_cpu(x) ((__u32)((((__u32)(x) & 0x000000ffU) << 24) | \
-                              (((__u32)(x) & 0x0000ff00U) << 8) | \
-                              (((__u32)(x) & 0x00ff0000U) >> 8) | \
-                              (((__u32)(x) & 0xff000000U) >> 24)))
-#define X64_to_cpu(x) ((__u64)((((__u64)(x) & 0x00000000000000ffULL) << 56) | \
-                              (((__u64)(x) & 0x000000000000ff00ULL) << 40) | \
-                              (((__u64)(x) & 0x0000000000ff0000ULL) << 24) | \
-                              (((__u64)(x) & 0x00000000ff000000ULL) <<  8) | \
-                              (((__u64)(x) & 0x000000ff00000000ULL) >>  8) | \
-                              (((__u64)(x) & 0x0000ff0000000000ULL) >> 24) | \
-                              (((__u64)(x) & 0x00ff000000000000ULL) >> 40) | \
-                              (((__u64)(x) & 0xff00000000000000ULL) >> 56)))
-#if  __BYTE_ORDER == __BIG_ENDIAN
-#define le16_to_cpu(x) X16_to_cpu(x)
-#define le32_to_cpu(x) X32_to_cpu(x)
-#define le64_to_cpu(x) X64_to_cpu(x)
-#define be16_to_cpu(x) (x)
-#define be32_to_cpu(x) (x)
-#define be64_to_cpu(x) (x)
+/*
+ * Byte swap functions
+ */
+#ifdef __GNUC__
+#define _INLINE_ extern __inline__
+#else                          /* For Watcom C */
+#define _INLINE_ extern inline
+#endif
+
+#if ((defined __GNUC__) && \
+     (defined(__i386__) || defined(__i486__) || defined(__i586__)))
+
+#define _BLKID_HAVE_ASM_BITOPS_
+
+_INLINE_ __u32 blkid_swab32(__u32 val)
+{
+#ifdef EXT2FS_REQUIRE_486
+       __asm__("bswap %0" : "=r" (val) : "0" (val));
 #else
-#define le16_to_cpu(x) (x)
-#define le32_to_cpu(x) (x)
-#define le64_to_cpu(x) (x)
-#define be16_to_cpu(x) X16_to_cpu(x)
-#define be32_to_cpu(x) X32_to_cpu(x)
-#define be64_to_cpu(x) X64_to_cpu(x)
+       __asm__("xchgb %b0,%h0\n\t"     /* swap lower bytes     */
+               "rorl $16,%0\n\t"       /* swap words           */
+               "xchgb %b0,%h0"         /* swap higher bytes    */
+               :"=q" (val)
+               : "0" (val));
+#endif
+       return val;
+}
+
+_INLINE_ __u16 blkid_swab16(__u16 val)
+{
+       __asm__("xchgb %b0,%h0"         /* swap bytes           */ \
+               : "=q" (val) \
+               :  "0" (val)); \
+               return val;
+}
+
+_INLINE_ __u64 blkid_swab64(__u64 val)
+{
+       return (blkid_swab32(val >> 32) |
+               (((__u64) blkid_swab32(val & 0xFFFFFFFFUL)) << 32));
+}
 #endif
+
+#if !defined(_BLKID_HAVE_ASM_BITOPS_)
+
+_INLINE_  __u16 blkid_swab16(__u16 val)
+{
+       return (val >> 8) | (val << 8);
+}
+
+_INLINE_ __u32 blkid_swab32(__u32 val)
+{
+       return ((val>>24) | ((val>>8)&0xFF00) |
+               ((val<<8)&0xFF0000) | (val<<24));
+}
+
+_INLINE_ u64 blkid_swab64(__u64 val)
+{
+       return (blkid_swab32(val >> 32) |
+               (((__u64) blkid_swab32(val & 0xFFFFFFFFUL)) << 32));
+}
+#endif 
+
+
+
+#if  __BYTE_ORDER == __BIG_ENDIAN
+#define blkid_le16(x) blkid_swab16(x)
+#define blkid_le32(x) blkid_swab32(x)
+#define blkid_le64(x) blkid_swab64(x)
+#define blkid_be16(x) (x)
+#define blkid_be32(x) (x)
+#define blkid_be64(x) (x)
+#else
+#define blkid_le16(x) (x)
+#define blkid_le32(x) (x)
+#define blkid_le64(x) (x)
+#define blkid_be16(x) blkid_swab16(x)
+#define blkid_be32(x) blkid_swab32(x)
+#define blkid_be64(x) blkid_swab64(x)
 #endif
 
 #endif /* _BLKID_PROBE_H */
index 3b7c2ba..89a7bfd 100644 (file)
@@ -299,8 +299,7 @@ static int parse_xml(char **name, char **value, char **cp)
  * Return 0 if no tag found.
  * Return -ve error code.
  */
-static int parse_tag(blkid_cache cache, blkid_dev dev, blkid_tag *tag,
-                    char **cp)
+static int parse_tag(blkid_cache cache, blkid_dev dev, char **cp)
 {
        char *name;
        char *value;
@@ -309,8 +308,6 @@ static int parse_tag(blkid_cache cache, blkid_dev dev, blkid_tag *tag,
        if (!cache || !dev)
                return -BLKID_ERR_PARAM;
 
-       *tag = NULL;
-
        if ((ret = parse_token(&name, &value, cp)) <= 0 /* &&
            (ret = parse_xml(&name, &value, cp)) <= 0 */)
                return ret;
@@ -328,7 +325,9 @@ static int parse_tag(blkid_cache cache, blkid_dev dev, blkid_tag *tag,
                /* FIXME: need to parse a long long eventually */
                dev->bid_time = strtol(value, 0, 0);
        else
-               ret = blkid_create_tag(dev, tag, name, value, strlen(value));
+               ret = blkid_create_tag(dev, name, value, strlen(value));
+
+       DBG(printf("    tag: %s=\"%s\"\n", name, value));
 
        return ret < 0 ? ret : 1;
 }
@@ -347,7 +346,6 @@ static int parse_tag(blkid_cache cache, blkid_dev dev, blkid_tag *tag,
 static int blkid_parse_line(blkid_cache cache, blkid_dev *dev_p, char *cp)
 {
        blkid_dev dev;
-       blkid_tag tag;
        int ret;
 
        if (!cache || !dev_p)
@@ -362,9 +360,8 @@ static int blkid_parse_line(blkid_cache cache, blkid_dev *dev_p, char *cp)
 
        dev = *dev_p;
 
-       while ((ret = parse_tag(cache, dev, &tag, &cp)) > 0) {
-               /* Added to tags for this device struct already */
-               DEB_DUMP_TAG(tag);
+       while ((ret = parse_tag(cache, dev, &cp)) > 0) {
+               ;
        }
 
        if (dev->bid_type == NULL) {
index bfa1aee..04ce149 100644 (file)
@@ -14,6 +14,7 @@
 #if HAVE_UNISTD_H
 #include <unistd.h>
 #endif
+#include <stdlib.h>
 #include <fcntl.h>
 #include <string.h>
 #include <sys/types.h>
@@ -34,7 +35,7 @@
 char *blkid_get_tagname_devname(blkid_cache cache, const char *tagname,
                                const char *devname)
 {
-       blkid_tag tag, found;
+       blkid_tag found;
        blkid_dev dev;
        char *ret = NULL;
 
@@ -43,14 +44,11 @@ char *blkid_get_tagname_devname(blkid_cache cache, const char *tagname,
        if (!devname)
                return NULL;
 
-       if (blkid_create_tag(NULL, &tag, tagname, NULL, 0) < 0)
-               return NULL;
-
        if (!cache)
                DBG(printf("no cache given, direct device probe\n"));
 
        if ((dev = blkid_get_devname(cache, devname)) &&
-           (found = blkid_find_tag_dev(dev, tag)))
+           (found = blkid_find_tag_dev(dev, tagname, NULL)))
                ret = string_copy(found->bit_val);
 
        if (!cache)
@@ -70,20 +68,17 @@ char *blkid_get_tagname_devname(blkid_cache cache, const char *tagname,
 char *blkid_get_token(blkid_cache cache, const char *token,
                      const char *value)
 {
-       blkid_tag tag = NULL, found = NULL;
+       blkid_dev dev;
        blkid_cache c = cache;
-       char *name = NULL;
+       char *t = 0, *v = 0;
+       char *ret = NULL;
 
+       if (!token)
+               return NULL;
+       
        DBG(printf("looking for %s%c%s %s\n", token, value ? '=' : ' ',
                   value ? value : "", cache ? "in cache" : "from disk"));
 
-       if (!(tag = blkid_token_to_tag(token))) {
-               if (!value)
-                       return string_copy(token);
-               if (blkid_create_tag(NULL,&tag,token,value,strlen(value)) < 0)
-                       return NULL;
-       }
-
        if (!cache) {
                if (blkid_read_cache(&c, NULL) < 0)
                        c = blkid_new_cache();
@@ -91,17 +86,30 @@ char *blkid_get_token(blkid_cache cache, const char *token,
                        return NULL;
        }
 
-       if ((found = blkid_get_tag_cache(c, tag)) && found->bit_dev)
-               name = string_copy(found->bit_dev->bid_name);
+       if (!value) {
+               blkid_parse_tag_string(token, &t, &v);
+               if (!t || !v)
+                       goto errout;
+               token = t;
+               value = v;
+       }
+
+       dev = blkid_find_dev_with_tag(c, token, value);
+       if (!dev)
+               goto errout;
 
+       ret = string_copy(blkid_devname_name(dev));
+
+errout:
+       if (t)
+               free(t);
+       if (v)
+               free(v);
        if (!cache) {
                blkid_save_cache(c, NULL);
                blkid_free_cache(c);
        }
-
-
-       blkid_free_tag(tag);
-       return name;
+       return (ret);
 }
 
 #ifdef TEST_PROGRAM
index 118d5a4..e888d0c 100644 (file)
@@ -130,6 +130,7 @@ int blkid_save_cache(blkid_cache cache, const char *filename)
                                        file = fdopen(fd, "w");
                                        opened = tmp;
                                }
+                               fchmod(fd, 0644);
                        }
                }
 
index bec0412..d0e1b0a 100644 (file)
@@ -54,48 +54,24 @@ void blkid_free_tag(blkid_tag tag)
 }
 
 /*
- * Find the desired tag on a list of tags with the same type.
- */
-blkid_tag blkid_find_tv_tags(blkid_tag head, const char *value)
-{
-       struct blkid_struct_tag *tag = NULL;
-       struct list_head *p;
-
-       if (!head || !value)
-               return NULL;
-
-       DBG(printf("looking for %s in %s list\n", value, head->bit_name));
-
-       list_for_each(p, &head->bit_names) {
-               blkid_tag tmp = list_entry(p, struct blkid_struct_tag, 
-                                          bit_names);
-
-               if (!strcmp(tmp->bit_val, value)) {
-                       tag = tmp;
-                       break;
-               }
-       }
-
-       return tag;
-}
-
-/*
- * Find the desired tag on a device.  If tag->bit_value is NULL, then the
+ * Find the desired tag on a device.  If value is NULL, then the
  * first such tag is returned, otherwise return only exact tag if found.
  */
-blkid_tag blkid_find_tag_dev(blkid_dev dev, blkid_tag tag)
+blkid_tag blkid_find_tag_dev(blkid_dev dev, const char *type,
+                            const char *value)
 {
        blkid_tag found = NULL;
        struct list_head *p;
 
-       if (!dev || !tag)
+       if (!dev || !type || !value)
                return NULL;
 
        list_for_each(p, &dev->bid_tags) {
-               blkid_tag tmp = list_entry(p, struct blkid_struct_tag, bit_tags);
+               blkid_tag tmp = list_entry(p, struct blkid_struct_tag,
+                                          bit_tags);
 
-               if (!strcmp(tmp->bit_name, tag->bit_name) &&
-                   (!tag->bit_val || !strcmp(tmp->bit_val, tag->bit_val))){
+               if (!strcmp(tmp->bit_name, type) &&
+                   (!value || !strcmp(tmp->bit_val, value))){
                        found = tmp;
                        break;
                }
@@ -108,20 +84,20 @@ blkid_tag blkid_find_tag_dev(blkid_dev dev, blkid_tag tag)
  * Find the desired tag type in the cache.
  * We return the head tag for this tag type.
  */
-blkid_tag blkid_find_head_cache(blkid_cache cache, blkid_tag tag)
+blkid_tag blkid_find_head_cache(blkid_cache cache, const char *type)
 {
        blkid_tag head = NULL;
        struct list_head *p;
 
-       if (!cache || !tag)
+       if (!cache || !type)
                return NULL;
 
        list_for_each(p, &cache->bic_tags) {
                blkid_tag tmp = list_entry(p, struct blkid_struct_tag, 
                                           bit_tags);
 
-               if (!strcmp(tmp->bit_name, tag->bit_name)) {
-                       DBG(printf("    found cache tag head %s\n", tag->bit_name));
+               if (!strcmp(tmp->bit_name, type)) {
+                       DBG(printf("    found cache tag head %s\n", type));
                        head = tmp;
                        break;
                }
@@ -131,46 +107,6 @@ blkid_tag blkid_find_head_cache(blkid_cache cache, blkid_tag tag)
 }
 
 /*
- * Find a specific tag value in the cache.  If not found return NULL.
- */
-blkid_tag blkid_find_tag_cache(blkid_cache cache, blkid_tag tag)
-{
-       blkid_tag head;
-
-       DBG(printf("looking for %s=%s in cache\n", tag->bit_name, tag->bit_val));
-
-       head = blkid_find_head_cache(cache, tag);
-
-       return blkid_find_tv_tags(head, tag->bit_val);
-}
-
-/*
- * Get a specific tag value in the cache.  If not found return NULL.
- * If we have not already probed the devices, do so and search again.
- */
-blkid_tag blkid_get_tag_cache(blkid_cache cache, blkid_tag tag)
-{
-       blkid_tag head, found;
-
-       if (!tag || !cache)
-               return NULL;
-
-       DBG(printf("looking for %s=%s in cache\n", tag->bit_name, tag->bit_val));
-
-       head = blkid_find_head_cache(cache, tag);
-       found = blkid_find_tv_tags(head, tag->bit_val);
-
-       if ((!head || !found) && !(cache->bic_flags & BLKID_BIC_FL_PROBED)) {
-               blkid_probe_all(&cache);
-               if (!head)
-                       head = blkid_find_head_cache(cache, tag);
-               found = blkid_find_tv_tags(head, tag->bit_val);
-       }
-
-       return found;
-}
-
-/*
  * Add a single tag to the given device.
  * This function is not currently exported because adding arbitrary tags to
  * a device will just get lost as soon as we verify the device (which
@@ -204,12 +140,12 @@ static void add_tag_to_dev(blkid_dev dev, blkid_tag tag)
  * if an identical tag does not already exist.
  * If tag is valid, the tag will be returned in this pointer.
  */
-int blkid_create_tag(blkid_dev dev, blkid_tag *tag, const char *name,
+int blkid_create_tag(blkid_dev dev, const char *name,
                     const char *value, const int vlength)
 {
        blkid_tag t, found;
 
-       if (!tag && !dev)
+       if (!dev)
                return -BLKID_ERR_PARAM;
 
        if (!name)
@@ -222,16 +158,12 @@ int blkid_create_tag(blkid_dev dev, blkid_tag *tag, const char *name,
        t->bit_name = string_copy(name);
        t->bit_val = stringn_copy(value, vlength);
 
-       if ((found = blkid_find_tag_dev(dev, t))) {
-               if (tag)
-                       *tag = found;
+       if ((found = blkid_find_tag_dev(dev, name, t->bit_val))) {
                blkid_free_tag(t);
                return 0;
        }
 
        add_tag_to_dev(dev, t);
-       if (tag)
-               *tag = t;
 
        return 0;
 }
@@ -281,28 +213,6 @@ errout:
 }
 
 /*
- * Convert a NAME=value pair into a token.
- */
-blkid_tag blkid_token_to_tag(const char *token)
-{
-       char *name, *value;
-       blkid_tag tag = NULL;
-
-       DBG(printf("trying to make '%s' into a tag\n", token));
-
-       if (blkid_parse_tag_string(token, &name, &value) != 0)
-               return NULL;
-
-       blkid_create_tag(NULL, &tag, name, value, sizeof(value));
-
-       string_free(name);
-       string_free(value);
-
-       return tag;
-}
-
-
-/*
  * Tag iteration routines for the public libblkid interface.
  *
  * These routines do not expose the list.h implementation, which are a
@@ -381,10 +291,32 @@ extern blkid_dev blkid_find_dev_with_tag(blkid_cache cache,
                                         const char *type,
                                         const char *value)
 {
-       blkid_tag tag = NULL, found;
+       blkid_tag head = 0, found;
+       struct list_head *p;
+
+       if (!cache || !type || !value)
+               return NULL;
+
+       DBG(printf("looking for %s=%s in cache\n", type, value));
        
-       blkid_create_tag(NULL, &tag, type, value, strlen(value));
-       found = blkid_get_tag_cache(cache, tag);
-       blkid_free_tag(tag);
+try_again:
+       if (!head)
+               head = blkid_find_head_cache(cache, type);
+
+       found = 0;
+       list_for_each(p, &head->bit_names) {
+               blkid_tag tmp = list_entry(p, struct blkid_struct_tag, 
+                                          bit_names);
+
+               if (!strcmp(tmp->bit_val, value)) {
+                       found = tmp;
+                       break;
+               }
+       }
+
+       if ((!head || !found) && !(cache->bic_flags & BLKID_BIC_FL_PROBED)) {
+               blkid_probe_all(&cache);
+               goto try_again;
+       }
        return (found ? found->bit_dev : NULL);
 }