nis-util  1.0.D108
lib/colon/passwd/slurp.cc
Go to the documentation of this file.
00001 //
00002 // nis-util - NIS Administration Utilities
00003 // Copyright (C) 2001, 2002, 2008, 2009, 2011, 2012 Peter Miller
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 2 of the License, or (at
00008 // your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program. If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 
00019 #include <lib/config.h>
00020 #include <libexplain/output.h>
00021 
00022 #include <lib/colon/passwd/functor.h>
00023 #include <lib/colon/passwd/slurp.h>
00024 #include <lib/symtab.h>
00025 
00026 
00027 colon_passwd_slurp::~colon_passwd_slurp()
00028 {
00029 }
00030 
00031 
00032 colon_passwd_slurp::colon_passwd_slurp(const rcstring &a_filename) :
00033     colon_passwd(a_filename)
00034 {
00035 }
00036 
00037 
00038 colon_passwd_slurp::pointer
00039 colon_passwd_slurp::create(const rcstring &a_filename)
00040 {
00041     return pointer(new colon_passwd_slurp(a_filename));
00042 }
00043 
00044 
00045 bool
00046 colon_passwd_slurp::process(const record::pointer &rp)
00047 {
00048     assert(rp);
00049 
00050     {
00051         rcstring lc_name = rp->get_name().downcase();
00052         by_name_t::iterator it = by_name.find(lc_name);
00053         if (it != by_name.end())
00054         {
00055             error
00056             (
00057                 rp->get_source_location(),
00058                 "duplicate %s user name...",
00059                 rp->get_name().quote_c().c_str()
00060             );
00061             error
00062             (
00063                 it->second->get_source_location(),
00064                 "... here is the first %s user name",
00065                 it->second->get_name().quote_c().c_str()
00066             );
00067             // don't cascade duplicate errors, e.g. uid, home dir
00068             return false;
00069         }
00070         by_name.insert(by_name_t::value_type(lc_name, rp));
00071     }
00072 
00073     {
00074         by_uid_t::iterator it = by_uid.find(rp->get_uid());
00075         if (it != by_uid.end())
00076         {
00077             error
00078             (
00079                 rp->get_source_location(),
00080                 "user %s: duplicate %s user id...",
00081                 rp->get_name().quote_c().c_str(),
00082                 rp->get_uid().quote_c().c_str()
00083             );
00084             error
00085             (
00086                 it->second->get_source_location(),
00087                 "... here is the first %s user id (user %s)",
00088                 it->second->get_uid().quote_c().c_str(),
00089                 it->second->get_name().quote_c().c_str()
00090             );
00091             // don't cascade duplicate errors, e.g. home dir
00092             return false;
00093         }
00094         by_uid.insert(by_uid_t::value_type(rp->get_uid(), rp));
00095     }
00096 
00097     {
00098         by_home_t::iterator it = by_home.find(rp->get_home());
00099         if (it != by_home.end())
00100         {
00101             error
00102             (
00103                 rp->get_source_location(),
00104                 "user %s: duplicate %s home directory...",
00105                 rp->get_name().quote_c().c_str(),
00106                 rp->get_home().quote_c().c_str()
00107             );
00108             error
00109             (
00110                 it->second->get_source_location(),
00111                 "... here is the first %s home directory (user %s)",
00112                 it->second->get_home().quote_c().c_str(),
00113                 it->second->get_name().quote_c().c_str()
00114             );
00115             // don't cascade duplicate errors
00116             return false;
00117         }
00118         by_home.insert(by_home_t::value_type(rp->get_home(), rp));
00119     }
00120 
00121     // derived classes can keep processing
00122     return true;
00123 }
00124 
00125 
00126 void
00127 colon_passwd_slurp::read_and_process(void)
00128 {
00129     for (;;)
00130     {
00131         record::pointer rp = get();
00132         if (!rp)
00133             break;
00134         process(rp);
00135     }
00136     // do not close() here, let the derived class, or the destructor, do it.
00137 }
00138 
00139 
00140 colon_passwd::record::pointer
00141 colon_passwd_slurp::query_by_name(const rcstring &name)
00142     const
00143 {
00144     by_name_t::const_iterator it = by_name.find(name.downcase());
00145     if (it == by_name.end())
00146         return record::pointer();
00147     return it->second;
00148 }
00149 
00150 
00151 colon_passwd::record::pointer
00152 colon_passwd_slurp::query_by_uid(const rcstring &uid)
00153     const
00154 {
00155     by_uid_t::const_iterator it = by_uid.find(uid);
00156     if (it == by_uid.end())
00157         return record::pointer();
00158     return it->second;
00159 }
00160 
00161 
00162 colon_passwd::record::pointer
00163 colon_passwd_slurp::query_by_uid(long uid)
00164     const
00165 {
00166     return query_by_uid(rcstring::format("%ld", uid));
00167 }
00168 
00169 
00170 void
00171 colon_passwd_slurp::walk(colon_passwd_functor &func)
00172     const
00173 {
00174     for
00175     (
00176         by_name_t::const_iterator it = by_name.begin();
00177         it != by_name.end();
00178         ++it
00179     )
00180     {
00181         record::pointer rp = it->second;
00182         assert(rp);
00183         func(rp);
00184     }
00185 }
00186 
00187 
00188 // vim: set ts=8 sw=4 et :