cctools
copy_tree.h
1/*
2 * Copyright (C) 2022 The University of Notre Dame
3 * This software is distributed under the GNU General Public License.
4 * See the file COPYING for details.
5 */
6
7#ifndef COPY_TREE_H
8#define COPY_TREE_H
9
10/* copy_dir copies the source dir into target. Like 'cp -r source target'.
11 * @param source: the source directory, which must be an existing directory.
12 * @param target: the target directory
13 * @return zero on success, non-zero on failure.
14 * If target does not exist, create the dir target and copy all the entries under the source dir into the target dir;
15 * If target exists, create a sub-dir basename(source) under target, and copy all the entries under the source dir into target/basename(source).
16 */
17int copy_dir(const char *source, const char *target);
18
19/* copy_symlink copies the symlink source to target.
20 * @param source: the source, which must be an existing symlink.
21 * @param target: the target, which must be non-exist.
22 * @return zero on success, non-zero on failure.
23 */
24int copy_symlink(const char *source, const char *target);
25
26/* copy_direntry copies the source dir or file into target. Like 'cp -r source target'.
27 * @param source: the source directory/file, which must be an existing directory/file.
28 * @param target: the target directory/file
29 * @return zero on success, non-zero on failure.
30 * If target does not exist, create the dir target and copy all the entries under the source dir into the target dir;
31 * If target exists, create a sub-dir basename(source) under target, and copy all the entries under the source dir into target/basename(source).
32 */
33int copy_direntry(const char *source, const char *target);
34
35/* Only copy regular files, directories, and symlinks. */
36typedef enum {
37 FILE_TYPE_REG,
38 FILE_TYPE_LNK,
39 FILE_TYPE_DIR,
40 FILE_TYPE_UNSUPPORTED
41} file_type;
42
43/* check_file_type checks the file types and whether the copying of the file type is supported.
44 * @param source: a file path.
45 * @return a file_type value denoting the file type of source.
46 */
47file_type check_file_type(const char *source);
48
49/* get_exist_ancestor_dir gets the closest existing ancestor dir.
50 * @param s: s will be modified during the exeution of the function, and can not be in text segement.
51 * If s = "a/b/c/d", and only d does not exist, returns "a/b/c".
52 * If s is an absolute path, in the worst case, the return string should be "/".
53 * If s is a relative path, and no any part of s exists, return an empty string.
54 * The caller should free the result string.
55 */
56char *get_exist_ancestor_dir(const char *s);
57
58/* is_subdir checks whether target is a (sub)directory of source.
59 * is_subdir finds the closest existing ancestor directory of target, and check whether it is a (sub)directory of source.
60 * source must exist, target must not exist.
61 * return -1 if source can not be copied, return 0 if source can be copied.
62 */
63int is_subdir(const char *source, const char *target);
64
65#endif
66
67/* vim: set noexpandtab tabstop=8: */