schroot-listmounts-main.cc

Go to the documentation of this file.
00001 /* Copyright © 2005-2006  Roger Leigh <rleigh@debian.org>
00002  *
00003  * schroot is free software; you can redistribute it and/or modify it
00004  * under the terms of the GNU General Public License as published by
00005  * the Free Software Foundation; either version 2 of the License, or
00006  * (at your option) any later version.
00007  *
00008  * schroot is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program; if not, write to the Free Software
00015  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00016  * MA  02111-1307  USA
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       // TRANSLATORS: %1% = file
00058       emap(main::FIND,  N_("Failed to find '%1%'")),
00059       // TRANSLATORS: %1% = file
00060       emap(main::OPEN,  N_("Failed to open '%1%'")),
00061       // TRANSLATORS: %1% = file
00062       emap(main::CLOSE, N_("Failed to close '%1%'"))
00063     };
00064 
00065 }
00066 
00067 template<>
00068 sbuild::error<main::error_code>::map_type
00069 sbuild::error<main::error_code>::error_strings
00070 (init_errors,
00071  init_errors + (sizeof(init_errors) / sizeof(init_errors[0])));
00072 
00073 main::main (options::ptr& options):
00074   schroot_base::main("schroot-listmounts",
00075                      // TRANSLATORS: '...' is an ellipsis e.g. U+2026,
00076                      // and '-' is an em-dash.
00077                      N_("[OPTION...] - list mount points"),
00078                      options),
00079   opts(options)
00080 {
00081 }
00082 
00083 main::~main ()
00084 {
00085 }
00086 
00087 sbuild::string_list
00088 main::list_mounts (std::string const& mountfile) const
00089 {
00090   sbuild::string_list ret;
00091 
00092   std::string to_find = sbuild::normalname(this->opts->mountpoint);
00093 
00094   // NOTE: This is a non-standard GNU extension.
00095   char *rpath = realpath(to_find.c_str(), NULL);
00096   if (rpath == 0)
00097     throw error(to_find, FIND, strerror(errno));
00098 
00099   to_find = rpath;
00100   free(rpath);
00101   rpath = 0;
00102 
00103   std::FILE *mntdb = std::fopen(mountfile.c_str(), "r");
00104   if (mntdb == 0)
00105     throw error(mountfile, OPEN, strerror(errno));
00106 
00107   mntent *mount;
00108   while ((mount = getmntent(mntdb)) != 0)
00109     {
00110       std::string mount_dir(mount->mnt_dir);
00111       if (to_find == "/" ||
00112           (mount_dir.find(to_find) == 0 &&
00113            (// Names are the same.
00114             mount_dir.size() == to_find.size() ||
00115             // Must have a following /, or not the same directory.
00116             (mount_dir.size() > to_find.size() &&
00117              mount_dir[to_find.size()] == '/'))))
00118         ret.push_back(mount_dir);
00119     }
00120 
00121   std::cout << std::flush;
00122 
00123   if (std::fclose(mntdb) == EOF)
00124     throw error(mountfile, CLOSE, strerror(errno));
00125 
00126   return ret;
00127 }
00128 
00129 void
00130 main::action_listmounts ()
00131 {
00132   // Check mounts.
00133   const sbuild::string_list mounts =
00134     list_mounts("/proc/mounts");
00135 
00136   for (sbuild::string_list::const_reverse_iterator pos = mounts.rbegin();
00137        pos != mounts.rend();
00138        ++pos)
00139     std::cout << *pos << '\n';
00140   std::cout << std::flush;
00141 }
00142 
00143 int
00144 main::run_impl ()
00145 {
00146   if (this->opts->action == options::ACTION_HELP)
00147     action_help(std::cerr);
00148   else if (this->opts->action == options::ACTION_VERSION)
00149     action_version(std::cerr);
00150   else if (this->opts->action == options::ACTION_LISTMOUNTS)
00151     action_listmounts();
00152   else
00153     assert(0); // Invalid action.
00154 
00155   return EXIT_SUCCESS;
00156 }

Generated on Sat Jan 27 16:11:04 2007 for schroot by  doxygen 1.5.1