Imported Upstream version 1.1.11
[platform/upstream/cdrkit.git] / 3rd-party / zisofs_tools / hash.c
1 /*
2  * This file has been modified for the cdrkit suite.
3  *
4  * The behaviour and appearence of the program code below can differ to a major
5  * extent from the version distributed by the original author(s).
6  *
7  * For details, see Changelog file distributed with the cdrkit package. If you
8  * received this file from another source then ask the distributing person for
9  * a log of modifications.
10  *
11  */
12
13 #ident "$Id: hash.c,v 1.3 2006/07/04 04:57:42 hpa Exp $"
14 /* ----------------------------------------------------------------------- *
15  *   
16  *   Copyright 2001 H. Peter Anvin - All Rights Reserved
17  *
18  *   This program is free software; you can redistribute it and/or modify
19  *   it under the terms of the GNU General Public License as published by
20  *   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
21  *   USA; either version 2 of the License, or (at your option) any later
22  *   version; incorporated herein by reference.
23  *
24  * ----------------------------------------------------------------------- */
25
26 /*
27  * hash.c
28  *
29  * Hash table used to find hard-linked files
30  */
31
32 #include "mkzftree.h"           /* Must be included first! */
33
34 #define HASH_BUCKETS      2683
35
36 struct file_hash {
37   struct file_hash *next;
38   struct stat st;
39   const char *outfile_name;
40 };
41
42 static struct file_hash *hashp[HASH_BUCKETS];
43
44 const char *hash_find_file(struct stat *st)
45 {
46   int bucket = (st->st_ino + st->st_dev) % HASH_BUCKETS;
47   struct file_hash *hp;
48
49   for ( hp = hashp[bucket] ; hp ; hp = hp->next ) {
50     if ( hp->st.st_ino   == st->st_ino &&
51          hp->st.st_dev   == st->st_dev &&
52          hp->st.st_mode  == st->st_mode &&
53          hp->st.st_nlink == st->st_nlink &&
54          hp->st.st_uid   == st->st_uid &&
55          hp->st.st_gid   == st->st_gid &&
56          hp->st.st_size  == st->st_size &&
57          hp->st.st_mtime == st->st_mtime ) {
58       /* Good enough, it's the same file */
59       return hp->outfile_name;
60     }
61   }
62   return NULL;                  /* No match */
63 }
64
65 /* Note: the stat structure is the input file; the name
66    is the output file to link to */
67 void hash_insert_file(struct stat *st, const char *outfile)
68 {
69   int bucket = (st->st_ino + st->st_dev) % HASH_BUCKETS;
70   struct file_hash *hp = xmalloc(sizeof(struct file_hash));
71
72   hp->next         = hashp[bucket];
73   memcpy(&hp->st, st, sizeof(struct stat));
74   hp->outfile_name = xstrdup(outfile);
75
76   hashp[bucket]    = hp;
77 }
78
79