- Added Linux parts for the zipdir tool.

SVN r1088 (trunk)
This commit is contained in:
Randy Heit 2008-07-23 23:33:59 +00:00
parent c7b4fc6270
commit 0f2633d78c
2 changed files with 132 additions and 37 deletions

View file

@ -1,4 +1,4 @@
cmake_minimum_required( VERSION 2.6 ) cmake_minimum_required( VERSION 2.4 )
add_executable( zipdir add_executable( zipdir
zipdir.c zipdir.c
ioapi.c ioapi.c

View file

@ -30,8 +30,14 @@
// HEADER FILES ------------------------------------------------------------ // HEADER FILES ------------------------------------------------------------
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <io.h> #ifdef _WIN32
#include <io.h>
#define stat _stat
#else
#include <dirent.h>
#include <fts.h>
#endif
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
@ -77,9 +83,12 @@ void print_usage(const char *cmdname);
dir_tree_t *alloc_dir_tree(const char *dir); 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 *alloc_file_entry(const char *prefix, const char *path, time_t last_written);
void free_dir_tree(dir_tree_t *tree); 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); 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 count_files(dir_tree_t *trees);
int __cdecl sort_cmp(const void *a, const void *b); int __cdecl sort_cmp(const void *a, const void *b);
file_sorted_t *sort_files(dir_tree_t *trees, int num_files); 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 *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); entry = malloc(sizeof(file_entry_t) + strlen(prefix) + strlen(path) + 1);
if (entry != NULL) if (entry != NULL)
{ {
strcpy(entry->path, prefix); strcpy(entry->path, prefix);
strcat(entry->path, path); strcat(entry->path, path);
strlwr(entry->path); for (p = entry->path; *p != '\0'; ++p)
{
*p = tolower(*p);
}
entry->next = NULL; entry->next = NULL;
entry->time_write = last_written; entry->time_write = last_written;
} }
@ -204,6 +217,8 @@ void free_dir_trees(dir_tree_t *tree)
free_dir_tree(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) void recurse_dir(dir_tree_t *tree, const char *dirpath)
{ {
struct _finddata_t fileinfo; struct _finddata_t fileinfo;
char *dirmatch;
intptr_t handle; intptr_t handle;
char *dirmatch;
dirmatch = malloc(strlen(dirpath) + 2); dirmatch = malloc(strlen(dirpath) + 2);
if (dirmatch == NULL) if (dirmatch == NULL)
@ -224,13 +239,13 @@ void recurse_dir(dir_tree_t *tree, const char *dirpath)
return; return;
} }
strcpy(dirmatch, dirpath); strcpy(dirmatch, dirpath);
strcat(dirmatch, "*"); strcat(dirmatch, "*");
if ((handle = _findfirst(dirmatch, &fileinfo)) == -1) if ((handle = _findfirst(dirmatch, &fileinfo)) == -1)
{ {
fprintf(stderr, "Could not scan '%s': %s\n", dirpath, strerror(errno)); fprintf(stderr, "Could not scan '%s': %s\n", dirpath, strerror(errno));
} }
else else
{ {
do do
{ {
if (fileinfo.attrib & _A_HIDDEN) if (fileinfo.attrib & _A_HIDDEN)
@ -270,7 +285,7 @@ void recurse_dir(dir_tree_t *tree, const char *dirpath)
tree->files = entry; tree->files = entry;
} }
} while (_findnext(handle, &fileinfo) == 0); } while (_findnext(handle, &fileinfo) == 0);
_findclose(handle); _findclose(handle);
} }
free(dirmatch); free(dirmatch);
} }
@ -291,6 +306,101 @@ dir_tree_t *add_dir(const char *dirpath)
} }
return tree; 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; int i;
dir_tree_t *tree, *trees; dir_tree_t *tree, *trees;
file_entry_t *file; file_entry_t *file;
struct _stat zipstat; struct stat zipstat;
int needwrite; int needwrite;
char *s; char *s;
@ -511,31 +621,16 @@ int __cdecl main (int argc, char **argv)
return 1; return 1;
} }
trees = NULL; trees = add_dirs(&argv[2]);
for (i = 2; i < argc; ++i) if(no_mem)
{ {
#ifdef _WIN32 free_dir_trees(trees);
for (s = argv[i]; *s != '\0'; ++s) fprintf(stderr, "Out of memory.\n");
{ return 1;
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;
}
} }
needwrite = 0; needwrite = 0;
if (_stat(argv[1], &zipstat) != 0) if (stat(argv[1], &zipstat) != 0)
{ {
if (errno == ENOENT) if (errno == ENOENT)
{ {