2008-08-06 11:15:24 -07:00
|
|
|
/* EINA - EFL data type library
|
2008-09-01 05:51:50 -07:00
|
|
|
* Copyright (C) 2007-2008 Jorge Luis Zapata Muga, Vincent Torri
|
2011-09-15 08:37:28 -07:00
|
|
|
* Copyright (C) 2010-2011 Cedric Bail
|
2008-08-06 11:15:24 -07:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library;
|
|
|
|
* if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-08-28 00:46:42 -07:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2009-09-06 21:32:49 -07:00
|
|
|
# include "config.h"
|
2008-08-28 00:46:42 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_ALLOCA_H
|
|
|
|
# include <alloca.h>
|
|
|
|
#elif defined __GNUC__
|
|
|
|
# define alloca __builtin_alloca
|
|
|
|
#elif defined _AIX
|
|
|
|
# define alloca __alloca
|
|
|
|
#elif defined _MSC_VER
|
|
|
|
# include <malloc.h>
|
|
|
|
# define alloca _alloca
|
|
|
|
#else
|
|
|
|
# include <stddef.h>
|
|
|
|
# ifdef __cplusplus
|
|
|
|
extern "C"
|
|
|
|
# endif
|
|
|
|
void *alloca (size_t);
|
|
|
|
#endif
|
|
|
|
|
2009-08-23 02:18:14 -07:00
|
|
|
#include <string.h>
|
2010-10-28 02:59:27 -07:00
|
|
|
#include <stddef.h>
|
2010-07-11 04:52:43 -07:00
|
|
|
#include <dirent.h>
|
2010-11-06 14:00:58 -07:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2011-04-13 09:15:30 -07:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <fcntl.h>
|
2009-08-23 02:18:14 -07:00
|
|
|
|
2010-11-06 14:00:58 -07:00
|
|
|
#define PATH_DELIM '/'
|
2008-07-30 06:35:49 -07:00
|
|
|
|
2009-08-28 05:03:34 -07:00
|
|
|
#include "eina_config.h"
|
2008-07-30 05:46:55 -07:00
|
|
|
#include "eina_private.h"
|
2009-09-06 15:21:56 -07:00
|
|
|
|
|
|
|
/* undefs EINA_ARG_NONULL() so NULL checks are not compiled out! */
|
2008-12-26 10:31:14 -08:00
|
|
|
#include "eina_safety_checks.h"
|
2009-08-28 05:03:34 -07:00
|
|
|
#include "eina_file.h"
|
2010-07-02 10:23:05 -07:00
|
|
|
#include "eina_stringshare.h"
|
2011-04-13 09:15:30 -07:00
|
|
|
#include "eina_hash.h"
|
|
|
|
#include "eina_list.h"
|
2011-06-06 10:08:06 -07:00
|
|
|
#include "eina_lock.h"
|
2011-07-04 02:29:59 -07:00
|
|
|
#include "eina_mmap.h"
|
2010-07-02 10:23:05 -07:00
|
|
|
|
2010-11-06 00:37:52 -07:00
|
|
|
/*============================================================================*
|
|
|
|
* Local *
|
|
|
|
*============================================================================*/
|
|
|
|
|
2011-04-13 10:03:24 -07:00
|
|
|
/**
|
|
|
|
* @cond LOCAL
|
|
|
|
*/
|
|
|
|
|
2011-04-13 09:15:30 -07:00
|
|
|
#ifndef EINA_LOG_COLOR_DEFAULT
|
|
|
|
#define EINA_LOG_COLOR_DEFAULT EINA_COLOR_CYAN
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ERR
|
|
|
|
#undef ERR
|
|
|
|
#endif
|
|
|
|
#define ERR(...) EINA_LOG_DOM_ERR(_eina_file_log_dom, __VA_ARGS__)
|
|
|
|
|
|
|
|
#ifdef WRN
|
|
|
|
#undef WRN
|
|
|
|
#endif
|
|
|
|
#define WRN(...) EINA_LOG_DOM_WARN(_eina_file_log_dom, __VA_ARGS__)
|
|
|
|
|
|
|
|
#ifdef DBG
|
|
|
|
#undef DBG
|
|
|
|
#endif
|
|
|
|
#define DBG(...) EINA_LOG_DOM_DBG(_eina_file_log_dom, __VA_ARGS__)
|
|
|
|
|
2011-05-13 03:44:29 -07:00
|
|
|
#define EINA_SMALL_PAGE 4096
|
|
|
|
# define EINA_HUGE_PAGE 16 * 1024 * 1024
|
|
|
|
|
2010-07-02 10:23:05 -07:00
|
|
|
typedef struct _Eina_File_Iterator Eina_File_Iterator;
|
2011-04-13 09:15:30 -07:00
|
|
|
typedef struct _Eina_File_Map Eina_File_Map;
|
|
|
|
|
2010-07-02 10:23:05 -07:00
|
|
|
struct _Eina_File_Iterator
|
|
|
|
{
|
|
|
|
Eina_Iterator iterator;
|
|
|
|
|
|
|
|
DIR *dirp;
|
|
|
|
int length;
|
|
|
|
|
|
|
|
char dir[1];
|
|
|
|
};
|
|
|
|
|
2011-04-13 09:15:30 -07:00
|
|
|
struct _Eina_File
|
|
|
|
{
|
|
|
|
const char *filename;
|
|
|
|
|
|
|
|
Eina_Hash *map;
|
|
|
|
Eina_Hash *rmap;
|
|
|
|
void *global_map;
|
|
|
|
|
2011-06-06 10:08:06 -07:00
|
|
|
Eina_Lock lock;
|
|
|
|
|
2011-05-13 03:44:29 -07:00
|
|
|
unsigned long long length;
|
2011-04-13 09:15:30 -07:00
|
|
|
time_t mtime;
|
2011-05-17 09:17:28 -07:00
|
|
|
ino_t inode;
|
2011-10-03 19:04:40 -07:00
|
|
|
#ifdef _STAT_VER_LINUX
|
|
|
|
unsigned long int mtime_nsec;
|
|
|
|
#endif
|
2011-04-13 09:15:30 -07:00
|
|
|
|
|
|
|
int refcount;
|
|
|
|
int global_refcount;
|
|
|
|
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
Eina_Bool shared : 1;
|
|
|
|
Eina_Bool delete_me : 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _Eina_File_Map
|
|
|
|
{
|
|
|
|
void *map;
|
|
|
|
|
|
|
|
unsigned long int offset;
|
|
|
|
unsigned long int length;
|
|
|
|
|
|
|
|
int refcount;
|
|
|
|
};
|
|
|
|
|
|
|
|
static Eina_Hash *_eina_file_cache = NULL;
|
2011-06-06 10:08:06 -07:00
|
|
|
static Eina_Lock _eina_file_lock_cache;
|
2011-04-13 09:15:30 -07:00
|
|
|
|
|
|
|
static int _eina_file_log_dom = -1;
|
|
|
|
|
2010-11-03 03:19:55 -07:00
|
|
|
/*
|
|
|
|
* This complex piece of code is needed due to possible race condition.
|
|
|
|
* The code and description of the issue can be found at :
|
|
|
|
* http://womble.decadent.org.uk/readdir_r-advisory.html
|
|
|
|
*/
|
2011-09-18 14:56:54 -07:00
|
|
|
static long
|
|
|
|
_eina_name_max(DIR *dirp)
|
2010-11-03 03:19:55 -07:00
|
|
|
{
|
|
|
|
long name_max;
|
|
|
|
|
|
|
|
#if defined(HAVE_FPATHCONF) && defined(HAVE_DIRFD) && defined(_PC_NAME_MAX)
|
|
|
|
name_max = fpathconf(dirfd(dirp), _PC_NAME_MAX);
|
|
|
|
|
|
|
|
if (name_max == -1)
|
|
|
|
{
|
|
|
|
# if defined(NAME_MAX)
|
|
|
|
name_max = (NAME_MAX > 255) ? NAME_MAX : 255;
|
|
|
|
# else
|
2010-11-03 10:32:07 -07:00
|
|
|
name_max = PATH_MAX;
|
2010-11-03 03:19:55 -07:00
|
|
|
# endif
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# if defined(NAME_MAX)
|
|
|
|
name_max = (NAME_MAX > 255) ? NAME_MAX : 255;
|
|
|
|
# else
|
|
|
|
# ifdef _PC_NAME_MAX
|
|
|
|
# warning "buffer size for readdir_r cannot be determined safely, best effort, but racy"
|
|
|
|
name_max = pathconf(dirp, _PC_NAME_MAX);
|
|
|
|
# else
|
|
|
|
# error "buffer size for readdir_r cannot be determined safely"
|
2010-11-06 00:37:52 -07:00
|
|
|
# endif
|
2010-11-03 03:19:55 -07:00
|
|
|
# endif
|
|
|
|
#endif
|
2011-09-18 14:56:54 -07:00
|
|
|
|
|
|
|
return name_max;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
_eina_dirent_buffer_size(DIR *dirp)
|
|
|
|
{
|
|
|
|
long name_max = _eina_name_max(dirp);
|
|
|
|
size_t name_end;
|
|
|
|
|
2010-11-03 03:19:55 -07:00
|
|
|
name_end = (size_t) offsetof(struct dirent, d_name) + name_max + 1;
|
|
|
|
|
|
|
|
return (name_end > sizeof (struct dirent) ? name_end : sizeof (struct dirent));
|
|
|
|
}
|
|
|
|
|
2010-07-02 10:23:05 -07:00
|
|
|
static Eina_Bool
|
|
|
|
_eina_file_ls_iterator_next(Eina_File_Iterator *it, void **data)
|
|
|
|
{
|
|
|
|
struct dirent *dp;
|
|
|
|
char *name;
|
|
|
|
size_t length;
|
|
|
|
|
2010-11-03 03:19:55 -07:00
|
|
|
dp = alloca(_eina_dirent_buffer_size(it->dirp));
|
2010-10-28 02:59:27 -07:00
|
|
|
|
2010-07-02 10:23:05 -07:00
|
|
|
do
|
|
|
|
{
|
2010-10-28 02:59:27 -07:00
|
|
|
if (readdir_r(it->dirp, dp, &dp))
|
|
|
|
return EINA_FALSE;
|
|
|
|
if (dp == NULL)
|
|
|
|
return EINA_FALSE;
|
2010-07-02 10:23:05 -07:00
|
|
|
}
|
2010-07-06 08:45:12 -07:00
|
|
|
while ((dp->d_name[0] == '.') &&
|
2010-07-27 19:37:05 -07:00
|
|
|
((dp->d_name[1] == '\0') ||
|
|
|
|
((dp->d_name[1] == '.') && (dp->d_name[2] == '\0'))));
|
2010-07-02 10:23:05 -07:00
|
|
|
|
2010-11-02 08:11:05 -07:00
|
|
|
#ifdef _DIRENT_HAVE_D_NAMLEN
|
2010-10-29 22:48:07 -07:00
|
|
|
length = dp->d_namlen;
|
|
|
|
#else
|
2010-07-02 10:23:05 -07:00
|
|
|
length = strlen(dp->d_name);
|
2010-11-02 08:11:05 -07:00
|
|
|
#endif
|
2010-07-02 10:23:05 -07:00
|
|
|
name = alloca(length + 2 + it->length);
|
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
memcpy(name, it->dir, it->length);
|
|
|
|
memcpy(name + it->length, "/", 1);
|
2010-07-02 10:23:05 -07:00
|
|
|
memcpy(name + it->length + 1, dp->d_name, length + 1);
|
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
*data = (char *)eina_stringshare_add(name);
|
2010-07-02 10:23:05 -07:00
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-10-25 05:25:24 -07:00
|
|
|
static DIR *
|
2010-07-02 10:23:05 -07:00
|
|
|
_eina_file_ls_iterator_container(Eina_File_Iterator *it)
|
|
|
|
{
|
2010-10-25 05:25:24 -07:00
|
|
|
return it->dirp;
|
2010-07-02 10:23:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_eina_file_ls_iterator_free(Eina_File_Iterator *it)
|
|
|
|
{
|
|
|
|
closedir(it->dirp);
|
|
|
|
|
|
|
|
EINA_MAGIC_SET(&it->iterator, 0);
|
|
|
|
free(it);
|
|
|
|
}
|
2008-08-08 22:47:15 -07:00
|
|
|
|
2010-07-06 08:45:12 -07:00
|
|
|
typedef struct _Eina_File_Direct_Iterator Eina_File_Direct_Iterator;
|
|
|
|
struct _Eina_File_Direct_Iterator
|
|
|
|
{
|
|
|
|
Eina_Iterator iterator;
|
|
|
|
|
|
|
|
DIR *dirp;
|
|
|
|
int length;
|
|
|
|
|
|
|
|
Eina_File_Direct_Info info;
|
|
|
|
|
|
|
|
char dir[1];
|
|
|
|
};
|
|
|
|
|
|
|
|
static Eina_Bool
|
|
|
|
_eina_file_direct_ls_iterator_next(Eina_File_Direct_Iterator *it, void **data)
|
|
|
|
{
|
|
|
|
struct dirent *dp;
|
|
|
|
size_t length;
|
2010-11-02 10:07:04 -07:00
|
|
|
|
2010-11-03 10:32:07 -07:00
|
|
|
dp = alloca(_eina_dirent_buffer_size(it->dirp));
|
2010-10-28 02:59:27 -07:00
|
|
|
|
2010-07-06 08:45:12 -07:00
|
|
|
do
|
|
|
|
{
|
2010-10-28 02:59:27 -07:00
|
|
|
if (readdir_r(it->dirp, dp, &dp))
|
|
|
|
return EINA_FALSE;
|
2010-07-27 19:37:05 -07:00
|
|
|
if (!dp)
|
|
|
|
return EINA_FALSE;
|
2010-07-06 08:45:12 -07:00
|
|
|
|
2010-11-02 08:11:05 -07:00
|
|
|
#ifdef _DIRENT_HAVE_D_NAMLEN
|
2010-10-29 22:48:07 -07:00
|
|
|
length = dp->d_namlen;
|
|
|
|
#else
|
2010-07-27 19:37:05 -07:00
|
|
|
length = strlen(dp->d_name);
|
2010-11-02 08:11:05 -07:00
|
|
|
#endif
|
2010-12-09 02:14:11 -08:00
|
|
|
if (it->info.name_start + length + 1 >= EINA_PATH_MAX)
|
2010-07-27 19:37:05 -07:00
|
|
|
continue;
|
2010-07-06 08:45:12 -07:00
|
|
|
}
|
|
|
|
while ((dp->d_name[0] == '.') &&
|
2010-07-27 19:37:05 -07:00
|
|
|
((dp->d_name[1] == '\0') ||
|
|
|
|
((dp->d_name[1] == '.') && (dp->d_name[2] == '\0'))));
|
2010-07-06 08:45:12 -07:00
|
|
|
|
|
|
|
memcpy(it->info.path + it->info.name_start, dp->d_name, length);
|
|
|
|
it->info.name_length = length;
|
|
|
|
it->info.path_length = it->info.name_start + length;
|
|
|
|
it->info.path[it->info.path_length] = '\0';
|
2010-11-02 08:11:05 -07:00
|
|
|
|
2010-10-29 22:48:07 -07:00
|
|
|
#ifdef _DIRENT_HAVE_D_TYPE
|
|
|
|
switch (dp->d_type)
|
|
|
|
{
|
|
|
|
case DT_FIFO:
|
|
|
|
it->info.type = EINA_FILE_FIFO;
|
|
|
|
break;
|
|
|
|
case DT_CHR:
|
|
|
|
it->info.type = EINA_FILE_CHR;
|
|
|
|
break;
|
|
|
|
case DT_DIR:
|
|
|
|
it->info.type = EINA_FILE_DIR;
|
|
|
|
break;
|
|
|
|
case DT_BLK:
|
|
|
|
it->info.type = EINA_FILE_BLK;
|
|
|
|
break;
|
|
|
|
case DT_REG:
|
|
|
|
it->info.type = EINA_FILE_REG;
|
|
|
|
break;
|
|
|
|
case DT_LNK:
|
|
|
|
it->info.type = EINA_FILE_LNK;
|
|
|
|
break;
|
|
|
|
case DT_SOCK:
|
|
|
|
it->info.type = EINA_FILE_SOCK;
|
|
|
|
break;
|
|
|
|
case DT_WHT:
|
|
|
|
it->info.type = EINA_FILE_WHT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
it->info.type = EINA_FILE_UNKNOWN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#else
|
2010-11-02 10:07:04 -07:00
|
|
|
it->info.type = EINA_FILE_UNKNOWN;
|
2010-10-29 22:48:07 -07:00
|
|
|
#endif
|
2010-11-02 10:07:04 -07:00
|
|
|
|
2010-07-06 08:45:12 -07:00
|
|
|
*data = &it->info;
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-10-25 05:25:24 -07:00
|
|
|
static DIR *
|
2010-07-06 08:45:12 -07:00
|
|
|
_eina_file_direct_ls_iterator_container(Eina_File_Direct_Iterator *it)
|
|
|
|
{
|
2010-10-25 05:25:24 -07:00
|
|
|
return it->dirp;
|
2010-07-06 08:45:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_eina_file_direct_ls_iterator_free(Eina_File_Direct_Iterator *it)
|
|
|
|
{
|
|
|
|
closedir(it->dirp);
|
|
|
|
|
|
|
|
EINA_MAGIC_SET(&it->iterator, 0);
|
|
|
|
free(it);
|
|
|
|
}
|
|
|
|
|
2010-11-02 10:07:04 -07:00
|
|
|
static Eina_Bool
|
|
|
|
_eina_file_stat_ls_iterator_next(Eina_File_Direct_Iterator *it, void **data)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (!_eina_file_direct_ls_iterator_next(it, data))
|
|
|
|
return EINA_FALSE;
|
|
|
|
|
|
|
|
if (it->info.type == EINA_FILE_UNKNOWN)
|
|
|
|
{
|
2010-11-02 10:47:19 -07:00
|
|
|
#ifdef HAVE_FSTATAT
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = dirfd(it->dirp);
|
|
|
|
if (fstatat(fd, it->info.path + it->info.name_start, &st, 0))
|
|
|
|
#else
|
2010-11-02 10:07:04 -07:00
|
|
|
if (stat(it->info.path, &st))
|
2010-11-02 10:47:19 -07:00
|
|
|
#endif
|
2010-11-02 10:07:04 -07:00
|
|
|
it->info.type = EINA_FILE_UNKNOWN;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (S_ISREG(st.st_mode))
|
|
|
|
it->info.type = EINA_FILE_REG;
|
|
|
|
else if (S_ISDIR(st.st_mode))
|
|
|
|
it->info.type = EINA_FILE_DIR;
|
|
|
|
else if (S_ISCHR(st.st_mode))
|
|
|
|
it->info.type = EINA_FILE_CHR;
|
|
|
|
else if (S_ISBLK(st.st_mode))
|
|
|
|
it->info.type = EINA_FILE_BLK;
|
|
|
|
else if (S_ISFIFO(st.st_mode))
|
|
|
|
it->info.type = EINA_FILE_FIFO;
|
|
|
|
else if (S_ISLNK(st.st_mode))
|
|
|
|
it->info.type = EINA_FILE_LNK;
|
|
|
|
else if (S_ISSOCK(st.st_mode))
|
|
|
|
it->info.type = EINA_FILE_SOCK;
|
|
|
|
else
|
|
|
|
it->info.type = EINA_FILE_UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
2011-04-13 09:15:30 -07:00
|
|
|
static void
|
|
|
|
_eina_file_real_close(Eina_File *file)
|
|
|
|
{
|
2011-06-22 05:47:12 -07:00
|
|
|
if (file->refcount != 0) return;
|
2011-05-17 09:17:28 -07:00
|
|
|
|
2011-04-13 09:15:30 -07:00
|
|
|
eina_hash_free(file->rmap);
|
|
|
|
eina_hash_free(file->map);
|
|
|
|
|
|
|
|
if (file->global_map != MAP_FAILED)
|
|
|
|
munmap(file->global_map, file->length);
|
|
|
|
|
|
|
|
close(file->fd);
|
|
|
|
|
|
|
|
free(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_eina_file_map_close(Eina_File_Map *map)
|
|
|
|
{
|
|
|
|
munmap(map->map, map->length);
|
|
|
|
free(map);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int
|
2011-04-13 09:29:29 -07:00
|
|
|
_eina_file_map_key_length(const void *key __UNUSED__)
|
2011-04-13 09:15:30 -07:00
|
|
|
{
|
|
|
|
return sizeof (unsigned long int) * 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-04-13 09:29:29 -07:00
|
|
|
_eina_file_map_key_cmp(const unsigned long int *key1, int key1_length __UNUSED__,
|
|
|
|
const unsigned long int *key2, int key2_length __UNUSED__)
|
2011-04-13 09:15:30 -07:00
|
|
|
{
|
|
|
|
if (key1[0] - key2[0] == 0) return key1[1] - key2[1];
|
|
|
|
return key1[0] - key2[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-04-13 09:29:29 -07:00
|
|
|
_eina_file_map_key_hash(const unsigned long int *key, int key_length __UNUSED__)
|
2011-04-13 09:15:30 -07:00
|
|
|
{
|
|
|
|
return eina_hash_int64(&key[0], sizeof (unsigned long int))
|
|
|
|
^ eina_hash_int64(&key[1], sizeof (unsigned long int));
|
|
|
|
}
|
|
|
|
|
2011-05-13 03:44:29 -07:00
|
|
|
#ifndef MAP_POPULATE
|
|
|
|
static int
|
|
|
|
_eina_file_map_populate(char *map, unsigned int size)
|
|
|
|
{
|
|
|
|
int r = 0xDEADBEEF;
|
|
|
|
int i;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
#ifdef MAP_HUGETLB
|
|
|
|
s = size > EINA_HUGE_PAGE ? EINA_HUGE_PAGE : EINA_SMALL_PAGE;
|
|
|
|
#else
|
|
|
|
s = EINA_SMALL_PAGE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (i = 0; i < size; i += s)
|
|
|
|
r ^= map[i];
|
|
|
|
|
|
|
|
r ^= map[size];
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int
|
2011-04-13 09:15:30 -07:00
|
|
|
_eina_file_map_rule_apply(Eina_File_Populate rule, void *addr, unsigned long int size)
|
|
|
|
{
|
2011-05-13 03:44:29 -07:00
|
|
|
int tmp = 42;
|
|
|
|
int flag = MADV_RANDOM;
|
2011-04-13 09:15:30 -07:00
|
|
|
|
|
|
|
switch (rule)
|
|
|
|
{
|
|
|
|
case EINA_FILE_RANDOM: flag = MADV_RANDOM; break;
|
|
|
|
case EINA_FILE_SEQUENTIAL: flag = MADV_SEQUENTIAL; break;
|
2011-05-13 03:44:29 -07:00
|
|
|
case EINA_FILE_POPULATE: flag = MADV_WILLNEED; break;
|
|
|
|
case EINA_FILE_WILLNEED: flag = MADV_WILLNEED; break;
|
2011-04-13 09:15:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
madvise(addr, size, flag);
|
2011-05-13 03:44:29 -07:00
|
|
|
|
|
|
|
#ifndef MAP_POPULATE
|
|
|
|
if (rule == EINA_FILE_POPULATE)
|
|
|
|
tmp ^= _eina_file_map_populate(addr, size);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return tmp;
|
2011-04-13 09:15:30 -07:00
|
|
|
}
|
|
|
|
|
2011-10-03 19:04:40 -07:00
|
|
|
static Eina_Bool
|
|
|
|
_eina_file_timestamp_compare(Eina_File *f, struct stat *st)
|
|
|
|
{
|
|
|
|
if (f->mtime != st->st_mtime) return EINA_FALSE;
|
|
|
|
if (f->length != (unsigned long long) st->st_size) return EINA_FALSE;
|
|
|
|
if (f->inode != st->st_ino) return EINA_FALSE;
|
|
|
|
#ifdef _STAT_VER_LINUX
|
|
|
|
# if (defined __USE_MISC && defined st_mtime)
|
|
|
|
if (f->mtime_nsec != (unsigned long int)st->st_mtim.tv_nsec)
|
|
|
|
return EINA_FALSE;
|
|
|
|
# else
|
|
|
|
if (f->mtime_nsec != (unsigned long int)st->st_mtimensec)
|
|
|
|
return EINA_FALSE;
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
2011-10-03 19:33:45 -07:00
|
|
|
static void
|
|
|
|
slprintf(char *str, size_t size, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
|
|
|
|
vsnprintf(str, size, format, ap);
|
|
|
|
str[size - 1] = 0;
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char*
|
|
|
|
_eina_file_escape(const char* path, int* length)
|
|
|
|
{
|
|
|
|
char *result = strdup(path ? path : "");
|
|
|
|
char *p = result;
|
|
|
|
char *q = result;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (length) len = *length;
|
|
|
|
else len = strlen(result);
|
|
|
|
|
|
|
|
while ((p = strchr(p, '/')))
|
|
|
|
{
|
|
|
|
// remove double `/'
|
|
|
|
if (p[1] == '/')
|
|
|
|
{
|
|
|
|
memmove(p, p + 1, --len - (p - result));
|
|
|
|
result[len] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (p[1] == '.'
|
|
|
|
&& p[2] == '.')
|
|
|
|
{
|
|
|
|
// remove `/../'
|
|
|
|
if (p[3] == '/')
|
|
|
|
{
|
|
|
|
char tmp;
|
|
|
|
|
|
|
|
len -= p + 3 - q;
|
|
|
|
memmove(q, p + 3, len - (q - result));
|
|
|
|
result[len] = '\0';
|
|
|
|
p = q;
|
|
|
|
|
|
|
|
/* Update q correctly. */
|
|
|
|
tmp = *p;
|
|
|
|
*p = '\0';
|
|
|
|
q = strrchr(result, '/');
|
|
|
|
if (!q) q = result;
|
|
|
|
*p = tmp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
// remove '/..$'
|
|
|
|
if (p[3] == '\0')
|
|
|
|
{
|
|
|
|
len -= p + 2 - q;
|
|
|
|
result[len] = '\0';
|
|
|
|
q = p;
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
q = p;
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
q = p;
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length)
|
|
|
|
*length = len;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-04-13 09:15:30 -07:00
|
|
|
Eina_Bool
|
|
|
|
eina_file_init(void)
|
|
|
|
{
|
|
|
|
_eina_file_log_dom = eina_log_domain_register("eina_file",
|
|
|
|
EINA_LOG_COLOR_DEFAULT);
|
|
|
|
if (_eina_file_log_dom < 0)
|
|
|
|
{
|
|
|
|
EINA_LOG_ERR("Could not register log domain: eina_file");
|
|
|
|
return EINA_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
_eina_file_cache = eina_hash_string_djb2_new(EINA_FREE_CB(_eina_file_real_close));
|
|
|
|
if (!_eina_file_cache)
|
|
|
|
{
|
|
|
|
ERR("Could not create cache.");
|
|
|
|
eina_log_domain_unregister(_eina_file_log_dom);
|
|
|
|
_eina_file_log_dom = -1;
|
|
|
|
return EINA_FALSE;
|
|
|
|
}
|
|
|
|
|
2011-06-06 10:08:06 -07:00
|
|
|
eina_lock_new(&_eina_file_lock_cache);
|
|
|
|
|
2011-04-13 09:15:30 -07:00
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Eina_Bool
|
|
|
|
eina_file_shutdown(void)
|
|
|
|
{
|
|
|
|
if (eina_hash_population(_eina_file_cache) > 0)
|
|
|
|
{
|
|
|
|
Eina_Iterator *it;
|
|
|
|
const char *key;
|
|
|
|
|
|
|
|
it = eina_hash_iterator_key_new(_eina_file_cache);
|
|
|
|
EINA_ITERATOR_FOREACH(it, key)
|
|
|
|
ERR("File [%s] still open !", key);
|
|
|
|
eina_iterator_free(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
eina_hash_free(_eina_file_cache);
|
|
|
|
|
2011-06-06 10:08:06 -07:00
|
|
|
eina_lock_free(&_eina_file_lock_cache);
|
|
|
|
|
2011-04-13 09:15:30 -07:00
|
|
|
eina_log_domain_unregister(_eina_file_log_dom);
|
|
|
|
_eina_file_log_dom = -1;
|
2011-04-13 14:50:23 -07:00
|
|
|
return EINA_TRUE;
|
2011-04-13 09:15:30 -07:00
|
|
|
}
|
|
|
|
|
2010-11-06 00:37:52 -07:00
|
|
|
/**
|
|
|
|
* @endcond
|
|
|
|
*/
|
|
|
|
|
2008-08-28 00:46:42 -07:00
|
|
|
/*============================================================================*
|
2010-11-06 14:00:58 -07:00
|
|
|
* Global *
|
|
|
|
*============================================================================*/
|
2008-08-28 00:46:42 -07:00
|
|
|
|
2008-07-30 05:46:55 -07:00
|
|
|
/*============================================================================*
|
2010-11-06 14:00:58 -07:00
|
|
|
* API *
|
|
|
|
*============================================================================*/
|
2008-08-08 22:47:15 -07:00
|
|
|
|
2011-10-03 19:33:45 -07:00
|
|
|
EAPI char *
|
|
|
|
eina_file_path_sanitize(const char *path)
|
|
|
|
{
|
|
|
|
char *result = NULL;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (!path) return NULL;
|
|
|
|
|
|
|
|
len = strlen(path);
|
|
|
|
|
|
|
|
if (*path != '/')
|
|
|
|
{
|
|
|
|
char cwd[PATH_MAX];
|
|
|
|
char *tmp = NULL;
|
|
|
|
|
|
|
|
tmp = getcwd(cwd, PATH_MAX);
|
|
|
|
if (!tmp) return NULL;
|
|
|
|
|
|
|
|
len += strlen(cwd) + 2;
|
|
|
|
tmp = alloca(sizeof (char) * len);
|
|
|
|
|
|
|
|
slprintf(tmp, len, "%s/%s", cwd, path);
|
|
|
|
|
|
|
|
result = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _eina_file_escape(result ? result : path, &len);
|
|
|
|
}
|
|
|
|
|
2008-08-18 01:18:16 -07:00
|
|
|
EAPI Eina_Bool
|
2010-07-27 19:37:05 -07:00
|
|
|
eina_file_dir_list(const char *dir,
|
|
|
|
Eina_Bool recursive,
|
|
|
|
Eina_File_Dir_List_Cb cb,
|
|
|
|
void *data)
|
2008-07-30 05:46:55 -07:00
|
|
|
{
|
2011-03-22 09:33:34 -07:00
|
|
|
Eina_File_Direct_Info *info;
|
|
|
|
Eina_Iterator *it;
|
2010-11-06 00:37:52 -07:00
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(cb, EINA_FALSE);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(dir, EINA_FALSE);
|
|
|
|
EINA_SAFETY_ON_TRUE_RETURN_VAL(dir[0] == '\0', EINA_FALSE);
|
2009-01-23 08:43:14 -08:00
|
|
|
|
2011-03-22 09:33:34 -07:00
|
|
|
it = eina_file_stat_ls(dir);
|
|
|
|
if (!it)
|
2010-07-27 19:37:05 -07:00
|
|
|
return EINA_FALSE;
|
2008-07-30 05:46:55 -07:00
|
|
|
|
2011-03-22 09:33:34 -07:00
|
|
|
EINA_ITERATOR_FOREACH(it, info)
|
2010-07-27 19:37:05 -07:00
|
|
|
{
|
2011-03-22 09:33:34 -07:00
|
|
|
cb(info->path + info->name_start, dir, data);
|
2008-08-18 01:18:16 -07:00
|
|
|
|
2011-03-22 09:33:34 -07:00
|
|
|
if (recursive == EINA_TRUE && info->type == EINA_FILE_DIR)
|
2010-07-27 19:37:05 -07:00
|
|
|
{
|
2011-03-22 09:33:34 -07:00
|
|
|
eina_file_dir_list(info->path, recursive, cb, data);
|
2010-07-27 19:37:05 -07:00
|
|
|
}
|
|
|
|
}
|
2008-08-18 01:18:16 -07:00
|
|
|
|
2011-03-22 09:33:34 -07:00
|
|
|
eina_iterator_free(it);
|
2008-07-30 05:46:55 -07:00
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
return EINA_TRUE;
|
2008-08-18 01:18:16 -07:00
|
|
|
}
|
2008-09-01 05:51:50 -07:00
|
|
|
|
2008-09-03 07:40:12 -07:00
|
|
|
EAPI Eina_Array *
|
|
|
|
eina_file_split(char *path)
|
2008-09-01 05:51:50 -07:00
|
|
|
{
|
2010-07-27 19:37:05 -07:00
|
|
|
Eina_Array *ea;
|
|
|
|
char *current;
|
|
|
|
size_t length;
|
2008-09-03 07:40:12 -07:00
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(path, NULL);
|
2008-09-03 07:40:12 -07:00
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
ea = eina_array_new(16);
|
2008-09-03 07:40:12 -07:00
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
if (!ea)
|
|
|
|
return NULL;
|
2008-09-01 05:51:50 -07:00
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
for (current = strchr(path, PATH_DELIM);
|
2010-08-21 06:52:25 -07:00
|
|
|
current;
|
2010-07-27 19:37:05 -07:00
|
|
|
path = current + 1, current = strchr(path, PATH_DELIM))
|
|
|
|
{
|
|
|
|
length = current - path;
|
2008-09-01 05:51:50 -07:00
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
if (length <= 0)
|
|
|
|
continue;
|
2008-09-03 07:40:12 -07:00
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
eina_array_push(ea, path);
|
|
|
|
*current = '\0';
|
|
|
|
}
|
2008-09-01 05:51:50 -07:00
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
if (*path != '\0')
|
|
|
|
eina_array_push(ea, path);
|
2008-09-03 07:40:12 -07:00
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
return ea;
|
2008-09-03 07:40:12 -07:00
|
|
|
}
|
2008-09-18 22:22:43 -07:00
|
|
|
|
2010-07-02 10:23:05 -07:00
|
|
|
EAPI Eina_Iterator *
|
|
|
|
eina_file_ls(const char *dir)
|
|
|
|
{
|
|
|
|
Eina_File_Iterator *it;
|
|
|
|
size_t length;
|
|
|
|
|
2011-09-15 06:57:27 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(dir, NULL);
|
2010-07-02 10:23:05 -07:00
|
|
|
|
|
|
|
length = strlen(dir);
|
2010-07-27 19:37:05 -07:00
|
|
|
if (length < 1)
|
|
|
|
return NULL;
|
2010-07-02 10:23:05 -07:00
|
|
|
|
2010-08-19 05:02:28 -07:00
|
|
|
it = calloc(1, sizeof (Eina_File_Iterator) + length);
|
2010-07-27 19:37:05 -07:00
|
|
|
if (!it)
|
|
|
|
return NULL;
|
2010-07-02 10:23:05 -07:00
|
|
|
|
|
|
|
EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR);
|
|
|
|
|
|
|
|
it->dirp = opendir(dir);
|
|
|
|
if (!it->dirp)
|
|
|
|
{
|
2010-07-27 19:37:05 -07:00
|
|
|
free(it);
|
|
|
|
return NULL;
|
2010-07-02 10:23:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(it->dir, dir, length + 1);
|
2010-07-06 08:45:12 -07:00
|
|
|
if (dir[length - 1] != '/')
|
2010-07-27 19:37:05 -07:00
|
|
|
it->length = length;
|
2010-07-06 08:45:12 -07:00
|
|
|
else
|
2010-07-27 19:37:05 -07:00
|
|
|
it->length = length - 1;
|
2010-07-02 10:23:05 -07:00
|
|
|
|
2010-08-19 05:02:28 -07:00
|
|
|
it->iterator.version = EINA_ITERATOR_VERSION;
|
2010-07-02 10:23:05 -07:00
|
|
|
it->iterator.next = FUNC_ITERATOR_NEXT(_eina_file_ls_iterator_next);
|
2010-07-27 19:37:05 -07:00
|
|
|
it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER(
|
|
|
|
_eina_file_ls_iterator_container);
|
2010-07-02 10:23:05 -07:00
|
|
|
it->iterator.free = FUNC_ITERATOR_FREE(_eina_file_ls_iterator_free);
|
|
|
|
|
|
|
|
return &it->iterator;
|
|
|
|
}
|
|
|
|
|
2010-07-06 08:45:12 -07:00
|
|
|
EAPI Eina_Iterator *
|
|
|
|
eina_file_direct_ls(const char *dir)
|
|
|
|
{
|
|
|
|
Eina_File_Direct_Iterator *it;
|
|
|
|
size_t length;
|
|
|
|
|
2011-09-15 06:57:27 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(dir, NULL);
|
2010-07-06 08:45:12 -07:00
|
|
|
|
|
|
|
length = strlen(dir);
|
2010-07-27 19:37:05 -07:00
|
|
|
if (length < 1)
|
|
|
|
return NULL;
|
|
|
|
|
2010-08-19 05:02:28 -07:00
|
|
|
it = calloc(1, sizeof(Eina_File_Direct_Iterator) + length);
|
2010-07-27 19:37:05 -07:00
|
|
|
if (!it)
|
|
|
|
return NULL;
|
2010-07-06 08:45:12 -07:00
|
|
|
|
|
|
|
EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR);
|
|
|
|
|
|
|
|
it->dirp = opendir(dir);
|
|
|
|
if (!it->dirp)
|
|
|
|
{
|
2010-07-27 19:37:05 -07:00
|
|
|
free(it);
|
|
|
|
return NULL;
|
2010-07-06 08:45:12 -07:00
|
|
|
}
|
|
|
|
|
2011-09-18 14:56:54 -07:00
|
|
|
if (length + _eina_name_max(it->dirp) + 2 >= EINA_PATH_MAX)
|
|
|
|
{
|
|
|
|
_eina_file_direct_ls_iterator_free(it);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
memcpy(it->dir, dir, length + 1);
|
2010-07-06 08:45:12 -07:00
|
|
|
it->length = length;
|
|
|
|
|
|
|
|
memcpy(it->info.path, dir, length);
|
|
|
|
if (dir[length - 1] == '/')
|
2010-07-27 19:37:05 -07:00
|
|
|
it->info.name_start = length;
|
2010-07-06 08:45:12 -07:00
|
|
|
else
|
|
|
|
{
|
2010-07-27 19:37:05 -07:00
|
|
|
it->info.path[length] = '/';
|
|
|
|
it->info.name_start = length + 1;
|
2010-07-06 08:45:12 -07:00
|
|
|
}
|
|
|
|
|
2010-08-19 05:02:28 -07:00
|
|
|
it->iterator.version = EINA_ITERATOR_VERSION;
|
2010-07-06 08:45:12 -07:00
|
|
|
it->iterator.next = FUNC_ITERATOR_NEXT(_eina_file_direct_ls_iterator_next);
|
2010-07-27 19:37:05 -07:00
|
|
|
it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER(
|
|
|
|
_eina_file_direct_ls_iterator_container);
|
2010-07-06 08:45:12 -07:00
|
|
|
it->iterator.free = FUNC_ITERATOR_FREE(_eina_file_direct_ls_iterator_free);
|
|
|
|
|
|
|
|
return &it->iterator;
|
|
|
|
}
|
|
|
|
|
2010-11-02 10:07:04 -07:00
|
|
|
EAPI Eina_Iterator *
|
|
|
|
eina_file_stat_ls(const char *dir)
|
|
|
|
{
|
|
|
|
Eina_File_Direct_Iterator *it;
|
|
|
|
size_t length;
|
|
|
|
|
2011-09-15 06:57:27 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(dir, NULL);
|
2010-11-02 10:07:04 -07:00
|
|
|
|
|
|
|
length = strlen(dir);
|
|
|
|
if (length < 1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
it = calloc(1, sizeof(Eina_File_Direct_Iterator) + length);
|
|
|
|
if (!it)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR);
|
|
|
|
|
|
|
|
it->dirp = opendir(dir);
|
|
|
|
if (!it->dirp)
|
|
|
|
{
|
|
|
|
free(it);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-09-18 14:56:54 -07:00
|
|
|
if (length + _eina_name_max(it->dirp) + 2 >= EINA_PATH_MAX)
|
|
|
|
{
|
|
|
|
_eina_file_direct_ls_iterator_free(it);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-11-02 10:07:04 -07:00
|
|
|
memcpy(it->dir, dir, length + 1);
|
|
|
|
it->length = length;
|
|
|
|
|
|
|
|
memcpy(it->info.path, dir, length);
|
|
|
|
if (dir[length - 1] == '/')
|
|
|
|
it->info.name_start = length;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
it->info.path[length] = '/';
|
|
|
|
it->info.name_start = length + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
it->iterator.version = EINA_ITERATOR_VERSION;
|
|
|
|
it->iterator.next = FUNC_ITERATOR_NEXT(_eina_file_stat_ls_iterator_next);
|
|
|
|
it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER(
|
|
|
|
_eina_file_direct_ls_iterator_container);
|
|
|
|
it->iterator.free = FUNC_ITERATOR_FREE(_eina_file_direct_ls_iterator_free);
|
|
|
|
|
|
|
|
return &it->iterator;
|
|
|
|
}
|
2011-04-13 09:15:30 -07:00
|
|
|
|
|
|
|
EAPI Eina_File *
|
2011-10-03 19:33:45 -07:00
|
|
|
eina_file_open(const char *path, Eina_Bool shared)
|
2011-04-13 09:15:30 -07:00
|
|
|
{
|
|
|
|
Eina_File *file;
|
|
|
|
Eina_File *n;
|
2011-10-03 19:33:45 -07:00
|
|
|
char *filename;
|
2011-04-13 09:15:30 -07:00
|
|
|
struct stat file_stat;
|
2011-10-03 19:33:45 -07:00
|
|
|
int fd = -1;
|
2011-04-13 22:52:24 -07:00
|
|
|
int flags;
|
2011-04-13 09:15:30 -07:00
|
|
|
|
2011-10-03 19:33:45 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(path, NULL);
|
2011-09-15 06:57:27 -07:00
|
|
|
|
2011-10-03 19:33:45 -07:00
|
|
|
filename = eina_file_path_sanitize(path);
|
|
|
|
if (!filename) return NULL;
|
2011-04-13 09:15:30 -07:00
|
|
|
|
|
|
|
if (shared)
|
2011-07-13 01:51:19 -07:00
|
|
|
#ifdef HAVE_SHMOPEN
|
2011-04-13 22:52:24 -07:00
|
|
|
fd = shm_open(filename, O_RDONLY, S_IRWXU | S_IRWXG | S_IRWXO);
|
2011-07-13 01:51:19 -07:00
|
|
|
#else
|
2011-10-03 19:33:45 -07:00
|
|
|
goto on_error;
|
2011-07-13 01:51:19 -07:00
|
|
|
#endif
|
2011-04-13 09:15:30 -07:00
|
|
|
else
|
2011-04-13 22:52:24 -07:00
|
|
|
fd = open(filename, O_RDONLY, S_IRWXU | S_IRWXG | S_IRWXO);
|
2011-04-13 09:15:30 -07:00
|
|
|
|
2011-10-03 19:33:45 -07:00
|
|
|
if (fd < 0) goto on_error;
|
2011-04-13 09:15:30 -07:00
|
|
|
|
2011-09-18 14:57:10 -07:00
|
|
|
#ifdef HAVE_EXECVP
|
2011-04-13 22:52:24 -07:00
|
|
|
flags = fcntl(fd, F_GETFD);
|
|
|
|
if (flags == -1)
|
|
|
|
goto on_error;
|
|
|
|
|
|
|
|
flags |= FD_CLOEXEC;
|
|
|
|
if (fcntl(fd, F_SETFD, flags) == -1)
|
|
|
|
goto on_error;
|
2011-09-18 14:57:10 -07:00
|
|
|
#endif
|
2011-04-13 22:52:24 -07:00
|
|
|
|
2011-04-13 09:15:30 -07:00
|
|
|
if (fstat(fd, &file_stat))
|
2011-04-13 22:52:24 -07:00
|
|
|
goto on_error;
|
2011-04-13 09:15:30 -07:00
|
|
|
|
2011-06-06 10:08:06 -07:00
|
|
|
eina_lock_take(&_eina_file_lock_cache);
|
|
|
|
|
2011-04-13 09:15:30 -07:00
|
|
|
file = eina_hash_find(_eina_file_cache, filename);
|
2011-10-03 19:04:40 -07:00
|
|
|
if ((file) && _eina_file_timestamp_compare(file, &file_stat))
|
2011-04-13 09:15:30 -07:00
|
|
|
{
|
2011-06-22 05:47:12 -07:00
|
|
|
file->delete_me = EINA_TRUE;
|
|
|
|
eina_hash_del(_eina_file_cache, file->filename, file);
|
|
|
|
file = NULL;
|
2011-04-13 09:15:30 -07:00
|
|
|
}
|
|
|
|
|
2011-06-22 05:47:12 -07:00
|
|
|
if (!file)
|
2011-04-13 09:15:30 -07:00
|
|
|
{
|
2011-05-17 09:17:28 -07:00
|
|
|
n = malloc(sizeof (Eina_File) + strlen(filename) + 1);
|
2011-10-03 18:55:41 -07:00
|
|
|
if (!n)
|
|
|
|
{
|
|
|
|
eina_lock_release(&_eina_file_lock_cache);
|
|
|
|
goto on_error;
|
|
|
|
}
|
2011-04-13 09:15:30 -07:00
|
|
|
|
2011-05-17 09:17:28 -07:00
|
|
|
n->filename = (char*) (n + 1);
|
|
|
|
strcpy((char*) n->filename, filename);
|
2011-04-13 09:15:30 -07:00
|
|
|
n->map = eina_hash_new(EINA_KEY_LENGTH(_eina_file_map_key_length),
|
|
|
|
EINA_KEY_CMP(_eina_file_map_key_cmp),
|
|
|
|
EINA_KEY_HASH(_eina_file_map_key_hash),
|
|
|
|
EINA_FREE_CB(_eina_file_map_close),
|
|
|
|
3);
|
|
|
|
n->rmap = eina_hash_pointer_new(NULL);
|
|
|
|
n->global_map = MAP_FAILED;
|
2011-05-17 09:17:28 -07:00
|
|
|
n->global_refcount = 0;
|
2011-04-13 09:15:30 -07:00
|
|
|
n->length = file_stat.st_size;
|
|
|
|
n->mtime = file_stat.st_mtime;
|
2011-05-17 09:17:28 -07:00
|
|
|
n->inode = file_stat.st_ino;
|
2011-04-13 09:15:30 -07:00
|
|
|
n->refcount = 0;
|
|
|
|
n->fd = fd;
|
|
|
|
n->shared = shared;
|
|
|
|
n->delete_me = EINA_FALSE;
|
2011-06-06 10:08:06 -07:00
|
|
|
eina_lock_new(&n->lock);
|
2011-05-17 09:17:28 -07:00
|
|
|
eina_hash_direct_add(_eina_file_cache, n->filename, n);
|
2011-04-13 09:15:30 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
n = file;
|
|
|
|
}
|
2011-06-06 10:08:06 -07:00
|
|
|
eina_lock_take(&n->lock);
|
2011-04-13 09:15:30 -07:00
|
|
|
n->refcount++;
|
2011-06-06 10:08:06 -07:00
|
|
|
eina_lock_release(&n->lock);
|
|
|
|
|
|
|
|
eina_lock_release(&_eina_file_lock_cache);
|
2011-04-13 09:15:30 -07:00
|
|
|
|
2011-10-03 19:33:45 -07:00
|
|
|
free(filename);
|
|
|
|
|
2011-04-13 09:15:30 -07:00
|
|
|
return n;
|
|
|
|
|
|
|
|
on_error:
|
2011-10-03 19:33:45 -07:00
|
|
|
free(filename);
|
|
|
|
if (fd >= 0) close(fd);
|
2011-04-13 09:15:30 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI void
|
|
|
|
eina_file_close(Eina_File *file)
|
|
|
|
{
|
2011-09-15 06:57:27 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN(file);
|
2011-10-03 18:55:41 -07:00
|
|
|
|
2011-06-06 10:08:06 -07:00
|
|
|
eina_lock_take(&file->lock);
|
2011-04-13 09:15:30 -07:00
|
|
|
file->refcount--;
|
2011-06-06 10:08:06 -07:00
|
|
|
eina_lock_release(&file->lock);
|
2011-04-13 09:15:30 -07:00
|
|
|
|
2011-06-22 05:47:12 -07:00
|
|
|
if (file->refcount != 0) return;
|
2011-10-03 18:55:41 -07:00
|
|
|
eina_lock_take(&_eina_file_lock_cache);
|
|
|
|
|
2011-06-22 05:47:12 -07:00
|
|
|
eina_hash_del(_eina_file_cache, file->filename, file);
|
2011-10-03 18:55:41 -07:00
|
|
|
|
|
|
|
eina_lock_release(&_eina_file_lock_cache);
|
2011-04-13 09:15:30 -07:00
|
|
|
}
|
|
|
|
|
2011-05-31 23:54:49 -07:00
|
|
|
EAPI size_t
|
2011-04-13 09:15:30 -07:00
|
|
|
eina_file_size_get(Eina_File *file)
|
|
|
|
{
|
2011-09-15 06:57:27 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(file, 0);
|
2011-04-13 09:15:30 -07:00
|
|
|
return file->length;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI time_t
|
|
|
|
eina_file_mtime_get(Eina_File *file)
|
|
|
|
{
|
2011-09-15 06:57:27 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(file, 0);
|
2011-04-13 09:15:30 -07:00
|
|
|
return file->mtime;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI const char *
|
|
|
|
eina_file_filename_get(Eina_File *file)
|
|
|
|
{
|
2011-09-15 06:57:27 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(file, NULL);
|
2011-04-13 09:15:30 -07:00
|
|
|
return file->filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI void *
|
|
|
|
eina_file_map_all(Eina_File *file, Eina_File_Populate rule)
|
|
|
|
{
|
|
|
|
int flags = MAP_SHARED;
|
2011-06-06 10:08:06 -07:00
|
|
|
void *ret = NULL;
|
2011-04-13 09:15:30 -07:00
|
|
|
|
2011-09-15 06:57:27 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(file, NULL);
|
|
|
|
|
2011-04-21 04:54:16 -07:00
|
|
|
// bsd people will lack this feature
|
2011-05-13 03:44:29 -07:00
|
|
|
#ifdef MAP_POPULATE
|
2011-04-13 09:15:30 -07:00
|
|
|
if (rule == EINA_FILE_POPULATE) flags |= MAP_POPULATE;
|
2011-04-21 04:54:16 -07:00
|
|
|
#endif
|
2011-05-13 03:44:29 -07:00
|
|
|
#ifdef MAP_HUGETLB
|
|
|
|
if (file->length > EINA_HUGE_PAGE) flags |= MAP_HUGETLB;
|
|
|
|
#endif
|
|
|
|
|
2011-07-04 02:29:59 -07:00
|
|
|
eina_mmap_safety_enabled_set(EINA_TRUE);
|
2011-06-06 10:08:06 -07:00
|
|
|
eina_lock_take(&file->lock);
|
2011-04-13 09:15:30 -07:00
|
|
|
if (file->global_map == MAP_FAILED)
|
|
|
|
file->global_map = mmap(NULL, file->length, PROT_READ, flags, file->fd, 0);
|
|
|
|
|
|
|
|
if (file->global_map != MAP_FAILED)
|
|
|
|
{
|
|
|
|
_eina_file_map_rule_apply(rule, file->global_map, file->length);
|
|
|
|
file->global_refcount++;
|
2011-06-06 10:08:06 -07:00
|
|
|
ret = file->global_map;
|
2011-04-13 09:15:30 -07:00
|
|
|
}
|
2011-06-06 10:08:06 -07:00
|
|
|
|
|
|
|
eina_lock_release(&file->lock);
|
|
|
|
return ret;
|
2011-04-13 09:15:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
EAPI void *
|
|
|
|
eina_file_map_new(Eina_File *file, Eina_File_Populate rule,
|
|
|
|
unsigned long int offset, unsigned long int length)
|
|
|
|
{
|
|
|
|
Eina_File_Map *map;
|
|
|
|
unsigned long int key[2];
|
|
|
|
|
2011-08-10 15:42:27 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(file, NULL);
|
|
|
|
|
2011-04-13 09:15:30 -07:00
|
|
|
if (offset > file->length)
|
|
|
|
return NULL;
|
|
|
|
if (offset + length > file->length)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (offset == 0 && length == file->length)
|
|
|
|
return eina_file_map_all(file, rule);
|
|
|
|
|
|
|
|
key[0] = offset;
|
|
|
|
key[1] = length;
|
|
|
|
|
2011-07-04 02:29:59 -07:00
|
|
|
eina_mmap_safety_enabled_set(EINA_TRUE);
|
2011-06-06 10:08:06 -07:00
|
|
|
eina_lock_take(&file->lock);
|
|
|
|
|
2011-04-13 09:15:30 -07:00
|
|
|
map = eina_hash_find(file->map, &key);
|
|
|
|
if (!map)
|
|
|
|
{
|
|
|
|
int flags = MAP_SHARED;
|
|
|
|
|
2011-04-21 04:54:16 -07:00
|
|
|
// bsd people will lack this feature
|
2011-05-13 03:44:29 -07:00
|
|
|
#ifdef MAP_POPULATE
|
2011-04-13 09:15:30 -07:00
|
|
|
if (rule == EINA_FILE_POPULATE) flags |= MAP_POPULATE;
|
2011-04-21 04:54:16 -07:00
|
|
|
#endif
|
2011-05-13 03:44:29 -07:00
|
|
|
#ifdef MAP_HUGETLB
|
|
|
|
if (length > EINA_HUGE_PAGE) flags |= MAP_HUGETLB;
|
|
|
|
#endif
|
|
|
|
|
2011-04-13 09:15:30 -07:00
|
|
|
map = malloc(sizeof (Eina_File_Map));
|
2011-06-06 10:08:06 -07:00
|
|
|
if (!map) goto on_error;
|
2011-04-13 09:15:30 -07:00
|
|
|
|
|
|
|
map->map = mmap(NULL, length, PROT_READ, flags, file->fd, offset);
|
|
|
|
map->offset = offset;
|
|
|
|
map->length = length;
|
|
|
|
map->refcount = 0;
|
|
|
|
|
2011-06-06 10:08:06 -07:00
|
|
|
if (map->map == MAP_FAILED) goto on_error;
|
2011-04-13 09:15:30 -07:00
|
|
|
|
|
|
|
eina_hash_add(file->map, &key, map);
|
|
|
|
eina_hash_direct_add(file->rmap, map->map, map);
|
|
|
|
}
|
|
|
|
|
|
|
|
map->refcount++;
|
|
|
|
|
|
|
|
_eina_file_map_rule_apply(rule, map->map, length);
|
|
|
|
|
2011-06-06 10:08:06 -07:00
|
|
|
eina_lock_release(&file->lock);
|
|
|
|
|
2011-04-13 09:15:30 -07:00
|
|
|
return map->map;
|
2011-06-06 10:08:06 -07:00
|
|
|
|
|
|
|
on_error:
|
|
|
|
free(map);
|
|
|
|
eina_lock_release(&file->lock);
|
|
|
|
|
|
|
|
return NULL;
|
2011-04-13 09:15:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
EAPI void
|
|
|
|
eina_file_map_free(Eina_File *file, void *map)
|
|
|
|
{
|
2011-09-15 06:57:27 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN(file);
|
|
|
|
|
2011-06-06 10:08:06 -07:00
|
|
|
eina_lock_take(&file->lock);
|
|
|
|
|
2011-04-13 09:15:30 -07:00
|
|
|
if (file->global_map == map)
|
|
|
|
{
|
|
|
|
file->global_refcount--;
|
|
|
|
|
2011-06-06 10:08:06 -07:00
|
|
|
if (file->global_refcount > 0) goto on_exit;
|
2011-04-13 09:15:30 -07:00
|
|
|
|
|
|
|
munmap(file->global_map, file->length);
|
|
|
|
file->global_map = MAP_FAILED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Eina_File_Map *em;
|
|
|
|
unsigned long int key[2];
|
|
|
|
|
|
|
|
em = eina_hash_find(file->rmap, &map);
|
|
|
|
if (!em) return ;
|
|
|
|
|
|
|
|
em->refcount--;
|
|
|
|
|
2011-06-06 10:08:06 -07:00
|
|
|
if (em->refcount > 0) goto on_exit;
|
2011-04-13 09:15:30 -07:00
|
|
|
|
|
|
|
key[0] = em->offset;
|
|
|
|
key[1] = em->length;
|
|
|
|
|
|
|
|
eina_hash_del(file->rmap, &map, em);
|
|
|
|
eina_hash_del(file->map, &key, em);
|
|
|
|
}
|
2011-06-06 10:08:06 -07:00
|
|
|
|
|
|
|
on_exit:
|
|
|
|
eina_lock_release(&file->lock);
|
2011-04-13 09:15:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|