/* tab-parse.c */
extern mnt_tab *mnt_new_tab_from_file(const char *filename);
-extern int mnt_tab_parse_file(mnt_tab *tb);
+extern int mnt_tab_parse_file(mnt_tab *tb, const char *filename);
extern int mnt_tab_set_parser_errcb(mnt_tab *tb,
int (*cb)(mnt_tab *tb, const char *filename, int line, int flag));
/* tab.c */
-extern mnt_tab *mnt_new_tab(const char *filename);
+extern mnt_tab *mnt_new_tab(void);
extern void mnt_free_tab(mnt_tab *tb);
extern int mnt_tab_get_nents(mnt_tab *tb);
extern int mnt_tab_set_cache(mnt_tab *tb, mnt_cache *mpc);
mnt_fs **fs);
extern int mnt_tab_fprintf(mnt_tab *tb, FILE *f, const char *fmt);
-extern int mnt_tab_update_file(mnt_tab *tb);
+extern int mnt_tab_update_file(mnt_tab *tb, const char *filename);
/*
* mtab/fstab/mountinfo file
*/
struct _mnt_tab {
- char *filename; /* file name or NULL */
int fmt; /* MNT_FMT_* file format */
int nents; /* number of valid entries */
/**
* mnt_new_tab:
- * @filename: file name or NULL
*
* The tab is a container for mnt_fs entries that usually represents a fstab,
* mtab or mountinfo file from your system.
*
- * Note that this function does not parse the file. See also
- * mnt_tab_parse_file().
+ * See also mnt_tab_parse_file().
*
* Returns: newly allocated tab struct.
*/
-mnt_tab *mnt_new_tab(const char *filename)
+mnt_tab *mnt_new_tab(void)
{
mnt_tab *tb = NULL;
tb = calloc(1, sizeof(struct _mnt_tab));
if (!tb)
- goto err;
+ return NULL;
+
+ DBG(DEBUG_TAB, fprintf(stderr, "libmount: new tab %p\n", tb));
- if (filename) {
- tb->filename = strdup(filename);
- if (!tb->filename)
- goto err;
- }
INIT_LIST_HEAD(&tb->ents);
return tb;
-err:
- free(tb);
- return NULL;
}
/**
{
if (!tb)
return;
- free(tb->filename);
while (!list_empty(&tb->ents)) {
mnt_fs *fs = list_entry(tb->ents.next, mnt_fs, ents);
return tb ? tb->cache : NULL;
}
-/**
- * mnt_tab_get_name:
- * @tb: tab pointer
- *
- * Returns: tab filename or NULL.
- */
-const char *mnt_tab_get_name(mnt_tab *tb)
-{
- assert(tb);
- return tb ? tb->filename : NULL;
-}
-
/**
* mnt_tab_add_fs:
* @tb: tab pointer
list_add_tail(&fs->ents, &tb->ents);
DBG(DEBUG_TAB, fprintf(stderr,
- "libmount: %s: add entry: %s %s\n",
- tb->filename, mnt_fs_get_source(fs),
+ "libmount: tab %p: add entry: %s %s\n",
+ tb, mnt_fs_get_source(fs),
mnt_fs_get_target(fs)));
tb->nents++;
if (!tb || !root)
return -1;
- DBG(DEBUG_TAB, fprintf(stderr,
- "libmount: %s: lookup root fs\n", tb->filename));
+ DBG(DEBUG_TAB, fprintf(stderr, "libmount: tab %p: lookup root fs\n", tb));
mnt_reset_iter(&itr, MNT_ITER_FORWARD);
while(mnt_tab_next_fs(tb, &itr, &fs) == 0) {
return -1;
DBG(DEBUG_TAB, fprintf(stderr,
- "libmount: %s: lookup next child of %s\n",
- tb->filename, mnt_fs_get_target(parent)));
+ "libmount: tab %p: lookup next child of %s\n",
+ tb, mnt_fs_get_target(parent)));
parent_id = mnt_fs_get_id(parent);
if (!parent_id)
return -1;
DBG(DEBUG_TAB, fprintf(stderr,
- "libmount: %s: lookup next fs\n", tb->filename));
+ "libmount: tab %p: lookup next fs\n", tb));
if (!itr->head)
MNT_ITER_INIT(itr, &tb->ents);
assert(path);
DBG(DEBUG_TAB, fprintf(stderr,
- "libmount: %s: lookup target: %s\n", tb->filename, path));
+ "libmount: tab %p: lookup target: %s\n", tb, path));
/* native @target */
mnt_reset_iter(&itr, direction);
assert(path);
DBG(DEBUG_TAB, fprintf(stderr,
- "libmount: %s: lookup srcpath: %s\n", tb->filename, path));
+ "libmount: tab %p: lookup srcpath: %s\n", tb, path));
/* native paths */
mnt_reset_iter(&itr, direction);
return NULL;
DBG(DEBUG_TAB, fprintf(stderr,
- "libmount: %s: lookup by TAG: %s %s\n", tb->filename, tag, val));
+ "libmount: tab %p: lookup by TAG: %s %s\n", tb, tag, val));
/* look up by TAG */
mnt_reset_iter(&itr, direction);
return NULL;
DBG(DEBUG_TAB, fprintf(stderr,
- "libmount: %s: lookup SOURCE: %s\n", tb->filename, source));
+ "libmount: tab %p: lookup SOURCE: %s\n", tb, source));
if (strchr(source, '=')) {
char *tag, *val;
*
* Returns: 0 on success, -1 in case of error.
*/
-int mnt_tab_update_file(mnt_tab *tb)
+int mnt_tab_update_file(mnt_tab *tb, const char *filename)
{
FILE *f = NULL;
char tmpname[PATH_MAX];
- const char *filename;
struct stat st;
int fd;
if (!tb)
goto error;
- filename = mnt_tab_get_name(tb);
- if (!filename)
- goto error;
-
if (snprintf(tmpname, sizeof(tmpname), "%s.tmp", filename)
>= sizeof(tmpname))
goto error;
if (!file)
return NULL;
- tb = mnt_new_tab(file);
+ tb = mnt_new_tab();
if (!tb)
goto err;
mnt_tab_set_parser_errcb(tb, parser_errcb);
- if (mnt_tab_parse_file(tb) != 0)
+ if (mnt_tab_parse_file(tb, file) != 0)
goto err;
return tb;
err:
/*
* Read and parse the next line from {fs,m}tab or mountinfo
*/
-static int mnt_tab_parse_next(mnt_tab *tb, FILE *f, mnt_fs *fs, int *nlines)
+static int mnt_tab_parse_next(mnt_tab *tb, FILE *f, mnt_fs *fs,
+ const char *filename, int *nlines)
{
char buf[BUFSIZ];
char *s;
if (feof(f)) {
DBG(DEBUG_TAB, fprintf(stderr,
"libmount: WARNING: no final newline at the end of %s\n",
- tb->filename));
+ filename));
s = index (buf, '\0');
} else {
DBG(DEBUG_TAB, fprintf(stderr,
"libmount: %s: %d: missing newline at line\n",
- tb->filename, *nlines));
+ filename, *nlines));
goto err;
}
}
} while (*s == '\0' || *s == '#');
DBG(DEBUG_TAB, fprintf(stderr, "libmount: %s:%d: %s\n",
- tb->filename, *nlines, s));
+ filename, *nlines, s));
if (!tb->fmt)
tb->fmt = detect_fmt(s);
}
DBG(DEBUG_TAB, fprintf(stderr,
- "libmount: %s:%d: SOURCE:%s, MNTPOINT:%s, TYPE:%s, "
+ "libmount: tab %p: %s:%d: SOURCE:%s, MNTPOINT:%s, TYPE:%s, "
"OPTS:%s, FREQ:%d, PASSNO:%d\n",
- tb->filename, *nlines,
+ tb, filename, *nlines,
fs->source, fs->target, fs->fstype,
fs->optstr, fs->freq, fs->passno));
return 0;
err:
DBG(DEBUG_TAB, fprintf(stderr,
- "libmount: %s:%d: parse error\n", tb->filename, *nlines));
+ "libmount: tab %p: %s:%d: parse error\n", tb, filename, *nlines));
- if (tb->errcb && tb->errcb(tb, tb->filename, *nlines, 0))
+ if (tb->errcb && tb->errcb(tb, filename, *nlines, 0))
return -1; /* fatal error */
return 1; /* recoverable error */
/**
* mnt_tab_parse_file:
* @tb: tab pointer
+ * @filename: file
*
* Parses whole table (e.g. /etc/fstab).
*
* <informalexample>
* <programlisting>
- * mnt_tab *tb = mnt_new_tab("/etc/fstab");
+ * mnt_tab *tb = mnt_new_tab();
* int rc;
*
- * rc = mnt_tab_parse_file(tb);
- * if (rc)
- * perror(mnt_tab_get_name(tb)); / * system error * /
- * else
+ * rc = mnt_tab_parse_file(tb, "/etc/fstab");
+ * if (!rc)
* mnt_fprintf_tab(tb, stdout, NULL);
- *
* mnt_free_tab(tb);
* </programlisting>
* </informalexample>
*
* Returns: 0 on success, -1 in case of error.
*/
-int mnt_tab_parse_file(mnt_tab *tb)
+int mnt_tab_parse_file(mnt_tab *tb, const char *filename)
{
FILE *f;
int nlines = 0;
assert(tb);
- assert(tb->filename);
+ assert(filename);
- if (!tb->filename)
+ if (!filename)
return -1;
- f = fopen(tb->filename, "r");
+ f = fopen(filename, "r");
if (!f)
return -1;
+ DBG(DEBUG_TAB,
+ fprintf(stderr, "libmount: tab %p: start parsing %s\n", tb, filename));
+
while (!feof(f)) {
int rc;
mnt_fs *fs = mnt_new_fs();
if (!fs)
goto error;
- rc = mnt_tab_parse_next(tb, f, fs, &nlines);
+ rc = mnt_tab_parse_next(tb, f, fs, filename, &nlines);
if (!rc)
rc = mnt_tab_add_fs(tb, fs);
if (rc) {
}
}
+ DBG(DEBUG_TAB,
+ fprintf(stderr, "libmount: tab %p: stop parsing %s\n", tb, filename));
fclose(f);
return 0;
error:
+ DBG(DEBUG_TAB,
+ fprintf(stderr, "libmount: tab %p: error parsing %s\n", tb, filename));
fclose(f);
return -1;
}
if (!filename)
return NULL;
- tb = mnt_new_tab(filename);
- if (tb && mnt_tab_parse_file(tb) != 0) {
+ tb = mnt_new_tab();
+ if (tb && mnt_tab_parse_file(tb, filename) != 0) {
mnt_free_tab(tb);
tb = NULL;
}