00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <config.h>
00021
00022 #include "schroot-listmounts-main.h"
00023
00024 #include <cerrno>
00025 #include <climits>
00026 #include <cstdio>
00027 #include <cstdlib>
00028 #include <ctime>
00029 #include <iostream>
00030 #include <locale>
00031
00032 #include <sys/types.h>
00033 #include <sys/stat.h>
00034 #include <unistd.h>
00035
00036 #include <boost/format.hpp>
00037
00038 #include <mntent.h>
00039
00040 #include <lockdev.h>
00041
00042 using std::endl;
00043 using boost::format;
00044 using namespace schroot_listmounts;
00045
00046 namespace
00047 {
00048
00049 typedef std::pair<main::error_code,const char *> emap;
00050
00055 emap init_errors[] =
00056 {
00057
00058 emap(main::OPEN, N_("Failed to open '%1%'")),
00059
00060 emap(main::CLOSE, N_("Failed to close '%1%'"))
00061 };
00062
00063 }
00064
00065 template<>
00066 sbuild::error<main::error_code>::map_type
00067 sbuild::error<main::error_code>::error_strings
00068 (init_errors,
00069 init_errors + (sizeof(init_errors) / sizeof(init_errors[0])));
00070
00071 main::main (options::ptr& options):
00072 schroot_base::main("schroot-listmounts",
00073
00074
00075 N_("[OPTION...] - list mount points"),
00076 options),
00077 opts(options)
00078 {
00079 }
00080
00081 main::~main ()
00082 {
00083 }
00084
00085 sbuild::string_list
00086 main::list_mounts (std::string const& mountfile) const
00087 {
00088 sbuild::string_list ret;
00089
00090 std::string to_find = sbuild::normalname(this->opts->mountpoint);
00091
00092
00093 char *rpath = realpath(to_find.c_str(), NULL);
00094 to_find = rpath;
00095 free(rpath);
00096 rpath = 0;
00097
00098 std::FILE *mntdb = std::fopen(mountfile.c_str(), "r");
00099 if (mntdb == 0)
00100 throw error(mountfile, OPEN, strerror(errno));
00101
00102 mntent *mount;
00103 while ((mount = getmntent(mntdb)) != 0)
00104 {
00105 std::string mount_dir(mount->mnt_dir);
00106 if (to_find == "/" ||
00107 (mount_dir.find(to_find) == 0 &&
00108 (
00109 mount_dir.size() == to_find.size() ||
00110
00111 (mount_dir.size() > to_find.size() &&
00112 mount_dir[to_find.size()] == '/'))))
00113 ret.push_back(mount_dir);
00114 }
00115
00116 std::cout << std::flush;
00117
00118 if (std::fclose(mntdb) == EOF)
00119 throw error(mountfile, CLOSE, strerror(errno));
00120
00121 return ret;
00122 }
00123
00124 void
00125 main::action_listmounts ()
00126 {
00127
00128 const sbuild::string_list mounts =
00129 list_mounts("/proc/mounts");
00130
00131 for (sbuild::string_list::const_reverse_iterator pos = mounts.rbegin();
00132 pos != mounts.rend();
00133 ++pos)
00134 std::cout << *pos << '\n';
00135 std::cout << std::flush;
00136 }
00137
00138 int
00139 main::run_impl ()
00140 {
00141 if (this->opts->action == options::ACTION_HELP)
00142 action_help(std::cerr);
00143 else if (this->opts->action == options::ACTION_VERSION)
00144 action_version(std::cerr);
00145 else if (this->opts->action == options::ACTION_LISTMOUNTS)
00146 action_listmounts();
00147 else
00148 assert(0);
00149
00150 return EXIT_SUCCESS;
00151 }