libfile: Add copy_recursive
To recursively copy a directory tree. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
parent
3249006a2f
commit
9f11887009
|
@ -15,6 +15,8 @@ int write_file(const char *filename, void *buf, size_t size);
|
||||||
|
|
||||||
int copy_file(const char *src, const char *dst, int verbose);
|
int copy_file(const char *src, const char *dst, int verbose);
|
||||||
|
|
||||||
|
int copy_recursive(const char *src, const char *dst);
|
||||||
|
|
||||||
int compare_file(const char *f1, const char *f2);
|
int compare_file(const char *f1, const char *f2);
|
||||||
|
|
||||||
#endif /* __LIBFILE_H */
|
#endif /* __LIBFILE_H */
|
||||||
|
|
|
@ -332,6 +332,46 @@ out:
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL(copy_file);
|
EXPORT_SYMBOL(copy_file);
|
||||||
|
|
||||||
|
int copy_recursive(const char *src, const char *dst)
|
||||||
|
{
|
||||||
|
struct stat s;
|
||||||
|
DIR *dir;
|
||||||
|
struct dirent *d;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
ret = stat(src, &s);
|
||||||
|
if (ret)
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
if (!S_ISDIR(s.st_mode))
|
||||||
|
return copy_file(src, dst, 0);
|
||||||
|
|
||||||
|
ret = make_directory(dst);
|
||||||
|
if (ret)
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
dir = opendir(src);
|
||||||
|
if (!dir)
|
||||||
|
return -EIO;
|
||||||
|
|
||||||
|
while ((d = readdir(dir))) {
|
||||||
|
char *from, *to;
|
||||||
|
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
from = asprintf("%s/%s", src, d->d_name);
|
||||||
|
to = asprintf("%s/%s", dst, d->d_name);
|
||||||
|
ret = copy_recursive(from, to);
|
||||||
|
if (ret)
|
||||||
|
break;
|
||||||
|
free(from);
|
||||||
|
free(to);
|
||||||
|
}
|
||||||
|
closedir(dir);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* compare_file - Compare two files
|
* compare_file - Compare two files
|
||||||
* @f1: The first file
|
* @f1: The first file
|
||||||
|
|
Loading…
Reference in New Issue