2005-02-20 04:17:07 -08:00
|
|
|
/*
|
|
|
|
* vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
|
|
|
|
*/
|
|
|
|
#include "ecore_file_private.h"
|
|
|
|
|
2005-09-07 01:50:25 -07:00
|
|
|
static int init = 0;
|
|
|
|
|
2005-02-20 04:17:07 -08:00
|
|
|
/* externally accessible functions */
|
|
|
|
int
|
|
|
|
ecore_file_init()
|
|
|
|
{
|
2005-09-07 01:57:08 -07:00
|
|
|
if (++init != 1) return init;
|
2005-09-07 01:50:25 -07:00
|
|
|
|
2005-02-20 04:17:07 -08:00
|
|
|
if (!ecore_file_monitor_init())
|
2005-09-07 01:50:25 -07:00
|
|
|
goto error;
|
2005-06-11 06:29:09 -07:00
|
|
|
if (!ecore_file_path_init())
|
2005-09-07 01:50:25 -07:00
|
|
|
goto error;
|
2005-06-17 16:52:32 -07:00
|
|
|
if (!ecore_file_download_init())
|
2005-09-07 01:50:25 -07:00
|
|
|
goto error;
|
|
|
|
return init;
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
|
|
|
ecore_file_monitor_shutdown();
|
|
|
|
ecore_file_path_shutdown();
|
|
|
|
ecore_file_download_shutdown();
|
|
|
|
|
|
|
|
return --init;
|
2005-02-20 04:17:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ecore_file_shutdown()
|
|
|
|
{
|
2005-09-07 01:57:08 -07:00
|
|
|
if (--init != 0) return init;
|
2005-09-07 01:50:25 -07:00
|
|
|
|
|
|
|
ecore_file_monitor_shutdown();
|
|
|
|
ecore_file_path_shutdown();
|
|
|
|
ecore_file_download_shutdown();
|
|
|
|
|
|
|
|
return init;
|
2005-02-20 04:17:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
time_t
|
2005-02-21 08:03:08 -08:00
|
|
|
ecore_file_mod_time(const char *file)
|
2005-02-20 04:17:07 -08:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat(file, &st) < 0) return 0;
|
|
|
|
return st.st_mtime;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-02-21 08:03:08 -08:00
|
|
|
ecore_file_exists(const char *file)
|
2005-02-20 04:17:07 -08:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
2005-08-26 21:17:36 -07:00
|
|
|
/*Workaround so that "/" returns a true, otherwise we can't monitor "/" in ecore_file_monitor*/
|
|
|
|
if (stat(file, &st) < 0 && strcmp(file, "/")) return 0;
|
2005-02-20 04:17:07 -08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-02-21 08:03:08 -08:00
|
|
|
ecore_file_is_dir(const char *file)
|
2005-02-20 04:17:07 -08:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat(file, &st) < 0) return 0;
|
|
|
|
if (S_ISDIR(st.st_mode)) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static mode_t default_mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
|
|
|
|
|
|
|
|
int
|
2005-02-21 08:03:08 -08:00
|
|
|
ecore_file_mkdir(const char *dir)
|
2005-02-20 04:17:07 -08:00
|
|
|
{
|
|
|
|
if (mkdir(dir, default_mode) < 0) return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-04-28 14:53:36 -07:00
|
|
|
int
|
|
|
|
ecore_file_rmdir(const char *dir)
|
|
|
|
{
|
|
|
|
if (rmdir(dir) < 0) return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ecore_file_unlink(const char *file)
|
|
|
|
{
|
|
|
|
if (unlink(file) < 0) return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-02-20 04:17:07 -08:00
|
|
|
int
|
2005-02-21 08:03:08 -08:00
|
|
|
ecore_file_mkpath(const char *path)
|
2005-02-20 04:17:07 -08:00
|
|
|
{
|
|
|
|
char ss[PATH_MAX];
|
2005-05-04 00:15:26 -07:00
|
|
|
int i;
|
2005-02-20 04:17:07 -08:00
|
|
|
|
|
|
|
ss[0] = 0;
|
|
|
|
i = 0;
|
|
|
|
while (path[i])
|
|
|
|
{
|
2005-05-04 00:15:26 -07:00
|
|
|
if (i == sizeof(ss) - 1) return 0;
|
|
|
|
ss[i] = path[i];
|
|
|
|
ss[i + 1] = 0;
|
2005-02-20 04:17:07 -08:00
|
|
|
if (path[i] == '/')
|
|
|
|
{
|
2005-05-04 00:15:26 -07:00
|
|
|
ss[i] = 0;
|
|
|
|
if ((ecore_file_exists(ss)) && (!ecore_file_is_dir(ss))) return 0;
|
|
|
|
else if (!ecore_file_exists(ss)) ecore_file_mkdir(ss);
|
|
|
|
ss[i] = '/';
|
2005-02-20 04:17:07 -08:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2005-05-04 00:15:26 -07:00
|
|
|
if ((ecore_file_exists(ss)) && (!ecore_file_is_dir(ss))) return 0;
|
|
|
|
else if (!ecore_file_exists(ss)) ecore_file_mkdir(ss);
|
2005-02-20 04:17:07 -08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-02-21 08:03:08 -08:00
|
|
|
ecore_file_cp(const char *src, const char *dst)
|
2005-02-20 04:17:07 -08:00
|
|
|
{
|
|
|
|
FILE *f1, *f2;
|
|
|
|
char buf[16384];
|
|
|
|
size_t num;
|
|
|
|
|
|
|
|
f1 = fopen(src, "rb");
|
|
|
|
if (!f1) return 0;
|
|
|
|
f2 = fopen(dst, "wb");
|
|
|
|
if (!f2)
|
|
|
|
{
|
|
|
|
fclose(f1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
while ((num = fread(buf, 1, 16384, f1)) > 0) fwrite(buf, 1, num, f2);
|
|
|
|
fclose(f1);
|
|
|
|
fclose(f2);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-05-11 01:52:27 -07:00
|
|
|
int
|
|
|
|
ecore_file_mv(const char *src, const char *dst)
|
|
|
|
{
|
|
|
|
if (ecore_file_exists(dst)) return 0;
|
|
|
|
if (rename(src, dst)) return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-02-20 04:17:07 -08:00
|
|
|
char *
|
2005-02-21 08:03:08 -08:00
|
|
|
ecore_file_realpath(const char *file)
|
2005-02-20 04:17:07 -08:00
|
|
|
{
|
|
|
|
char buf[PATH_MAX];
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (!realpath(file, buf) || stat(buf, &st)) return strdup("");
|
|
|
|
return strdup(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2005-02-22 04:38:17 -08:00
|
|
|
ecore_file_get_file(char *path)
|
2005-02-20 04:17:07 -08:00
|
|
|
{
|
|
|
|
char *result = NULL;
|
|
|
|
|
|
|
|
if (!path) return NULL;
|
|
|
|
if ((result = strrchr(path, '/'))) result++;
|
|
|
|
else result = path;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2005-02-22 04:38:17 -08:00
|
|
|
ecore_file_get_dir(char *file)
|
2005-02-20 04:17:07 -08:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
char buf[PATH_MAX];
|
|
|
|
|
|
|
|
strncpy(buf, file, PATH_MAX);
|
|
|
|
p = strrchr(buf, '/');
|
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
return strdup(file);
|
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
return strdup(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-02-21 08:03:08 -08:00
|
|
|
ecore_file_can_exec(const char *file)
|
2005-02-20 04:17:07 -08:00
|
|
|
{
|
|
|
|
static int have_uid = 0;
|
|
|
|
static uid_t uid = -1;
|
|
|
|
static gid_t gid = -1;
|
|
|
|
struct stat st;
|
|
|
|
int ok;
|
2005-09-29 10:50:04 -07:00
|
|
|
char buf[PATH_MAX];
|
2005-02-20 04:17:07 -08:00
|
|
|
|
|
|
|
if (!file) return 0;
|
2005-09-29 10:50:04 -07:00
|
|
|
if (!realpath(file, buf)) return 0;
|
|
|
|
if (stat(buf, &st) < 0) return 0;
|
2005-02-20 04:17:07 -08:00
|
|
|
|
|
|
|
ok = 0;
|
|
|
|
if (!have_uid) uid = getuid();
|
|
|
|
if (!have_uid) gid = getgid();
|
|
|
|
have_uid = 1;
|
|
|
|
if (st.st_uid == uid)
|
|
|
|
{
|
|
|
|
if (st.st_mode & S_IXUSR) ok = 1;
|
|
|
|
}
|
|
|
|
else if (st.st_gid == gid)
|
|
|
|
{
|
|
|
|
if (st.st_mode & S_IXGRP) ok = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (st.st_mode & S_IXOTH) ok = 1;
|
|
|
|
}
|
|
|
|
return(ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2005-02-21 08:03:08 -08:00
|
|
|
ecore_file_readlink(const char *link)
|
2005-02-20 04:17:07 -08:00
|
|
|
{
|
|
|
|
char buf[PATH_MAX];
|
|
|
|
int count;
|
|
|
|
|
|
|
|
if ((count = readlink(link, buf, sizeof(buf))) < 0) return NULL;
|
|
|
|
buf[count] = 0;
|
|
|
|
return strdup(buf);
|
|
|
|
}
|
|
|
|
|
2005-04-14 03:52:58 -07:00
|
|
|
Ecore_List *
|
2005-02-21 08:03:08 -08:00
|
|
|
ecore_file_ls(const char *dir)
|
2005-02-20 04:17:07 -08:00
|
|
|
{
|
|
|
|
DIR *dirp;
|
|
|
|
struct dirent *dp;
|
2005-04-14 06:35:50 -07:00
|
|
|
Ecore_List *list;
|
2005-02-20 04:17:07 -08:00
|
|
|
|
|
|
|
dirp = opendir(dir);
|
|
|
|
if (!dirp) return NULL;
|
2005-04-14 06:35:50 -07:00
|
|
|
list = ecore_list_new();
|
2005-02-20 04:17:07 -08:00
|
|
|
while ((dp = readdir(dirp)))
|
|
|
|
{
|
|
|
|
if ((strcmp(dp->d_name, ".")) && (strcmp(dp->d_name, "..")))
|
|
|
|
{
|
2005-04-14 03:52:58 -07:00
|
|
|
char *file, *f;
|
2005-02-20 04:17:07 -08:00
|
|
|
|
|
|
|
/* insertion sort */
|
2005-04-14 06:35:50 -07:00
|
|
|
ecore_list_goto_first(list);
|
|
|
|
while ((file = ecore_list_current(list)))
|
2005-02-20 04:17:07 -08:00
|
|
|
{
|
2005-09-11 19:42:39 -07:00
|
|
|
if (strcasecmp(file, dp->d_name) > 0)
|
2005-02-20 04:17:07 -08:00
|
|
|
{
|
|
|
|
f = strdup(dp->d_name);
|
2005-04-14 06:35:50 -07:00
|
|
|
ecore_list_insert(list, f);
|
2005-02-20 04:17:07 -08:00
|
|
|
break;
|
|
|
|
}
|
2005-04-14 06:35:50 -07:00
|
|
|
ecore_list_next(list);
|
2005-02-20 04:17:07 -08:00
|
|
|
}
|
|
|
|
/* nowhwre to go? just append it */
|
2005-04-14 03:52:58 -07:00
|
|
|
if (!file)
|
2005-03-22 03:30:33 -08:00
|
|
|
{
|
|
|
|
f = strdup(dp->d_name);
|
2005-09-11 19:42:39 -07:00
|
|
|
ecore_list_insert(list, f);
|
2005-03-22 03:30:33 -08:00
|
|
|
}
|
2005-02-20 04:17:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dirp);
|
|
|
|
|
2005-04-14 06:35:50 -07:00
|
|
|
ecore_list_goto_first(list);
|
|
|
|
return list;
|
2005-02-20 04:17:07 -08:00
|
|
|
}
|