rename.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /* Work around rename bugs in some systems.
  2. Copyright (C) 2001-2003, 2005-2006, 2009-2013 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* Written by Volker Borchert, Eric Blake. */
  14. #include <config.h>
  15. #include <stdio.h>
  16. #undef rename
  17. #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  18. /* The mingw rename has problems with trailing slashes; it also
  19. requires use of native Windows calls to allow atomic renames over
  20. existing files. */
  21. # include <errno.h>
  22. # include <stdbool.h>
  23. # include <stdlib.h>
  24. # include <sys/stat.h>
  25. # include <unistd.h>
  26. # define WIN32_LEAN_AND_MEAN
  27. # include <windows.h>
  28. # include "dirname.h"
  29. /* Rename the file SRC to DST. This replacement is necessary on
  30. Windows, on which the system rename function will not replace
  31. an existing DST. */
  32. int
  33. rpl_rename (char const *src, char const *dst)
  34. {
  35. int error;
  36. size_t src_len = strlen (src);
  37. size_t dst_len = strlen (dst);
  38. char *src_base = last_component (src);
  39. char *dst_base = last_component (dst);
  40. bool src_slash;
  41. bool dst_slash;
  42. bool dst_exists;
  43. struct stat src_st;
  44. struct stat dst_st;
  45. /* Filter out dot as last component. */
  46. if (!src_len || !dst_len)
  47. {
  48. errno = ENOENT;
  49. return -1;
  50. }
  51. if (*src_base == '.')
  52. {
  53. size_t len = base_len (src_base);
  54. if (len == 1 || (len == 2 && src_base[1] == '.'))
  55. {
  56. errno = EINVAL;
  57. return -1;
  58. }
  59. }
  60. if (*dst_base == '.')
  61. {
  62. size_t len = base_len (dst_base);
  63. if (len == 1 || (len == 2 && dst_base[1] == '.'))
  64. {
  65. errno = EINVAL;
  66. return -1;
  67. }
  68. }
  69. /* Presence of a trailing slash requires directory semantics. If
  70. the source does not exist, or if the destination cannot be turned
  71. into a directory, give up now. Otherwise, strip trailing slashes
  72. before calling rename. There are no symlinks on mingw, so stat
  73. works instead of lstat. */
  74. src_slash = ISSLASH (src[src_len - 1]);
  75. dst_slash = ISSLASH (dst[dst_len - 1]);
  76. if (stat (src, &src_st))
  77. return -1;
  78. if (stat (dst, &dst_st))
  79. {
  80. if (errno != ENOENT || (!S_ISDIR (src_st.st_mode) && dst_slash))
  81. return -1;
  82. dst_exists = false;
  83. }
  84. else
  85. {
  86. if (S_ISDIR (dst_st.st_mode) != S_ISDIR (src_st.st_mode))
  87. {
  88. errno = S_ISDIR (dst_st.st_mode) ? EISDIR : ENOTDIR;
  89. return -1;
  90. }
  91. dst_exists = true;
  92. }
  93. /* There are no symlinks, so if a file existed with a trailing
  94. slash, it must be a directory, and we don't have to worry about
  95. stripping strip trailing slash. However, mingw refuses to
  96. replace an existing empty directory, so we have to help it out.
  97. And canonicalize_file_name is not yet ported to mingw; however,
  98. for directories, getcwd works as a viable alternative. Ensure
  99. that we can get back to where we started before using it; later
  100. attempts to return are fatal. Note that we can end up losing a
  101. directory if rename then fails, but it was empty, so not much
  102. damage was done. */
  103. if (dst_exists && S_ISDIR (dst_st.st_mode))
  104. {
  105. char *cwd = getcwd (NULL, 0);
  106. char *src_temp;
  107. char *dst_temp;
  108. if (!cwd || chdir (cwd))
  109. return -1;
  110. if (IS_ABSOLUTE_FILE_NAME (src))
  111. {
  112. dst_temp = chdir (dst) ? NULL : getcwd (NULL, 0);
  113. src_temp = chdir (src) ? NULL : getcwd (NULL, 0);
  114. }
  115. else
  116. {
  117. src_temp = chdir (src) ? NULL : getcwd (NULL, 0);
  118. if (!IS_ABSOLUTE_FILE_NAME (dst) && chdir (cwd))
  119. abort ();
  120. dst_temp = chdir (dst) ? NULL : getcwd (NULL, 0);
  121. }
  122. if (chdir (cwd))
  123. abort ();
  124. free (cwd);
  125. if (!src_temp || !dst_temp)
  126. {
  127. free (src_temp);
  128. free (dst_temp);
  129. errno = ENOMEM;
  130. return -1;
  131. }
  132. src_len = strlen (src_temp);
  133. if (strncmp (src_temp, dst_temp, src_len) == 0
  134. && (ISSLASH (dst_temp[src_len]) || dst_temp[src_len] == '\0'))
  135. {
  136. error = dst_temp[src_len];
  137. free (src_temp);
  138. free (dst_temp);
  139. if (error)
  140. {
  141. errno = EINVAL;
  142. return -1;
  143. }
  144. return 0;
  145. }
  146. if (rmdir (dst))
  147. {
  148. error = errno;
  149. free (src_temp);
  150. free (dst_temp);
  151. errno = error;
  152. return -1;
  153. }
  154. free (src_temp);
  155. free (dst_temp);
  156. }
  157. /* MoveFileEx works if SRC is a directory without any flags, but
  158. fails with MOVEFILE_REPLACE_EXISTING, so try without flags first.
  159. Thankfully, MoveFileEx handles hard links correctly, even though
  160. rename() does not. */
  161. if (MoveFileEx (src, dst, 0))
  162. return 0;
  163. /* Retry with MOVEFILE_REPLACE_EXISTING if the move failed
  164. due to the destination already existing. */
  165. error = GetLastError ();
  166. if (error == ERROR_FILE_EXISTS || error == ERROR_ALREADY_EXISTS)
  167. {
  168. if (MoveFileEx (src, dst, MOVEFILE_REPLACE_EXISTING))
  169. return 0;
  170. error = GetLastError ();
  171. }
  172. switch (error)
  173. {
  174. case ERROR_FILE_NOT_FOUND:
  175. case ERROR_PATH_NOT_FOUND:
  176. case ERROR_BAD_PATHNAME:
  177. case ERROR_DIRECTORY:
  178. errno = ENOENT;
  179. break;
  180. case ERROR_ACCESS_DENIED:
  181. case ERROR_SHARING_VIOLATION:
  182. errno = EACCES;
  183. break;
  184. case ERROR_OUTOFMEMORY:
  185. errno = ENOMEM;
  186. break;
  187. case ERROR_CURRENT_DIRECTORY:
  188. errno = EBUSY;
  189. break;
  190. case ERROR_NOT_SAME_DEVICE:
  191. errno = EXDEV;
  192. break;
  193. case ERROR_WRITE_PROTECT:
  194. errno = EROFS;
  195. break;
  196. case ERROR_WRITE_FAULT:
  197. case ERROR_READ_FAULT:
  198. case ERROR_GEN_FAILURE:
  199. errno = EIO;
  200. break;
  201. case ERROR_HANDLE_DISK_FULL:
  202. case ERROR_DISK_FULL:
  203. case ERROR_DISK_TOO_FRAGMENTED:
  204. errno = ENOSPC;
  205. break;
  206. case ERROR_FILE_EXISTS:
  207. case ERROR_ALREADY_EXISTS:
  208. errno = EEXIST;
  209. break;
  210. case ERROR_BUFFER_OVERFLOW:
  211. case ERROR_FILENAME_EXCED_RANGE:
  212. errno = ENAMETOOLONG;
  213. break;
  214. case ERROR_INVALID_NAME:
  215. case ERROR_DELETE_PENDING:
  216. errno = EPERM; /* ? */
  217. break;
  218. # ifndef ERROR_FILE_TOO_LARGE
  219. /* This value is documented but not defined in all versions of windows.h. */
  220. # define ERROR_FILE_TOO_LARGE 223
  221. # endif
  222. case ERROR_FILE_TOO_LARGE:
  223. errno = EFBIG;
  224. break;
  225. default:
  226. errno = EINVAL;
  227. break;
  228. }
  229. return -1;
  230. }
  231. #else /* ! W32 platform */
  232. # include <errno.h>
  233. # include <stdio.h>
  234. # include <stdlib.h>
  235. # include <string.h>
  236. # include <sys/stat.h>
  237. # include <unistd.h>
  238. # include "dirname.h"
  239. # include "same-inode.h"
  240. /* Rename the file SRC to DST, fixing any trailing slash bugs. */
  241. int
  242. rpl_rename (char const *src, char const *dst)
  243. {
  244. size_t src_len = strlen (src);
  245. size_t dst_len = strlen (dst);
  246. char *src_temp = (char *) src;
  247. char *dst_temp = (char *) dst;
  248. bool src_slash;
  249. bool dst_slash;
  250. bool dst_exists;
  251. int ret_val = -1;
  252. int rename_errno = ENOTDIR;
  253. struct stat src_st;
  254. struct stat dst_st;
  255. if (!src_len || !dst_len)
  256. return rename (src, dst); /* Let strace see the ENOENT failure. */
  257. # if RENAME_DEST_EXISTS_BUG
  258. {
  259. char *src_base = last_component (src);
  260. char *dst_base = last_component (dst);
  261. if (*src_base == '.')
  262. {
  263. size_t len = base_len (src_base);
  264. if (len == 1 || (len == 2 && src_base[1] == '.'))
  265. {
  266. errno = EINVAL;
  267. return -1;
  268. }
  269. }
  270. if (*dst_base == '.')
  271. {
  272. size_t len = base_len (dst_base);
  273. if (len == 1 || (len == 2 && dst_base[1] == '.'))
  274. {
  275. errno = EINVAL;
  276. return -1;
  277. }
  278. }
  279. }
  280. # endif /* RENAME_DEST_EXISTS_BUG */
  281. src_slash = src[src_len - 1] == '/';
  282. dst_slash = dst[dst_len - 1] == '/';
  283. # if !RENAME_HARD_LINK_BUG && !RENAME_DEST_EXISTS_BUG
  284. /* If there are no trailing slashes, then trust the native
  285. implementation unless we also suspect issues with hard link
  286. detection or file/directory conflicts. */
  287. if (!src_slash && !dst_slash)
  288. return rename (src, dst);
  289. # endif /* !RENAME_HARD_LINK_BUG && !RENAME_DEST_EXISTS_BUG */
  290. /* Presence of a trailing slash requires directory semantics. If
  291. the source does not exist, or if the destination cannot be turned
  292. into a directory, give up now. Otherwise, strip trailing slashes
  293. before calling rename. */
  294. if (lstat (src, &src_st))
  295. return -1;
  296. if (lstat (dst, &dst_st))
  297. {
  298. if (errno != ENOENT || (!S_ISDIR (src_st.st_mode) && dst_slash))
  299. return -1;
  300. dst_exists = false;
  301. }
  302. else
  303. {
  304. if (S_ISDIR (dst_st.st_mode) != S_ISDIR (src_st.st_mode))
  305. {
  306. errno = S_ISDIR (dst_st.st_mode) ? EISDIR : ENOTDIR;
  307. return -1;
  308. }
  309. # if RENAME_HARD_LINK_BUG
  310. if (SAME_INODE (src_st, dst_st))
  311. return 0;
  312. # endif /* RENAME_HARD_LINK_BUG */
  313. dst_exists = true;
  314. }
  315. # if (RENAME_TRAILING_SLASH_SOURCE_BUG || RENAME_DEST_EXISTS_BUG \
  316. || RENAME_HARD_LINK_BUG)
  317. /* If the only bug was that a trailing slash was allowed on a
  318. non-existing file destination, as in Solaris 10, then we've
  319. already covered that situation. But if there is any problem with
  320. a trailing slash on an existing source or destination, as in
  321. Solaris 9, or if a directory can overwrite a symlink, as on
  322. Cygwin 1.5, or if directories cannot be created with trailing
  323. slash, as on NetBSD 1.6, then we must strip the offending slash
  324. and check that we have not encountered a symlink instead of a
  325. directory.
  326. Stripping a trailing slash interferes with POSIX semantics, where
  327. rename behavior on a symlink with a trailing slash operates on
  328. the corresponding target directory. We prefer the GNU semantics
  329. of rejecting any use of a symlink with trailing slash, but do not
  330. enforce them, since Solaris 10 is able to obey POSIX semantics
  331. and there might be clients expecting it, as counter-intuitive as
  332. those semantics are.
  333. Technically, we could also follow the POSIX behavior by chasing a
  334. readlink trail, but that is harder to implement. */
  335. if (src_slash)
  336. {
  337. src_temp = strdup (src);
  338. if (!src_temp)
  339. {
  340. /* Rather than rely on strdup-posix, we set errno ourselves. */
  341. rename_errno = ENOMEM;
  342. goto out;
  343. }
  344. strip_trailing_slashes (src_temp);
  345. if (lstat (src_temp, &src_st))
  346. {
  347. rename_errno = errno;
  348. goto out;
  349. }
  350. if (S_ISLNK (src_st.st_mode))
  351. goto out;
  352. }
  353. if (dst_slash)
  354. {
  355. dst_temp = strdup (dst);
  356. if (!dst_temp)
  357. {
  358. rename_errno = ENOMEM;
  359. goto out;
  360. }
  361. strip_trailing_slashes (dst_temp);
  362. if (lstat (dst_temp, &dst_st))
  363. {
  364. if (errno != ENOENT)
  365. {
  366. rename_errno = errno;
  367. goto out;
  368. }
  369. }
  370. else if (S_ISLNK (dst_st.st_mode))
  371. goto out;
  372. }
  373. # endif /* RENAME_TRAILING_SLASH_SOURCE_BUG || RENAME_DEST_EXISTS_BUG
  374. || RENAME_HARD_LINK_BUG */
  375. # if RENAME_DEST_EXISTS_BUG
  376. /* Cygwin 1.5 sometimes behaves oddly when moving a non-empty
  377. directory on top of an empty one (the old directory name can
  378. reappear if the new directory tree is removed). Work around this
  379. by removing the target first, but don't remove the target if it
  380. is a subdirectory of the source. Note that we can end up losing
  381. a directory if rename then fails, but it was empty, so not much
  382. damage was done. */
  383. if (dst_exists && S_ISDIR (dst_st.st_mode))
  384. {
  385. if (src_st.st_dev != dst_st.st_dev)
  386. {
  387. rename_errno = EXDEV;
  388. goto out;
  389. }
  390. if (src_temp != src)
  391. free (src_temp);
  392. src_temp = canonicalize_file_name (src);
  393. if (dst_temp != dst)
  394. free (dst_temp);
  395. dst_temp = canonicalize_file_name (dst);
  396. if (!src_temp || !dst_temp)
  397. {
  398. rename_errno = ENOMEM;
  399. goto out;
  400. }
  401. src_len = strlen (src_temp);
  402. if (strncmp (src_temp, dst_temp, src_len) == 0
  403. && dst_temp[src_len] == '/')
  404. {
  405. rename_errno = EINVAL;
  406. goto out;
  407. }
  408. if (rmdir (dst))
  409. {
  410. rename_errno = errno;
  411. goto out;
  412. }
  413. }
  414. # endif /* RENAME_DEST_EXISTS_BUG */
  415. ret_val = rename (src_temp, dst_temp);
  416. rename_errno = errno;
  417. out:
  418. if (src_temp != src)
  419. free (src_temp);
  420. if (dst_temp != dst)
  421. free (dst_temp);
  422. errno = rename_errno;
  423. return ret_val;
  424. }
  425. #endif /* ! W32 platform */