2016-04-29 03:55:42 +00:00
|
|
|
#include <qse/si/fs.h>
|
2014-12-03 15:27:45 +00:00
|
|
|
#include <qse/cmn/mem.h>
|
|
|
|
#include <qse/cmn/str.h>
|
2016-04-29 03:55:42 +00:00
|
|
|
#include <qse/si/sio.h>
|
2014-12-03 15:27:45 +00:00
|
|
|
#include <qse/cmn/path.h>
|
|
|
|
#include <qse/cmn/main.h>
|
|
|
|
#include <qse/cmn/mbwc.h>
|
2014-12-09 13:41:58 +00:00
|
|
|
#include <qse/cmn/opt.h>
|
2014-12-11 16:03:58 +00:00
|
|
|
#include <qse/cmn/fmt.h>
|
2014-12-03 15:27:45 +00:00
|
|
|
#include <locale.h>
|
|
|
|
|
2014-12-11 16:03:58 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
# include <windows.h>
|
|
|
|
#endif
|
2014-12-03 15:27:45 +00:00
|
|
|
|
|
|
|
static void print_usage (const qse_char_t* argv0)
|
|
|
|
{
|
2014-12-09 13:41:58 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("Usage: %s [options] source-filename target-filename\n"), qse_basename(argv0));
|
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("Options include:\n"));
|
|
|
|
qse_fprintf (QSE_STDERR, QSE_T(" -f force\n"));
|
|
|
|
qse_fprintf (QSE_STDERR, QSE_T(" -o overwrite\n"));
|
|
|
|
qse_fprintf (QSE_STDERR, QSE_T(" -p preserve\n"));
|
|
|
|
qse_fprintf (QSE_STDERR, QSE_T(" -r recursive\n"));
|
|
|
|
qse_fprintf (QSE_STDERR, QSE_T(" -s symlink\n"));
|
2016-12-12 16:19:59 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T(" -g glob\n"));
|
2019-04-23 08:41:05 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T(" -w chown instead. wins over other options. honors -s\n"));
|
2014-12-03 15:27:45 +00:00
|
|
|
}
|
|
|
|
|
2016-12-18 17:12:21 +00:00
|
|
|
static int fs_actcb (qse_fs_t* fs, qse_fs_action_t act, const qse_char_t* srcpath, const qse_char_t* dstpath, qse_uintmax_t total, qse_uintmax_t done)
|
2016-12-12 16:19:59 +00:00
|
|
|
{
|
2016-12-18 17:12:21 +00:00
|
|
|
switch (act)
|
|
|
|
{
|
|
|
|
case QSE_FS_CPFILE:
|
|
|
|
if (total == done) qse_printf (QSE_T("Copied [%s] to [%s]\n"), srcpath, dstpath);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_FS_RMFILE:
|
|
|
|
qse_printf (QSE_T("Removed file [%s]\n"), srcpath);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_FS_SYMLINK:
|
|
|
|
qse_printf (QSE_T("Symlinked [%s] to [%s]\n"), srcpath, dstpath);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_FS_MKDIR:
|
|
|
|
qse_printf (QSE_T("Made directory [%s]\n"), srcpath);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_FS_RMDIR:
|
|
|
|
qse_printf (QSE_T("Removed directory [%s]\n"), srcpath);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_FS_RENFILE:
|
|
|
|
qse_printf (QSE_T("renamed [%s] to [%s]\n"), srcpath);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-12-12 16:19:59 +00:00
|
|
|
/*if (qse_strcmp(path, QSE_T("b/c")) == 0) return 0;*/
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-03 15:27:45 +00:00
|
|
|
static int fs_main (int argc, qse_char_t* argv[])
|
|
|
|
{
|
|
|
|
qse_fs_t* fs;
|
|
|
|
qse_fs_cbs_t cbs;
|
|
|
|
int ret = 0;
|
2014-12-09 13:41:58 +00:00
|
|
|
qse_cint_t c;
|
|
|
|
int cpfile_flags = 0;
|
2019-04-23 08:41:05 +00:00
|
|
|
int act_chown = 0;
|
2014-12-03 15:27:45 +00:00
|
|
|
|
2014-12-09 13:41:58 +00:00
|
|
|
static qse_opt_t opt =
|
2014-12-03 15:27:45 +00:00
|
|
|
{
|
2019-04-23 08:41:05 +00:00
|
|
|
QSE_T("foprsgw"),
|
2014-12-09 13:41:58 +00:00
|
|
|
QSE_NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
while ((c = qse_getopt (argc, argv, &opt)) != QSE_CHAR_EOF)
|
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case QSE_T('f'):
|
|
|
|
cpfile_flags |= QSE_FS_CPFILE_FORCE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_T('o'):
|
|
|
|
cpfile_flags |= QSE_FS_CPFILE_REPLACE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_T('p'):
|
|
|
|
cpfile_flags |= QSE_FS_CPFILE_PRESERVE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_T('r'):
|
|
|
|
cpfile_flags |= QSE_FS_CPFILE_RECURSIVE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_T('s'):
|
|
|
|
cpfile_flags |= QSE_FS_CPFILE_SYMLINK;
|
|
|
|
break;
|
|
|
|
|
2016-12-12 16:19:59 +00:00
|
|
|
case QSE_T('g'):
|
|
|
|
cpfile_flags |= QSE_FS_CPFILE_GLOB;
|
|
|
|
break;
|
|
|
|
|
2019-04-23 08:41:05 +00:00
|
|
|
case QSE_T('w'):
|
|
|
|
act_chown = 1;
|
|
|
|
break;
|
|
|
|
|
2014-12-09 13:41:58 +00:00
|
|
|
case QSE_T('?'):
|
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("illegal option - '%c'\n"), opt.opt);
|
|
|
|
goto wrong_usage;
|
|
|
|
|
|
|
|
case QSE_T(':'):
|
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("bad argument for '%c'\n"), opt.opt);
|
|
|
|
goto wrong_usage;
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto wrong_usage;
|
|
|
|
}
|
2014-12-03 15:27:45 +00:00
|
|
|
}
|
2014-12-09 13:41:58 +00:00
|
|
|
|
|
|
|
if (opt.ind + 2 != argc) goto wrong_usage;
|
|
|
|
|
2014-12-03 15:27:45 +00:00
|
|
|
fs = qse_fs_open (QSE_MMGR_GETDFL(), 0);
|
|
|
|
|
2019-04-23 08:41:05 +00:00
|
|
|
if (act_chown)
|
2014-12-03 15:27:45 +00:00
|
|
|
{
|
2019-04-23 08:41:05 +00:00
|
|
|
qse_fattr_t attr;
|
|
|
|
int attr_flags = QSE_FILE_ATTR_OWNER;
|
|
|
|
if (cpfile_flags & QSE_FS_CPFILE_SYMLINK) attr_flags |= QSE_FILE_ATTR_SYMLINK;
|
|
|
|
attr.uid = qse_strtoulong(argv[opt.ind], 10, QSE_NULL);
|
|
|
|
attr.gid = -1;
|
|
|
|
if (qse_fs_setattr(fs, argv[opt.ind + 1], &attr, attr_flags) <= -1)
|
|
|
|
{
|
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("cannot change file uid - %d\n"), qse_fs_geterrnum(fs));
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qse_memset (&cbs, 0, QSE_SIZEOF(cbs));
|
|
|
|
cbs.actcb = fs_actcb;
|
|
|
|
qse_fs_setopt (fs, QSE_FS_CBS, &cbs);
|
|
|
|
|
|
|
|
if (qse_fs_cpfile(fs, argv[opt.ind], argv[opt.ind + 1], cpfile_flags) <= -1)
|
|
|
|
{
|
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("cannot copy file - %d\n"), qse_fs_geterrnum(fs));
|
|
|
|
ret = -1;
|
|
|
|
}
|
2014-12-03 15:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
qse_fs_close (fs);
|
|
|
|
return ret;
|
2014-12-09 13:41:58 +00:00
|
|
|
|
|
|
|
wrong_usage:
|
|
|
|
print_usage (argv[0]);
|
|
|
|
return -1;
|
2014-12-03 15:27:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main (int argc, qse_achar_t* argv[])
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
#if defined(_WIN32)
|
|
|
|
char locale[100];
|
|
|
|
UINT codepage = GetConsoleOutputCP();
|
|
|
|
if (codepage == CP_UTF8)
|
|
|
|
{
|
|
|
|
/*SetConsoleOUtputCP (CP_UTF8);*/
|
|
|
|
qse_setdflcmgrbyid (QSE_CMGR_UTF8);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-11 16:03:58 +00:00
|
|
|
/* .codepage */
|
|
|
|
qse_fmtuintmaxtombs (locale, QSE_COUNTOF(locale),
|
|
|
|
codepage, 10, -1, QSE_MT('\0'), QSE_MT("."));
|
2014-12-03 15:27:45 +00:00
|
|
|
setlocale (LC_ALL, locale);
|
2015-04-27 08:37:57 +00:00
|
|
|
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
|
2014-12-03 15:27:45 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
setlocale (LC_ALL, "");
|
2015-04-27 08:37:57 +00:00
|
|
|
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
|
2014-12-03 15:27:45 +00:00
|
|
|
#endif
|
|
|
|
|
2017-09-16 08:54:25 +00:00
|
|
|
qse_open_stdsios ();
|
2014-12-03 15:27:45 +00:00
|
|
|
|
2018-09-13 03:16:23 +00:00
|
|
|
x = qse_run_main (argc, argv, fs_main);
|
2014-12-03 15:27:45 +00:00
|
|
|
|
2017-09-16 08:54:25 +00:00
|
|
|
qse_close_stdsios ();
|
2014-12-03 15:27:45 +00:00
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|