Blame src/IdNameCache.cc

Packit Service a2489d
/*
Packit Service a2489d
 * lftp - file transfer program
Packit Service a2489d
 *
Packit Service a2489d
 * Copyright (c) 1996-2012 by Alexander V. Lukyanov (lav@yars.free.net)
Packit Service a2489d
 *
Packit Service a2489d
 * This program is free software; you can redistribute it and/or modify
Packit Service a2489d
 * it under the terms of the GNU General Public License as published by
Packit Service a2489d
 * the Free Software Foundation; either version 3 of the License, or
Packit Service a2489d
 * (at your option) any later version.
Packit Service a2489d
 *
Packit Service a2489d
 * This program is distributed in the hope that it will be useful,
Packit Service a2489d
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service a2489d
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service a2489d
 * GNU General Public License for more details.
Packit Service a2489d
 *
Packit Service a2489d
 * You should have received a copy of the GNU General Public License
Packit Service a2489d
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit Service a2489d
 */
Packit Service a2489d
Packit Service a2489d
#include <config.h>
Packit Service a2489d
#include <pwd.h>
Packit Service a2489d
#include <grp.h>
Packit Service a2489d
#include <string.h>
Packit Service a2489d
#include <ctype.h>
Packit Service a2489d
#include "IdNameCache.h"
Packit Service a2489d
Packit Service a2489d
void IdNameCache::init()
Packit Service a2489d
{
Packit Service a2489d
   memset(table_id,0,sizeof(table_id));
Packit Service a2489d
   memset(table_name,0,sizeof(table_name));
Packit Service a2489d
}
Packit Service a2489d
void IdNameCache::free_list(IdNamePair *list)
Packit Service a2489d
{
Packit Service a2489d
   while(list)
Packit Service a2489d
   {
Packit Service a2489d
      IdNamePair *next=list->next;
Packit Service a2489d
      delete list;
Packit Service a2489d
      list=next;
Packit Service a2489d
   }
Packit Service a2489d
}
Packit Service a2489d
void IdNameCache::free()
Packit Service a2489d
{
Packit Service a2489d
   for(int i=0; i
Packit Service a2489d
   {
Packit Service a2489d
      free_list(table_id[i]);
Packit Service a2489d
      free_list(table_name[i]);
Packit Service a2489d
   }
Packit Service a2489d
}
Packit Service a2489d
void IdNameCache::add(unsigned h,IdNamePair **p,IdNamePair *r)
Packit Service a2489d
{
Packit Service a2489d
   r->next=p[h];
Packit Service a2489d
   p[h]=r;
Packit Service a2489d
}
Packit Service a2489d
IdNamePair *IdNameCache::lookup(int id)
Packit Service a2489d
{
Packit Service a2489d
   unsigned h=hash(id);
Packit Service a2489d
   for(IdNamePair *scan=table_id[h]; scan; scan=scan->next)
Packit Service a2489d
      if(id==scan->id)
Packit Service a2489d
	 return scan;
Packit Service a2489d
   IdNamePair *r=get_record(id);
Packit Service a2489d
   if(!r)
Packit Service a2489d
      r=new IdNamePair(id,0);
Packit Service a2489d
   add(h,table_id,r);
Packit Service a2489d
   if(r->name)
Packit Service a2489d
      add(hash(r->name),table_name,new IdNamePair(r));
Packit Service a2489d
   return r;
Packit Service a2489d
}
Packit Service a2489d
IdNamePair *IdNameCache::lookup(const char *name)
Packit Service a2489d
{
Packit Service a2489d
   unsigned h=hash(name);
Packit Service a2489d
   for(IdNamePair *scan=table_name[h]; scan; scan=scan->next)
Packit Service a2489d
      if(!xstrcmp(name,scan->name))
Packit Service a2489d
	 return scan;
Packit Service a2489d
   IdNamePair *r=get_record(name);
Packit Service a2489d
   if(!r)
Packit Service a2489d
      r=new IdNamePair(-1,name);
Packit Service a2489d
   add(h,table_name,r);
Packit Service a2489d
   if(r->id!=-1)
Packit Service a2489d
      add(hash(r->id),table_id,new IdNamePair(r));
Packit Service a2489d
   return r;
Packit Service a2489d
}
Packit Service a2489d
const char *IdNameCache::Lookup(int id)
Packit Service a2489d
{
Packit Service a2489d
   const char *name=lookup(id)->name;
Packit Service a2489d
   if(name && name[0])
Packit Service a2489d
      return name;
Packit Service a2489d
   static char buf[32];
Packit Service a2489d
   snprintf(buf,sizeof(buf),"%d",id);
Packit Service a2489d
   return buf;
Packit Service a2489d
}
Packit Service a2489d
int IdNameCache::Lookup(const char *name)
Packit Service a2489d
{
Packit Service a2489d
   return lookup(name)->id;
Packit Service a2489d
}
Packit Service a2489d
IdNameCache::IdNameCache()
Packit Service a2489d
{
Packit Service a2489d
   init();
Packit Service a2489d
}
Packit Service a2489d
IdNameCache::~IdNameCache()
Packit Service a2489d
{
Packit Service a2489d
   free();
Packit Service a2489d
}
Packit Service a2489d
int IdNameCache::Do()
Packit Service a2489d
{
Packit Service a2489d
   if(expire_timer && expire_timer->Stopped())
Packit Service a2489d
      Delete(this);
Packit Service a2489d
   return STALL;
Packit Service a2489d
}
Packit Service a2489d
Packit Service a2489d
unsigned IdNameCache::hash(int id)
Packit Service a2489d
{
Packit Service a2489d
   return unsigned(id)%table_size;
Packit Service a2489d
}
Packit Service a2489d
unsigned IdNameCache::hash(const char *name)
Packit Service a2489d
{
Packit Service a2489d
   unsigned h=0;
Packit Service a2489d
   while(*name)
Packit Service a2489d
      h+=(h<<4)+*name++;
Packit Service a2489d
   return h%table_size;
Packit Service a2489d
}
Packit Service a2489d
Packit Service a2489d
IdNamePair *PasswdCache::get_record(int id)
Packit Service a2489d
{
Packit Service a2489d
   struct passwd *p=getpwuid(id);
Packit Service a2489d
   if(!p)
Packit Service a2489d
      return 0;
Packit Service a2489d
   return new IdNamePair(p->pw_uid,p->pw_name);
Packit Service a2489d
}
Packit Service a2489d
IdNamePair *GroupCache::get_record(int id)
Packit Service a2489d
{
Packit Service a2489d
   struct group *p=getgrgid(id);
Packit Service a2489d
   if(!p)
Packit Service a2489d
      return 0;
Packit Service a2489d
   return new IdNamePair(p->gr_gid,p->gr_name);
Packit Service a2489d
}
Packit Service a2489d
Packit Service a2489d
IdNamePair *IdNameCache::get_record(const char *name)
Packit Service a2489d
{
Packit Service a2489d
   int id,n;
Packit Service a2489d
   if(sscanf(name,"%d%n",&id,&n)==1 && !name[n])
Packit Service a2489d
      return new IdNamePair(id,name);
Packit Service a2489d
   return 0;
Packit Service a2489d
}
Packit Service a2489d
IdNamePair *PasswdCache::get_record(const char *name)
Packit Service a2489d
{
Packit Service a2489d
   struct passwd *p=getpwnam(name);
Packit Service a2489d
   if(p)
Packit Service a2489d
      return new IdNamePair(p->pw_uid,name);
Packit Service a2489d
   return IdNameCache::get_record(name);
Packit Service a2489d
}
Packit Service a2489d
IdNamePair *GroupCache::get_record(const char *name)
Packit Service a2489d
{
Packit Service a2489d
   struct group *p=getgrnam(name);
Packit Service a2489d
   if(p)
Packit Service a2489d
      return new IdNamePair(p->gr_gid,name);
Packit Service a2489d
   return IdNameCache::get_record(name);
Packit Service a2489d
}
Packit Service a2489d
Packit Service a2489d
PasswdCache *PasswdCache::instance;
Packit Service a2489d
GroupCache *GroupCache::instance;
Packit Service a2489d
PasswdCache *PasswdCache::GetInstance()
Packit Service a2489d
{
Packit Service a2489d
   if(instance)
Packit Service a2489d
      return instance;
Packit Service a2489d
   instance=new PasswdCache();
Packit Service a2489d
   instance->SetExpireTimer(new Timer(30));
Packit Service a2489d
   return instance;
Packit Service a2489d
}
Packit Service a2489d
GroupCache *GroupCache::GetInstance()
Packit Service a2489d
{
Packit Service a2489d
   if(instance)
Packit Service a2489d
      return instance;
Packit Service a2489d
   instance=new GroupCache();
Packit Service a2489d
   instance->SetExpireTimer(new Timer(30));
Packit Service a2489d
   return instance;
Packit Service a2489d
}
Packit Service a2489d
PasswdCache::~PasswdCache()
Packit Service a2489d
{
Packit Service a2489d
   if(this==instance)
Packit Service a2489d
      instance=0;
Packit Service a2489d
}
Packit Service a2489d
GroupCache::~GroupCache()
Packit Service a2489d
{
Packit Service a2489d
   if(this==instance)
Packit Service a2489d
      instance=0;
Packit Service a2489d
}