hash.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * File hash.c - generate hash tables for iso9660 filesystem.
  3. Written by Eric Youngdale (1993).
  4. Copyright 1993 Yggdrasil Computing, Incorporated
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  16. #include <stdlib.h>
  17. #include "config.h"
  18. #include "mkisofs.h"
  19. #define NR_HASH 1024
  20. #define HASH_FN(DEV, INO) ((DEV + INO + (INO >> 2) + (INO << 8)) % NR_HASH)
  21. static struct file_hash * hash_table[NR_HASH] = {0,};
  22. void FDECL1(add_hash, struct directory_entry *, spnt){
  23. struct file_hash * s_hash;
  24. unsigned int hash_number;
  25. if(spnt->size == 0 || spnt->starting_block == 0)
  26. if(spnt->size != 0 || spnt->starting_block != 0) {
  27. fprintf(stderr,"Non zero-length file assigned zero extent.\n");
  28. exit(1);
  29. };
  30. if (spnt->dev == (dev_t) UNCACHED_DEVICE || spnt->inode == UNCACHED_INODE) return;
  31. hash_number = HASH_FN((unsigned int) spnt->dev, (unsigned int) spnt->inode);
  32. #if 0
  33. if (verbose > 1) fprintf(stderr,"%s ",spnt->name);
  34. #endif
  35. s_hash = (struct file_hash *) e_malloc(sizeof(struct file_hash));
  36. s_hash->next = hash_table[hash_number];
  37. s_hash->inode = spnt->inode;
  38. s_hash->dev = spnt->dev;
  39. s_hash->starting_block = spnt->starting_block;
  40. s_hash->size = spnt->size;
  41. hash_table[hash_number] = s_hash;
  42. }
  43. struct file_hash * FDECL2(find_hash, dev_t, dev, ino_t, inode){
  44. unsigned int hash_number;
  45. struct file_hash * spnt;
  46. hash_number = HASH_FN((unsigned int) dev, (unsigned int) inode);
  47. if (dev == (dev_t) UNCACHED_DEVICE || inode == UNCACHED_INODE) return NULL;
  48. spnt = hash_table[hash_number];
  49. while(spnt){
  50. if(spnt->inode == inode && spnt->dev == dev) return spnt;
  51. spnt = spnt->next;
  52. };
  53. return NULL;
  54. }
  55. static struct file_hash * directory_hash_table[NR_HASH] = {0,};
  56. void FDECL2(add_directory_hash, dev_t, dev, ino_t, inode){
  57. struct file_hash * s_hash;
  58. unsigned int hash_number;
  59. if (dev == (dev_t) UNCACHED_DEVICE || inode == UNCACHED_INODE) return;
  60. hash_number = HASH_FN((unsigned int) dev, (unsigned int) inode);
  61. s_hash = (struct file_hash *) e_malloc(sizeof(struct file_hash));
  62. s_hash->next = directory_hash_table[hash_number];
  63. s_hash->inode = inode;
  64. s_hash->dev = dev;
  65. directory_hash_table[hash_number] = s_hash;
  66. }
  67. struct file_hash * FDECL2(find_directory_hash, dev_t, dev, ino_t, inode){
  68. unsigned int hash_number;
  69. struct file_hash * spnt;
  70. hash_number = HASH_FN((unsigned int) dev, (unsigned int) inode);
  71. if (dev == (dev_t) UNCACHED_DEVICE || inode == UNCACHED_INODE) return NULL;
  72. spnt = directory_hash_table[hash_number];
  73. while(spnt){
  74. if(spnt->inode == inode && spnt->dev == dev) return spnt;
  75. spnt = spnt->next;
  76. };
  77. return NULL;
  78. }
  79. struct name_hash
  80. {
  81. struct name_hash * next;
  82. struct directory_entry * de;
  83. };
  84. #define NR_NAME_HASH 128
  85. static struct name_hash * name_hash_table[NR_NAME_HASH] = {0,};
  86. /*
  87. * Find the hash bucket for this name.
  88. */
  89. static unsigned int FDECL1(name_hash, const char *, name)
  90. {
  91. unsigned int hash = 0;
  92. const char * p;
  93. p = name;
  94. while (*p)
  95. {
  96. /*
  97. * Don't hash the iso9660 version number. This way
  98. * we can detect duplicates in cases where we have
  99. * directories (i.e. foo) and non-directories
  100. * (i.e. foo;1).
  101. */
  102. if( *p == ';' )
  103. {
  104. break;
  105. }
  106. hash = (hash << 15) + (hash << 3) + (hash >> 3) + *p++;
  107. }
  108. return hash % NR_NAME_HASH;
  109. }
  110. void FDECL1(add_file_hash, struct directory_entry *, de){
  111. struct name_hash * new;
  112. int hash;
  113. new = (struct name_hash *) e_malloc(sizeof(struct name_hash));
  114. new->de = de;
  115. new->next = NULL;
  116. hash = name_hash(de->isorec.name);
  117. /* Now insert into the hash table */
  118. new->next = name_hash_table[hash];
  119. name_hash_table[hash] = new;
  120. }
  121. struct directory_entry * FDECL1(find_file_hash, char *, name)
  122. {
  123. struct name_hash * nh;
  124. char * p1;
  125. char * p2;
  126. for(nh = name_hash_table[name_hash(name)]; nh; nh = nh->next)
  127. {
  128. p1 = name;
  129. p2 = nh->de->isorec.name;
  130. /*
  131. * Look for end of string, or a mismatch.
  132. */
  133. while(1==1)
  134. {
  135. if( (*p1 == '\0' || *p1 == ';')
  136. || (*p2 == '\0' || *p2 == ';')
  137. || (*p1 != *p2) )
  138. {
  139. break;
  140. }
  141. p1++;
  142. p2++;
  143. }
  144. /*
  145. * If we are at the end of both strings, then
  146. * we have a match.
  147. */
  148. if( (*p1 == '\0' || *p1 == ';')
  149. && (*p2 == '\0' || *p2 == ';') )
  150. {
  151. return nh->de;
  152. }
  153. }
  154. return NULL;
  155. }
  156. int FDECL1(delete_file_hash, struct directory_entry *, de){
  157. struct name_hash * nh, *prev;
  158. int hash;
  159. prev = NULL;
  160. hash = name_hash(de->isorec.name);
  161. for(nh = name_hash_table[hash]; nh; nh = nh->next) {
  162. if(nh->de == de) break;
  163. prev = nh;
  164. }
  165. if(!nh) return 1;
  166. if(!prev)
  167. name_hash_table[hash] = nh->next;
  168. else
  169. prev->next = nh->next;
  170. free(nh);
  171. return 0;
  172. }
  173. void flush_file_hash(){
  174. struct name_hash * nh, *nh1;
  175. int i;
  176. for(i=0; i<NR_NAME_HASH; i++) {
  177. nh = name_hash_table[i];
  178. while(nh) {
  179. nh1 = nh->next;
  180. free(nh);
  181. nh = nh1;
  182. }
  183. name_hash_table[i] = NULL;
  184. }
  185. }