* better to reuse the blkid_cache.
*/
blkid_cache bc;
+ blkid_probe pr;
+
+ char *filename;
};
/**
free(e->native);
}
free(cache->ents);
+ free(cache->filename);
if (cache->bc)
blkid_put_cache(cache->bc);
+ blkid_free_probe(cache->pr);
free(cache);
}
return NULL;
}
+/*
+ * returns (in @res) blkid prober, the @cache argument is optional
+ */
+static int mnt_cache_get_probe(mnt_cache *cache, const char *devname,
+ blkid_probe *res)
+{
+ blkid_probe pr = cache ? cache->pr : NULL;
+
+ assert(devname);
+ assert(res);
+
+ if (cache && cache->pr && strcmp(devname, cache->filename)) {
+ blkid_free_probe(cache->pr);
+ free(cache->filename);
+ cache->filename = NULL;
+ pr = cache->pr = NULL;
+ }
+
+ if (!pr) {
+ pr = blkid_new_probe_from_filename(devname);
+ if (!pr)
+ return -1;
+ if (cache) {
+ cache->pr = pr;
+ cache->filename = strdup(devname);
+ if (!cache->filename)
+ return -ENOMEM;
+ }
+
+ }
+
+ *res = pr;
+ return 0;
+}
+
/**
* mnt_cache_read_tags
* @cache: pointer to mnt_cache instance
*/
int mnt_cache_read_tags(mnt_cache *cache, const char *devname)
{
- int i, ntags = 0;
- static blkid_probe pr;
- const char *tags[] = { "LABEL", "UUID" };
+ int i, ntags = 0, rc;
+ blkid_probe pr;
+ const char *tags[] = { "LABEL", "UUID", "TYPE" };
assert(cache);
assert(devname);
return 0;
}
- pr = blkid_new_probe_from_filename(devname);
- if (!pr)
- return -1;
+ rc = mnt_cache_get_probe(cache, devname, &pr);
+ if (rc)
+ return rc;
- blkid_probe_enable_superblocks(pr, 1);
+ blkid_probe_enable_superblocks(cache->pr, 1);
- blkid_probe_set_superblocks_flags(pr,
- BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID);
+ blkid_probe_set_superblocks_flags(cache->pr,
+ BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID |
+ BLKID_SUBLKS_TYPE);
- if (blkid_do_safeprobe(pr))
+ if (blkid_do_safeprobe(cache->pr))
goto error;
DBG(CACHE, mnt_debug_h(cache, "reading tags for: %s", devname));
const char *data;
char *dev;
- if (blkid_probe_lookup_value(pr, tags[i], &data, NULL))
+ if (blkid_probe_lookup_value(cache->pr, tags[i], &data, NULL))
continue;
if (mnt_cache_find_tag(cache, tags[i], data))
continue; /* already cached */
return ntags ? 0 : 1;
error:
- blkid_free_probe(pr);
return -1;
}
return NULL;
}
+/**
+ * mnt_get_fstype:
+ * @devname: device name
+ * @cache: cache for results or NULL
+ *
+ * Returns: fileststem type or NULL in case of error. The result has to be
+ * deallocated by free() if @cache is NULL.
+ */
+char *mnt_get_fstype(const char *devname, mnt_cache *cache)
+{
+ blkid_probe pr;
+ const char *data;
+ char *type = NULL;
+
+ if (cache)
+ return mnt_cache_find_tag_value(cache, devname, "TYPE");
+
+ if (mnt_cache_get_probe(NULL, devname, &pr))
+ return NULL;
+
+ blkid_probe_enable_superblocks(pr, 1);
+
+ blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_TYPE);
+
+ if (!blkid_do_safeprobe(pr) &&
+ !blkid_probe_lookup_value(pr, "TYPE", &data, NULL))
+ type = strdup(data);
+
+ blkid_free_probe(pr);
+ return type;
+}
+
/**
* mnt_resolve_path:
* @path: "native" path
{
char line[BUFSIZ];
mnt_cache *cache;
+ int i;
cache = mnt_new_cache();
if (!cache)
if (line[sz - 1] == '\n')
line[sz - 1] = '\0';
+ if (!strcmp(line, "quit"))
+ break;
+
if (*line == '/') {
if (mnt_cache_read_tags(cache, line) < 0)
fprintf(stderr, "%s: read tags faild\n", line);
printf("%s: not cached\n", line);
}
}
+
+ for (i = 0; i < cache->nents; i++) {
+ struct mnt_cache_entry *e = &cache->ents[i];
+ if (!(e->flag & MNT_CACHE_ISTAG))
+ continue;
+
+ printf("%15s : %5s : %s\n", e->real, e->native,
+ e->native + strlen(e->native) + 1);
+ }
+
mnt_free_cache(cache);
return 0;
struct mtest ts[] = {
{ "--resolve-path", test_resolve_path, " resolve paths from stdin" },
{ "--resolve-spec", test_resolve_spec, " evaluate specs from stdin" },
- { "--read-tags", test_read_tags, " read devname or TAG from stdin" },
+ { "--read-tags", test_read_tags, " read devname or TAG from stdin (\"quit\" to exit)" },
{ NULL }
};
static int mnt_context_detect_fstype(mnt_context *cxt)
{
- return 0; /* TODO */
+ char *type = NULL;
+
+ if (!cxt || !cxt->fs)
+ return -EINVAL;
+
+ if (cxt->mountflags & (MS_BIND | MS_MOVE | MS_PROPAGATION)) {
+ mnt_fs_set_fstype(cxt->fs, "none");
+ return 0;
+ }
+
+ type = (char *) mnt_fs_get_fstype(cxt->fs);
+ if (type && !strcmp(type, "auto")) {
+ mnt_fs_set_fstype(cxt->fs, NULL);
+ type = NULL;
+ }
+
+ if (!type && !(cxt->flags & MS_REMOUNT)) {
+ mnt_cache *cache;
+ const char *dev = mnt_fs_get_srcpath(cxt->fs);
+
+ if (!dev)
+ return -EINVAL;
+
+ cache = mnt_context_get_cache(cxt);
+ type = mnt_get_fstype(dev, cache);
+
+ if (!type)
+ return -EINVAL;
+ mnt_fs_set_fstype(cxt->fs, type);
+ if (!cache)
+ free(type); /* type is not cached */
+ return 0;
+ }
+
+ return 0;
}
static int mnt_context_merge_mountflags(mnt_context *cxt)
{
assert(fs);
- if (!fstype)
- return -EINVAL;
-
if (fstype != fs->fstype)
free(fs->fstype);
fs->flags &= ~MNT_FS_NET;
/* save info about pseudo filesystems */
- if (mnt_fstype_is_pseudofs(fs->fstype))
- fs->flags |= MNT_FS_PSEUDO;
- else if (mnt_fstype_is_netfs(fs->fstype))
- fs->flags |= MNT_FS_NET;
- else if (!strcmp(fs->fstype, "swap"))
- fs->flags |= MNT_FS_SWAP;
-
+ if (fs->fstype) {
+ if (mnt_fstype_is_pseudofs(fs->fstype))
+ fs->flags |= MNT_FS_PSEUDO;
+ else if (mnt_fstype_is_netfs(fs->fstype))
+ fs->flags |= MNT_FS_NET;
+ else if (!strcmp(fs->fstype, "swap"))
+ fs->flags |= MNT_FS_SWAP;
+ }
return 0;
}
*/
int mnt_fs_set_fstype(mnt_fs *fs, const char *fstype)
{
- char *p;
+ char *p = NULL;
int rc;
- if (!fs || !fstype)
+ if (!fs)
return -EINVAL;
- p = strdup(fstype);
- if (!p)
- return -ENOMEM;
+ if (fstype) {
+ p = strdup(fstype);
+ if (!p)
+ return -ENOMEM;
+ }
rc = __mnt_fs_set_fstype_ptr(fs, p);
if (rc)
free(p);