2015-09-09 02:11:49 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS CD-ROM Maker
|
|
|
|
* FILE: tools/cdmake/dirhash.c
|
|
|
|
* PURPOSE: CD-ROM Premastering Utility - Directory names hashing
|
|
|
|
* PROGRAMMERS: Art Yerkes
|
|
|
|
*/
|
2016-01-08 18:43:46 +00:00
|
|
|
|
2013-07-21 13:33:03 +00:00
|
|
|
#include <string.h>
|
2013-07-24 20:33:33 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
2015-01-31 18:44:28 +00:00
|
|
|
#include "config.h"
|
2013-07-21 13:33:03 +00:00
|
|
|
#include "dirhash.h"
|
|
|
|
|
2016-01-08 20:25:30 +00:00
|
|
|
#ifndef max
|
2016-01-08 18:43:46 +00:00
|
|
|
#define max(a, b) ((a) > (b) ? (a) : (b))
|
2016-01-08 20:25:30 +00:00
|
|
|
#endif
|
2016-01-08 18:43:46 +00:00
|
|
|
|
2013-07-21 13:33:03 +00:00
|
|
|
/* This is the famous DJB hash */
|
|
|
|
static unsigned int
|
|
|
|
djb_hash(const char *name)
|
|
|
|
{
|
|
|
|
unsigned int val = 5381;
|
|
|
|
int i = 0;
|
2014-01-16 10:29:51 +00:00
|
|
|
|
2013-07-21 13:33:03 +00:00
|
|
|
for (i = 0; name[i]; i++)
|
|
|
|
{
|
|
|
|
val = (33 * val) + name[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-01-08 18:43:46 +00:00
|
|
|
split_path(const char *path, char **dirname, char **filename /* OPTIONAL */)
|
2013-07-21 13:33:03 +00:00
|
|
|
{
|
2016-01-08 18:43:46 +00:00
|
|
|
const char *result;
|
|
|
|
|
|
|
|
/* Retrieve the file name */
|
|
|
|
char *last_slash_1 = strrchr(path, '/');
|
|
|
|
char *last_slash_2 = strrchr(path, '\\');
|
|
|
|
|
|
|
|
if (last_slash_1 || last_slash_2)
|
|
|
|
result = max(last_slash_1, last_slash_2) + 1;
|
2013-07-21 13:33:03 +00:00
|
|
|
else
|
2016-01-08 18:43:46 +00:00
|
|
|
result = path;
|
2013-07-21 13:33:03 +00:00
|
|
|
|
2016-01-08 18:43:46 +00:00
|
|
|
/* Duplicate the file name for the user if needed */
|
|
|
|
if (filename)
|
|
|
|
*filename = strdup(result);
|
2013-07-21 13:33:03 +00:00
|
|
|
|
2016-01-08 18:43:46 +00:00
|
|
|
/* Remove any trailing directory separators */
|
|
|
|
while (result > path && (*(result-1) == '/' || *(result-1) == '\\'))
|
|
|
|
result--;
|
|
|
|
|
|
|
|
/* Retrieve and duplicate the directory */
|
|
|
|
*dirname = malloc(result - path + 1);
|
|
|
|
if (result > path)
|
|
|
|
memcpy(*dirname, path, result - path);
|
|
|
|
(*dirname)[result - path] = '\0'; // NULL-terminate
|
2014-01-16 10:29:51 +00:00
|
|
|
}
|
|
|
|
|
2013-07-21 13:33:03 +00:00
|
|
|
void normalize_dirname(char *filename)
|
|
|
|
{
|
|
|
|
int i, tgt;
|
|
|
|
int slash = 1;
|
|
|
|
|
2015-01-18 13:04:43 +00:00
|
|
|
for (i = 0, tgt = 0; filename[i]; i++)
|
|
|
|
{
|
|
|
|
if (slash)
|
|
|
|
{
|
|
|
|
if (filename[i] != '/' && filename[i] != '\\')
|
|
|
|
{
|
2013-07-21 13:33:03 +00:00
|
|
|
filename[tgt++] = toupper(filename[i]);
|
|
|
|
slash = 0;
|
|
|
|
}
|
2015-01-18 13:04:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (filename[i] == '/' || filename[i] == '\\')
|
|
|
|
{
|
2013-07-21 13:33:03 +00:00
|
|
|
slash = 1;
|
|
|
|
filename[tgt++] = DIR_SEPARATOR_CHAR;
|
2015-01-18 13:04:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-21 13:33:03 +00:00
|
|
|
filename[tgt++] = toupper(filename[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-08 18:43:46 +00:00
|
|
|
filename[tgt] = '\0'; // NULL-terminate
|
|
|
|
}
|
2015-01-18 13:11:35 +00:00
|
|
|
|
2016-01-08 18:43:46 +00:00
|
|
|
static struct target_dir_entry *
|
|
|
|
get_entry_by_normname(struct target_dir_hash *dh, const char *norm)
|
|
|
|
{
|
|
|
|
unsigned int hashcode;
|
|
|
|
struct target_dir_entry *de;
|
|
|
|
hashcode = djb_hash(norm);
|
|
|
|
de = dh->buckets[hashcode % NUM_DIR_HASH_BUCKETS];
|
|
|
|
while (de && strcmp(de->normalized_name, norm))
|
|
|
|
de = de->next_dir_hash_entry;
|
|
|
|
return de;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
delete_entry(struct target_dir_hash *dh, struct target_dir_entry *de)
|
|
|
|
{
|
|
|
|
struct target_dir_entry **ent;
|
|
|
|
ent = &dh->buckets[de->hashcode % NUM_DIR_HASH_BUCKETS];
|
|
|
|
while (*ent && ((*ent) != de))
|
|
|
|
ent = &(*ent)->next_dir_hash_entry;
|
|
|
|
if (*ent)
|
|
|
|
*ent = (*ent)->next_dir_hash_entry;
|
2013-07-21 13:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct target_dir_entry *
|
|
|
|
dir_hash_create_dir(struct target_dir_hash *dh, const char *casename, const char *targetnorm)
|
|
|
|
{
|
|
|
|
struct target_dir_entry *de, *parent_de;
|
|
|
|
char *parentcase = NULL;
|
2016-01-08 18:43:46 +00:00
|
|
|
char *case_name = NULL;
|
|
|
|
char *parentname = NULL;
|
2013-07-21 13:33:03 +00:00
|
|
|
struct target_dir_entry **ent;
|
2015-01-28 20:26:56 +00:00
|
|
|
|
2013-07-21 13:33:03 +00:00
|
|
|
if (!dh->root.normalized_name)
|
|
|
|
{
|
|
|
|
dh->root.normalized_name = strdup("");
|
|
|
|
dh->root.case_name = strdup("");
|
2015-01-28 20:26:56 +00:00
|
|
|
dh->root.hashcode = djb_hash("");
|
|
|
|
dh->buckets[dh->root.hashcode % NUM_DIR_HASH_BUCKETS] = &dh->root;
|
2013-07-21 13:33:03 +00:00
|
|
|
}
|
2015-01-28 20:26:56 +00:00
|
|
|
|
2016-01-08 18:43:46 +00:00
|
|
|
/* Check whether the directory was already created and just return it if so */
|
2013-07-21 13:33:03 +00:00
|
|
|
de = get_entry_by_normname(dh, targetnorm);
|
|
|
|
if (de)
|
|
|
|
return de;
|
2015-01-28 20:26:56 +00:00
|
|
|
|
2016-01-08 18:43:46 +00:00
|
|
|
/*
|
|
|
|
* If *case_name == '\0' after the following call to split_path(...),
|
|
|
|
* for example in the case where casename == "subdir/dir/", then just
|
|
|
|
* create the directories "subdir" and "dir" by a recursive call to
|
|
|
|
* dir_hash_create_dir(...) and return 'parent_de' instead (see after).
|
|
|
|
* We do not (and we never) create a no-name directory inside it.
|
|
|
|
*/
|
|
|
|
split_path(casename, &parentcase, &case_name);
|
|
|
|
split_path(targetnorm, &parentname, NULL);
|
2013-07-21 13:33:03 +00:00
|
|
|
parent_de = dir_hash_create_dir(dh, parentcase, parentname);
|
|
|
|
free(parentname);
|
|
|
|
free(parentcase);
|
2015-01-28 20:26:56 +00:00
|
|
|
|
2016-01-08 18:43:46 +00:00
|
|
|
/* See the remark above */
|
2016-01-08 20:25:30 +00:00
|
|
|
if (!*case_name)
|
|
|
|
{
|
|
|
|
free(case_name);
|
|
|
|
return parent_de;
|
|
|
|
}
|
2016-01-08 18:43:46 +00:00
|
|
|
|
|
|
|
/* Now create the directory */
|
2013-07-21 16:15:16 +00:00
|
|
|
de = calloc(1, sizeof(*de));
|
2015-01-28 20:26:56 +00:00
|
|
|
de->parent = parent_de;
|
2013-07-21 13:33:03 +00:00
|
|
|
de->normalized_name = strdup(targetnorm);
|
2016-01-08 18:43:46 +00:00
|
|
|
de->case_name = case_name;
|
2015-01-28 20:26:56 +00:00
|
|
|
de->hashcode = djb_hash(targetnorm);
|
|
|
|
|
2013-07-21 13:33:03 +00:00
|
|
|
de->next = parent_de->child;
|
|
|
|
parent_de->child = de;
|
2015-01-28 20:26:56 +00:00
|
|
|
|
|
|
|
ent = &dh->buckets[de->hashcode % NUM_DIR_HASH_BUCKETS];
|
2015-01-24 00:15:08 +00:00
|
|
|
while (*ent)
|
2015-01-28 20:26:56 +00:00
|
|
|
ent = &(*ent)->next_dir_hash_entry;
|
2013-07-21 13:33:03 +00:00
|
|
|
*ent = de;
|
2015-01-28 20:26:56 +00:00
|
|
|
|
2013-07-21 13:33:03 +00:00
|
|
|
return de;
|
|
|
|
}
|
|
|
|
|
2016-01-08 20:25:30 +00:00
|
|
|
struct target_file *
|
|
|
|
dir_hash_add_file(struct target_dir_hash *dh, const char *source, const char *target)
|
2013-07-21 13:33:03 +00:00
|
|
|
{
|
|
|
|
struct target_file *tf;
|
|
|
|
struct target_dir_entry *de;
|
2016-01-08 18:43:46 +00:00
|
|
|
char *targetdir = NULL;
|
|
|
|
char *targetfile = NULL;
|
2013-07-21 13:33:03 +00:00
|
|
|
char *targetnorm;
|
2015-01-28 20:26:56 +00:00
|
|
|
|
2016-01-08 20:25:30 +00:00
|
|
|
/* First create the directory; check whether the file name is valid and bail out if not */
|
2016-01-08 18:43:46 +00:00
|
|
|
split_path(target, &targetdir, &targetfile);
|
2016-01-08 20:25:30 +00:00
|
|
|
if (!*targetfile)
|
|
|
|
{
|
|
|
|
free(targetdir);
|
|
|
|
free(targetfile);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-07-21 13:33:03 +00:00
|
|
|
targetnorm = strdup(targetdir);
|
|
|
|
normalize_dirname(targetnorm);
|
|
|
|
de = dir_hash_create_dir(dh, targetdir, targetnorm);
|
2013-07-25 15:04:56 +00:00
|
|
|
free(targetnorm);
|
2014-01-16 18:36:32 +00:00
|
|
|
free(targetdir);
|
2015-01-28 20:26:56 +00:00
|
|
|
|
2016-01-08 18:43:46 +00:00
|
|
|
/* Now add the file */
|
2013-07-21 16:15:16 +00:00
|
|
|
tf = calloc(1, sizeof(*tf));
|
2013-07-21 13:33:03 +00:00
|
|
|
tf->next = de->head;
|
|
|
|
de->head = tf;
|
|
|
|
tf->source_name = strdup(source);
|
2016-01-08 18:43:46 +00:00
|
|
|
tf->target_name = targetfile;
|
2016-01-08 20:25:30 +00:00
|
|
|
|
|
|
|
return tf;
|
2013-07-21 13:33:03 +00:00
|
|
|
}
|
|
|
|
|
2014-01-16 10:29:51 +00:00
|
|
|
static void
|
|
|
|
dir_hash_destroy_dir(struct target_dir_hash *dh, struct target_dir_entry *de)
|
2013-07-21 13:33:03 +00:00
|
|
|
{
|
|
|
|
struct target_file *tf;
|
|
|
|
struct target_dir_entry *te;
|
2015-01-28 20:26:56 +00:00
|
|
|
|
2013-07-21 13:33:03 +00:00
|
|
|
while ((te = de->child))
|
|
|
|
{
|
|
|
|
de->child = te->next;
|
2014-01-16 10:29:51 +00:00
|
|
|
dir_hash_destroy_dir(dh, te);
|
2013-07-21 13:33:03 +00:00
|
|
|
free(te);
|
|
|
|
}
|
|
|
|
while ((tf = de->head))
|
|
|
|
{
|
|
|
|
de->head = tf->next;
|
|
|
|
free(tf->source_name);
|
|
|
|
free(tf->target_name);
|
|
|
|
free(tf);
|
|
|
|
}
|
2015-01-28 20:26:56 +00:00
|
|
|
|
|
|
|
delete_entry(dh, de);
|
2013-07-21 13:33:03 +00:00
|
|
|
free(de->normalized_name);
|
|
|
|
free(de->case_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dir_hash_destroy(struct target_dir_hash *dh)
|
|
|
|
{
|
2014-01-16 10:29:51 +00:00
|
|
|
dir_hash_destroy_dir(dh, &dh->root);
|
2013-07-21 13:33:03 +00:00
|
|
|
}
|