added in-place editing to sed command.

relocated files in lib/fs to lib/cmn
This commit is contained in:
2011-11-20 16:24:11 +00:00
parent 1f9de59543
commit 5bd21d36b2
28 changed files with 1463 additions and 1566 deletions

View File

@ -1,2 +1,2 @@
SUBDIRS = cmn awk cut sed fs net stx
SUBDIRS = cmn awk cut sed net stx
DIST_SUBDIRS = $(SUBDIRS)

View File

@ -230,7 +230,7 @@ target_alias = @target_alias@
top_build_prefix = @top_build_prefix@
top_builddir = @top_builddir@
top_srcdir = @top_srcdir@
SUBDIRS = cmn awk cut sed fs net stx
SUBDIRS = cmn awk cut sed net stx
DIST_SUBDIRS = $(SUBDIRS)
all: all-recursive

View File

@ -9,6 +9,7 @@ AM_CPPFLAGS = \
lib_LTLIBRARIES = libqsecmn.la
noinst_HEADERS = \
fs.h \
mem.h \
syscall.h \
tre.h \
@ -33,6 +34,9 @@ libqsecmn_la_SOURCES = \
fio.c \
fma.c \
fmt.c \
fs.c \
fs-err.c \
fs-move.c \
main.c \
mem.c \
oht.c \

View File

@ -77,16 +77,16 @@ LTLIBRARIES = $(lib_LTLIBRARIES)
libqsecmn_la_DEPENDENCIES =
am_libqsecmn_la_OBJECTS = alg-rand.lo alg-search.lo alg-sort.lo \
assert.lo chr.lo chr-cnv.lo dll.lo env.lo gdl.lo htb.lo lda.lo \
fio.lo fma.lo fmt.lo main.lo mem.lo oht.lo opt.lo \
path-basename.lo path-canon.lo pio.lo pma.lo rbt.lo rex.lo \
sio.lo sll.lo stdio.lo str-beg.lo str-cat.lo str-chr.lo \
str-cnv.lo str-cmp.lo str-cpy.lo str-del.lo str-dup.lo \
str-dynm.lo str-dynw.lo str-end.lo str-excl.lo str-fcpy.lo \
str-fnmat.lo str-incl.lo str-len.lo str-pac.lo str-pbrk.lo \
str-put.lo str-rev.lo str-rot.lo str-set.lo str-spl.lo \
str-spn.lo str-str.lo str-subst.lo str-tok.lo str-trm.lo \
str-word.lo time.lo tio.lo tio-get.lo tio-put.lo tre.lo \
tre-ast.lo tre-compile.lo tre-match-backtrack.lo \
fio.lo fma.lo fmt.lo fs.lo fs-err.lo fs-move.lo main.lo mem.lo \
oht.lo opt.lo path-basename.lo path-canon.lo pio.lo pma.lo \
rbt.lo rex.lo sio.lo sll.lo stdio.lo str-beg.lo str-cat.lo \
str-chr.lo str-cnv.lo str-cmp.lo str-cpy.lo str-del.lo \
str-dup.lo str-dynm.lo str-dynw.lo str-end.lo str-excl.lo \
str-fcpy.lo str-fnmat.lo str-incl.lo str-len.lo str-pac.lo \
str-pbrk.lo str-put.lo str-rev.lo str-rot.lo str-set.lo \
str-spl.lo str-spn.lo str-str.lo str-subst.lo str-tok.lo \
str-trm.lo str-word.lo time.lo tio.lo tio-get.lo tio-put.lo \
tre.lo tre-ast.lo tre-compile.lo tre-match-backtrack.lo \
tre-match-parallel.lo tre-parse.lo tre-stack.lo utf8.lo xma.lo
libqsecmn_la_OBJECTS = $(am_libqsecmn_la_OBJECTS)
libqsecmn_la_LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) \
@ -278,6 +278,7 @@ AM_CPPFLAGS = \
lib_LTLIBRARIES = libqsecmn.la $(am__append_1)
noinst_HEADERS = \
fs.h \
mem.h \
syscall.h \
tre.h \
@ -302,6 +303,9 @@ libqsecmn_la_SOURCES = \
fio.c \
fma.c \
fmt.c \
fs.c \
fs-err.c \
fs-move.c \
main.c \
mem.c \
oht.c \
@ -453,6 +457,9 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fio.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fma.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fmt.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fs-err.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fs-move.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fs.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gdl.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/htb.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/lda.Plo@am__quote@

99
qse/lib/cmn/fs-err.c Normal file
View File

