4
0

unlink.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* Unlink files.
  2. Copyright 2009, 2013-2014 Free Software Foundation, Inc.
  3. This file is part of GNU tar.
  4. GNU tar is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. GNU tar is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <system.h>
  15. #include "common.h"
  16. #include <quotearg.h>
  17. struct deferred_unlink
  18. {
  19. struct deferred_unlink *next; /* Next unlink in the queue */
  20. int dir_idx; /* Directory index in wd */
  21. char *file_name; /* Name of the file to unlink, relative
  22. to dir_idx */
  23. bool is_dir; /* True if file_name is a directory */
  24. off_t records_written; /* Number of records written when this
  25. entry got added to the queue */
  26. };
  27. #define IS_CWD(p) \
  28. ((p)->is_dir \
  29. && ((p)->file_name[0] == 0 || strcmp ((p)->file_name, ".") == 0))
  30. /* The unlink queue */
  31. static struct deferred_unlink *dunlink_head, *dunlink_tail;
  32. /* Number of entries in the queue */
  33. static size_t dunlink_count;
  34. /* List of entries available for allocation */
  35. static struct deferred_unlink *dunlink_avail;
  36. /* Delay (number of records written) between adding entry to the
  37. list and its actual removal. */
  38. static size_t deferred_unlink_delay = 0;
  39. static struct deferred_unlink *
  40. dunlink_alloc (void)
  41. {
  42. struct deferred_unlink *p;
  43. if (dunlink_avail)
  44. {
  45. p = dunlink_avail;
  46. dunlink_avail = p->next;
  47. p->next = NULL;
  48. }
  49. else
  50. p = xmalloc (sizeof (*p));
  51. return p;
  52. }
  53. static void
  54. dunlink_insert (struct deferred_unlink *anchor, struct deferred_unlink *p)
  55. {
  56. if (anchor)
  57. {
  58. p->next = anchor->next;
  59. anchor->next = p;
  60. }
  61. else
  62. {
  63. p->next = dunlink_head;
  64. dunlink_head = p;
  65. }
  66. if (!p->next)
  67. dunlink_tail = p;
  68. dunlink_count++;
  69. }
  70. static void
  71. dunlink_reclaim (struct deferred_unlink *p)
  72. {
  73. free (p->file_name);
  74. p->next = dunlink_avail;
  75. dunlink_avail = p;
  76. }
  77. static void
  78. flush_deferred_unlinks (bool force)
  79. {
  80. struct deferred_unlink *p, *prev = NULL;
  81. int saved_chdir = chdir_current;
  82. for (p = dunlink_head; p; )
  83. {
  84. struct deferred_unlink *next = p->next;
  85. if (force
  86. || records_written > p->records_written + deferred_unlink_delay)
  87. {
  88. chdir_do (p->dir_idx);
  89. if (p->is_dir)
  90. {
  91. const char *fname;
  92. if (p->dir_idx && IS_CWD (p))
  93. {
  94. prev = p;
  95. p = next;
  96. continue;
  97. }
  98. else
  99. fname = p->file_name;
  100. if (unlinkat (chdir_fd, fname, AT_REMOVEDIR) != 0)
  101. {
  102. switch (errno)
  103. {
  104. case ENOENT:
  105. /* nothing to worry about */
  106. break;
  107. case ENOTEMPTY:
  108. /* Keep the record in list, in the hope we'll
  109. be able to remove it later */
  110. prev = p;
  111. p = next;
  112. continue;
  113. default:
  114. rmdir_error (fname);
  115. }
  116. }
  117. }
  118. else
  119. {
  120. if (unlinkat (chdir_fd, p->file_name, 0) != 0 && errno != ENOENT)
  121. unlink_error (p->file_name);
  122. }
  123. dunlink_reclaim (p);
  124. dunlink_count--;
  125. p = next;
  126. if (prev)
  127. prev->next = p;
  128. else
  129. dunlink_head = p;
  130. }
  131. else
  132. {
  133. prev = p;
  134. p = next;
  135. }
  136. }
  137. if (!dunlink_head)
  138. dunlink_tail = NULL;
  139. else if (force)
  140. {
  141. for (p = dunlink_head; p; )
  142. {
  143. struct deferred_unlink *next = p->next;
  144. const char *fname;
  145. chdir_do (p->dir_idx);
  146. if (p->dir_idx && IS_CWD (p))
  147. {
  148. fname = tar_dirname ();
  149. chdir_do (p->dir_idx - 1);
  150. }
  151. else
  152. fname = p->file_name;
  153. if (unlinkat (chdir_fd, fname, AT_REMOVEDIR) != 0)
  154. {
  155. if (errno != ENOENT)
  156. rmdir_error (fname);
  157. }
  158. dunlink_reclaim (p);
  159. dunlink_count--;
  160. p = next;
  161. }
  162. dunlink_head = dunlink_tail = NULL;
  163. }
  164. chdir_do (saved_chdir);
  165. }
  166. void
  167. finish_deferred_unlinks (void)
  168. {
  169. flush_deferred_unlinks (true);
  170. while (dunlink_avail)
  171. {
  172. struct deferred_unlink *next = dunlink_avail->next;
  173. free (dunlink_avail);
  174. dunlink_avail = next;
  175. }
  176. }
  177. void
  178. queue_deferred_unlink (const char *name, bool is_dir)
  179. {
  180. struct deferred_unlink *p;
  181. if (dunlink_head
  182. && records_written > dunlink_head->records_written + deferred_unlink_delay)
  183. flush_deferred_unlinks (false);
  184. p = dunlink_alloc ();
  185. p->next = NULL;
  186. p->dir_idx = chdir_current;
  187. p->file_name = xstrdup (name);
  188. normalize_filename_x (p->file_name);
  189. p->is_dir = is_dir;
  190. p->records_written = records_written;
  191. if (IS_CWD (p))
  192. {
  193. struct deferred_unlink *q, *prev;
  194. for (q = dunlink_head, prev = NULL; q; prev = q, q = q->next)
  195. if (IS_CWD (q) && q->dir_idx < p->dir_idx)
  196. break;
  197. if (q)
  198. dunlink_insert (prev, p);
  199. else
  200. dunlink_insert (dunlink_tail, p);
  201. }
  202. else
  203. dunlink_insert (dunlink_tail, p);
  204. }