2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
gbx_archive.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-03-21 01:04:10 +01:00
|
|
|
(c) 2000-2011 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2009-08-17 12:41:51 +02:00
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
2007-12-30 17:41:49 +01:00
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2011-06-03 02:51:09 +02:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBX_ARCHIVE_C
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_error.h"
|
|
|
|
#include "gb_alloc.h"
|
|
|
|
|
|
|
|
#include "gbx_string.h"
|
|
|
|
#include "gbx_stream.h"
|
|
|
|
#include "gbx_component.h"
|
|
|
|
#include "gbx_regexp.h"
|
|
|
|
#include "gbx_exec.h"
|
|
|
|
#include "gbx_class.h"
|
2008-12-31 16:55:40 +01:00
|
|
|
#include "gbx_project.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#include "gbx_archive.h"
|
|
|
|
|
|
|
|
#include "gb_arch_temp.h"
|
|
|
|
|
|
|
|
/* main archive project (used only if gbx is run with -x flag) */
|
2008-01-17 22:39:26 +01:00
|
|
|
ARCHIVE *ARCHIVE_main = NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
static ARCHIVE *_archive_list = NULL;
|
|
|
|
|
|
|
|
static char *arch_pattern = NULL;
|
|
|
|
static int arch_index = 0;
|
|
|
|
static ARCH *arch_dir = NULL;
|
2008-11-02 03:40:10 +01:00
|
|
|
static char *arch_prefix = NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-04-25 12:46:57 +02:00
|
|
|
ARCHIVE *ARCHIVE_create(const char *name, const char *path)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
ARCHIVE *arch;
|
|
|
|
|
|
|
|
ALLOC_ZERO(&arch, sizeof(ARCHIVE), "ARCHIVE_create");
|
|
|
|
|
|
|
|
arch->name = name;
|
2010-04-25 12:46:57 +02:00
|
|
|
arch->path = path;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
arch->domain = STRING_new_zero(name ? name : "gb");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
arch->translation_loaded = FALSE;
|
|
|
|
|
|
|
|
TABLE_create(&arch->classes, sizeof(CLASS_SYMBOL), TF_IGNORE_CASE);
|
|
|
|
|
|
|
|
LIST_insert(&_archive_list, arch, &arch->list);
|
|
|
|
|
|
|
|
return arch;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void load_exported_class(ARCHIVE *arch)
|
|
|
|
{
|
|
|
|
/*STREAM stream;*/
|
|
|
|
char *buffer;
|
|
|
|
int len;
|
|
|
|
char *name;
|
2009-01-09 15:07:56 +01:00
|
|
|
CLASS *class;
|
2009-05-01 18:27:45 +02:00
|
|
|
CLASS **exported = NULL;
|
|
|
|
int i;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
/* COMPONENT_current is set => it will look in the archive */
|
|
|
|
|
2010-04-25 12:46:57 +02:00
|
|
|
#if DEBUG_COMP
|
|
|
|
fprintf(stderr, "load_exported_class: %s (component: %s)\n", arch->name, COMPONENT_current ? COMPONENT_current->name : "?");
|
|
|
|
#endif
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
if (!FILE_exist(".list"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
STREAM_load(".list", &buffer, &len);
|
|
|
|
/* The file must end with a newline !*/
|
|
|
|
buffer[len - 1] = 0;
|
|
|
|
|
|
|
|
#if DEBUG_COMP
|
|
|
|
fprintf(stderr, ".list file:\n-----------\n\n%s\n----------\n\n", buffer);
|
|
|
|
#endif
|
|
|
|
|
2009-05-01 18:27:45 +02:00
|
|
|
ARRAY_create(&exported);
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
name = strtok(buffer, "\n");
|
|
|
|
while (name)
|
|
|
|
{
|
|
|
|
#if DEBUG_COMP
|
|
|
|
fprintf(stderr, "Check %s global\n", name);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CLASS_check_global(name);
|
2009-05-01 18:27:45 +02:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
len = strlen(name);
|
|
|
|
if (len > 2 && name[len - 1] == '?')
|
|
|
|
{
|
|
|
|
name[len - 1] = 0;
|
|
|
|
if (!CLASS_look_global(name, strlen(name)))
|
2009-01-09 15:07:56 +01:00
|
|
|
class = CLASS_find_global(name);
|
|
|
|
else
|
|
|
|
class = NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
else
|
2009-08-07 20:14:26 +02:00
|
|
|
class = CLASS_find_global(name);
|
2009-01-09 15:07:56 +01:00
|
|
|
|
|
|
|
if (class)
|
|
|
|
{
|
|
|
|
class->component = COMPONENT_current;
|
2009-05-01 18:27:45 +02:00
|
|
|
*((CLASS **)ARRAY_add(&exported)) = class;
|
2009-01-09 15:07:56 +01:00
|
|
|
}
|
2009-05-01 18:27:45 +02:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
name = strtok(NULL, "\n");
|
|
|
|
}
|
|
|
|
|
2009-05-01 18:27:45 +02:00
|
|
|
for (i = 0; i < ARRAY_count(exported); i++)
|
2009-05-25 15:43:44 +02:00
|
|
|
CLASS_load_without_init(exported[i]);
|
2009-05-01 18:27:45 +02:00
|
|
|
|
|
|
|
ARRAY_delete(&exported);
|
2007-12-30 17:41:49 +01:00
|
|
|
FREE(&buffer, "load_exported_class");
|
|
|
|
}
|
|
|
|
|
2011-08-21 04:27:16 +02:00
|
|
|
static void load_component(char *name)
|
|
|
|
{
|
|
|
|
COMPONENT *comp;
|
|
|
|
|
|
|
|
comp = COMPONENT_create(name);
|
|
|
|
COMPONENT_load(comp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void load_dependencies(ARCHIVE *arch)
|
|
|
|
{
|
|
|
|
char *buffer;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
STREAM_load(".startup", &buffer, &len);
|
|
|
|
PROJECT_analyze_startup(buffer, len, load_component);
|
|
|
|
FREE(&buffer, "load_dependencies");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void load_archive(ARCHIVE *arch, const char *path, bool dep)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-04-28 02:17:12 +02:00
|
|
|
arch->arch = ARCH_open(path);
|
2007-12-30 17:41:49 +01:00
|
|
|
load_exported_class(arch);
|
2011-08-21 04:27:16 +02:00
|
|
|
if (dep)
|
|
|
|
load_dependencies(arch);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2010-04-25 12:46:57 +02:00
|
|
|
static char *exist_library(const char *dir, const char *name)
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
path = (char *)FILE_cat(dir, name, NULL);
|
|
|
|
if (FILE_exist(path))
|
|
|
|
return path;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-08-21 04:27:16 +02:00
|
|
|
void ARCHIVE_load(ARCHIVE *arch, bool dep)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-04-25 12:46:57 +02:00
|
|
|
char *path;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-04-25 12:46:57 +02:00
|
|
|
if (arch->path)
|
|
|
|
{
|
|
|
|
if (EXEC_debug)
|
|
|
|
path = (char *)arch->path;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
path = exist_library(PROJECT_path, arch->name);
|
|
|
|
if (!path) path = exist_library("/usr/bin", arch->name);
|
|
|
|
if (!path) path = exist_library("/bin", arch->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!path || !FILE_exist(path))
|
|
|
|
THROW(E_LIBRARY, arch->name, "cannot find library");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
path = FILE_buffer();
|
|
|
|
sprintf(path, ARCH_PATTERN, COMPONENT_path, arch->name);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-08-21 04:27:16 +02:00
|
|
|
load_archive(arch, path, dep);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void ARCHIVE_create_main(const char *path)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-04-25 12:46:57 +02:00
|
|
|
ARCHIVE_main = ARCHIVE_create(NULL, NULL);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (path)
|
|
|
|
ARCHIVE_main->arch = ARCH_open(path);
|
|
|
|
else
|
|
|
|
ARCHIVE_main->arch = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void ARCHIVE_load_main()
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
load_exported_class(ARCHIVE_main);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void ARCHIVE_delete(ARCHIVE *arch)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
LIST_remove(&_archive_list, arch, &arch->list);
|
|
|
|
|
|
|
|
if (arch->arch)
|
|
|
|
ARCH_close(arch->arch);
|
|
|
|
|
|
|
|
TABLE_delete(&arch->classes);
|
|
|
|
STRING_free(&arch->domain);
|
|
|
|
|
|
|
|
FREE(&arch, "ARCHIVE_delete");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void ARCHIVE_init(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
//ARCH_create(path);
|
|
|
|
//ARCHIVE_main = _arch_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void ARCHIVE_exit(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
if (ARCHIVE_main)
|
|
|
|
ARCHIVE_delete(ARCHIVE_main);
|
|
|
|
|
|
|
|
STRING_free(&arch_pattern);
|
2008-11-02 03:40:10 +01:00
|
|
|
STRING_free(&arch_prefix);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ### *parch must be initialized to NULL or a valid archive */
|
|
|
|
/* Returns a true archive, never the main archive if we are not running an executable */
|
2009-01-02 18:15:07 +01:00
|
|
|
static bool get_current(ARCHIVE **parch, const char **ppath)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
if (*parch)
|
|
|
|
return FALSE;
|
|
|
|
|
2009-01-02 18:15:07 +01:00
|
|
|
if (strncmp(*ppath, "../", 3) == 0)
|
2008-12-31 16:55:40 +01:00
|
|
|
{
|
|
|
|
*parch = EXEC_arch ? ARCHIVE_main : NULL;
|
2009-01-02 18:15:07 +01:00
|
|
|
*ppath += 3;
|
2008-12-31 16:55:40 +01:00
|
|
|
}
|
|
|
|
else if (COMPONENT_current && COMPONENT_current->archive)
|
2007-12-30 17:41:49 +01:00
|
|
|
*parch = COMPONENT_current->archive;
|
|
|
|
else if (CP && CP->component && CP->component->archive)
|
|
|
|
*parch = CP->component->archive;
|
|
|
|
else
|
|
|
|
*parch = EXEC_arch ? ARCHIVE_main : NULL;
|
|
|
|
|
|
|
|
return *parch == NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Can return the main archive even if we are not running an executable */
|
2008-01-17 22:39:26 +01:00
|
|
|
bool ARCHIVE_get_current(ARCHIVE **parch)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
if (COMPONENT_current && COMPONENT_current->archive)
|
|
|
|
*parch = COMPONENT_current->archive;
|
|
|
|
else if (CP && CP->component && CP->component->archive)
|
|
|
|
*parch = CP->component->archive;
|
|
|
|
else
|
|
|
|
*parch = ARCHIVE_main;
|
|
|
|
|
|
|
|
return *parch == NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-02 18:15:07 +01:00
|
|
|
bool ARCHIVE_get(ARCHIVE *arch, const char **ppath, ARCHIVE_FIND *find)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
ARCH_FIND f;
|
2008-12-31 16:55:40 +01:00
|
|
|
struct stat buf;
|
2009-07-07 18:37:56 +02:00
|
|
|
|
|
|
|
if (!*ppath || **ppath == 0)
|
|
|
|
return TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 18:15:07 +01:00
|
|
|
if (get_current(&arch, ppath))
|
2008-12-31 16:55:40 +01:00
|
|
|
{
|
|
|
|
// no archive found, we try a lstat
|
2010-11-28 00:00:08 +01:00
|
|
|
if (!PROJECT_path)
|
|
|
|
return TRUE;
|
2010-10-31 23:05:29 +01:00
|
|
|
FILE_chdir(PROJECT_path);
|
2009-01-02 18:15:07 +01:00
|
|
|
if (stat(*ppath, &buf))
|
2008-12-31 16:55:40 +01:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
find->pos = S_ISDIR(buf.st_mode) ? -1 : 0;
|
|
|
|
find->sym = NULL;
|
|
|
|
find->len = (int)buf.st_size;
|
|
|
|
find->index = -1;
|
|
|
|
find->arch = NULL;
|
|
|
|
return FALSE;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 18:15:07 +01:00
|
|
|
if (ARCH_find(arch->arch, *ppath, 0, &f))
|
2007-12-30 17:41:49 +01:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
find->sym = f.sym;
|
|
|
|
find->pos = f.pos;
|
|
|
|
find->len = f.len;
|
2008-11-02 03:40:10 +01:00
|
|
|
find->index = f.index;
|
2007-12-30 17:41:49 +01:00
|
|
|
find->arch = arch;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
bool ARCHIVE_exist(ARCHIVE *arch, const char *path)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
ARCHIVE_FIND find;
|
|
|
|
|
2008-12-31 16:55:40 +01:00
|
|
|
//if (get_current(&arch, &path))
|
|
|
|
// return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 18:15:07 +01:00
|
|
|
return (!ARCHIVE_get(arch, &path, &find));
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
bool ARCHIVE_is_dir(ARCHIVE *arch, const char *path)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
ARCHIVE_FIND find;
|
|
|
|
|
2009-01-02 18:15:07 +01:00
|
|
|
if (ARCHIVE_get(arch, &path, &find))
|
2007-12-30 17:41:49 +01:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return (find.pos < 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void ARCHIVE_stat(ARCHIVE *arch, const char *path, FILE_STAT *info)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
ARCHIVE_FIND find;
|
|
|
|
struct stat buf;
|
|
|
|
|
|
|
|
//if (get_current(&arch))
|
|
|
|
// THROW_SYSTEM(ENOENT, path);
|
|
|
|
|
2009-01-02 18:15:07 +01:00
|
|
|
if (ARCHIVE_get(arch, &path, &find))
|
2007-12-30 17:41:49 +01:00
|
|
|
THROW_SYSTEM(ENOENT, path);
|
|
|
|
|
2010-04-05 03:44:43 +02:00
|
|
|
if (find.arch)
|
|
|
|
fstat(find.arch->arch->fd, &buf);
|
|
|
|
else
|
|
|
|
stat(PROJECT_path, &buf);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
info->type = find.pos < 0 ? GB_STAT_DIRECTORY : GB_STAT_FILE;
|
|
|
|
info->mode = 0400;
|
|
|
|
|
|
|
|
info->size = find.len;
|
|
|
|
info->atime = (int)buf.st_mtime;
|
|
|
|
info->mtime = (int)buf.st_mtime;
|
|
|
|
info->ctime = (int)buf.st_mtime;
|
|
|
|
info->hidden = (*FILE_get_name(path) == '.');
|
|
|
|
info->uid = buf.st_uid;
|
|
|
|
info->gid = buf.st_gid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
bool ARCHIVE_read(ARCHIVE *arch, int pos, void *buffer, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
return ARCH_read(arch->arch, pos, buffer, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-31 16:55:40 +01:00
|
|
|
void ARCHIVE_dir_first(ARCHIVE *arch, const char *path, const char *pattern, int attr)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-11-02 03:40:10 +01:00
|
|
|
ARCHIVE_FIND find;
|
|
|
|
char abs_path[16];
|
|
|
|
int abs_len;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-12-31 16:55:40 +01:00
|
|
|
/*if (get_current(&arch, &path))
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
arch_dir = NULL;
|
|
|
|
return;
|
2008-12-31 16:55:40 +01:00
|
|
|
}*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-02 18:15:07 +01:00
|
|
|
if (ARCHIVE_get(arch, &path, &find))
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-11-02 03:40:10 +01:00
|
|
|
arch_dir = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-07-05 21:06:11 +02:00
|
|
|
// ?? "." means that we want to browse the archive.
|
|
|
|
if (!find.sym && !find.arch) // && !(path[0] == '.' && path[1] == 0))
|
2008-12-31 16:55:40 +01:00
|
|
|
{
|
2009-11-30 05:25:48 +01:00
|
|
|
// By calling FILE_dir_first() again with an absolute path, we are sure that next calls to
|
2008-12-31 16:55:40 +01:00
|
|
|
// FILE_dir_next() will never call ARCHIVE_dir_next().
|
2009-01-02 18:15:07 +01:00
|
|
|
FILE_dir_first(FILE_cat(PROJECT_path, path, NULL), pattern, attr);
|
2008-12-31 16:55:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pattern == NULL)
|
|
|
|
pattern = "*";
|
|
|
|
|
2009-01-04 19:55:25 +01:00
|
|
|
arch = find.arch;
|
2008-11-02 03:40:10 +01:00
|
|
|
arch_dir = arch->arch;
|
|
|
|
arch_index = 0;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
STRING_free(&arch_pattern);
|
2010-06-05 01:48:53 +02:00
|
|
|
arch_pattern = STRING_new_zero(pattern);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-11-02 03:40:10 +01:00
|
|
|
//if (arch_dir->header.version == 2)
|
|
|
|
//{
|
2010-04-05 03:44:43 +02:00
|
|
|
if (find.index >= 0)
|
2008-11-02 03:40:10 +01:00
|
|
|
abs_len = sprintf(abs_path, "/%d:", find.index);
|
|
|
|
else
|
|
|
|
abs_len = 0;
|
|
|
|
/*}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ARCH_get_absolute_path(path, strlen(path), abs_path, &abs_len);
|
|
|
|
|
|
|
|
if (abs_len && abs_path[abs_len - 1] != '/')
|
|
|
|
{
|
|
|
|
abs_path[abs_len] = '/';
|
|
|
|
abs_len++;
|
|
|
|
}
|
|
|
|
}*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-11-02 03:40:10 +01:00
|
|
|
STRING_free(&arch_prefix);
|
|
|
|
if (abs_len)
|
2010-06-05 01:48:53 +02:00
|
|
|
arch_prefix = STRING_new(abs_path, abs_len);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
bool ARCHIVE_dir_next(char **name, int *len, int attr)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-12-24 03:02:05 +01:00
|
|
|
ARCH_SYMBOL *asym;
|
2007-12-30 17:41:49 +01:00
|
|
|
SYMBOL *sym;
|
|
|
|
char *s = NULL;
|
|
|
|
int l = 0;
|
2008-11-02 03:40:10 +01:00
|
|
|
int lenp;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
/*if (arch_fd < 0)
|
|
|
|
return FILE_dir_next(name, len);*/
|
|
|
|
|
|
|
|
if (!arch_dir)
|
|
|
|
return TRUE;
|
|
|
|
|
2008-11-02 03:40:10 +01:00
|
|
|
lenp = STRING_length(arch_prefix);
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if (arch_index >= arch_dir->header.n_symbol)
|
|
|
|
return TRUE;
|
|
|
|
|
2010-05-27 15:51:09 +02:00
|
|
|
//sym = &arch_dir->symbol[arch_index].sym;
|
|
|
|
asym = &arch_dir->symbol[arch_dir->sort[arch_index]];
|
2009-12-24 03:02:05 +01:00
|
|
|
sym = &asym->sym;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (arch_pattern == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
arch_index++;
|
|
|
|
|
2009-12-24 03:02:05 +01:00
|
|
|
if (attr == GB_STAT_DIRECTORY && (asym->pos >= 0))
|
2007-12-30 17:41:49 +01:00
|
|
|
continue;
|
|
|
|
|
2008-11-02 03:40:10 +01:00
|
|
|
if (sym->len < lenp)
|
2007-12-30 17:41:49 +01:00
|
|
|
continue;
|
|
|
|
|
2008-11-02 03:40:10 +01:00
|
|
|
if (lenp)
|
|
|
|
{
|
|
|
|
if (strncmp(sym->name, arch_prefix, lenp))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else // special case: root directory when header.version == 2
|
|
|
|
{
|
|
|
|
if (!strncmp(sym->name, "/", 1))
|
|
|
|
continue;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-11-02 03:40:10 +01:00
|
|
|
s = sym->name + lenp;
|
|
|
|
l = sym->len - lenp;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (l < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (memchr(s, '/', l))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!REGEXP_match(arch_pattern, STRING_length(arch_pattern), s, l))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
*name = s;
|
|
|
|
*len = l;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
bool ARCHIVE_check_addr(char *addr)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
ARCHIVE *arch;
|
|
|
|
ARCH *a;
|
|
|
|
|
|
|
|
LIST_for_each(arch, _archive_list)
|
|
|
|
{
|
|
|
|
a = arch->arch;
|
|
|
|
if (a && addr >= a->addr && addr < &a->addr[a->length])
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-04-05 03:44:43 +02:00
|
|
|
void ARCHIVE_browse(ARCHIVE *arch, void (*found)(const char *path, int64_t size))
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
ARCH_SYMBOL *asym;
|
|
|
|
SYMBOL *sym;
|
|
|
|
ARCH *a = arch->arch;
|
|
|
|
char *path;
|
|
|
|
char *temp;
|
|
|
|
int size;
|
|
|
|
int ip;
|
|
|
|
|
|
|
|
for (i = 0; i < a->header.n_symbol; i++)
|
|
|
|
{
|
|
|
|
asym = &a->symbol[i];
|
|
|
|
sym = &asym->sym;
|
|
|
|
|
|
|
|
size = asym->len;
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
path = STRING_new(sym->name, sym->len);
|
2010-04-05 03:44:43 +02:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if (*path != '/')
|
|
|
|
break;
|
|
|
|
|
|
|
|
ip = atoi(&path[1]);
|
|
|
|
sym = &a->symbol[ip].sym;
|
|
|
|
|
|
|
|
temp = path;
|
2010-06-05 01:48:53 +02:00
|
|
|
path = STRING_new(sym->name, sym->len);
|
2010-04-05 03:44:43 +02:00
|
|
|
if (path[sym->len - 1] != '/')
|
2011-09-07 14:38:29 +02:00
|
|
|
path = STRING_add(path, "/", 1);
|
|
|
|
path = STRING_add(path, strchr(temp, ':') + 1, 0);
|
2010-04-05 03:44:43 +02:00
|
|
|
STRING_free(&temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
(*found)(path, size);
|
|
|
|
STRING_free(&path);
|
|
|
|
}
|
|
|
|
}
|