2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
|
|
|
gb_file_temp.h
|
|
|
|
|
2022-09-12 15:13:13 +02:00
|
|
|
(c) 2000-2017 Benoît Minisini <benoit.minisini@gambas-basic.org>
|
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,
|
2011-12-31 03:39:20 +01:00
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GB_FILE_C
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_error.h"
|
|
|
|
#include "gb_limit.h"
|
|
|
|
#include "gb_component.h"
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <dirent.h>
|
2016-02-01 03:18:49 +01:00
|
|
|
#include <pwd.h>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#ifdef PROJECT_EXEC
|
|
|
|
|
2018-08-29 23:59:11 +02:00
|
|
|
#include <sys/statvfs.h>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <grp.h>
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gb_file.h"
|
|
|
|
|
2016-02-21 16:48:40 +01:00
|
|
|
#define OPT_NLINK 1
|
|
|
|
#define OPT_FSTATAT 1
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
#ifdef PROJECT_EXEC
|
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
//FILE_STAT FILE_stat_info = { 0 };
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
static DIR *file_dir = NULL;
|
|
|
|
static char *file_pattern = NULL;
|
|
|
|
static char *file_path = NULL;
|
|
|
|
static bool file_dir_arch = FALSE;
|
|
|
|
static int file_attr;
|
|
|
|
static char *file_rdir_path = NULL;
|
2015-11-11 04:40:50 +01:00
|
|
|
static bool _temp_used = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static char file_buffer[PATH_MAX + 16];
|
|
|
|
static int file_buffer_length;
|
|
|
|
|
2009-04-04 00:29:51 +02:00
|
|
|
#ifdef _DIRENT_HAVE_D_TYPE
|
|
|
|
#undef _DIRENT_HAVE_D_TYPE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _DIRENT_HAVE_D_TYPE
|
|
|
|
static bool _last_is_dir;
|
|
|
|
#endif
|
|
|
|
|
2016-02-01 03:18:49 +01:00
|
|
|
static char *_home = NULL;
|
|
|
|
static uid_t _uid = 0;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
#ifdef PROJECT_EXEC
|
|
|
|
|
|
|
|
typedef
|
2010-01-01 19:45:35 +01:00
|
|
|
struct _path {
|
|
|
|
struct _path *next;
|
|
|
|
char *path;
|
|
|
|
}
|
|
|
|
FILE_PATH;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
typedef
|
|
|
|
struct {
|
|
|
|
unsigned int mask;
|
|
|
|
unsigned char test[4];
|
|
|
|
unsigned int mode[4];
|
|
|
|
}
|
|
|
|
FILE_MODE_DECODE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
static void push_path(void **list, const char *path)
|
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
FILE_PATH *slot;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-03-30 00:33:01 +01:00
|
|
|
ALLOC(&slot, sizeof(FILE_PATH));
|
2010-06-05 01:48:53 +02:00
|
|
|
slot->path = STRING_new_zero(path);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
slot->next = *list;
|
|
|
|
*list = slot;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
//printf("push_path: %s\n", path);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *pop_path(void **list)
|
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
char *path;
|
|
|
|
FILE_PATH *slot;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (!*list)
|
|
|
|
return NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
path = ((FILE_PATH *)*list)->path;
|
|
|
|
slot = *list;
|
|
|
|
*list = ((FILE_PATH *)*list)->next;
|
2013-03-30 00:33:01 +01:00
|
|
|
FREE(&slot);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
//printf("pop_path: %s\n", path);
|
|
|
|
return path;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void dir_exit(void)
|
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
if (file_dir != NULL)
|
|
|
|
{
|
|
|
|
closedir(file_dir);
|
|
|
|
file_dir = NULL;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
STRING_free(&file_pattern);
|
|
|
|
STRING_free(&file_path);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
char *FILE_make_temp(int *len, const char *pattern)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
static int count = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2015-11-11 04:40:50 +01:00
|
|
|
_temp_used = TRUE;
|
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (len)
|
|
|
|
{
|
|
|
|
if (pattern)
|
|
|
|
*len = snprintf(file_buffer, sizeof(file_buffer), FILE_TEMP_PATTERN, (int)getuid(), (int)getpid(), pattern);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
*len = snprintf(file_buffer, sizeof(file_buffer), FILE_TEMP_FILE, (int)getuid(), (int)getpid(), count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
snprintf(file_buffer, sizeof(file_buffer), FILE_TEMP_DIR, (int)getuid(), (int)getpid());
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
return file_buffer;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_temp_file(const char *path)
|
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
if (FILE_is_dir(path))
|
|
|
|
{
|
|
|
|
//fprintf(stderr, "rmdir: %s\n", path);
|
|
|
|
rmdir(path);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2010-01-01 19:45:35 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
//fprintf(stderr, "unlink: %s\n", path);
|
|
|
|
unlink(path);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
void FILE_remove_temp_file(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2015-11-11 04:40:50 +01:00
|
|
|
if (_temp_used)
|
|
|
|
FILE_recursive_dir(FILE_make_temp(NULL, NULL), NULL, remove_temp_file, 0, FALSE);
|
2010-01-01 19:45:35 +01:00
|
|
|
rmdir(FILE_make_temp(NULL, NULL));
|
2015-11-11 04:40:50 +01:00
|
|
|
_temp_used = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2013-09-29 22:54:47 +02:00
|
|
|
void FILE_remove_temp_file_pid(pid_t pid)
|
|
|
|
{
|
|
|
|
snprintf(file_buffer, sizeof(file_buffer), FILE_TEMP_DIR, (int)getuid(), (int)pid);
|
|
|
|
FILE_recursive_dir(file_buffer, NULL, remove_temp_file, 0, FALSE);
|
|
|
|
rmdir(FILE_make_temp(NULL, NULL));
|
|
|
|
}
|
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
void FILE_init(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-12-22 20:51:30 +01:00
|
|
|
struct stat info;
|
2015-11-11 04:40:50 +01:00
|
|
|
|
|
|
|
_temp_used = TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
FILE_remove_temp_file();
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
snprintf(file_buffer, sizeof(file_buffer), FILE_TEMP_PREFIX, (int)getuid());
|
2013-01-06 17:13:31 +01:00
|
|
|
(void)mkdir(file_buffer, S_IRWXU);
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2012-12-22 20:51:30 +01:00
|
|
|
if (lstat(file_buffer, &info) == 0 && S_ISDIR(info.st_mode) && chown(file_buffer, getuid(), getgid()) == 0 && chmod(file_buffer, S_IRWXU) == 0)
|
2012-12-22 01:23:51 +01:00
|
|
|
{
|
|
|
|
snprintf(file_buffer, sizeof(file_buffer), FILE_TEMP_DIR, (int)getuid(), (int)getpid());
|
2013-01-06 17:13:31 +01:00
|
|
|
(void)mkdir(file_buffer, S_IRWXU);
|
2012-12-22 20:51:30 +01:00
|
|
|
if (lstat(file_buffer, &info) == 0 && S_ISDIR(info.st_mode) && chown(file_buffer, getuid(), getgid()) == 0 && chmod(file_buffer, S_IRWXU) == 0)
|
2012-12-22 01:23:51 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ERROR_fatal("cannot initialize interpreter temporary directory. Do you try to hijack Gambas?");
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
void FILE_exit(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2016-02-01 03:18:49 +01:00
|
|
|
STRING_free(&_home);
|
2007-12-30 17:41:49 +01:00
|
|
|
FILE_remove_temp_file();
|
2010-01-01 19:45:35 +01:00
|
|
|
STRING_free(&file_rdir_path);
|
|
|
|
dir_exit();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2016-02-01 03:18:49 +01:00
|
|
|
#else
|
|
|
|
|
|
|
|
void FILE_exit(void)
|
|
|
|
{
|
|
|
|
STR_free(_home);
|
|
|
|
}
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
#endif
|
|
|
|
|
2010-04-25 12:46:57 +02:00
|
|
|
static char *stradd(char *d, const char *s)
|
|
|
|
{
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if ((*d = *s) == 0)
|
|
|
|
break;
|
|
|
|
d++;
|
|
|
|
s++;
|
|
|
|
}
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2010-04-25 12:46:57 +02:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*#define stradd(d, s) \
|
2007-12-30 17:41:49 +01:00
|
|
|
({ \
|
2010-01-01 19:45:35 +01:00
|
|
|
char *_d = (d); \
|
|
|
|
const char *_s = (s); \
|
|
|
|
\
|
|
|
|
for(;;) \
|
|
|
|
{ \
|
|
|
|
if ((*_d = *_s) == 0) \
|
|
|
|
break; \
|
|
|
|
\
|
|
|
|
_d++; \
|
|
|
|
_s++; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
_d; \
|
2010-04-25 12:46:57 +02:00
|
|
|
})*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
const char *FILE_cat(const char *path, ...)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
char *p;
|
|
|
|
va_list args;
|
|
|
|
int len;
|
|
|
|
bool end_slash = FALSE;
|
|
|
|
bool add_slash = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
va_start(args, path);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
p = file_buffer;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (path != file_buffer)
|
|
|
|
*p = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if (*path == '/' && p != file_buffer)
|
|
|
|
path++;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
len = strlen(path);
|
|
|
|
if (add_slash)
|
|
|
|
len++;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (len > 0)
|
|
|
|
{
|
2010-01-28 11:55:24 +01:00
|
|
|
if ((p + len) > &file_buffer[PATH_MAX])
|
2013-01-05 00:24:37 +01:00
|
|
|
THROW(E_TOOLONG);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (p != path)
|
|
|
|
{
|
|
|
|
if (add_slash)
|
|
|
|
p = stradd(p, "/");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
p = stradd(p, path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
p += len;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
end_slash = (p[-1] == '/');
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
path = va_arg(args, char *);
|
|
|
|
if (path == NULL)
|
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
add_slash = ((!end_slash) && (*path != 0) && (*path != '/'));
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-06 17:13:31 +01:00
|
|
|
va_end(args);
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
file_buffer_length = p - file_buffer;
|
|
|
|
return file_buffer;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
char *FILE_buffer(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
return file_buffer;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
int FILE_buffer_length(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
if (file_buffer_length < 0)
|
|
|
|
file_buffer_length = strlen(file_buffer);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
return file_buffer_length;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2013-01-05 00:24:37 +01:00
|
|
|
static void init_file_buffer(const char *path)
|
|
|
|
{
|
|
|
|
int len;
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2013-01-05 00:24:37 +01:00
|
|
|
if (path == file_buffer)
|
|
|
|
return;
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2013-01-05 00:24:37 +01:00
|
|
|
len = strlen(path);
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2013-01-05 00:24:37 +01:00
|
|
|
if (len > PATH_MAX)
|
|
|
|
THROW(E_TOOLONG);
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2013-01-05 00:24:37 +01:00
|
|
|
strcpy(file_buffer, path);
|
|
|
|
file_buffer_length = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
//#define INIT_FILE_BUFFER(_path) (init_file_buffer(), _path = file_buffer)
|
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
const char *FILE_get_dir(const char *path)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
char *p;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (path == NULL || path[0] == 0)
|
|
|
|
return NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (path[0] == '/' && path[1] == 0)
|
|
|
|
return "/";
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-05 00:24:37 +01:00
|
|
|
init_file_buffer(path);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
p = rindex(file_buffer, '/');
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (p == NULL)
|
|
|
|
*file_buffer = 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*p = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (file_buffer[0] == 0 && path[0] == '/')
|
|
|
|
strcpy(file_buffer, "/");
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
file_buffer_length = -1;
|
|
|
|
return file_buffer;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
const char *FILE_get_name(const char *path)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
const char *p;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
p = rindex(path, '/');
|
|
|
|
if (p)
|
|
|
|
return &p[1];
|
|
|
|
else
|
|
|
|
return path;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
const char *FILE_get_ext(const char *path)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
const char *p;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
p = rindex(path, '/');
|
|
|
|
if (p)
|
|
|
|
path = &p[1];
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
p = rindex(path, '.');
|
|
|
|
if (p == NULL)
|
|
|
|
return &path[strlen(path)];
|
|
|
|
else
|
|
|
|
return p + 1;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
const char *FILE_set_ext(const char *path, const char *ext)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
char *p;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-05 00:24:37 +01:00
|
|
|
init_file_buffer(path);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-05 00:24:37 +01:00
|
|
|
p = (char *)FILE_get_ext(file_buffer);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (!ext)
|
|
|
|
{
|
|
|
|
if (p > file_buffer && p[-1] == '.')
|
|
|
|
p[-1] = 0;
|
|
|
|
else
|
|
|
|
*p = 0;
|
2013-01-05 00:24:37 +01:00
|
|
|
return file_buffer;
|
2010-01-01 19:45:35 +01:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-28 11:55:24 +01:00
|
|
|
if (&p[strlen(ext)] >= &file_buffer[PATH_MAX])
|
2013-01-05 00:24:37 +01:00
|
|
|
THROW(E_TOOLONG);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-05 00:24:37 +01:00
|
|
|
if (p == file_buffer || p[-1] != '.')
|
2010-01-01 19:45:35 +01:00
|
|
|
*p++ = '.';
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (*ext == '.')
|
|
|
|
ext++;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
strcpy(p, ext);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
file_buffer_length = -1;
|
2013-01-05 00:24:37 +01:00
|
|
|
return file_buffer;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
const char *FILE_get_basename(const char *path)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
char *p;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
path = FILE_get_name(path);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-05 00:24:37 +01:00
|
|
|
init_file_buffer(path);
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
p = rindex(file_buffer, '.');
|
|
|
|
if (p)
|
|
|
|
*p = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
file_buffer_length = -1;
|
|
|
|
return file_buffer;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
bool FILE_is_dir(const char *path)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
struct stat buf;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#ifdef PROJECT_EXEC
|
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (FILE_is_relative(path))
|
|
|
|
{
|
|
|
|
/*if (!EXEC_arch)
|
|
|
|
{
|
|
|
|
chdir(PROJECT_path);
|
|
|
|
if (lstat(path, &buf) == 0)
|
|
|
|
goto __OK;
|
|
|
|
}*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
return ARCHIVE_is_dir(NULL, path);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (stat(path, &buf))
|
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-12-31 16:55:40 +01:00
|
|
|
/*#ifdef PROJECT_EXEC
|
2007-12-30 17:41:49 +01:00
|
|
|
__OK:
|
2008-12-31 16:55:40 +01:00
|
|
|
#endif*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
return (S_ISDIR(buf.st_mode));
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef PROJECT_EXEC
|
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
bool FILE_exist_real(const char *path)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
struct stat buf;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-10-31 23:05:29 +01:00
|
|
|
if (chdir(PROJECT_path)) return FALSE;
|
2010-01-01 19:45:35 +01:00
|
|
|
return (stat(path, &buf) == 0);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
void FILE_stat(const char *path, FILE_STAT *info, bool follow)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
struct stat buf;
|
|
|
|
int ret;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-05-27 01:03:06 +02:00
|
|
|
//fprintf(stderr, "FILE_stat: %s\n", path);
|
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (FILE_is_relative(path))
|
|
|
|
{
|
|
|
|
/*if (!EXEC_arch)
|
|
|
|
{
|
|
|
|
chdir(PROJECT_path);
|
|
|
|
if (lstat(path, &buf) == 0)
|
|
|
|
goto _OK;
|
|
|
|
}*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
ARCHIVE_stat(NULL, path, info);
|
|
|
|
return;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (follow)
|
|
|
|
ret = stat(path, &buf);
|
|
|
|
else
|
|
|
|
ret = lstat(path, &buf);
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (ret)
|
|
|
|
THROW_SYSTEM(errno, path);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-12-31 16:55:40 +01:00
|
|
|
//_OK:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (S_ISREG(buf.st_mode))
|
|
|
|
info->type = GB_STAT_FILE;
|
|
|
|
else if (S_ISDIR(buf.st_mode))
|
|
|
|
info->type = GB_STAT_DIRECTORY;
|
|
|
|
else if (S_ISCHR(buf.st_mode) || S_ISBLK(buf.st_mode))
|
|
|
|
info->type = GB_STAT_DEVICE;
|
|
|
|
else if (S_ISFIFO(buf.st_mode))
|
|
|
|
info->type = GB_STAT_PIPE;
|
|
|
|
else if (S_ISSOCK(buf.st_mode))
|
|
|
|
info->type = GB_STAT_SOCKET;
|
|
|
|
else if (S_ISLNK(buf.st_mode))
|
|
|
|
info->type = GB_STAT_LINK;
|
|
|
|
|
|
|
|
info->mode = buf.st_mode & 07777;
|
|
|
|
info->size = buf.st_size;
|
|
|
|
info->atime = (int)buf.st_atime;
|
|
|
|
info->mtime = (int)buf.st_mtime;
|
|
|
|
info->ctime = (int)buf.st_ctime;
|
|
|
|
info->hidden = (*FILE_get_name(path) == '.');
|
2013-09-29 18:56:12 +02:00
|
|
|
info->uid = buf.st_uid;
|
|
|
|
info->gid = buf.st_gid;
|
2020-12-19 17:37:31 +01:00
|
|
|
info->device = buf.st_dev;
|
|
|
|
info->blkdev = S_ISBLK(buf.st_mode);
|
|
|
|
info->chrdev = S_ISCHR(buf.st_mode);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
char *FILE_mode_to_string(mode_t mode)
|
|
|
|
{
|
|
|
|
char *str = file_buffer;
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
str[0] = mode & S_IRUSR ? 'r' : '-';
|
|
|
|
str[1] = mode & S_IWUSR ? 'w' : '-';
|
|
|
|
str[2] = (mode & S_ISUID
|
|
|
|
? (mode & S_IXUSR ? 's' : 'S')
|
|
|
|
: (mode & S_IXUSR ? 'x' : '-'));
|
|
|
|
str[3] = mode & S_IRGRP ? 'r' : '-';
|
|
|
|
str[4] = mode & S_IWGRP ? 'w' : '-';
|
|
|
|
str[5] = (mode & S_ISGID
|
|
|
|
? (mode & S_IXGRP ? 's' : 'S')
|
|
|
|
: (mode & S_IXGRP ? 'x' : '-'));
|
|
|
|
str[6] = mode & S_IROTH ? 'r' : '-';
|
|
|
|
str[7] = mode & S_IWOTH ? 'w' : '-';
|
|
|
|
str[8] = (mode & S_ISVTX
|
|
|
|
? (mode & S_IXOTH ? 't' : 'T')
|
|
|
|
: (mode & S_IXOTH ? 'x' : '-'));
|
|
|
|
str[9] = 0;
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
file_buffer_length = 9;
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
mode_t FILE_mode_from_string(mode_t mode, const char *str)
|
|
|
|
{
|
|
|
|
static FILE_MODE_DECODE decode[] = {
|
|
|
|
{ S_IRUSR, { '-', 'r' }, { 0, S_IRUSR } },
|
|
|
|
{ S_IWUSR, { '-', 'w' }, { 0, S_IWUSR } },
|
|
|
|
{ S_IXUSR | S_ISUID, { '-', 'x', 'S', 's' }, { 0, S_IXUSR, S_ISUID, S_IXUSR | S_ISUID } },
|
|
|
|
{ S_IRGRP, { '-', 'r' }, { 0, S_IRGRP } },
|
|
|
|
{ S_IWGRP, { '-', 'w' }, { 0, S_IWGRP } },
|
|
|
|
{ S_IXGRP | S_ISGID, { '-', 'x', 'S', 's' }, { 0, S_IXGRP, S_ISGID, S_IXGRP | S_ISGID } },
|
|
|
|
{ S_IROTH, { '-', 'r' }, { 0, S_IROTH } },
|
|
|
|
{ S_IWOTH, { '-', 'w' }, { 0, S_IWOTH } },
|
|
|
|
{ S_IXOTH | S_ISVTX, { '-', 'x', 'T', 't' }, { 0, S_IXOTH, S_ISVTX, S_IXOTH | S_ISVTX } },
|
|
|
|
{ 0 }
|
|
|
|
};
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
unsigned char c, test;
|
|
|
|
FILE_MODE_DECODE *d = decode;
|
|
|
|
int i;
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
while (d->mask)
|
|
|
|
{
|
|
|
|
c = *str++;
|
|
|
|
if (!c)
|
|
|
|
break;
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2018-06-12 13:39:53 +02:00
|
|
|
for (i = 0; i <= 3; i++)
|
2012-01-15 09:54:57 +01:00
|
|
|
{
|
|
|
|
test = d->test[i];
|
|
|
|
if (!test)
|
|
|
|
break;
|
|
|
|
if (c == test)
|
|
|
|
{
|
|
|
|
mode = (mode & ~(d->mask)) | d->mode[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
d++;
|
|
|
|
}
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FILE_chmod(const char *path, mode_t mode)
|
|
|
|
{
|
2012-01-15 11:22:12 +01:00
|
|
|
if (FILE_is_relative(path))
|
|
|
|
THROW(E_ACCESS);
|
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
if (chmod(path, mode))
|
|
|
|
THROW_SYSTEM(errno, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FILE_chown(const char *path, const char *user)
|
|
|
|
{
|
|
|
|
struct passwd *pwd;
|
|
|
|
uid_t uid;
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2012-01-15 11:22:12 +01:00
|
|
|
if (FILE_is_relative(path))
|
|
|
|
THROW(E_ACCESS);
|
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
if (!strcmp(user, "root"))
|
|
|
|
uid = (uid_t)0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
pwd = getpwnam(user);
|
|
|
|
if (errno)
|
|
|
|
THROW_SYSTEM(errno, path);
|
|
|
|
if (!pwd)
|
|
|
|
THROW(E_USER);
|
|
|
|
uid = pwd->pw_uid;
|
|
|
|
}
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
if (chown(path, uid, (gid_t)-1))
|
|
|
|
THROW_SYSTEM(errno, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FILE_chgrp(const char *path, const char *group)
|
|
|
|
{
|
|
|
|
struct group *grp;
|
|
|
|
gid_t gid;
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2012-01-15 11:22:12 +01:00
|
|
|
if (FILE_is_relative(path))
|
|
|
|
THROW(E_ACCESS);
|
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
if (!strcmp(group, "root"))
|
|
|
|
gid = (gid_t)0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
grp = getgrnam(group);
|
|
|
|
if (errno)
|
|
|
|
THROW_SYSTEM(errno, path);
|
|
|
|
if (!grp)
|
|
|
|
THROW(E_USER);
|
|
|
|
gid = grp->gr_gid;
|
|
|
|
}
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
if (chown(path, (uid_t)-1, gid))
|
|
|
|
THROW_SYSTEM(errno, path);
|
|
|
|
}
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
void FILE_dir_first(const char *path, const char *pattern, int attr)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
dir_exit();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (!path || *path == 0)
|
|
|
|
path = ".";
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (attr == (GB_STAT_FILE | GB_STAT_DIRECTORY))
|
|
|
|
attr = 0;
|
2016-02-21 16:48:40 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
file_attr = attr;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (FILE_is_relative(path))
|
|
|
|
{
|
|
|
|
/*if (!EXEC_arch)
|
|
|
|
{
|
|
|
|
chdir(PROJECT_path);
|
|
|
|
if (lstat(path, &buf) == 0)
|
|
|
|
goto _OK;
|
|
|
|
}*/
|
|
|
|
|
|
|
|
file_dir_arch = TRUE;
|
|
|
|
ARCHIVE_dir_first(NULL, path, pattern, attr);
|
|
|
|
return;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
file_dir_arch = FALSE;
|
|
|
|
file_dir = opendir(path);
|
|
|
|
if (file_dir == NULL)
|
|
|
|
THROW_SYSTEM(errno, path);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
file_pattern = STRING_new_zero(pattern);
|
|
|
|
file_path = STRING_new_zero(path);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2016-02-21 16:48:40 +01:00
|
|
|
#if OPT_FSTATAT
|
|
|
|
#else
|
|
|
|
#ifdef FSTATAT
|
|
|
|
#undef FSTATAT
|
|
|
|
#endif
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
bool FILE_dir_next(char **path, int *len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
struct dirent *entry;
|
|
|
|
int len_entry;
|
|
|
|
bool ret;
|
2010-05-22 20:02:34 +02:00
|
|
|
char *name;
|
2016-02-21 16:48:40 +01:00
|
|
|
#ifndef _DIRENT_HAVE_D_TYPE
|
2010-01-01 19:45:35 +01:00
|
|
|
struct stat info;
|
2016-02-21 16:48:40 +01:00
|
|
|
#ifndef HAVE_FSTATAT
|
|
|
|
char *p = file_buffer;
|
|
|
|
#endif
|
2009-04-04 00:29:51 +02:00
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (file_dir_arch)
|
|
|
|
{
|
|
|
|
ret = ARCHIVE_dir_next(path, len, file_attr);
|
|
|
|
if (ret)
|
|
|
|
file_dir_arch = FALSE;
|
|
|
|
return ret;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (file_dir == NULL)
|
|
|
|
return TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2016-02-21 16:48:40 +01:00
|
|
|
#ifndef _DIRENT_HAVE_D_TYPE
|
|
|
|
#ifndef HAVE_FSTATAT
|
|
|
|
if (file_attr)
|
|
|
|
{
|
|
|
|
init_file_buffer(file_path);
|
|
|
|
p += file_buffer_length;
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2016-02-21 16:48:40 +01:00
|
|
|
if (p[-1] != '/' && (file_buffer[1] || file_buffer[0] != '/'))
|
|
|
|
*p++ = '/';
|
|
|
|
}
|
|
|
|
#endif
|
2009-04-04 00:29:51 +02:00
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
entry = readdir(file_dir);
|
|
|
|
if (entry == NULL)
|
|
|
|
{
|
|
|
|
dir_exit();
|
|
|
|
return TRUE;
|
|
|
|
}
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2010-05-22 20:02:34 +02:00
|
|
|
name = entry->d_name;
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2010-05-22 20:02:34 +02:00
|
|
|
if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0)))
|
2010-01-01 19:45:35 +01:00
|
|
|
continue;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-06 17:13:31 +01:00
|
|
|
len_entry = strlen(name);
|
|
|
|
if ((len_entry + file_buffer_length) > PATH_MAX)
|
|
|
|
continue;
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (file_attr)
|
|
|
|
{
|
2009-04-04 00:29:51 +02:00
|
|
|
#ifdef _DIRENT_HAVE_D_TYPE
|
2010-01-01 19:45:35 +01:00
|
|
|
if ((file_attr == GB_STAT_DIRECTORY) ^ (entry->d_type == DT_DIR))
|
|
|
|
continue;
|
2009-04-04 00:29:51 +02:00
|
|
|
#else
|
2016-02-21 16:48:40 +01:00
|
|
|
#if HAVE_FSTATAT
|
|
|
|
if (fstatat(dirfd(file_dir), name, &info, 0))
|
|
|
|
#else
|
|
|
|
strcpy(p, name);
|
|
|
|
if (stat(file_buffer, &info))
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (file_attr == GB_STAT_DIRECTORY)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if ((file_attr == GB_STAT_DIRECTORY) ^ (S_ISDIR(info.st_mode) != 0))
|
2011-08-23 14:51:25 +02:00
|
|
|
continue;
|
2009-04-04 00:29:51 +02:00
|
|
|
#endif
|
2010-01-01 19:45:35 +01:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (file_pattern == NULL)
|
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-05-22 20:02:34 +02:00
|
|
|
if (REGEXP_match(file_pattern, STRING_length(file_pattern), name, len_entry))
|
2010-01-01 19:45:35 +01:00
|
|
|
break;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-05-22 20:02:34 +02:00
|
|
|
*path = name;
|
2010-01-01 19:45:35 +01:00
|
|
|
*len = len_entry;
|
2009-04-04 00:29:51 +02:00
|
|
|
#ifdef _DIRENT_HAVE_D_TYPE
|
|
|
|
_last_is_dir = entry->d_type == DT_DIR;
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2009-04-04 00:29:51 +02:00
|
|
|
//#undef _DIRENT_HAVE_D_TYPE
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-03-25 03:00:55 +01:00
|
|
|
void FILE_recursive_dir(const char *dir, void (*found)(const char *), void (*afterfound)(const char *), int attr, bool follow)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
void *list = NULL;
|
2009-04-04 00:29:51 +02:00
|
|
|
void *dir_list = NULL;
|
2010-01-01 19:45:35 +01:00
|
|
|
char *file;
|
|
|
|
int len;
|
|
|
|
char *path;
|
|
|
|
//struct stat buf;
|
2009-04-04 00:29:51 +02:00
|
|
|
#ifdef _DIRENT_HAVE_D_TYPE
|
|
|
|
#else
|
2010-01-01 19:45:35 +01:00
|
|
|
FILE_STAT info;
|
2009-04-04 00:29:51 +02:00
|
|
|
#endif
|
2010-01-01 19:45:35 +01:00
|
|
|
char *temp;
|
2009-04-04 00:29:51 +02:00
|
|
|
bool is_dir;
|
2016-02-21 16:48:40 +01:00
|
|
|
#if OPT_NLINK
|
2018-03-16 14:02:58 +01:00
|
|
|
int nsubdir = -1;
|
2016-02-21 16:48:40 +01:00
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (!dir || *dir == 0)
|
|
|
|
dir = ".";
|
2013-01-06 17:13:31 +01:00
|
|
|
else if (!FILE_is_dir(dir))
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
STRING_free(&file_rdir_path);
|
2010-06-05 01:48:53 +02:00
|
|
|
file_rdir_path = STRING_new_zero(dir);
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2012-03-14 03:11:36 +01:00
|
|
|
FILE_dir_first(dir, NULL, attr != GB_STAT_DIRECTORY ? 0 : GB_STAT_DIRECTORY);
|
2016-02-21 16:48:40 +01:00
|
|
|
|
|
|
|
#if OPT_NLINK
|
2018-03-16 14:02:58 +01:00
|
|
|
if (file_dir && !FILE_is_relative(dir))
|
2016-02-21 16:48:40 +01:00
|
|
|
{
|
|
|
|
struct stat dinfo;
|
|
|
|
fstat(dirfd(file_dir), &dinfo);
|
2017-09-01 04:22:08 +02:00
|
|
|
// If the number of links N to the directory:
|
|
|
|
// - Is > 2, then the directory has N - 2 sub-directories
|
|
|
|
// - Is = 2, then the directory has no sub-directory
|
|
|
|
// - Is < 2, then the file system is not POSIX, so we must scan everything
|
|
|
|
nsubdir = dinfo.st_nlink - 2;
|
2016-02-21 16:48:40 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
while (!FILE_dir_next(&file, &len))
|
|
|
|
{
|
2010-06-05 01:48:53 +02:00
|
|
|
temp = STRING_new_temp(file, len);
|
2009-04-04 00:29:51 +02:00
|
|
|
path = (char *)FILE_cat(file_rdir_path, temp, NULL);
|
2016-02-21 16:48:40 +01:00
|
|
|
|
|
|
|
#if OPT_NLINK
|
2017-09-01 04:22:08 +02:00
|
|
|
if (nsubdir || follow)
|
2016-02-21 16:48:40 +01:00
|
|
|
#endif
|
2010-04-02 14:34:21 +02:00
|
|
|
{
|
2016-02-21 16:48:40 +01:00
|
|
|
#ifdef _DIRENT_HAVE_D_TYPE
|
|
|
|
is_dir = _last_is_dir;
|
|
|
|
#else
|
|
|
|
if (follow)
|
|
|
|
is_dir = FILE_is_dir(path);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FILE_stat(path, &info, FALSE);
|
|
|
|
is_dir = info.type == GB_STAT_DIRECTORY;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (is_dir)
|
|
|
|
{
|
2017-09-01 04:22:08 +02:00
|
|
|
nsubdir--;
|
2016-02-21 16:48:40 +01:00
|
|
|
push_path(&dir_list, path);
|
|
|
|
continue;
|
|
|
|
}
|
2010-04-02 14:34:21 +02:00
|
|
|
}
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2016-02-21 16:48:40 +01:00
|
|
|
push_path(&list, path);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
while (dir_list)
|
|
|
|
{
|
|
|
|
path = pop_path(&dir_list);
|
2007-12-30 17:41:49 +01:00
|
|
|
//fprintf(stderr, "%s\n", path);
|
|
|
|
|
|
|
|
TRY
|
|
|
|
{
|
2012-03-14 03:11:36 +01:00
|
|
|
if (found && (!attr || (attr & GB_STAT_DIRECTORY))) (*found)(path);
|
2010-03-25 03:00:55 +01:00
|
|
|
FILE_recursive_dir(path, found, afterfound, attr, follow);
|
2012-03-14 03:11:36 +01:00
|
|
|
if (afterfound && (!attr || (attr & GB_STAT_DIRECTORY))) (*afterfound)(path);
|
2009-04-04 00:29:51 +02:00
|
|
|
}
|
|
|
|
CATCH
|
|
|
|
{
|
|
|
|
//ERROR_print_at(stdout);
|
|
|
|
}
|
|
|
|
END_TRY
|
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
STRING_free((char **)&path);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
while (list)
|
|
|
|
{
|
|
|
|
path = pop_path(&list);
|
2009-04-04 00:29:51 +02:00
|
|
|
//fprintf(stderr, "%s\n", path);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-04-04 00:29:51 +02:00
|
|
|
TRY
|
|
|
|
{
|
|
|
|
if (found) (*found)(path);
|
2007-12-30 17:41:49 +01:00
|
|
|
if (afterfound) (*afterfound)(path);
|
|
|
|
}
|
|
|
|
CATCH
|
|
|
|
{
|
|
|
|
//ERROR_print_at(stdout);
|
|
|
|
}
|
|
|
|
END_TRY
|
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
STRING_free((char **)&path);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
void FILE_rmdir(const char *path)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
if (FILE_is_relative(path))
|
|
|
|
THROW(E_ACCESS);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (rmdir(path) != 0)
|
2012-01-15 09:54:57 +01:00
|
|
|
{
|
|
|
|
if (errno == ENOTEMPTY || errno == EEXIST)
|
|
|
|
THROW(E_NEMPTY);
|
|
|
|
else
|
|
|
|
THROW_SYSTEM(errno, path);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-14 01:09:30 +01:00
|
|
|
void FILE_mkdir_mode(const char *path, mode_t mode)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
if (FILE_is_relative(path))
|
|
|
|
THROW(E_ACCESS);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2021-01-14 01:09:30 +01:00
|
|
|
if (mkdir(path, mode) != 0)
|
2010-01-01 19:45:35 +01:00
|
|
|
THROW_SYSTEM(errno, path);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
void FILE_make_path_dir(const char *path)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
int i;
|
|
|
|
char c;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (FILE_is_relative(path))
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-05 00:24:37 +01:00
|
|
|
init_file_buffer(path);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
for (i = 1;; i++)
|
|
|
|
{
|
|
|
|
c = file_buffer[i];
|
|
|
|
if (c == 0)
|
|
|
|
break;
|
|
|
|
if (c == '/')
|
|
|
|
{
|
|
|
|
file_buffer[i] = 0;
|
2013-01-06 17:13:31 +01:00
|
|
|
(void)mkdir(file_buffer, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
|
2010-01-01 19:45:35 +01:00
|
|
|
file_buffer[i] = c;
|
|
|
|
}
|
|
|
|
c++;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
void FILE_copy(const char *src, const char *dst)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
STREAM stream_src;
|
|
|
|
STREAM stream_dst;
|
|
|
|
int64_t len;
|
|
|
|
int64_t n;
|
|
|
|
char *buf = NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
CLEAR(&stream_src);
|
|
|
|
CLEAR(&stream_dst);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (FILE_exist(dst))
|
|
|
|
THROW(E_EXIST, dst);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-03-30 00:33:01 +01:00
|
|
|
ALLOC(&buf, MAX_IO);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
TRY
|
|
|
|
{
|
2020-11-09 17:38:28 +01:00
|
|
|
STREAM_open(&stream_src, src, GB_ST_READ);
|
|
|
|
STREAM_open(&stream_dst, dst, GB_ST_CREATE);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
STREAM_lof(&stream_src, &len);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
while (len)
|
|
|
|
{
|
|
|
|
n = len > MAX_IO ? MAX_IO : len;
|
|
|
|
STREAM_read(&stream_src, buf, n);
|
|
|
|
STREAM_write(&stream_dst, buf, n);
|
|
|
|
len -= n;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
STREAM_close(&stream_src);
|
|
|
|
STREAM_close(&stream_dst);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-03-30 00:33:01 +01:00
|
|
|
FREE(&buf);
|
2010-01-01 19:45:35 +01:00
|
|
|
}
|
|
|
|
CATCH
|
|
|
|
{
|
|
|
|
if (stream_src.type)
|
|
|
|
STREAM_close(&stream_src);
|
|
|
|
if (stream_dst.type)
|
|
|
|
STREAM_close(&stream_dst);
|
2013-03-30 00:33:01 +01:00
|
|
|
FREE(&buf);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
PROPAGATE();
|
|
|
|
}
|
|
|
|
END_TRY
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
bool FILE_access(const char *path, int mode)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2020-11-09 17:38:28 +01:00
|
|
|
int m;
|
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (FILE_is_relative(path))
|
|
|
|
{
|
2020-11-09 17:38:28 +01:00
|
|
|
if (mode & (GB_ST_WRITE | GB_ST_EXEC))
|
2010-01-01 19:45:35 +01:00
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
/*if (!EXEC_arch)
|
|
|
|
{
|
|
|
|
chdir(PROJECT_path);
|
|
|
|
if (access(path, mode) == 0)
|
|
|
|
return TRUE;
|
|
|
|
}*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
return ARCHIVE_exist(NULL, path);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2020-11-09 17:38:28 +01:00
|
|
|
m = 0;
|
|
|
|
if (mode & GB_ST_READ) m += R_OK;
|
|
|
|
if (mode & GB_ST_WRITE) m += W_OK;
|
|
|
|
if (mode & GB_ST_EXEC) m += X_OK;
|
|
|
|
|
|
|
|
return (access(path, m) == 0);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-17 12:57:55 +02:00
|
|
|
bool FILE_exist_follow(const char *path, bool follow)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
struct stat buf;
|
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (FILE_is_relative(path))
|
|
|
|
return ARCHIVE_exist(NULL, path);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-07-17 12:57:55 +02:00
|
|
|
return follow ? (stat(path, &buf) == 0) : (lstat(path, &buf) == 0);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
void FILE_link(const char *src, const char *dst)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
/* src can be relative */
|
|
|
|
if (FILE_is_relative(dst))
|
|
|
|
THROW(E_ACCESS);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (FILE_exist(dst))
|
|
|
|
THROW(E_EXIST, dst);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (symlink(src, dst) != 0)
|
|
|
|
THROW_SYSTEM(errno, dst);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
int64_t FILE_free(const char *path)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2018-08-29 23:59:11 +02:00
|
|
|
struct statvfs info;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (FILE_is_relative(path))
|
|
|
|
return 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-08-29 23:59:11 +02:00
|
|
|
statvfs(path, &info);
|
2010-01-01 19:45:35 +01:00
|
|
|
return (int64_t)(getuid() == 0 ? info.f_bfree : info.f_bavail) * info.f_bsize;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
bool FILE_exist(const char *path)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
return (access(path, F_OK) == 0);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
time_t FILE_get_time(const char *path)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
struct stat info;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (stat(path, &info) == 0)
|
|
|
|
return info.st_mtime;
|
|
|
|
else
|
|
|
|
return (time_t)-1L;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 01:43:27 +01:00
|
|
|
size_t FILE_get_size(const char *path)
|
|
|
|
{
|
|
|
|
struct stat info;
|
|
|
|
|
|
|
|
if (stat(path, &info) == 0)
|
|
|
|
return info.st_size;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-03-31 07:51:47 +02:00
|
|
|
bool FILE_copy(const char *src, const char *dst)
|
|
|
|
{
|
|
|
|
int src_fd;
|
|
|
|
int dst_fd;
|
|
|
|
ssize_t len;
|
|
|
|
char *buf = NULL;
|
|
|
|
int save_errno;
|
2013-03-31 08:24:30 +02:00
|
|
|
struct stat info;
|
2013-03-31 07:51:47 +02:00
|
|
|
|
2013-03-31 08:24:30 +02:00
|
|
|
fprintf(stderr, "FILE_copy: %s -> %s\n", src, dst);
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2013-03-31 08:24:30 +02:00
|
|
|
if (stat(src, &info))
|
|
|
|
return TRUE;
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2013-03-31 07:51:47 +02:00
|
|
|
src_fd = open(src, O_RDONLY);
|
|
|
|
if (src_fd < 0)
|
2013-03-31 08:24:30 +02:00
|
|
|
{
|
|
|
|
fprintf(stderr, "open src failed\n");
|
2013-03-31 07:51:47 +02:00
|
|
|
return TRUE;
|
2013-03-31 08:24:30 +02:00
|
|
|
}
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2013-03-31 08:24:30 +02:00
|
|
|
dst_fd = creat(dst, info.st_mode);
|
2013-03-31 07:51:47 +02:00
|
|
|
if (dst_fd < 0)
|
|
|
|
{
|
2013-03-31 08:24:30 +02:00
|
|
|
fprintf(stderr, "open dst failed\n");
|
2013-03-31 07:51:47 +02:00
|
|
|
save_errno = errno;
|
|
|
|
close(src_fd);
|
|
|
|
errno = save_errno;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2013-03-31 07:51:47 +02:00
|
|
|
ALLOC(&buf, MAX_IO);
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
len = read(src_fd, buf, MAX_IO);
|
|
|
|
if (len == 0)
|
|
|
|
break;
|
|
|
|
if (len < 0 && errno == EINTR)
|
|
|
|
continue;
|
|
|
|
if (write(dst_fd, buf, len) < 0)
|
|
|
|
{
|
|
|
|
save_errno = errno;
|
|
|
|
close(src_fd);
|
|
|
|
close(dst_fd);
|
|
|
|
unlink(dst);
|
|
|
|
errno = save_errno;
|
|
|
|
IFREE(buf);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2013-03-31 07:51:47 +02:00
|
|
|
close(src_fd);
|
|
|
|
close(dst_fd);
|
|
|
|
IFREE(buf);
|
2015-11-11 04:40:50 +01:00
|
|
|
|
2013-03-31 07:51:47 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
#endif
|
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
const char *FILE_getcwd(const char *subdir)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
if (getcwd(file_buffer, PATH_MAX) == NULL)
|
|
|
|
return NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
file_buffer_length = strlen(file_buffer);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (subdir != NULL)
|
|
|
|
return FILE_cat(file_buffer, subdir, NULL);
|
|
|
|
else
|
|
|
|
return file_buffer;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
const char *FILE_readlink(const char *link)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-28 11:55:24 +01:00
|
|
|
int len = readlink(link, file_buffer, PATH_MAX);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
if (len < 0)
|
|
|
|
return NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
file_buffer[len] = 0;
|
|
|
|
file_buffer_length = len;
|
|
|
|
return file_buffer;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-01-27 02:24:19 +01:00
|
|
|
const char *FILE_find_gambas(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-01-01 19:45:35 +01:00
|
|
|
const char *path;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-10-26 23:06:22 +02:00
|
|
|
path = getenv("GB_PATH");
|
|
|
|
|
|
|
|
if (!path || !*path)
|
2010-01-01 19:45:35 +01:00
|
|
|
{
|
2013-10-26 23:06:22 +02:00
|
|
|
if (FILE_exist(GAMBAS_LINK_PATH))
|
|
|
|
{
|
|
|
|
path = FILE_readlink(GAMBAS_LINK_PATH);
|
|
|
|
if (!path)
|
|
|
|
path = GAMBAS_LINK_PATH;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
path = GAMBAS_PATH "/gbx" GAMBAS_VERSION_STRING;
|
|
|
|
}
|
2010-01-01 19:45:35 +01:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-01-01 19:45:35 +01:00
|
|
|
return path;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2010-10-31 23:05:29 +01:00
|
|
|
void FILE_chdir(const char *path)
|
|
|
|
{
|
|
|
|
#ifdef PROJECT_EXEC
|
|
|
|
if (chdir(path))
|
|
|
|
THROW_SYSTEM(errno, path);
|
|
|
|
#else
|
|
|
|
if (chdir(path))
|
2011-03-29 02:24:15 +02:00
|
|
|
THROW("Cannot change current directory to '&1': &2", path, strerror(errno));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void FILE_unlink(const char *path)
|
|
|
|
{
|
|
|
|
#ifdef PROJECT_EXEC
|
|
|
|
if (FILE_is_relative(path))
|
|
|
|
THROW(E_ACCESS);
|
|
|
|
|
|
|
|
if (unlink(path) != 0)
|
|
|
|
THROW_SYSTEM(errno, path);
|
|
|
|
#else
|
|
|
|
if (unlink(path) != 0 && errno != ENOENT)
|
|
|
|
THROW("Cannot remove file '&1': &2", path, strerror(errno));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-27 15:18:35 +02:00
|
|
|
void FILE_rename_ext(const char *src, const char *dst, bool kill)
|
2011-03-29 02:24:15 +02:00
|
|
|
{
|
|
|
|
#ifdef PROJECT_EXEC
|
|
|
|
if (FILE_is_relative(src) || FILE_is_relative(dst))
|
|
|
|
THROW(E_ACCESS);
|
|
|
|
|
2017-04-27 15:18:35 +02:00
|
|
|
if (!kill)
|
|
|
|
{
|
|
|
|
if (FILE_exist(dst))
|
|
|
|
THROW(E_EXIST, dst);
|
|
|
|
}
|
2011-03-29 02:24:15 +02:00
|
|
|
|
|
|
|
if (rename(src, dst) != 0)
|
|
|
|
THROW_SYSTEM(errno, dst);
|
|
|
|
#else
|
|
|
|
if (rename(src, dst) != 0)
|
|
|
|
THROW("Cannot rename file '&1' to '&2': &3", src, dst, strerror(errno));
|
2010-10-31 23:05:29 +01:00
|
|
|
#endif
|
|
|
|
}
|
2011-03-29 02:24:15 +02:00
|
|
|
|
2016-02-01 03:18:49 +01:00
|
|
|
char *FILE_get_home(void)
|
|
|
|
{
|
|
|
|
struct passwd *info;
|
|
|
|
uid_t uid = getuid();
|
|
|
|
|
|
|
|
if (!_home || _uid != uid)
|
|
|
|
{
|
|
|
|
#ifdef PROJECT_EXEC
|
|
|
|
STRING_free(&_home);
|
|
|
|
#else
|
|
|
|
STR_free(_home);
|
|
|
|
#endif
|
|
|
|
info = getpwuid(uid);
|
|
|
|
if (info)
|
|
|
|
#ifdef PROJECT_EXEC
|
|
|
|
_home = STRING_new_zero(info->pw_dir);
|
|
|
|
#else
|
|
|
|
_home = STR_copy(info->pw_dir);
|
|
|
|
#endif
|
|
|
|
_uid = uid;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _home;
|
|
|
|
}
|
2019-03-21 23:14:22 +01:00
|
|
|
|