@ -0,0 +1,99 @@
/*
* $Id$
*
Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE.
QSE 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 3 of
the License, or (at your option) any later version.
QSE 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 QSE. If not, see <http://www.gnu.org/licenses/>.
*/
#include "fs.h"
qse_fs_errnum_t qse_fs_geterrnum (qse_fs_t* fs)
{
return fs->errnum;
}
const qse_char_t* qse_fs_geterrmsg (qse_fs_t* fs)
{
static const qse_char_t* errstr[] =
{
QSE_T("no error"),
QSE_T("internal error that should never have happened"),
QSE_T("insufficient memory"),
QSE_T("invalid parameter or data"),
QSE_T("permission denied"),
QSE_T("no such entry"),
QSE_T("no working fsectory set"),
QSE_T("already exists"),
QSE_T("system error")
};
return (fs->errnum >= 0 && fs->errnum < QSE_COUNTOF(errstr))?
errstr[fs->errnum]: QSE_T("unknown error");
}
qse_fs_errnum_t qse_fs_syserrtoerrnum (qse_fs_t* fs, qse_fs_syserr_t e)
{
#if defined(_WIN32)
switch (e)
{
case ERROR_INVALID_NAME:
case ERROR_DIRECTORY:
return QSE_FS_EINVAL;
case ERROR_ACCESS_DENIED:
return QSE_FS_EACCES;
case ERROR_FILE_NOT_FOUND:
case ERROR_PATH_NOT_FOUND:
return QSE_FS_ENOENT;
case ERROR_NOT_ENOUGH_MEMORY:
case ERROR_OUTOFMEMORY:
return QSE_FS_ENOMEM;
case ERROR_ALREADY_EXISTS:
case ERROR_FILE_EXISTS:
return QSE_FS_EEXIST;
default:
return QSE_FS_ESYSTEM;
}
#else
switch (e)
{
case EINVAL:
return QSE_FS_EINVAL;
case EACCES:
case EPERM:
return QSE_FS_EACCES;
case ENOENT:
case ENOTDIR:
return QSE_FS_ENOENT;
case ENOMEM:
return QSE_FS_ENOMEM;
case EEXIST:
return QSE_FS_EEXIST;
default:
return QSE_FS_ESYSTEM;
}
#endif
}

123
qse/lib/cmn/fs-move.c Normal file
View File

@ -0,0 +1,123 @@
/*
* $Id$
*
Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE.
QSE 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 3 of
the License, or (at your option) any later version.
QSE 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 QSE. If not, see <http://www.gnu.org/licenses/>.
*/
#include "fs.h"
#include <qse/cmn/str.h>
#include "mem.h"
int qse_fs_move (qse_fs_t* fs, const qse_char_t* oldpath, const qse_char_t* newpath)
{
#if defined(_WIN32)
/* TODO: improve it... */
/* TODO: support cross-volume move, move by copy/delete, etc ... */
if (MoveFile (oldpath, newpath) == FALSE)
{
DWORD e = GetLastError();
if (e == ERROR_ALREADY_EXISTS)
{
DeleteFile (newpath);
if (MoveFile (oldpath, newpath) == FALSE)
{
fs->errnum = qse_fs_syserrtoerrnum (fs, GetLastError());
return -1;
}
}
else
{
fs->errnum = qse_fs_syserrtoerrnum (fs, e);
return -1;
}
}
return 0;
#elif defined(__OS2__)
# error NOT IMPLEMENTED
#elif defined(__DOS__)
# error NOT IMPLEMENTED
#else
const qse_mchar_t* mbsoldpath;
const qse_mchar_t* mbsnewpath;
#if defined(QSE_CHAR_IS_MCHAR)
mbsoldpath = oldpath;
mbsnewpath = newpath;
#else
mbsoldpath = qse_wcstombsdup (oldpath, fs->mmgr);
mbsnewpath = qse_wcstombsdup (newpath, fs->mmgr);
if (mbsoldpath == QSE_NULL || mbsnewpath == QSE_NULL)
{
fs->errnum = QSE_FS_ENOMEM;
goto oops;
}
/* TOOD: implement confirmatio
if (overwrite_callback is set)
{
checkif the the mbsnewpat exists.
if (it exists)
{
call fs->confirm_overwrite_callback (....)
}
}
*/
/* TODO: make it better to be able to move non-empty diretories
improve it to be able to move by copy/delete across volume */
if (QSE_RENAME (mbsoldpath, mbsnewpath) == -1)
{
if (errno == EXDEV)
{
/* TODO: move it by copy and delete intead of returnign error... */
fs->errnum = qse_fs_syserrtoerrnum (fs, errno);
goto oops;
}
else
{
fs->errnum = qse_fs_syserrtoerrnum (fs, errno);
goto oops;
}
}
#if defined(QSE_CHAR_IS_MCHAR)
/* nothing to free */
#else
QSE_MMGR_FREE (fs->mmgr, mbsoldpath);
QSE_MMGR_FREE (fs->mmgr, mbsnewpath);
#endif
return 0;
oops:
#if defined(QSE_CHAR_IS_MCHAR)
/* nothing to free */
#else
if (mbsoldpath) QSE_MMGR_FREE (fs->mmgr, mbsoldpath);
if (mbsnewpath) QSE_MMGR_FREE (fs->mmgr, mbsnewpath);
#endif
return -1;
#endif
#endif
}

695
qse/lib/cmn/fs.c Normal file
View File

