From 74a9c6f7b709cb2c3e3863cb91ed1c869a25da84 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 3 Feb 2009 22:12:03 +0100 Subject: [PATCH] mount: move realpath.c code to lib/ Signed-off-by: Karel Zak --- TODO | 2 + include/canonicalize.h | 11 ++++ mount/realpath.c => lib/canonicalize.c | 77 +++++++++----------------- mount/Makefile.am | 21 ++++--- mount/fsprobe.c | 1 - mount/fsprobe_volumeid.c | 1 - mount/fstab.c | 5 -- mount/lomount.c | 1 - mount/mount.c | 1 - mount/realpath.h | 14 ----- mount/sundries.c | 48 +++++++++++++++- mount/sundries.h | 5 ++ mount/swapon.c | 1 - mount/umount.c | 1 - 14 files changed, 103 insertions(+), 86 deletions(-) create mode 100644 include/canonicalize.h rename mount/realpath.c => lib/canonicalize.c (74%) delete mode 100644 mount/realpath.h diff --git a/TODO b/TODO index 42cbed41..65cc55aa 100644 --- a/TODO +++ b/TODO @@ -12,6 +12,8 @@ Although mkswap has recently been -L option to create a label nothing appears to have been change to swapon to display said labels. (rh#430386) + * use canonicalize_file_name() when exist in glibc (see lib/canonicalize.v) + * try improve compilation against others libc: - klibc - ??? diff --git a/include/canonicalize.h b/include/canonicalize.h new file mode 100644 index 00000000..b8906d43 --- /dev/null +++ b/include/canonicalize.h @@ -0,0 +1,11 @@ +#ifndef CANONICALIZE_H +#define CANONICALIZE_H + +#include +#ifndef PATH_MAX +# define PATH_MAX 4096 +#endif + +extern char *canonicalize_path(const char *path); + +#endif /* CANONICALIZE_H */ diff --git a/mount/realpath.c b/lib/canonicalize.c similarity index 74% rename from mount/realpath.c rename to lib/canonicalize.c index 9b46951b..b888fbb8 100644 --- a/mount/realpath.c +++ b/lib/canonicalize.c @@ -1,5 +1,5 @@ /* - * realpath.c -- canonicalize pathname by removing symlinks + * canonicalize.c -- canonicalize pathname by removing symlinks * Copyright (C) 1993 Rick Sladkey * * This program is free software; you can redistribute it and/or modify @@ -11,67 +11,27 @@ * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library Public License for more details. + * */ -#define resolve_symlinks - /* * This routine is part of libc. We include it nevertheless, * since the libc version has some security flaws. + * + * TODO: use canonicalize_file_name() when exist in glibc */ #include #include #include -#include "realpath.h" -#include "sundries.h" /* for xstrdup */ +#include + +#include "canonicalize.h" #ifndef MAXSYMLINKS # define MAXSYMLINKS 256 #endif -int -is_pseudo_fs(const char *type) -{ - if (type == NULL || *type == '/') - return 0; - if (streq(type, "none") || - streq(type, "proc") || - streq(type, "tmpfs") || - streq(type, "sysfs") || - streq(type, "devpts")) - return 1; - return 0; -} - -/* Make a canonical pathname from PATH. Returns a freshly malloced string. - It is up the *caller* to ensure that the PATH is sensible. i.e. - canonicalize ("/dev/fd0/.") returns "/dev/fd0" even though ``/dev/fd0/.'' - is not a legal pathname for ``/dev/fd0''. Anything we cannot parse - we return unmodified. */ -char * -canonicalize_spec (const char *path) -{ - if (path == NULL) - return NULL; - if (is_pseudo_fs(path)) - return xstrdup(path); - return canonicalize(path); -} - -char * -canonicalize (const char *path) { - char canonical[PATH_MAX+2]; - - if (path == NULL) - return NULL; - - if (myrealpath (path, canonical, PATH_MAX+1)) - return xstrdup(canonical); - - return xstrdup(path); -} - -char * +static char * myrealpath(const char *path, char *resolved_path, int maxreslth) { int readlinks = 0; char *npath; @@ -137,7 +97,6 @@ myrealpath(const char *path, char *resolved_path, int maxreslth) { if (errno != EINVAL) goto err; } else { -#ifdef resolve_symlinks /* Richard Gooch dislikes sl resolution */ int m; char *newbuf; @@ -153,12 +112,13 @@ myrealpath(const char *path, char *resolved_path, int maxreslth) { /* Insert symlink contents into path. */ m = strlen(path); - newbuf = xmalloc(m + n + 1); + newbuf = malloc(m + n + 1); + if (!newbuf) + goto err; memcpy(newbuf, link_path, n); memcpy(newbuf + n, path, m + 1); free(buf); path = buf = newbuf; -#endif } *npath++ = '/'; } @@ -175,3 +135,18 @@ myrealpath(const char *path, char *resolved_path, int maxreslth) { free(buf); return NULL; } + +char * +canonicalize_path(const char *path) { + char canonical[PATH_MAX+2]; + + if (path == NULL) + return NULL; + + if (myrealpath (path, canonical, PATH_MAX+1)) + return strdup(canonical); + + return strdup(path); +} + + diff --git a/mount/Makefile.am b/mount/Makefile.am index 3963fdaf..90d956eb 100644 --- a/mount/Makefile.am +++ b/mount/Makefile.am @@ -6,15 +6,15 @@ bin_PROGRAMS = mount umount sbin_PROGRAMS = losetup swapon dist_man_MANS = fstab.5 mount.8 swapoff.8 swapon.8 umount.8 losetup.8 -utils_common = sundries.c xmalloc.c realpath.c fsprobe.c +utils_common = sundries.c xmalloc.c ../lib/canonicalize.c headers_common = fstab.h mount_mntent.h mount_constants.h \ - lomount.h fsprobe.h realpath.h xmalloc.h \ + lomount.h fsprobe.h xmalloc.h \ getusername.h loop.h sundries.h mount_common = fstab.c mount_mntent.c getusername.c lomount.c \ $(utils_common) $(headers_common) ../lib/env.c ../lib/linux_version.c \ - ../lib/blkdev.c + ../lib/blkdev.c fsprobe.c mount_SOURCES = mount.c $(mount_common) ../lib/setproctitle.c mount_CFLAGS = $(SUID_CFLAGS) $(AM_CFLAGS) @@ -25,10 +25,10 @@ umount_CFLAGS = $(SUID_CFLAGS) $(AM_CFLAGS) umount_LDFLAGS = $(SUID_LDFLAGS) $(AM_LDFLAGS) swapon_SOURCES = swapon.c swap_constants.h $(utils_common) \ - ../lib/linux_version.c ../lib/blkdev.c + ../lib/linux_version.c ../lib/blkdev.c fsprobe.c -losetup_SOURCES = lomount.c sundries.c xmalloc.c realpath.c \ - loop.h lomount.h xmalloc.h sundries.h realpath.h +losetup_SOURCES = lomount.c $(utils_common) \ + loop.h lomount.h xmalloc.h sundries.h losetup_CPPFLAGS = -DMAIN $(AM_CPPFLAGS) mount_LDADD = $(LDADD_common) @@ -61,7 +61,8 @@ losetup_static_CPPFLAGS = -DMAIN $(AM_CPPFLAGS) endif if HAVE_BLKID -utils_common += fsprobe_blkid.c +mount_common += fsprobe_blkid.c +swapon_SOURCES += fsprobe_blkid.c LDADD_common += $(BLKID_LIBS) LDADD_common_static += $(BLKID_LIBS_STATIC) endif @@ -72,13 +73,15 @@ mount_static_LDADD += $(SELINUX_LIBS_STATIC) endif if HAVE_VOLUME_ID -utils_common += fsprobe_volumeid.c +mount_common += fsprobe_volumeid.c +swapon_SOURCES += fsprobe_volumeid.c LDADD_common += $(VOLUMEID_LIBS) LDADD_common_static += $(VOLUMEID_LIBS_STATIC) endif noinst_PROGRAMS = mtab_lock_test -mtab_lock_test_SOURCES = fstab.c sundries.c xmalloc.c $(headers_common) +mtab_lock_test_SOURCES = fstab.c sundries.c xmalloc.c $(headers_common) \ + ../lib/canonicalize.c mtab_lock_test_CPPFLAGS = -DMAIN_TEST_MTABLOCK $(AM_CPPFLAGS) install-exec-hook: diff --git a/mount/fsprobe.c b/mount/fsprobe.c index 07ffbd9a..04e267f3 100644 --- a/mount/fsprobe.c +++ b/mount/fsprobe.c @@ -9,7 +9,6 @@ #include "fsprobe.h" #include "sundries.h" /* for xstrdup */ #include "nls.h" -#include "realpath.h" /* list of already tested filesystems by fsprobe_procfsloop_mount() */ static struct tried { diff --git a/mount/fsprobe_volumeid.c b/mount/fsprobe_volumeid.c index b9b137b8..a5c92823 100644 --- a/mount/fsprobe_volumeid.c +++ b/mount/fsprobe_volumeid.c @@ -12,7 +12,6 @@ #include "blkdev.h" #include "fsprobe.h" -#include "realpath.h" #include "pathnames.h" #include "sundries.h" diff --git a/mount/fstab.c b/mount/fstab.c index c60c5f4d..bcbc3a9d 100644 --- a/mount/fstab.c +++ b/mount/fstab.c @@ -19,7 +19,6 @@ #include "fsprobe.h" #include "pathnames.h" #include "nls.h" -#include "realpath.h" #define streq(s, t) (strcmp ((s), (t)) == 0) @@ -924,10 +923,6 @@ mntFILE *my_setmntent (const char *file, char *mode) { return NULL; } void my_endmntent (mntFILE *mfp) { } int my_addmntent (mntFILE *mfp, struct my_mntent *mnt) { return 0; } -char *canonicalize (const char *path) { return NULL; } -char *canonicalize_spec (const char *path) { return NULL; } -int is_pseudo_fs(const char *type) { return 0; }; - int main(int argc, char **argv) { diff --git a/mount/lomount.c b/mount/lomount.c index 5675eac0..ece0003e 100644 --- a/mount/lomount.c +++ b/mount/lomount.c @@ -22,7 +22,6 @@ #include "nls.h" #include "sundries.h" #include "xmalloc.h" -#include "realpath.h" #include "pathnames.h" #define SIZE(a) (sizeof(a)/sizeof(a[0])) diff --git a/mount/mount.c b/mount/mount.c index 23f27d67..e04dbdb5 100644 --- a/mount/mount.c +++ b/mount/mount.c @@ -40,7 +40,6 @@ #include "env.h" #include "nls.h" #include "blkdev.h" -#include "realpath.h" #define DO_PS_FIDDLING diff --git a/mount/realpath.h b/mount/realpath.h deleted file mode 100644 index 15450c1d..00000000 --- a/mount/realpath.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef REALPATH_H -#define REALPATH_H - -#include -#ifndef PATH_MAX -# define PATH_MAX 4096 -#endif - -extern char *myrealpath(const char *path, char *resolved_path, int m); -extern char *canonicalize (const char *path); -extern char *canonicalize_spec (const char *path); -extern int is_pseudo_fs(const char *type); - -#endif /* REALPATH_H */ diff --git a/mount/sundries.c b/mount/sundries.c index fad8eaec..bdaf1172 100644 --- a/mount/sundries.c +++ b/mount/sundries.c @@ -11,9 +11,11 @@ #include #include #include /* for MNTTYPE_SWAP */ + +#include "canonicalize.h" + #include "fstab.h" #include "sundries.h" -#include "realpath.h" #include "xmalloc.h" #include "nls.h" @@ -276,3 +278,47 @@ parse_spec(const char *spec, char **name, char **value) return 0; } +int +is_pseudo_fs(const char *type) +{ + if (type == NULL || *type == '/') + return 0; + if (streq(type, "none") || + streq(type, "proc") || + streq(type, "tmpfs") || + streq(type, "sysfs") || + streq(type, "devpts")) + return 1; + return 0; +} + +/* Make a canonical pathname from PATH. Returns a freshly malloced string. + It is up the *caller* to ensure that the PATH is sensible. i.e. + canonicalize ("/dev/fd0/.") returns "/dev/fd0" even though ``/dev/fd0/.'' + is not a legal pathname for ``/dev/fd0''. Anything we cannot parse + we return unmodified. */ +char * +canonicalize_spec (const char *path) +{ + char *res; + + if (path == NULL) + return NULL; + if (is_pseudo_fs(path)) + return xstrdup(path); + + res = canonicalize_path(path); + if (!res) + die(EX_SYSERR, _("not enough memory")); + return res; +} + +char *canonicalize (const char *path) +{ + char *res = canonicalize_path(path); + + if (!res) + die(EX_SYSERR, _("not enough memory")); + return res; +} + diff --git a/mount/sundries.h b/mount/sundries.h index f64d15db..c8570128 100644 --- a/mount/sundries.h +++ b/mount/sundries.h @@ -37,6 +37,11 @@ char *xstrconcat4 (char *, const char *, const char *, const char *); int parse_spec(const char *spec, char **name, char **value); +int is_pseudo_fs(const char *type); + +char *canonicalize (const char *path); +char *canonicalize_spec (const char *path); + /* exit status - bits below are ORed */ #define EX_USAGE 1 /* incorrect invocation or permission */ #define EX_SYSERR 2 /* out of memory, cannot fork, ... */ diff --git a/mount/swapon.c b/mount/swapon.c index 0d5067e0..c50c32ae 100644 --- a/mount/swapon.c +++ b/mount/swapon.c @@ -20,7 +20,6 @@ #include "swap_constants.h" #include "nls.h" #include "fsprobe.h" -#include "realpath.h" #include "pathnames.h" #include "sundries.h" #include "swapheader.h" diff --git a/mount/umount.c b/mount/umount.c index 738d05c3..9901a809 100644 --- a/mount/umount.c +++ b/mount/umount.c @@ -19,7 +19,6 @@ #include "fstab.h" #include "env.h" #include "nls.h" -#include "realpath.h" #if defined(MNT_FORCE) /* Interesting ... it seems libc knows about MNT_FORCE and presumably -- 2.39.5