libmount: mnt_resolve_path: use strcmp() only if both are canonical

Signed-off-by: Eric Rannaud <e@nanocritical.com>
libmount: mnt_resolve_path: don't canonicalize fs->target for swap

This is how mnt_table_find_target() does it. It makes sense because
@fs->target is "none" for swap and is never a sensible match for a
user-specified target.

Signed-off-by: Eric Rannaud <e@nanocritical.com>
libmount: mnt_resolve_target: tiptoe around active mount points

Current code in mnt_fs_match_target() and mnt_table_find_target()
already does not canonicalize active mount points (when read from
mountinfo), because they are already canonicalized by the kernel.
Calling realpath(fs->target) on a mount point can hang -- e.g. if the
NFS server is unreachable.

This patch optionally extends this strategy to the general case, that is
when @fs does not directly come from the kernel through mountinfo (for
instance, it may have been parsed from /etc/fstab).

Given @mtab parsed from mountinfo, and if mnt_cache_set_targets(cache,
mtab) is used, then mnt_fs_match_target() and mnt_table_find_target()
check whether @fs->target is a known mount point in the cached
mountinfo, before attempting to canonicalize @fs->target, no matter
where @fs itself comes from. If found in the cached mountinfo,
@fs->target is not canonicalized.

[kzak@redhat.com: - don't allocate libmnt_iter,
                  - add docs for mnt_cache_set_targets(),
                  - fallback to mnt_resolve_path() if no cache->mtab specified,
                  - use streq_except_trailing_slash() to compare paths]

Signed-off-by: Eric Rannaud <e@nanocritical.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
findmnt: use mnt_cache_set_targets() for non-kernel table

findmnt compares the user-supplied path <target> with each entry in the
parsed table. To do this comparison, libmount attempts to canonicalize
the target path of each table entry, when the entry does not originate
from the kernel (kernel supplied target paths are already
canonicalized). However, if one of these entries is an active mount
point, stat(2) or readlink(2) on the mount target path can hang (e.g.
unreachable NFS server).

If the main table is not a kernel table, we parse /proc/self/mountinfo
into a secondary table and call mnt_cache_set_targets(). This allows
libmount to check that the target path of each entry in the main table
is not an active mount point, so it can avoid canonicalizing it.

Signed-off-by: Eric Rannaud <e@nanocritical.com>
libmount: always use mnt_resolve_target() in mnt_fs_match_target()

The requested path is also target mountpoint, so let's optimize
realpath() usage as well.

Signed-off-by: Karel Zak <kzak@redhat.com>
findmnt: simplify cache_set_targets()

Signed-off-by: Karel Zak <kzak@redhat.com>
libblkid: fix probe_ddf() [clang-analyze]

Signed-off-by: Karel Zak <kzak@redhat.com>
cfdisk: fix division by zero [clang-analyze]

Signed-off-by: Karel Zak <kzak@redhat.com>
libfdisk: fix possible memory leak [clang-analyze]

Signed-off-by: Karel Zak <kzak@redhat.com>
fsck: uninitialized argument value [clang-analyze]

Signed-off-by: Karel Zak <kzak@redhat.com>
lib/ismounted: more robust buffer usage [clang-analyze]

Signed-off-by: Karel Zak <kzak@redhat.com>
rename: allow renaming in subdirectories

Earlier the rename(1) considered path as possible string to be renamed,
could lead to an issue with none existing destination.  See below for
demonstration of this issue.  After this change all directory elements
are ignored when the match finding happens.

$ cd $(mktemp -d)
$ mkdir aa ab
$ touch a{a,b}/aa
$ rename -v a x */aa
rename: aa/aa: rename to xa/aa failed: No such file or directory

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
tests: add function to change directory reliable way

Without arguments bash cd will move to $HOME. Ensure also that when
directory is assumed to be changed the current directory and intented
destination are the same location.

Reference: http://www.spinics.net/lists/util-linux-ng/msg09509.html
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
tests: use ts_cd everywhere to change direcrory

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
tests: add rename(1) checks

Check basic file moves, symlink relinking, and both file moves and
symlinks when operations are have directory in destination path.

Reviewed-by: Ruediger Meier <sweet_f_a@gmx.de>
Reviewed-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
rename: continue despite something failed

Try to do all file operations even when one or some of them fail, and
use exit value to inform what happen.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
rename: use function pointer to select file or symlink operation

Add separate functions to different functionality, and add a function for
the stuff that is in common for both.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
tests: add rename(1) return value check

Signed-off-by: Sami Kerola <kerolasa@iki.fi>