rename.c 13 KB

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