@ -0,0 +1,695 @@
/*
* $Id$
*
Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE.
QSE 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 3 of
the License, or (at your option) any later version.
QSE 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 QSE. If not, see <http://www.gnu.org/licenses/>.
*/
#include "fs.h"
#include <qse/cmn/str.h>
#include <qse/cmn/path.h>
#include "mem.h"
#if defined(_WIN32)
/* nothing else */
#elif defined(__OS2__)
/* nothing else */
#elif defined(__DOS__)
/* nothing else */
#else
# include <dirent.h>
# include <errno.h>
#endif
typedef struct info_t info_t;
struct info_t
{
qse_xstr_t name;
#if defined(_WIN32)
HANDLE handle;
WIN32_FIND_DATA wfd;
int just_changed_fs;
#elif defined(__OS2__)
#elif defined(__DOS__)
#else
DIR* handle;
qse_mchar_t* mcurdir;
#endif
};
QSE_IMPLEMENT_COMMON_FUNCTIONS (fs)
qse_fs_t* qse_fs_open (qse_mmgr_t* mmgr, qse_size_t xtnsize)
{
qse_fs_t* fs;
if (mmgr == QSE_NULL)
{
mmgr = QSE_MMGR_GETDFL();
QSE_ASSERTX (mmgr != QSE_NULL,
"Set the memory manager with QSE_MMGR_SETDFL()");
if (mmgr == QSE_NULL) return QSE_NULL;
}
fs = QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(*fs) + xtnsize);
if (fs == QSE_NULL) return QSE_NULL;
if (qse_fs_init (fs, mmgr) <= -1)
{
QSE_MMGR_FREE (mmgr, fs);
return QSE_NULL;
}
return fs;
}
void qse_fs_close (qse_fs_t* fs)
{
qse_fs_fini (fs);
QSE_MMGR_FREE (fs->mmgr, fs);
}
int qse_fs_init (qse_fs_t* fs, qse_mmgr_t* mmgr)
{
QSE_MEMSET (fs, 0, QSE_SIZEOF(*fs));
fs->mmgr = mmgr;
return 0;
}
void qse_fs_fini (qse_fs_t* fs)
{
info_t* info;
info = fs->info;
if (info)
{
if (info->name.ptr)
{
QSE_ASSERT (info->name.len > 0);
QSE_MMGR_FREE (fs->mmgr, info->name.ptr);
info->name.ptr = QSE_NULL;
info->name.len = 0;
}
#if defined(_WIN32)
if (info->handle != INVALID_HANDLE_VALUE)
{
FindClose (info->handle);
info->handle = INVALID_HANDLE_VALUE;
}
#elif defined(__OS2__)
# error NOT IMPLEMENTED
#elif defined(__DOS__)
# error NOT IMPLEMENTED
#else
if (info->mcurdir && info->mcurdir != fs->curdir)
QSE_MMGR_FREE (fs->mmgr, info->mcurdir);
info->mcurdir = QSE_NULL;
if (info->handle)
{
closedir (info->handle);
info->handle = QSE_NULL;
}
#endif
QSE_MMGR_FREE (fs->mmgr, info);
fs->info = QSE_NULL;
}
if (fs->curdir)
{
QSE_MMGR_FREE (fs->mmgr, fs->curdir);
fs->curdir = QSE_NULL;
}
}
static QSE_INLINE info_t* get_info (qse_fs_t* fs)
{
info_t* info;
info = fs->info;
if (info == QSE_NULL)
{
info = QSE_MMGR_ALLOC (fs->mmgr, QSE_SIZEOF(*info));
if (info == QSE_NULL)
{
fs->errnum = QSE_FS_ENOMEM;
return QSE_NULL;
}
QSE_MEMSET (info, 0, QSE_SIZEOF(*info));
#if defined(_WIN32)
info->handle = INVALID_HANDLE_VALUE;
#endif
fs->info = info;
}
return info;
}
int qse_fs_chdir (qse_fs_t* fs, const qse_char_t* name)
{
qse_char_t* fsname;
info_t* info;
#if defined(_WIN32)
HANDLE handle;
WIN32_FIND_DATA wfd;
const qse_char_t* tmp_name[4];
qse_size_t idx;
#elif defined(__OS2__)
# error NOT IMPLEMENTED
#elif defined(__DOS__)
# error NOT IMPLEMENTED
#else
DIR* handle;
qse_mchar_t* mfsname;
const qse_char_t* tmp_name[4];
qse_size_t idx;
#endif
if (name[0] == QSE_T('\0'))
{
fs->errnum = QSE_FS_EINVAL;
return -1;
}
info = get_info (fs);
if (info == QSE_NULL) return -1;
#if defined(_WIN32)
idx = 0;
if (!qse_isabspath(name) && fs->curdir)
tmp_name[idx++] = fs->curdir;
tmp_name[idx++] = name;
if (qse_isdrivecurpath(name))
tmp_name[idx++] = QSE_T(" ");
else
tmp_name[idx++] = QSE_T("\\ ");
tmp_name[idx] = QSE_NULL;
fsname = qse_stradup (tmp_name, fs->mmgr);
if (fsname == QSE_NULL)
{
fs->errnum = QSE_FS_ENOMEM;
return -1;
}
idx = qse_canonpath (fsname, fsname, 0);
/* Put an asterisk after canonicalization to prevent side-effects.
* otherwise, .\* would be transformed to * by qse_canonpath() */
fsname[idx-1] = QSE_T('*');
/* Using FindExInfoBasic won't resolve cAlternatFileName.
* so it can get faster a little bit. The problem is that
* it is not supported on old windows. just stick to the
* simple API instead. */
#if 0
handle = FindFirstFileEx (
fsname, FindExInfoBasic,
&wfd, FindExSearchNameMatch,
NULL, 0/*FIND_FIRST_EX_CASE_SENSITIVE*/);
#endif
handle = FindFirstFile (fsname, &wfd);
if (handle == INVALID_HANDLE_VALUE)
{
fs->errnum = qse_fs_syserrtoerrnum (fs, GetLastError());
QSE_MMGR_FREE (fs->mmgr, fsname);
return -1;
}
if (info->handle != INVALID_HANDLE_VALUE)
FindClose (info->handle);
QSE_MEMSET (info, 0, QSE_SIZEOF(*info));
info->handle = handle;
info->wfd = wfd;
info->just_changed_fs = 1;
if (fs->curdir) QSE_MMGR_FREE (fs->mmgr, fs->curdir);
fsname[idx-1] = QSE_T('\0'); /* drop the asterisk */
fs->curdir = fsname;
return 0;
#elif defined(__OS2__)
# error NOT IMPLEMENTED
#elif defined(__DOS__)
# error NOT IMPLEMENTED
#else
idx = 0;
if (!qse_isabspath(name) && fs->curdir)
{
tmp_name[idx++] = fs->curdir;
tmp_name[idx++] = QSE_T("/");
}
tmp_name[idx++] = name;
tmp_name[idx] = QSE_NULL;
fsname = qse_stradup (tmp_name, fs->mmgr);
if (fsname == QSE_NULL)
{
fs->errnum = QSE_FS_ENOMEM;
return -1;
}
qse_canonpath (fsname, fsname, 0);
#if defined(QSE_CHAR_IS_MCHAR)
mfsname = fsname;
#else
mfsname = qse_wcstombsdup (fsname, fs->mmgr);
if (mfsname == QSE_NULL)
{
fs->errnum = QSE_FS_ENOMEM;
QSE_MMGR_FREE (fs->mmgr, fsname);
return -1;
}
#endif
handle = opendir (mfsname);
if (handle == QSE_NULL)
{
fs->errnum = qse_fs_syserrtoerrnum (fs, errno);
if (mfsname != fsname)
QSE_MMGR_FREE (fs->mmgr, mfsname);
QSE_MMGR_FREE (fs->mmgr, fsname);
return -1;
}
if (info->handle) closedir (info->handle);
info->handle = handle;
if (info->mcurdir && info->mcurdir != fs->curdir)
QSE_MMGR_FREE (fs->mmgr, info->mcurdir);
info->mcurdir = mfsname;
if (fs->curdir) QSE_MMGR_FREE (fs->mmgr, fs->curdir);
fs->curdir = fsname;
return 0;
#endif
}
#if defined(QSE_CHAR_IS_MCHAR) || defined(_WIN32)
static int set_entry_name (qse_fs_t* fs, const qse_char_t* name)
#else
static int set_entry_name (qse_fs_t* fs, const qse_mchar_t* name)
#endif
{
info_t* info;
qse_size_t len;
info = fs->info;
QSE_ASSERT (info != QSE_NULL);
#if defined(QSE_CHAR_IS_MCHAR) || defined(_WIN32)
len = qse_strlen (name);
#else
{
qse_size_t mlen;
/* TODO: ignore MBWCERR */
mlen = qse_mbstowcslen (name, &len);
if (name[mlen] != QSE_MT('\0'))
{
/* invalid name ??? */
return -1;
}
}
#endif
if (len > info->name.len)
{
qse_char_t* tmp;
/* TOOD: round up len to the nearlest multiples of something (32, 64, ??)*/
tmp = QSE_MMGR_REALLOC (
fs->mmgr,
info->name.ptr,
(len + 1) * QSE_SIZEOF(*tmp)
);
if (tmp == QSE_NULL)
{
fs->errnum = QSE_FS_ENOMEM;
return -1;
}
info->name.len = len;
info->name.ptr = tmp;
}
#if defined(QSE_CHAR_IS_MCHAR) || defined(_WIN32)
qse_strcpy (info->name.ptr, name);
#else
len++;
qse_mbstowcs (name, info->name.ptr, &len);
#endif
fs->ent.name.base = info->name.ptr;
fs->ent.flags |= QSE_FS_ENT_NAME;
return 0;
}
#if defined(_WIN32)
static QSE_INLINE qse_ntime_t filetime_to_ntime (const FILETIME* ft)
{
/* reverse of http://support.microsoft.com/kb/167296/en-us */
ULARGE_INTEGER li;
li.LowPart = ft->dwLowDateTime;
li.HighPart = ft->dwHighDateTime;
#if (QSE_SIZEOF_LONG_LONG>=8)
li.QuadPart -= 116444736000000000ull;
#elif (QSE_SIZEOF___INT64>=8)
li.QuadPart -= 116444736000000000ui64;
#else
# error Unsupported 64bit integer type
#endif
/*li.QuadPart /= 10000000;*/
li.QuadPart /= 10000;
return li.QuadPart;
}
#endif
qse_fs_ent_t* qse_fs_read (qse_fs_t* fs, int flags)
{
#if defined(_WIN32)
info_t* info;
info = fs->info;
if (info == QSE_NULL)
{
fs->errnum = QSE_FS_ENODIR;
return QSE_NULL;
}
if (info->just_changed_fs)
{
info->just_changed_fs = 0;
}
else
{
if (FindNextFile (info->handle, &info->wfd) == FALSE)
{
DWORD e = GetLastError();
if (e == ERROR_NO_MORE_FILES)
{
fs->errnum = QSE_FS_ENOERR;
return QSE_NULL;
}
else
{
fs->errnum = qse_fs_syserrtoerrnum (fs, e);
return QSE_NULL;
}
}
}
/* call set_entry_name before changing other fields
* in fs->ent not to pollute it in case set_entry_name fails */
QSE_MEMSET (&fs->ent, 0, QSE_SIZEOF(fs->ent));
if (set_entry_name (fs, info->wfd.cFileName) <= -1) return QSE_NULL;
if (flags & QSE_FS_ENT_TYPE)
{
if (info->wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
fs->ent.type = QSE_FS_ENT_SUBDIR;
}
else if ((info->wfd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
(info->wfd.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
{
fs->ent.type = QSE_FS_ENT_SYMLINK;
}
else
{
HANDLE h;
qse_char_t* tmp_name[4];
qse_char_t* fname;
/* TODO: use a buffer in info... instead of allocating an deallocating every time */
tmp_name[0] = fs->curdir;
tmp_name[1] = QSE_T("\\");
tmp_name[2] = info->wfd.cFileName;
tmp_name[3] = QSE_NULL;
fname = qse_stradup (tmp_name, fs->mmgr);
if (fname == QSE_NULL)
{
fs->errnum = QSE_FS_ENOMEM;
return QSE_NULL;
}
h = CreateFile (
fname,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
QSE_NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0
);
QSE_MMGR_FREE (fs->mmgr, fname);
if (h != INVALID_HANDLE_VALUE)
{
DWORD t = GetFileType (h);
switch (t)
{
case FILE_TYPE_CHAR:
fs->ent.type = QSE_FS_ENT_CHRDEV;
break;
case FILE_TYPE_DISK:
fs->ent.type = QSE_FS_ENT_BLKDEV;
break;
case FILE_TYPE_PIPE:
fs->ent.type = QSE_FS_ENT_PIPE;
break;
default:
fs->ent.type = QSE_FS_ENT_UNKNOWN;
break;
}
CloseHandle (h);
}
else
{
fs->ent.type = QSE_FS_ENT_UNKNOWN;
}
}
fs->ent.type |= QSE_FS_ENT_TYPE;
}
if (flags & QSE_FS_ENT_SIZE)
{
LARGE_INTEGER li;
li.LowPart = info->wfd.nFileSizeLow;
li.HighPart = info->wfd.nFileSizeHigh;
fs->ent.size = li.QuadPart;
fs->ent.type |= QSE_FS_ENT_SIZE;
}
if (flags & QSE_FS_ENT_TIME)
{
fs->ent.time.create = filetime_to_ntime (&info->wfd.ftCreationTime);
fs->ent.time.access = filetime_to_ntime (&info->wfd.ftLastAccessTime);
fs->ent.time.modify = filetime_to_ntime (&info->wfd.ftLastWriteTime);
fs->ent.type |= QSE_FS_ENT_TIME;
}
#elif defined(__OS2__)
# error NOT IMPLEMENTED
#elif defined(__DOS__)
# error NOT IMPLEMENTED
#else
info_t* info;
struct dirent* ent;
int x;
int stat_needed;
#if defined(QSE_LSTAT64)
struct stat64 st;
#else
struct stat st;
#endif
info = fs->info;
if (info == QSE_NULL)
{
fs->errnum = QSE_FS_ENODIR;
return QSE_NULL;
}
errno = 0;
ent = readdir (info->handle);
if (ent == QSE_NULL)
{
if (errno != 0) fs->errnum = qse_fs_syserrtoerrnum (fs, errno);
return QSE_NULL;
}
QSE_MEMSET (&fs->ent, 0, QSE_SIZEOF(fs->ent));
if (set_entry_name (fs, ent->d_name) <= -1) return QSE_NULL;
stat_needed =
#if !defined(HAVE_STRUCT_DIRENT_D_TYPE)
(flags & QSE_FS_ENT_TYPE) ||
#endif
(flags & QSE_FS_ENT_SIZE) ||
(flags & QSE_FS_ENT_TIME);
if (stat_needed)
{
qse_mchar_t* tmp_name[4];
qse_mchar_t* mfname;
/* TODO: use a buffer in info... instead of allocating an deallocating every time */
tmp_name[0] = info->mcurdir;
tmp_name[1] = QSE_MT("/");
tmp_name[2] = ent->d_name;
tmp_name[3] = QSE_NULL;
mfname = qse_mbsadup (tmp_name, fs->mmgr);
if (mfname == QSE_NULL)
{
fs->errnum = QSE_FS_ENOMEM;
return QSE_NULL;
}
#if defined(QSE_LSTAT64)
x = QSE_LSTAT64 (mfname, &st);
#else
x = QSE_LSTAT (mfname, &st);
#endif
QSE_MMGR_FREE (fs->mmgr, mfname);
if (x == -1)
{
fs->errnum = qse_fs_syserrtoerrnum (fs, errno);
return QSE_NULL;
}
}
if (flags & QSE_FS_ENT_TYPE)
{
#if defined(HAVE_STRUCT_DIRENT_D_TYPE)
switch (ent->d_type)
{
case DT_DIR:
fs->ent.type = QSE_FS_ENT_SUBDIR;
break;
case DT_REG:
fs->ent.type = QSE_FS_ENT_REGULAR;
break;
case DT_LNK:
fs->ent.type = QSE_FS_ENT_SYMLINK;
break;
case DT_BLK:
fs->ent.type = QSE_FS_ENT_BLKDEV;
break;
case DT_CHR:
fs->ent.type = QSE_FS_ENT_CHRDEV;
break;
case DT_FIFO:
#if defined(DT_SOCK)
case DT_SOCK:
#endif
fs->ent.type = QSE_FS_ENT_PIPE;
break;
default:
fs->ent.type = QSE_FS_ENT_UNKNOWN;
break;
}
#else
#define IS_TYPE(st,type) ((st.st_mode & S_IFMT) == S_IFDIR)
fs->ent.type = IS_TYPE(st,S_IFDIR)? QSE_FS_ENT_SUBDIR:
IS_TYPE(st,S_IFREG)? QSE_FS_ENT_REGULAR:
IS_TYPE(st,S_IFLNK)? QSE_FS_ENT_SYMLINK:
IS_TYPE(st,S_IFCHR)? QSE_FS_ENT_CHRDEV:
IS_TYPE(st,S_IFBLK)? QSE_FS_ENT_BLKDEV:
IS_TYPE(st,S_IFIFO)? QSE_FS_ENT_PIPE:
IS_TYPE(st,S_IFSOCK)? QSE_FS_ENT_PIPE:
QSE_FS_ENT_UNKNOWN;
#undef IS_TYPE
#endif
fs->ent.flags |= QSE_FS_ENT_TYPE;
}
if (flags & QSE_FS_ENT_SIZE)
{
fs->ent.size = st.st_size;
fs->ent.flags |= QSE_FS_ENT_SIZE;
}
if (flags & QSE_FS_ENT_TIME)
{
#if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIME)
fs->ent.time.create =
QSE_SECNSEC_TO_MSEC(st.st_birthtim.tv_sec,st.st_birthtim.tv_nsec);
#endif
fs->ent.time.access =
QSE_SECNSEC_TO_MSEC(st.st_atim.tv_sec,st.st_atim.tv_nsec);
fs->ent.time.modify =
QSE_SECNSEC_TO_MSEC(st.st_mtim.tv_sec,st.st_mtim.tv_nsec);
fs->ent.time.change =
QSE_SECNSEC_TO_MSEC(st.st_ctim.tv_sec,st.st_ctim.tv_nsec);
#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIME)
fs->ent.time.create = st.st_birthtime;
QSE_SECNSEC_TO_MSEC(st.st_birthtimespec.tv_sec,st.st_birthtimespec.tv_nsec);
#endif
fs->ent.time.access =
QSE_SECNSEC_TO_MSEC(st.st_atimespec.tv_sec,st.st_atimespec.tv_nsec);
fs->ent.time.modify =
QSE_SECNSEC_TO_MSEC(st.st_mtimespec.tv_sec,st.st_mtimespec.tv_nsec);
fs->ent.time.change =
QSE_SECNSEC_TO_MSEC(st.st_ctimespec.tv_sec,st.st_ctimespec.tv_nsec);
#else
#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIME)
fs->ent.time.create = st.st_birthtime * QSE_MSECS_PER_SEC;
#endif
fs->ent.time.access = st.st_atime * QSE_MSECS_PER_SEC;
fs->ent.time.modify = st.st_mtime * QSE_MSECS_PER_SEC;
fs->ent.time.change = st.st_ctime * QSE_MSECS_PER_SEC;
#endif
fs->ent.flags |= QSE_FS_ENT_TIME;
}
#endif
return &fs->ent;
}
int qse_fs_rewind (qse_fs_t* fs)
{
return 0;
}

47
qse/lib/cmn/fs.h Normal file
View File

@ -0,0 +1,47 @@
/*
* $Id$
*
Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE.
QSE 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 3 of
the License, or (at your option) any later version.
QSE 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 QSE. If not, see <http://www.gnu.org/licenses/>.
*/
#include <qse/cmn/fs.h>
#if defined(_WIN32)
# include <windows.h>
typedef DWORD qse_fs_syserr_t;
#elif defined(__OS2__)
# error NOT IMPLEMENTED
#elif defined(__DOS__)
# error NOT IMPLEMENTED
#else
# include "syscall.h"
# include <errno.h>
typedef int qse_fs_syserr_t;
#endif
#ifdef __cplusplus
extern "C" {
#endif
qse_fs_errnum_t qse_fs_syserrtoerrnum (
qse_fs_t* fs,
qse_fs_syserr_t e
);
#ifdef __cplusplus
}
#endif

View File

@ -58,25 +58,25 @@
# include <sys/syscall.h>
#endif
#ifdef SYS_open
#if defined(SYS_open)
# define QSE_OPEN(path,flags,mode) syscall(SYS_open,path,flags,mode)
#else
# define QSE_OPEN(path,flags,mode) open(path,flags,mode)
#endif
#ifdef SYS_close
#if defined(SYS_close)
# define QSE_CLOSE(handle) syscall(SYS_close,handle)
#else
# define QSE_CLOSE(handle) close(handle)
#endif
#ifdef SYS_read
#if defined(SYS_read)
# define QSE_READ(handle,buf,size) syscall(SYS_read,handle,buf,size)
#else
# define QSE_READ(handle,buf,size) read(handle,buf,size)
#endif
#ifdef SYS_write
#if defined(SYS_write)
# define QSE_WRITE(handle,buf,size) syscall(SYS_write,handle,buf,size)
#else
# define QSE_WRITE(handle,buf,size) write(handle,buf,size)
@ -100,18 +100,6 @@
# define QSE_LSEEK(handle,offset,whence) lseek(handle,offset,whence)
#endif
#if defined(SYS_lstat64)
# define QSE_LSTAT64(path,stbuf) syscall(SYS_lstat64,path,stbuf)
#elif defined(HAVE_lstat64)
# define QSE_LSTAT64(path,stbuf) lstat64(path,stbuf)
#endif
#if defined(SYS_lstat)
# define QSE_LSTAT(path,stbuf) syscall(SYS_lstat,path,stbuf)
#else
# define QSE_LSTAT(path,stbuf) lstat(path,stbuf)
#endif
#if !defined(_LP64) && defined(SYS_ftruncate64)
# define QSE_FTRUNCATE(handle,size) syscall(SYS_ftruncate64,handle,size)
#elif defined(SYS_ftruncate)
@ -128,6 +116,12 @@
# define QSE_FCHMOD(handle,mode) fchmod(handle,mode)
#endif
#if defined(SYS_fchown)
# define QSE_FCHOWN(handle,owner,group) syscall(SYS_fchown,handle,owner,group)
#else
# define QSE_FCHOWN(handle,owner,group) fchown(handle,owner,group)
#endif
#if defined(SYS_fsync)
# define QSE_FSYNC(handle) syscall(SYS_fsync,handle)
#else
@ -140,118 +134,193 @@
# define QSE_FCNTL(handle,cmd,arg) fcntl(handle,cmd,arg)
#endif
#ifdef SYS_dup2
#if defined(SYS_dup2)
# define QSE_DUP2(ofd,nfd) syscall(SYS_dup2,ofd,nfd)
#else
# define QSE_DUP2(ofd,nfd) dup2(ofd,nfd)
#endif
#ifdef SYS_pipe
#if defined(SYS_pipe)
# define QSE_PIPE(pfds) syscall(SYS_pipe,pfds)
#else
# define QSE_PIPE(pfds) pipe(pfds)
#endif
#ifdef SYS_exit
#if defined(SYS_exit)
# define QSE_EXIT(code) syscall(SYS_exit,code)
#else
# define QSE_EXIT(code) _exit(code)
#endif
#ifdef SYS_fork
#if defined(SYS_fork)
# define QSE_FORK() syscall(SYS_fork)
#else
# define QSE_FORK() fork()
#endif
#ifdef SYS_execve
#if defined(SYS_execve)
# define QSE_EXECVE(path,argv,envp) syscall(SYS_execve,path,argv,envp)
#else
# define QSE_EXECVE(path,argv,envp) execve(path,argv,envp)
#endif
#ifdef SYS_waitpid
#if defined(SYS_waitpid)
# define QSE_WAITPID(pid,status,options) syscall(SYS_waitpid,pid,status,options)
#else
# define QSE_WAITPID(pid,status,options) waitpid(pid,status,options)
#endif
#ifdef SYS_kill
#if defined(SYS_kill)
# define QSE_KILL(pid,sig) syscall(SYS_kill,pid,sig)
#else
# define QSE_KILL(pid,sig) kill(pid,sig)
#endif
#ifdef SYS_getpid
#if defined(SYS_getpid)
# define QSE_GETPID() syscall(SYS_getpid)
#else
# define QSE_GETPID() getpid()
#endif
#ifdef SYS_getuid
#if defined(SYS_getuid)
# define QSE_GETUID() syscall(SYS_getuid)
#else
# define QSE_GETUID() getuid()
#endif
#ifdef SYS_geteuid
#if defined(SYS_geteuid)
# define QSE_GETEUID() syscall(SYS_geteuid)
#else
# define QSE_GETEUID() geteuid()
#endif
#ifdef SYS_getgid
#if defined(SYS_getgid)
# define QSE_GETGID() syscall(SYS_getgid)
#else
# define QSE_GETGID() getgid()
#endif
#ifdef SYS_getegid
#if defined(SYS_getegid)
# define QSE_GETEGID() syscall(SYS_getegid)
#else
# define QSE_GETEGID() getegid()
#endif
#ifdef SYS_chroot
# define QSE_CHROOT(path) syscall(SYS_chroot,path)
#else
# define QSE_cHROOT(path) chroot(path)
#endif
#ifdef SYS_gettimeofday
#if defined(SYS_gettimeofday)
# define QSE_GETTIMEOFDAY(tv,tz) syscall(SYS_gettimeofday,tv,tz)
#else
# define QSE_GETTIMEOFDAY(tv,tz) gettimeofday(tv,tz)
#endif
#ifdef SYS_settimeofday
#if defined(SYS_settimeofday)
# define QSE_SETTIMEOFDAY(tv,tz) syscall(SYS_settimeofday,tv,tz)
#else
# define QSE_SETTIMEOFDAY(tv,tz) settimeofday(tv,tz)
#endif
#ifdef SYS_utime
# define QSE_UTIME(file,t) syscall(SYS_utime,file,t)
#else
# define QSE_UTIME(file,t) utime(file,t)
#endif
#ifdef SYS_utimes
# define QSE_UTIMES(file,t) syscall(SYS_utimes,file,t)
#else
# define QSE_UTIMES(file,t) utimes(file,t)
#endif
#ifdef SYS_getrlimit
#if defined(SYS_getrlimit)
# define QSE_GETRLIMIT(res,lim) syscall(SYS_getrlimit,res,lim)
#else
# define QSE_GETRLIMIT(res,lim) getrlimit(res,lim)
#endif
#ifdef SYS_setrlimit
#if defined(SYS_setrlimit)
# define QSE_SETRLIMIT(res,lim) syscall(SYS_setrlimit,res,lim)
#else
# define QSE_SETRLIMIT(res,lim) setrlimit(res,lim)
#endif
/* ===== FILE SYSTEM CALLS ===== */
#if defined(SYS_chmod)
# define QSE_CHMOD(path,mode) syscall(SYS_chmod,path,mode)
#else
# define QSE_CHMOD(path,mode) chmod(path,mode)
#endif
#if defined(SYS_chown)
# define QSE_CHOWN(path,owner,group) syscall(SYS_chown,path,owner,group)
#else
# define QSE_CHOWN(path,owner,group) chown(path,owner,group)
#endif
#if defined(SYS_chroot)
# define QSE_CHROOT(path) syscall(SYS_chroot,path)
#else
# define QSE_CHROOT(path) chroot(path)
#endif
#if defined(SYS_lchown)
# define QSE_LCHOWN(path,owner,group) syscall(SYS_lchown,path,owner,group)
#else
# define QSE_LCHOWN(path,owner,group) lchown(path,owner,group)
#endif
#if defined(SYS_link)
# define QSE_LINK(oldpath,newpath) syscall(SYS_link,oldpath,newpath)
#else
# define QSE_LINK(oldpath,newpath) link(oldpath,newpath)
#endif
#if defined(SYS_lstat64)
# define QSE_LSTAT64(path,stbuf) syscall(SYS_lstat64,path,stbuf)
#elif defined(HAVE_lstat64)
# define QSE_LSTAT64(path,stbuf) lstat64(path,stbuf)
#endif
#if defined(SYS_lstat)
# define QSE_LSTAT(path,stbuf) syscall(SYS_lstat,path,stbuf)
#else
# define QSE_LSTAT(path,stbuf) lstat(path,stbuf)
#endif
#if defined(SYS_rename)
# define QSE_RENAME(oldpath,newpath) syscall(SYS_rename,oldpath,newpath)
#else
# define QSE_RENAME(oldpath,newpath) rename(oldpath,newpath)
#endif
#if defined(SYS_rmdir)
# define QSE_RMDIR(path) syscall(SYS_rmdir,path)
#else
# define QSE_RMDIR(path) rmdir(path)
#endif
#if defined(SYS_stat64)
# define QSE_STAT64(path,stbuf) syscall(SYS_stat64,path,stbuf)
#elif defined(HAVE_stat64)
# define QSE_STAT64(path,stbuf) stat64(path,stbuf)
#endif
#if defined(SYS_stat)
# define QSE_STAT(path,stbuf) syscall(SYS_stat,path,stbuf)
#else
# define QSE_STAT(path,stbuf) stat(path,stbuf)
#endif
#if defined(SYS_symlink)
# define QSE_SYMLINK(oldpath,newpath) syscall(SYS_symlink,oldpath,newpath)
#else
# define QSE_SYMLINK(oldpath,newpath) symlink(oldpath,newpath)
#endif
#if defined(SYS_unlink)
# define QSE_UNLINK(path) syscall(SYS_unlink,path)
#else
# define QSE_UNLINK(path) unlink(path)
#endif
#if defined(SYS_utime)
# define QSE_UTIME(path,t) syscall(SYS_utime,path,t)
#else
# define QSE_UTIME(path,t) utime(path,t)
#endif
#if defined(SYS_utimes)
# define QSE_UTIMES(path,t) syscall(SYS_utimes,path,t)
#else
# define QSE_UTIMES(path,t) utimes(path,t)
#endif
#endif