copy.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* core functions for copying files and directories
  2. Copyright (C) 1989-2018 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. /* Extracted from cp.c and librarified by Jim Meyering. */
  14. #ifndef COPY_H
  15. # define COPY_H
  16. # include <stdbool.h>
  17. # include "hash.h"
  18. /* Control creation of sparse files (files with holes). */
  19. enum Sparse_type
  20. {
  21. SPARSE_UNUSED,
  22. /* Never create holes in DEST. */
  23. SPARSE_NEVER,
  24. /* This is the default. Use a crude (and sometimes inaccurate)
  25. heuristic to determine if SOURCE has holes. If so, try to create
  26. holes in DEST. */
  27. SPARSE_AUTO,
  28. /* For every sufficiently long sequence of bytes in SOURCE, try to
  29. create a corresponding hole in DEST. There is a performance penalty
  30. here because CP has to search for holes in SRC. But if the holes are
  31. big enough, that penalty can be offset by the decrease in the amount
  32. of data written to disk. */
  33. SPARSE_ALWAYS
  34. };
  35. /* Control creation of COW files. */
  36. enum Reflink_type
  37. {
  38. /* Default to a standard copy. */
  39. REFLINK_NEVER,
  40. /* Try a COW copy and fall back to a standard copy. */
  41. REFLINK_AUTO,
  42. /* Require a COW copy and fail if not available. */
  43. REFLINK_ALWAYS
  44. };
  45. /* This type is used to help mv (via copy.c) distinguish these cases. */
  46. enum Interactive
  47. {
  48. I_ALWAYS_YES = 1,
  49. I_ALWAYS_NO,
  50. I_ASK_USER,
  51. I_UNSPECIFIED
  52. };
  53. /* How to handle symbolic links. */
  54. enum Dereference_symlink
  55. {
  56. DEREF_UNDEFINED = 1,
  57. /* Copy the symbolic link itself. -P */
  58. DEREF_NEVER,
  59. /* If the symbolic is a command line argument, then copy
  60. its referent. Otherwise, copy the symbolic link itself. -H */
  61. DEREF_COMMAND_LINE_ARGUMENTS,
  62. /* Copy the referent of the symbolic link. -L */
  63. DEREF_ALWAYS
  64. };
  65. # define VALID_SPARSE_MODE(Mode) \
  66. ((Mode) == SPARSE_NEVER \
  67. || (Mode) == SPARSE_AUTO \
  68. || (Mode) == SPARSE_ALWAYS)
  69. # define VALID_REFLINK_MODE(Mode) \
  70. ((Mode) == REFLINK_NEVER \
  71. || (Mode) == REFLINK_AUTO \
  72. || (Mode) == REFLINK_ALWAYS)
  73. /* These options control how files are copied by at least the
  74. following programs: mv (when rename doesn't work), cp, install.
  75. So, if you add a new member, be sure to initialize it in
  76. mv.c, cp.c, and install.c. */
  77. struct cp_options
  78. {
  79. enum backup_type backup_type;
  80. /* How to handle symlinks in the source. */
  81. enum Dereference_symlink dereference;
  82. /* This value is used to determine whether to prompt before removing
  83. each existing destination file. It works differently depending on
  84. whether move_mode is set. See code/comments in copy.c. */
  85. enum Interactive interactive;
  86. /* Control creation of sparse files. */
  87. enum Sparse_type sparse_mode;
  88. /* Set the mode of the destination file to exactly this value
  89. if SET_MODE is nonzero. */
  90. mode_t mode;
  91. /* If true, copy all files except (directories and, if not dereferencing
  92. them, symbolic links,) as if they were regular files. */
  93. bool copy_as_regular;
  94. /* If true, remove each existing destination nondirectory before
  95. trying to open it. */
  96. bool unlink_dest_before_opening;
  97. /* If true, first try to open each existing destination nondirectory,
  98. then, if the open fails, unlink and try again.
  99. This option must be set for 'cp -f', in case the destination file
  100. exists when the open is attempted. It is irrelevant to 'mv' since
  101. any destination is sure to be removed before the open. */
  102. bool unlink_dest_after_failed_open;
  103. /* If true, create hard links instead of copying files.
  104. Create destination directories as usual. */
  105. bool hard_link;
  106. /* If true, rather than copying, first attempt to use rename.
  107. If that fails, then resort to copying. */
  108. bool move_mode;
  109. /* If true, install(1) is the caller. */
  110. bool install_mode;
  111. /* Whether this process has appropriate privileges to chown a file
  112. whose owner is not the effective user ID. */
  113. bool chown_privileges;
  114. /* Whether this process has appropriate privileges to do the
  115. following operations on a file even when it is owned by some
  116. other user: set the file's atime, mtime, mode, or ACL; remove or
  117. rename an entry in the file even though it is a sticky directory,
  118. or to mount on the file. */
  119. bool owner_privileges;
  120. /* If true, when copying recursively, skip any subdirectories that are
  121. on different file systems from the one we started on. */
  122. bool one_file_system;
  123. /* If true, attempt to give the copies the original files' permissions,
  124. owner, group, and timestamps. */
  125. bool preserve_ownership;
  126. bool preserve_mode;
  127. bool preserve_timestamps;
  128. bool explicit_no_preserve_mode;
  129. /* If true, attempt to set specified security context */
  130. bool set_security_context;
  131. /* Enabled for mv, and for cp by the --preserve=links option.
  132. If true, attempt to preserve in the destination files any
  133. logical hard links between the source files. If used with cp's
  134. --no-dereference option, and copying two hard-linked files,
  135. the two corresponding destination files will also be hard linked.
  136. If used with cp's --dereference (-L) option, then, as that option implies,
  137. hard links are *not* preserved. However, when copying a file F and
  138. a symlink S to F, the resulting S and F in the destination directory
  139. will be hard links to the same file (a copy of F). */
  140. bool preserve_links;
  141. /* Optionally don't copy the data, either with CoW reflink files or
  142. explicitly with the --attributes-only option. */
  143. bool data_copy_required;
  144. /* If true and any of the above (for preserve) file attributes cannot
  145. be applied to a destination file, treat it as a failure and return
  146. nonzero immediately. E.g. for cp -p this must be true, for mv it
  147. must be false. */
  148. bool require_preserve;
  149. /* If true, attempt to preserve the SELinux security context, too.
  150. Set this only if the kernel is SELinux enabled. */
  151. bool preserve_security_context;
  152. /* Useful only when preserve_context is true.
  153. If true, a failed attempt to preserve file's security context
  154. propagates failure "out" to the caller, along with full diagnostics.
  155. If false, a failure to preserve file's security context does not
  156. change the invoking application's exit status, but may output diagnostics.
  157. For example, with 'cp --preserve=context' this flag is "true",
  158. while with 'cp --preserve=all' or 'cp -a', it is "false". */
  159. bool require_preserve_context;
  160. /* If true, attempt to preserve extended attributes using libattr.
  161. Ignored if coreutils are compiled without xattr support. */
  162. bool preserve_xattr;
  163. /* Useful only when preserve_xattr is true.
  164. If true, a failed attempt to preserve file's extended attributes
  165. propagates failure "out" to the caller, along with full diagnostics.
  166. If false, a failure to preserve file's extended attributes does not
  167. change the invoking application's exit status, but may output diagnostics.
  168. For example, with 'cp --preserve=xattr' this flag is "true",
  169. while with 'cp --preserve=all' or 'cp -a', it is "false". */
  170. bool require_preserve_xattr;
  171. /* This allows us to output warnings in cases 2 and 4 below,
  172. while being quiet for case 1 (when reduce_diagnostics is true).
  173. 1. cp -a try to copy xattrs with no errors
  174. 2. cp --preserve=all copy xattrs with all but ENOTSUP warnings
  175. 3. cp --preserve=xattr,context copy xattrs with all errors
  176. 4. mv copy xattrs with all but ENOTSUP warnings
  177. */
  178. bool reduce_diagnostics;
  179. /* If true, copy directories recursively and copy special files
  180. as themselves rather than copying their contents. */
  181. bool recursive;
  182. /* If true, set file mode to value of MODE. Otherwise,
  183. set it based on current umask modified by UMASK_KILL. */
  184. bool set_mode;
  185. /* If true, create symbolic links instead of copying files.
  186. Create destination directories as usual. */
  187. bool symbolic_link;
  188. /* If true, do not copy a nondirectory that has an existing destination
  189. with the same or newer modification time. */
  190. bool update;
  191. /* If true, display the names of the files before copying them. */
  192. bool verbose;
  193. /* If true, stdin is a tty. */
  194. bool stdin_tty;
  195. /* If true, open a dangling destination symlink when not in move_mode.
  196. Otherwise, copy_reg gives a diagnostic (it refuses to write through
  197. such a symlink) and returns false. */
  198. bool open_dangling_dest_symlink;
  199. /* If true, this is the last filed to be copied. mv uses this to
  200. avoid some unnecessary work. */
  201. bool last_file;
  202. /* Zero if the source has already been renamed to the destination; a
  203. positive errno number if this failed with the given errno; -1 if
  204. no attempt has been made to rename. Always -1, except for mv. */
  205. int rename_errno;
  206. /* Control creation of COW files. */
  207. enum Reflink_type reflink_mode;
  208. /* This is a set of destination name/inode/dev triples. Each such triple
  209. represents a file we have created corresponding to a source file name
  210. that was specified on the command line. Use it to avoid clobbering
  211. source files in commands like this:
  212. rm -rf a b c; mkdir a b c; touch a/f b/f; mv a/f b/f c
  213. For now, it protects only regular files when copying (i.e., not renaming).
  214. When renaming, it protects all non-directories.
  215. Use dest_info_init to initialize it, or set it to NULL to disable
  216. this feature. */
  217. Hash_table *dest_info;
  218. /* FIXME */
  219. Hash_table *src_info;
  220. };
  221. # define XSTAT(X, Src_name, Src_sb) \
  222. ((X)->dereference == DEREF_NEVER \
  223. ? lstat (Src_name, Src_sb) \
  224. : stat (Src_name, Src_sb))
  225. /* Arrange to make rename calls go through the wrapper function
  226. on systems with a rename function that fails for a source file name
  227. specified with a trailing slash. */
  228. # if RENAME_TRAILING_SLASH_BUG
  229. int rpl_rename (const char *, const char *);
  230. # undef rename
  231. # define rename rpl_rename
  232. # endif
  233. bool copy (char const *src_name, char const *dst_name,
  234. bool nonexistent_dst, const struct cp_options *options,
  235. bool *copy_into_self, bool *rename_succeeded);
  236. extern bool set_process_security_ctx (char const *src_name,
  237. char const *dst_name,
  238. mode_t mode, bool new_dst,
  239. const struct cp_options *x);
  240. extern bool set_file_security_ctx (char const *dst_name, bool process_local,
  241. bool recurse, const struct cp_options *x);
  242. void dest_info_init (struct cp_options *);
  243. void src_info_init (struct cp_options *);
  244. void cp_options_default (struct cp_options *);
  245. bool chown_failure_ok (struct cp_options const *) _GL_ATTRIBUTE_PURE;
  246. mode_t cached_umask (void);
  247. #endif