cctools
copy_tree.h
1 /*
2  * Copyright (C) 2016- 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  */
17 int 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  */
24 int copy_symlink(const char *source, const char *target);
25 
26 /* Only copy regular files, directories, and symlinks. */
27 typedef enum {
28  FILE_TYPE_REG,
29  FILE_TYPE_LNK,
30  FILE_TYPE_DIR,
31  FILE_TYPE_UNSUPPORTED
32 } file_type;
33 
34 /* check_file_type checks the file types and whether the copying of the file type is supported.
35  * @param source: a file path.
36  * @return a file_type value denoting the file type of source.
37  */
38 file_type check_file_type(const char *source);
39 
40 /* get_exist_ancestor_dir gets the closest existing ancestor dir.
41  * @param s: s will be modified during the exeution of the function, and can not be in text segement.
42  * If s = "a/b/c/d", and only d does not exist, returns "a/b/c".
43  * If s is an absolute path, in the worst case, the return string should be "/".
44  * If s is a relative path, and no any part of s exists, return an empty string.
45  * The caller should free the result string.
46  */
47 char *get_exist_ancestor_dir(const char *s);
48 
49 /* is_subdir checks whether target is a (sub)directory of source.
50  * is_subdir finds the closest existing ancestor directory of target, and check whether it is a (sub)directory of source.
51  * source must exist, target must not exist.
52  * return -1 if source can not be copied, return 0 if source can be copied.
53  */
54 int is_subdir(const char *source, const char *target);
55 
56 #endif
57 
58 /* vim: set noexpandtab tabstop=4: */