/**
* mnt_table_get_root_fs:
* @tb: mountinfo file (/proc/self/mountinfo)
- * @root: returns pointer to the root filesystem (/)
+ * @root: NULL or returns pointer to the root filesystem (/)
*
* The function uses the parent ID from the mountinfo file to determine the
* root filesystem (the filesystem with the smallest ID with parent ID missing
int mnt_table_get_root_fs(struct libmnt_table *tb, struct libmnt_fs **root)
{
struct libmnt_iter itr;
- struct libmnt_fs *fs;
+ struct libmnt_fs *fs, *root_fs = NULL;
int root_id = 0;
- if (!tb || !root || !is_mountinfo(tb))
+ if (!tb || !is_mountinfo(tb))
return -EINVAL;
DBG(TAB, ul_debugobj(tb, "lookup root fs"));
- *root = NULL;
-
/* get smallest possible ID from the table */
mnt_reset_iter(&itr, MNT_ITER_FORWARD);
- while(mnt_table_next_fs(tb, &itr, &fs) == 0) {
+ while (mnt_table_next_fs(tb, &itr, &fs) == 0) {
int id = mnt_fs_get_parent_id(fs);
- if (!*root || id < root_id) {
- *root = fs;
+ if (!root_fs || id < root_id) {
+ root_fs = fs;
root_id = id;
}
}
/* go to the root node by "parent_id -> id" relation */
- while (*root) {
- struct libmnt_fs *x = get_parent_fs(tb, *root);
- if (!x || x == *root)
+ while (root_fs) {
+ struct libmnt_fs *x = get_parent_fs(tb, root_fs);
+ if (!x || x == root_fs)
break;
DBG(TAB, ul_debugobj(tb, " messy mountinfo, walk to %s", mnt_fs_get_target(x)));
- *root = x;
+ root_fs = x;
}
- return *root ? 0 : -EINVAL;
+ if (root)
+ *root = root_fs;
+
+ return root_fs ? 0 : -EINVAL;
}
/**
* @tb: mountinfo file (/proc/self/mountinfo)
* @itr: iterator
* @parent: parental FS
- * @chld: returns the next child filesystem
+ * @chld: NULL or returns the next child filesystem
*
* Note that filesystems are returned in the order of mounting (according to
* IDs in /proc/self/mountinfo).
int mnt_table_next_child_fs(struct libmnt_table *tb, struct libmnt_iter *itr,
struct libmnt_fs *parent, struct libmnt_fs **chld)
{
- struct libmnt_fs *fs;
+ struct libmnt_fs *fs, *chfs = NULL;
int parent_id, lastchld_id = 0, chld_id = 0;
if (!tb || !itr || !parent || !is_mountinfo(tb))
DBG(TAB, ul_debugobj(tb, "lookup next child of '%s'",
mnt_fs_get_target(parent)));
-
parent_id = mnt_fs_get_id(parent);
/* get ID of the previously returned child */
if (itr->head && itr->p != itr->head) {
- MNT_ITER_ITERATE(itr, fs, struct libmnt_fs, ents);
+ fs = MNT_ITER_GET_ENTRY(itr, struct libmnt_fs, ents);
+ MNT_ITER_ITERATE(itr);
lastchld_id = mnt_fs_get_id(fs);
}
- *chld = NULL;
-
mnt_reset_iter(itr, MNT_ITER_FORWARD);
- while(mnt_table_next_fs(tb, itr, &fs) == 0) {
+ while (mnt_table_next_fs(tb, itr, &fs) == 0) {
int id;
if (mnt_fs_get_parent_id(fs) != parent_id)
continue;
if ((!lastchld_id || id > lastchld_id) &&
- (!*chld || id < chld_id)) {
- *chld = fs;
+ (!chfs || id < chld_id)) {
+ chfs = fs;
chld_id = id;
}
}
- if (!*chld)
+ if (chld)
+ *chld = chfs;
+ if (!chfs)
return 1; /* end of iterator */
- /* set the iterator to the @chld for the next call */
- mnt_table_set_iter(tb, itr, *chld);
+ /* set the iterator to the @chfs for the next call */
+ mnt_table_set_iter(tb, itr, chfs);
return 0;
}
* mnt_table_next_fs:
* @tb: tab pointer
* @itr: iterator
- * @fs: returns the next tab entry
+ * @fs: NULL or returns the next tab entry
*
* Returns: 0 on success, negative number in case of error or 1 at the end of list.
*
{
int rc = 1;
- if (!tb || !itr || !fs)
+ if (!tb || !itr)
return -EINVAL;
- *fs = NULL;
+ if (fs)
+ *fs = NULL;
if (!itr->head)
MNT_ITER_INIT(itr, &tb->ents);
if (itr->p != itr->head) {
- MNT_ITER_ITERATE(itr, *fs, struct libmnt_fs, ents);
+ if (fs)
+ *fs = MNT_ITER_GET_ENTRY(itr, struct libmnt_fs, ents);
+ MNT_ITER_ITERATE(itr);
rc = 0;
}
/**
* mnt_table_first_fs:
* @tb: tab pointer
- * @fs: returns the first tab entry
+ * @fs: NULL or returns the first tab entry
*
* Returns: 0 on success, negative number in case of error or 1 at the end of list.
*/
int mnt_table_first_fs(struct libmnt_table *tb, struct libmnt_fs **fs)
{
- if (!tb || !fs)
+ if (!tb)
return -EINVAL;
if (list_empty(&tb->ents))
return 1;
- *fs = list_first_entry(&tb->ents, struct libmnt_fs, ents);
+ if (fs)
+ *fs = list_first_entry(&tb->ents, struct libmnt_fs, ents);
return 0;
}
/**
* mnt_table_last_fs:
* @tb: tab pointer
- * @fs: returns the last tab entry
+ * @fs: NULL or returns the last tab entry
*
* Returns: 0 on success, negative number in case of error or 1 at the end of list.
*/
int mnt_table_last_fs(struct libmnt_table *tb, struct libmnt_fs **fs)
{
- if (!tb || !fs)
+ if (!tb)
return -EINVAL;
if (list_empty(&tb->ents))
return 1;
- *fs = list_last_entry(&tb->ents, struct libmnt_fs, ents);
+ if (fs)
+ *fs = list_last_entry(&tb->ents, struct libmnt_fs, ents);
return 0;
}
* @itr: iterator
* @match_func: function returning 1 or 0
* @userdata: extra data for match_func
- * @fs: returns pointer to the next matching table entry
+ * @fs: NULL or returns pointer to the next matching table entry
*
* This function allows searching in @tb.
*
int (*match_func)(struct libmnt_fs *, void *), void *userdata,
struct libmnt_fs **fs)
{
- if (!tb || !itr || !fs || !match_func)
+ struct libmnt_fs *re = NULL;
+ int match = 0;
+
+ if (!tb || !itr || !match_func)
return -EINVAL;
DBG(TAB, ul_debugobj(tb, "lookup next fs"));
+ if (fs)
+ *fs = NULL;
if (!itr->head)
MNT_ITER_INIT(itr, &tb->ents);
- do {
- if (itr->p != itr->head)
- MNT_ITER_ITERATE(itr, *fs, struct libmnt_fs, ents);
- else
- break; /* end */
+ while (!match) {
+ if (itr->p != itr->head) {
+ re = MNT_ITER_GET_ENTRY(itr, struct libmnt_fs, ents);
+ MNT_ITER_ITERATE(itr);
+ } else
+ return 1; /*end */
- if (match_func(*fs, userdata))
- return 0;
- } while(1);
+ match = match_func(re, userdata);
+ }
- *fs = NULL;
- return 1;
+ if (fs)
+ *fs = re;
+ return 0;
}
static int mnt_table_move_parent(struct libmnt_table *tb, int oldid, int newid)