From 0f2633d78cf945c616140890365c6c9fd98d9f21 Mon Sep 17 00:00:00 2001 From: Randy Heit Date: Wed, 23 Jul 2008 23:33:59 +0000 Subject: [PATCH] - Added Linux parts for the zipdir tool. SVN r1088 (trunk) --- tools/zipdir/CMakeLists.txt | 2 +- tools/zipdir/zipdir.c | 167 ++++++++++++++++++++++++++++-------- 2 files changed, 132 insertions(+), 37 deletions(-) diff --git a/tools/zipdir/CMakeLists.txt b/tools/zipdir/CMakeLists.txt index 1b188fa30f..177cd1eddd 100644 --- a/tools/zipdir/CMakeLists.txt +++ b/tools/zipdir/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required( VERSION 2.6 ) +cmake_minimum_required( VERSION 2.4 ) add_executable( zipdir zipdir.c ioapi.c diff --git a/tools/zipdir/zipdir.c b/tools/zipdir/zipdir.c index f1a1809127..043eca1bb7 100644 --- a/tools/zipdir/zipdir.c +++ b/tools/zipdir/zipdir.c @@ -30,8 +30,14 @@ // HEADER FILES ------------------------------------------------------------ #include -#include -#include +#include +#ifdef _WIN32 +#include +#define stat _stat +#else +#include +#include +#endif #include #include #include @@ -77,9 +83,12 @@ void print_usage(const char *cmdname); dir_tree_t *alloc_dir_tree(const char *dir); file_entry_t *alloc_file_entry(const char *prefix, const char *path, time_t last_written); void free_dir_tree(dir_tree_t *tree); -void free_dir_trees(dir_tree_t *tree); +void free_dir_trees(dir_tree_t *tree); +#ifdef _WIN32 void recurse_dir(dir_tree_t *tree, const char *dirpath); -dir_tree_t *add_dir(const char *dirpath); +dir_tree_t *add_dir(const char *dirpath); +#endif +dir_tree_t *add_dirs(char **argv); int count_files(dir_tree_t *trees); int __cdecl sort_cmp(const void *a, const void *b); file_sorted_t *sort_files(dir_tree_t *trees, int num_files); @@ -153,14 +162,18 @@ dir_tree_t *alloc_dir_tree(const char *dir) file_entry_t *alloc_file_entry(const char *prefix, const char *path, time_t last_written) { - file_entry_t *entry; + file_entry_t *entry; + char *p; entry = malloc(sizeof(file_entry_t) + strlen(prefix) + strlen(path) + 1); if (entry != NULL) { strcpy(entry->path, prefix); - strcat(entry->path, path); - strlwr(entry->path); + strcat(entry->path, path); + for (p = entry->path; *p != '\0'; ++p) + { + *p = tolower(*p); + } entry->next = NULL; entry->time_write = last_written; } @@ -204,6 +217,8 @@ void free_dir_trees(dir_tree_t *tree) free_dir_tree(tree); } } + +#ifdef _WIN32 //========================================================================== // @@ -212,10 +227,10 @@ void free_dir_trees(dir_tree_t *tree) //========================================================================== void recurse_dir(dir_tree_t *tree, const char *dirpath) -{ - struct _finddata_t fileinfo; - char *dirmatch; +{ + struct _finddata_t fileinfo; intptr_t handle; + char *dirmatch; dirmatch = malloc(strlen(dirpath) + 2); if (dirmatch == NULL) @@ -224,13 +239,13 @@ void recurse_dir(dir_tree_t *tree, const char *dirpath) return; } strcpy(dirmatch, dirpath); - strcat(dirmatch, "*"); - if ((handle = _findfirst(dirmatch, &fileinfo)) == -1) + strcat(dirmatch, "*"); + if ((handle = _findfirst(dirmatch, &fileinfo)) == -1) { fprintf(stderr, "Could not scan '%s': %s\n", dirpath, strerror(errno)); } else - { + { do { if (fileinfo.attrib & _A_HIDDEN) @@ -270,7 +285,7 @@ void recurse_dir(dir_tree_t *tree, const char *dirpath) tree->files = entry; } } while (_findnext(handle, &fileinfo) == 0); - _findclose(handle); + _findclose(handle); } free(dirmatch); } @@ -291,6 +306,101 @@ dir_tree_t *add_dir(const char *dirpath) } return tree; } + +//========================================================================== +// +// add_dirs +// Windows version +// +// Given NULL-terminated array of directory paths, create trees for them. +// +//========================================================================== + +dir_tree_t *add_dirs(char **argv) +{ + dir_tree_t *tree, *trees = NULL; + + while (*argv != NULL) + { + for (s = *argv; *s != '\0'; ++s) + { + if (*s == '\\') + { + *s = '/'; + } + } + tree = add_dir(*argv); + tree->next = trees; + trees = tree; + if (no_mem) + { + break; + } + } + return trees; +} + +#else + +//========================================================================== +// +// add_dirs +// 4.4BSD version +// +// Given NULL-terminated array of directory paths, create trees for them. +// +//========================================================================== + +dir_tree_t *add_dirs(char **argv) +{ + FTS *fts; + FTSENT *ent; + dir_tree_t *tree, *trees = NULL; + file_entry_t *file; + + fts = fts_open(argv, FTS_LOGICAL, NULL); + if (fts == NULL) + { + fprintf(stderr, "Failed to start directory traversal: %s\n", strerror(errno)); + return NULL; + } + while ((ent = fts_read(fts)) != NULL) + { + if (ent->fts_info == FTS_D && ent->fts_name[0] == '.') + { + // Skip hidden directories. (Prevents SVN bookkeeping + // info from being included.) + fts_set(fts, ent, FTS_SKIP); + } + if (ent->fts_info == FTS_D && ent->fts_level == 0) + { + tree = alloc_dir_tree(ent->fts_path); + if (tree == NULL) + { + no_mem = 1; + break; + } + tree->next = trees; + trees = tree; + } + if (ent->fts_info != FTS_F) + { + // We're only interested in remembering files. + continue; + } + file = alloc_file_entry("", ent->fts_path, ent->fts_statp->st_mtime); + if (file == NULL) + { + no_mem = 1; + break; + } + file->next = tree->files; + tree->files = file; + } + fts_close(fts); + return trees; +} +#endif //========================================================================== // @@ -501,7 +611,7 @@ int __cdecl main (int argc, char **argv) int i; dir_tree_t *tree, *trees; file_entry_t *file; - struct _stat zipstat; + struct stat zipstat; int needwrite; char *s; @@ -511,31 +621,16 @@ int __cdecl main (int argc, char **argv) return 1; } - trees = NULL; - for (i = 2; i < argc; ++i) + trees = add_dirs(&argv[2]); + if(no_mem) { -#ifdef _WIN32 - for (s = argv[i]; *s != '\0'; ++s) - { - if (*s == '\\') - { - *s = '/'; - } - } -#endif - tree = add_dir(argv[i]); - tree->next = trees; - trees = tree; - if(no_mem) - { - free_dir_trees(trees); - fprintf(stderr, "Out of memory.\n"); - return 1; - } + free_dir_trees(trees); + fprintf(stderr, "Out of memory.\n"); + return 1; } needwrite = 0; - if (_stat(argv[1], &zipstat) != 0) + if (stat(argv[1], &zipstat) != 0) { if (errno == ENOENT) {