日期:2014-05-16  浏览次数:20640 次

关于qmail源码中的cdb_seek函数问题
这个函数我始终看不懂它要实现一个什么功能,请高手帮忙解答下
这个函数我始终看不懂它要实现一个什么功能,请高手帮忙解答
int cdb_seek(fd,key,len,dlen)
int fd;
char *key;
unsigned int len;
uint32 *dlen;
{
  char packbuf[8];
  uint32 pos;
  uint32 h;
  uint32 lenhash;
  uint32 h2;
  uint32 loop;
  uint32 poskd;

  h = cdb_hash(key,len);  

  pos = 8 * (h & 255);
  if (lseek(fd,(off_t) pos,SEEK_SET) == -1) return -1;

  if (cdb_bread(fd,packbuf,8) == -1) return -1;

  pos = cdb_unpack(packbuf);
  lenhash = cdb_unpack(packbuf + 4);

  if (!lenhash) return 0;
  h2 = (h >> 8) % lenhash;

  for (loop = 0;loop < lenhash;++loop) {
  if (lseek(fd,(off_t) (pos + 8 * h2),SEEK_SET) == -1) return -1;
  if (cdb_bread(fd,packbuf,8) == -1) return -1;
  poskd = cdb_unpack(packbuf + 4);
  if (!poskd) return 0;
  if (cdb_unpack(packbuf) == h) {
  if (lseek(fd,(off_t) poskd,SEEK_SET) == -1) return -1;
  if (cdb_bread(fd,packbuf,8) == -1) return -1;
  if (cdb_unpack(packbuf) == len)
switch(match(fd,key,len)) {
case -1:
return -1;
case 1:
*dlen = cdb_unpack(packbuf + 4);
return 1;
}
  }
  if (++h2 == lenhash) h2 = 0;
  }
  return 0;
}

------解决方案--------------------
cdb_seek(fd,key,len,dlen)
大概意思在fd句柄指的cdb格式文件中,查找key值和其len,找到相应的位置返回给dlen指针,同时返回1,没有找到返回-1,返回0表示已经是最好。没有细看,难免有误