00001 /* 00002 * Copyright (C) 2016- The University of Notre Dame 00003 * This software is distributed under the GNU General Public License. 00004 * See the file COPYING for details. 00005 */ 00006 00007 #ifndef COPY_TREE_H 00008 #define COPY_TREE_H 00009 00010 /* copy_dir copies the source dir into target. Like 'cp -r source target'. 00011 * @param source: the source directory, which must be an existing directory. 00012 * @param target: the target directory 00013 * @return zero on success, non-zero on failure. 00014 * If target does not exist, create the dir target and copy all the entries under the source dir into the target dir; 00015 * If target exists, create a sub-dir basename(source) under target, and copy all the entries under the source dir into target/basename(source). 00016 */ 00017 int copy_dir(const char *source, const char *target); 00018 00019 /* copy_symlink copies the symlink source to target. 00020 * @param source: the source, which must be an existing symlink. 00021 * @param target: the target, which must be non-exist. 00022 * @return zero on success, non-zero on failure. 00023 */ 00024 int copy_symlink(const char *source, const char *target); 00025 00026 /* Only copy regular files, directories, and symlinks. */ 00027 typedef enum { 00028 FILE_TYPE_REG, 00029 FILE_TYPE_LNK, 00030 FILE_TYPE_DIR, 00031 FILE_TYPE_UNSUPPORTED 00032 } file_type; 00033 00034 /* check_file_type checks the file types and whether the copying of the file type is supported. 00035 * @param source: a file path. 00036 * @return a file_type value denoting the file type of source. 00037 */ 00038 file_type check_file_type(const char *source); 00039 00040 /* get_exist_ancestor_dir gets the closest existing ancestor dir. 00041 * @param s: s will be modified during the exeution of the function, and can not be in text segement. 00042 * If s = "a/b/c/d", and only d does not exist, returns "a/b/c". 00043 * If s is an absolute path, in the worst case, the return string should be "/". 00044 * If s is a relative path, and no any part of s exists, return an empty string. 00045 * The caller should free the result string. 00046 */ 00047 char *get_exist_ancestor_dir(const char *s); 00048 00049 /* is_subdir checks whether target is a (sub)directory of source. 00050 * is_subdir finds the closest existing ancestor directory of target, and check whether it is a (sub)directory of source. 00051 * source must exist, target must not exist. 00052 * return -1 if source can not be copied, return 0 if source can be copied. 00053 */ 00054 int is_subdir(const char *source, const char *target); 00055 00056 #endif 00057 00058 /* vim: set noexpandtab tabstop=4: */