Source

sbbdep / src / sbbdep / lddirs.cpp

Full commit
/*
--------------Copyright (c) 2011-2013 H a r a l d  A c h i t z---------------
-----------< h a r a l d dot a c h i t z at g m a i l dot c o m >------------
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/


#include <sbbdep/lddirs.hpp>

#include <sbbdep/path.hpp>
#include <sbbdep/dircontent.hpp>
#include <sbbdep/error.hpp>
#include <sbbdep/log.hpp>
#include <sbbdep/elffile.hpp>

#include <fstream>
#include <string>
#include <boost/algorithm/string/trim.hpp>



namespace sbbdep
{


LDDirs::LDDirs()
{
}
//--------------------------------------------------------------------------------------------------

LDDirs::~LDDirs()
{
}
//-------------------------------------------------------------------------------------------------- 

auto
LDDirs::readLdDirs() -> const LDDirs::StringSet&
{
  m_lddirs.clear();
  m_lddirs.insert("/lib");
  if(Path("/lib64").isFolder() )m_lddirs.insert("/lib64");
  m_lddirs.insert("/usr/lib");
  if(Path("/usr/lib64").isFolder() )m_lddirs.insert("/usr/lib64");

  std::ifstream ldso_conf("/etc/ld.so.conf");

  if( ldso_conf.is_open() )
    {
      for( std::string line ; std::getline(ldso_conf, line) ; )
        {
          std::size_t commentpos = line.find_first_of("#") ;
          if( commentpos != std::string::npos )
            line.erase(commentpos) ;
          std::string dirname = boost::algorithm::trim_copy(line) ;
          if( dirname.size() )
            { //  folder path could also be a link
              Path p(dirname) ;
              p.makeRealPath() ;
              if( p.isFolder() )
                m_lddirs.insert(p.getURL()) ;
            }
        }
    }
  else
    {
      throw ErrGeneric("unable to read /etc/ld.so.conf") ;
    }
  
  
  return m_lddirs; 
}

int64_t
LDDirs::getLdSoConfTime()
{
  Path p("/etc/ld.so.conf");

  if(p.isValid())
    return p.getLastModificationTime();

  return 0 ;
}

//--------------------------------------------------------------------------------------------------


/*
 * get all directory names that have files with link in an lddir....
 */
auto
LDDirs::readLdLinkDirs() -> const LDDirs::StringSet&
{
  if(m_lddirs.empty()) // ensure loading cause need it later to filter what already exists
    readLdDirs();

  m_ldlnkdirs.clear();
  if(m_lddirs.size()== 0 )readLdDirs();

  for (StringSet::const_iterator pos=m_lddirs.begin(); pos!=m_lddirs.end(); ++pos)
    {
      DirContent dir(*pos) ;
      try
        {
          dir.Open();
        }
      catch ( const Error& e )
        {
          LogError()<< "read linked dirs, unable to read " << *pos << std::endl;
          //throw ErrStat("unable to open dir " + *pos);
        }
      
      std::string contend;
      while (dir.getNext(contend))
        {  
          if (contend == "." || contend == "..") continue;
          Path path(dir.getDirName() + "/" + contend);
          if ( path.isLink() )
            {
              std::string orig = path.Str();
              path.makeRealPath();
              if (path.isRegularFile() && isElfLib( path ) )
                { // filter out what is a regular ld dir , TODO since this is a set lookup not reuqired
                  if(m_lddirs.find(path.getDir())== m_lddirs.end())
                    m_ldlnkdirs.insert(path.getDir());

                  // TODO , why did I do this ? only for debug ??
                  if(path.getDir()=="/usr/bin" || path.getDir()=="/usr/sbin")
                    LogInfo()<<"in bin: " << orig << "->" << path.getURL() << std::endl;
                }
            }
          
        }      
      
    }
  
  return m_ldlnkdirs; 
}
//--------------------------------------------------------------------------------------------------  


//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
}