schroot-listmounts-main.cc

Go to the documentation of this file.
00001 /* Copyright © 2005-2007  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 3 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, see
00015  * <http://www.gnu.org/licenses/>.
00016  *
00017  *********************************************************************/
00018 
00019 #include <config.h>
00020 
00021 #include <sbuild/sbuild-mntstream.h>
00022 
00023 #include "schroot-listmounts-main.h"
00024 
00025 #include <cerrno>
00026 #include <climits>
00027 #include <cstdio>
00028 #include <cstdlib>
00029 #include <ctime>
00030 #include <iostream>
00031 #include <locale>
00032 
00033 #include <sys/types.h>
00034 #include <sys/stat.h>
00035 #include <unistd.h>
00036 
00037 #include <boost/format.hpp>
00038 
00039 #include <lockdev.h>
00040 
00041 using std::endl;
00042 using boost::format;
00043 using sbuild::_;
00044 using sbuild::N_;
00045 using namespace schroot_listmounts;
00046 
00047 namespace
00048 {
00049 
00050   typedef std::pair<main::error_code,const char *> emap;
00051 
00056   emap init_errors[] =
00057     {
00058       // TRANSLATORS: %1% = file
00059       emap(main::FIND,  N_("Failed to find '%1%'"))
00060     };
00061 
00062 }
00063 
00064 template<>
00065 sbuild::error<main::error_code>::map_type
00066 sbuild::error<main::error_code>::error_strings
00067 (init_errors,
00068  init_errors + (sizeof(init_errors) / sizeof(init_errors[0])));
00069 
00070 main::main (options::ptr& options):
00071   schroot_base::main("schroot-listmounts",
00072                      // TRANSLATORS: '...' is an ellipsis e.g. U+2026,
00073                      // and '-' is an em-dash.
00074                      _("[OPTION...] - list mount points"),
00075                      options,
00076                      false),
00077   opts(options)
00078 {
00079 }
00080 
00081 main::~main ()
00082 {
00083 }
00084 
00085 void
00086 main::action_listmounts ()
00087 {
00088   std::string to_find = sbuild::normalname(this->opts->mountpoint);
00089 
00090   {
00091     // NOTE: This is a non-standard GNU extension.
00092     char *rpath = realpath(to_find.c_str(), NULL);
00093     if (rpath == 0)
00094       throw error(to_find, FIND, strerror(errno));
00095 
00096     to_find = rpath;
00097     free(rpath);
00098     rpath = 0;
00099   }
00100 
00101   // Check mounts.
00102   sbuild::mntstream mounts("/proc/mounts");
00103   sbuild::mntstream::mntentry entry;
00104   sbuild::string_list mountlist;
00105 
00106   while (mounts >> entry)
00107     {
00108       std::string mount_dir(entry.directory);
00109       if (to_find == "/" ||
00110           (mount_dir.find(to_find) == 0 &&
00111            (// Names are the same.
00112             mount_dir.size() == to_find.size() ||
00113             // Must have a following /, or not the same directory.
00114             (mount_dir.size() > to_find.size() &&
00115              mount_dir[to_find.size()] == '/'))))
00116         mountlist.push_back(mount_dir);
00117     }
00118 
00119   for (sbuild::string_list::const_reverse_iterator pos = mountlist.rbegin();
00120        pos != mountlist.rend();
00121        ++pos)
00122     std::cout << *pos << '\n';
00123   std::cout << std::flush;
00124 }
00125 
00126 int
00127 main::run_impl ()
00128 {
00129   if (this->opts->action == options::ACTION_HELP)
00130     action_help(std::cerr);
00131   else if (this->opts->action == options::ACTION_VERSION)
00132     action_version(std::cerr);
00133   else if (this->opts->action == options::ACTION_LISTMOUNTS)
00134     action_listmounts();
00135   else
00136     assert(0); // Invalid action.
00137 
00138   return EXIT_SUCCESS;
00139 }

Generated on Mon Jan 21 00:38:28 2008 for schroot by  doxygen 1.5.4