Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

297 lines
8.3 KiB

  1. /* $Source: /u/mark/src/pax/RCS/link.c,v $
  2. *
  3. * $Revision: 1.2 $
  4. *
  5. * link.c - functions for handling multiple file links
  6. *
  7. * DESCRIPTION
  8. *
  9. * These function manage the link chains which are used to keep track
  10. * of outstanding links during archive reading and writing.
  11. *
  12. * AUTHOR
  13. *
  14. * Mark H. Colburn, NAPS International (mark@jhereg.mn.org)
  15. *
  16. * Sponsored by The USENIX Association for public distribution.
  17. *
  18. * Copyright (c) 1989 Mark H. Colburn.
  19. * All rights reserved.
  20. *
  21. * Redistribution and use in source and binary forms are permitted
  22. * provided that the above copyright notice is duplicated in all such
  23. * forms and that any documentation, advertising materials, and other
  24. * materials related to such distribution and use acknowledge that the
  25. * software was developed * by Mark H. Colburn and sponsored by The
  26. * USENIX Association.
  27. *
  28. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  29. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  30. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  31. *
  32. * $Log: link.c,v $
  33. * Revision 1.2 89/02/12 10:04:38 mark
  34. * 1.2 release fixes
  35. *
  36. * Revision 1.1 88/12/23 18:02:12 mark
  37. * Initial revision
  38. *
  39. */
  40. #ifndef lint
  41. static char *ident = "$Id: link.c,v 1.2 89/02/12 10:04:38 mark Exp $";
  42. static char *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
  43. #endif /* ! lint */
  44. /* Headers */
  45. #include "pax.h"
  46. /* Defines */
  47. /*
  48. * Address link information base.
  49. */
  50. #define LINKHASH(ino) (linkbase + (ino) % NEL(linkbase))
  51. /*
  52. * Number of array elements.
  53. */
  54. #define NEL(a) (sizeof(a) / sizeof(*(a)))
  55. /* Internal Identifiers */
  56. static Link *linkbase[256]; /* Unresolved link information */
  57. /* linkfrom - find a file to link from
  58. *
  59. * DESCRIPTION
  60. *
  61. * Linkfrom searches the link chain to see if there is a file in the
  62. * link chain which has the same inode number as the file specified
  63. * by the stat block pointed at by asb. If a file is found, the
  64. * name is returned to the caller, otherwise a NULL is returned.
  65. *
  66. * PARAMETERS
  67. *
  68. * char *name - name of the file which we are attempting
  69. * to find a link for
  70. * Stat *asb - stat structure of file to find a link to
  71. *
  72. * RETURNS
  73. *
  74. * Returns a pointer to a link structure, or NULL if unsuccessful.
  75. *
  76. */
  77. Link *linkfrom(char *name, Stat *asb)
  78. {
  79. Link *linkp;
  80. Link *linknext;
  81. Path *path;
  82. Path *pathnext;
  83. Link **abase;
  84. #ifdef DF_TRACE_DEBUG
  85. printf("DF_TRACE_DEBUG: Link *linkfrom() in link.c\n");
  86. #endif
  87. for (linkp = *(abase = LINKHASH(asb->sb_ino)); linkp; linkp = linknext) {
  88. if (linkp->l_nlink == 0) {
  89. if (linkp->l_name) {
  90. free((char *) linkp->l_name);
  91. }
  92. if (linknext = linkp->l_forw) {
  93. linknext->l_back = linkp->l_back;
  94. }
  95. if (linkp->l_back) {
  96. linkp->l_back->l_forw = linkp->l_forw;
  97. }
  98. free((char *) linkp);
  99. *abase = (Link *)NULL;
  100. } else if (linkp->l_ino == asb->sb_ino && linkp->l_dev == asb->sb_dev) {
  101. /*
  102. * check to see if a file with the name "name" exists in the
  103. * chain of files which we have for this particular link
  104. */
  105. for (path = linkp->l_path; path; path = pathnext) {
  106. if (strcmp(path->p_name, name) == 0) {
  107. --linkp->l_nlink;
  108. if (path->p_name) {
  109. free(path->p_name);
  110. }
  111. if (pathnext = path->p_forw) {
  112. pathnext->p_back = path->p_back;
  113. }
  114. if (path->p_back) {
  115. path->p_back->p_forw = pathnext;
  116. }
  117. if (linkp->l_path == path) {
  118. linkp->l_path = pathnext;
  119. }
  120. free(path);
  121. return (linkp);
  122. }
  123. pathnext = path->p_forw;
  124. }
  125. return ((Link *)NULL);
  126. } else {
  127. linknext = linkp->l_forw;
  128. }
  129. }
  130. return ((Link *)NULL);
  131. }
  132. /* islink - determine whether a given file really a link
  133. *
  134. * DESCRIPTION
  135. *
  136. * Islink searches the link chain to see if there is a file in the
  137. * link chain which has the same inode number as the file specified
  138. * by the stat block pointed at by asb. If a file is found, a
  139. * non-zero value is returned to the caller, otherwise a 0 is
  140. * returned.
  141. *
  142. * PARAMETERS
  143. *
  144. * char *name - name of file to check to see if it is link.
  145. * Stat *asb - stat structure of file to find a link to
  146. *
  147. * RETURNS
  148. *
  149. * Returns a pointer to a link structure, or NULL if unsuccessful.
  150. *
  151. */
  152. Link *islink(char *name, Stat *asb)
  153. {
  154. Link *linkp;
  155. Link *linknext;
  156. #ifdef DF_TRACE_DEBUG
  157. printf("DF_TRACE_DEBUG: Link *islink() in link.c\n");
  158. #endif
  159. for (linkp = *(LINKHASH(asb->sb_ino)); linkp; linkp = linknext) {
  160. if (linkp->l_ino == asb->sb_ino && linkp->l_dev == asb->sb_dev) {
  161. if (strcmp(name, linkp->l_name) == 0) {
  162. return ((Link *)NULL);
  163. }
  164. return (linkp);
  165. } else {
  166. linknext = linkp->l_forw;
  167. }
  168. }
  169. return ((Link *)NULL);
  170. }
  171. /* linkto - remember a file with outstanding links
  172. *
  173. * DESCRIPTION
  174. *
  175. * Linkto adds the specified file to the link chain. Any subsequent
  176. * calls to linkfrom which have the same inode will match the file
  177. * just entered. If not enough space is available to make the link
  178. * then the item is not added to the link chain, and a NULL is
  179. * returned to the calling function.
  180. *
  181. * PARAMETERS
  182. *
  183. * char *name - name of file to remember
  184. * Stat *asb - pointer to stat structure of file to remember
  185. *
  186. * RETURNS
  187. *
  188. * Returns a pointer to the associated link structure, or NULL when
  189. * linking is not possible.
  190. *
  191. */
  192. Link *linkto(char *name, Stat *asb)
  193. {
  194. Link *linkp;
  195. Link *linknext;
  196. Path *path;
  197. Link **abase;
  198. #ifdef DF_TRACE_DEBUG
  199. printf("DF_TRACE_DEBUG: Link *linkto() in link.c\n");
  200. #endif
  201. for (linkp = *(LINKHASH(asb->sb_ino)); linkp; linkp = linknext) {
  202. if (linkp->l_ino == asb->sb_ino && linkp->l_dev == asb->sb_dev) {
  203. if ((path = (Path *) mem_get(sizeof(Path))) == (Path *)NULL ||
  204. (path->p_name = mem_str(name)) == (char *)NULL) {
  205. #ifdef DF_TRACE_DEBUG
  206. printf("DF_TRACE_DEBUG: () in link.c\n");
  207. #endif
  208. return ((Link *)NULL);
  209. }
  210. if (path->p_forw = linkp->l_path) {
  211. if (linkp->l_path->p_forw) {
  212. linkp->l_path->p_forw->p_back = path;
  213. }
  214. } else {
  215. linkp->l_path = path;
  216. }
  217. path->p_back = (Path *)NULL;
  218. return (linkp);
  219. } else {
  220. linknext = linkp->l_forw;
  221. }
  222. }
  223. /*
  224. * This is a brand new link, for which there is no other information
  225. */
  226. if ((asb->sb_mode & S_IFMT) == S_IFDIR
  227. || (linkp = (Link *) mem_get(sizeof(Link))) == (Link *)NULL
  228. || (linkp->l_name = mem_str(name)) == (char *)NULL) {
  229. return ((Link *)NULL);
  230. }
  231. linkp->l_dev = asb->sb_dev;
  232. linkp->l_ino = asb->sb_ino;
  233. linkp->l_nlink = (ushort)(asb->sb_nlink - 1);
  234. linkp->l_size = asb->sb_size;
  235. linkp->l_path = (Path *)NULL;
  236. if (linkp->l_forw = *(abase = LINKHASH(asb->sb_ino))) {
  237. linkp->l_forw->l_back = linkp;
  238. } else {
  239. *abase = linkp;
  240. }
  241. linkp->l_back = (Link *)NULL;
  242. return (linkp);
  243. }
  244. /* linkleft - complain about files with unseen links
  245. *
  246. * DESCRIPTION
  247. *
  248. * Linksleft scans through the link chain to see if there were any
  249. * files which have outstanding links that were not processed by the
  250. * archive. For each file in the link chain for which there was not
  251. * a file, and error message is printed.
  252. */
  253. void linkleft(void)
  254. {
  255. Link *lp;
  256. Link **base;
  257. #ifdef DF_TRACE_DEBUG
  258. printf("DF_TRACE_DEBUG: void linkleft() in link.c\n");
  259. #endif
  260. for (base = linkbase; base < linkbase + NEL(linkbase); ++base) {
  261. for (lp = *base; lp; lp = lp->l_forw) {
  262. if (lp->l_nlink) {
  263. warn(lp->l_path->p_name, "Unseen link(s)");
  264. }
  265. }
  266. }
  267. }