Windows: handle wchar/utf-8 conversions in some other places.

This commit is contained in:
Jean-Francois Dockes 2020-01-13 12:02:22 +01:00
parent 6efd91c057
commit c5a1208457
7 changed files with 189 additions and 107 deletions

View File

@ -336,7 +336,11 @@ static QString myGetFileName(bool isdir, QString caption, bool filenosave)
void ConfParamW::setValue(const QString& value)
{
if (m_fsencoding) {
#ifdef _WIN32
m_cflink->set(string((const char *)value.toUtf8()));
#else
m_cflink->set(string((const char *)value.toLocal8Bit()));
#endif
} else {
m_cflink->set(string((const char *)value.toUtf8()));
}
@ -462,7 +466,11 @@ void ConfParamStrW::loadValue()
string s;
m_cflink->get(s);
if (m_fsencoding) {
#ifdef _WIN32
m_le->setText(m_origvalue = QString::fromUtf8(s.c_str()));
#else
m_le->setText(m_origvalue = QString::fromLocal8Bit(s.c_str()));
#endif
} else {
m_le->setText(m_origvalue = QString::fromUtf8(s.c_str()));
}
@ -513,7 +521,11 @@ void ConfParamCStrW::loadValue()
m_cflink->get(s);
QString cs;
if (m_fsencoding) {
#ifdef _WIN32
cs = QString::fromUtf8(s.c_str());
#else
cs = QString::fromLocal8Bit(s.c_str());
#endif
} else {
cs = QString::fromUtf8(s.c_str());
}
@ -619,7 +631,11 @@ void ConfParamFNW::loadValue()
{
string s;
m_cflink->get(s);
#ifdef _WIN32
m_le->setText(m_origvalue = QString::fromUtf8(s.c_str()));
#else
m_le->setText(m_origvalue = QString::fromLocal8Bit(s.c_str()));
#endif
}
void ConfParamFNW::showBrowserDialog()
@ -765,7 +781,11 @@ void ConfParamSLW::loadValue()
QStringList qls;
for (const auto& str : ls) {
if (m_fsencoding) {
#ifdef _WIN32
qls.push_back(QString::fromUtf8(str.c_str()));
#else
qls.push_back(QString::fromLocal8Bit(str.c_str()));
#endif
} else {
qls.push_back(QString::fromUtf8(str.c_str()));
}

View File

@ -77,7 +77,7 @@ void multiSave(QWidget *p, vector<Rcl::Doc>& docs)
*/
set<string> existingNames;
string reason;
if (!readdir(dir, reason, existingNames)) {
if (!listdir(dir, reason, existingNames)) {
QMessageBox::warning(0, "Recoll",
QWidget::tr("Could not read directory: ") +
QString::fromLocal8Bit(reason.c_str()));

View File

@ -0,0 +1,55 @@
#include "wipedir.h"
#include <stdio.h>
#include <stdlib.h>
using namespace std;
static const char *thisprog;
static int op_flags;
#define OPT_MOINS 0x1
#define OPT_r 0x2
#define OPT_s 0x4
static char usage [] =
"wipedir [-r -s] topdir\n"
" -r : recurse\n"
" -s : also delete topdir\n"
;
static void
Usage(void)
{
fprintf(stderr, "%s: usage:\n%s", thisprog, usage);
exit(1);
}
int main(int argc, const char **argv)
{
thisprog = argv[0];
argc--; argv++;
while (argc > 0 && **argv == '-') {
(*argv)++;
if (!(**argv))
/* Cas du "adb - core" */
Usage();
while (**argv)
switch (*(*argv)++) {
case 'r': op_flags |= OPT_r; break;
case 's': op_flags |= OPT_s; break;
default: Usage(); break;
}
b1: argc--; argv++;
}
if (argc != 1)
Usage();
string dir = *argv++;argc--;
bool topalso = ((op_flags&OPT_s) != 0);
bool recurse = ((op_flags&OPT_r) != 0);
int cnt = wipedir(dir, topalso, recurse);
printf("wipedir returned %d\n", cnt);
exit(0);
}

View File

@ -61,6 +61,11 @@
#define LSTAT _wstati64
#define STATBUF _stati64
#define ACCESS _waccess
#define OPENDIR _wopendir
#define CLOSEDIR _wclosedir
#define READDIR _wreaddir
#define DIRENT _wdirent
#define DIRHDL _WDIR
#else // Not windows ->
#include <fcntl.h>
@ -76,6 +81,11 @@
#define LSTAT lstat
#define STATBUF stat
#define ACCESS access
#define OPENDIR opendir
#define CLOSEDIR closedir
#define READDIR readdir
#define DIRENT dirent
#define DIRHDL DIR
#endif
#include <cstdlib>
@ -996,43 +1006,55 @@ ParsedUri::ParsedUri(std::string uri)
}
}
bool readdir(const string& dir, string& reason, set<string>& entries)
bool listdir(const string& dir, string& reason, set<string>& entries)
{
struct stat st;
struct STATBUF st;
int statret;
ostringstream msg;
DIR *d = 0;
statret = lstat(dir.c_str(), &st);
DIRHDL *d = 0;
SYSPATH(dir, sysdir);
statret = LSTAT(sysdir, &st);
if (statret == -1) {
msg << "readdir: cant stat " << dir << " errno " << errno;
msg << "listdir: cant stat " << dir << " errno " << errno;
goto out;
}
if (!S_ISDIR(st.st_mode)) {
msg << "readdir: " << dir << " not a directory";
msg << "listdir: " << dir << " not a directory";
goto out;
}
if (access(dir.c_str(), R_OK) < 0) {
msg << "readdir: no read access to " << dir;
if (ACCESS(sysdir, R_OK) < 0) {
msg << "listdir: no read access to " << dir;
goto out;
}
d = opendir(dir.c_str());
d = OPENDIR(sysdir);
if (d == 0) {
msg << "readdir: cant opendir " << dir << ", errno " << errno;
msg << "listdir: cant opendir " << dir << ", errno " << errno;
goto out;
}
struct dirent *ent;
while ((ent = readdir(d)) != 0) {
if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) {
struct DIRENT *ent;
while ((ent = READDIR(d)) != 0) {
#ifdef _WIN32
string sdname;
if (!wchartoutf8(ent->d_name, sdname)) {
continue;
}
entries.insert(ent->d_name);
const char *dname = sdname.c_str();
#else
const char *dname = ent->d_name;
#endif
if (!strcmp(dname, ".") || !strcmp(dname, "..")) {
continue;
}
entries.insert(dname);
}
out:
if (d) {
closedir(d);
CLOSEDIR(d);
}
reason = msg.str();
if (reason.empty()) {

View File

@ -102,7 +102,7 @@ extern std::string path_PATHsep();
#endif
/// Dump directory
extern bool readdir(const std::string& dir, std::string& reason,
extern bool listdir(const std::string& dir, std::string& reason,
std::set<std::string>& entries);
/** A small wrapper around statfs et al, to return percentage of disk

View File

@ -175,7 +175,7 @@ bool path_empty(const string& path)
if (path_isdir(path)) {
string reason;
std::set<string> entries;
if (!readdir(path, reason, entries) || entries.empty()) {
if (!listdir(path, reason, entries) || entries.empty()) {
return true;
}
return false;

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2004 J.F.Dockes
/* Copyright (C) 2004-2019 J.F.Dockes
*
* 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
@ -15,15 +15,12 @@
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef TEST_WIPEDIR
#include "autoconfig.h"
#include "wipedir.h"
#include <stdio.h>
#include <errno.h>
#include "safefcntl.h"
#include <sys/types.h>
#include "safesysstat.h"
#include "safeunistd.h"
#include <dirent.h>
#include <cstring>
@ -31,48 +28,96 @@
#include "log.h"
#include "pathut.h"
#include "wipedir.h"
#ifdef _WIN32
#include "safefcntl.h"
#include "safeunistd.h"
#include "safewindows.h"
#include "safesysstat.h"
#include "transcode.h"
#define STAT _wstati64
#define LSTAT _wstati64
#define STATBUF _stati64
#define ACCESS _waccess
#define OPENDIR _wopendir
#define CLOSEDIR _wclosedir
#define READDIR _wreaddir
#define DIRENT _wdirent
#define DIRHDL _WDIR
#define UNLINK _wunlink
#define RMDIR _wrmdir
#else // Not windows ->
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#define STAT stat
#define LSTAT lstat
#define STATBUF stat
#define ACCESS access
#define OPENDIR opendir
#define CLOSEDIR closedir
#define READDIR readdir
#define DIRENT dirent
#define DIRHDL DIR
#define UNLINK unlink
#define RMDIR rmdir
#endif
using namespace std;
int wipedir(const string& dir, bool selfalso, bool recurse)
{
struct stat st;
struct STATBUF st;
int statret;
int ret = -1;
statret = lstat(dir.c_str(), &st);
SYSPATH(dir, sysdir);
statret = LSTAT(sysdir, &st);
if (statret == -1) {
LOGERR("wipedir: cant stat " << (dir) << ", errno " << (errno) << "\n" );
LOGSYSERR("wipedir", "stat", dir);
return -1;
}
if (!S_ISDIR(st.st_mode)) {
LOGERR("wipedir: " << (dir) << " not a directory\n" );
LOGERR("wipedir: " << dir << " not a directory\n");
return -1;
}
if (access(dir.c_str(), R_OK|W_OK|X_OK) < 0) {
LOGERR("wipedir: no write access to " << (dir) << "\n" );
if (ACCESS(sysdir, R_OK|W_OK|X_OK) < 0) {
LOGSYSERR("wipedir", "access", dir);
return -1;
}
DIR *d = opendir(dir.c_str());
DIRHDL *d = OPENDIR(sysdir);
if (d == 0) {
LOGERR("wipedir: cant opendir " << (dir) << ", errno " << (errno) << "\n" );
LOGSYSERR("wipedir", "opendir", dir);
return -1;
}
int remaining = 0;
struct dirent *ent;
while ((ent = readdir(d)) != 0) {
if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
struct DIRENT *ent;
while ((ent = READDIR(d)) != 0) {
#ifdef _WIN32
string sdname;
if (!wchartoutf8(ent->d_name, sdname)) {
continue;
}
const char *dname = sdname.c_str();
#else
const char *dname = ent->d_name;
#endif
if (!strcmp(dname, ".") || !strcmp(dname, ".."))
continue;
string fn = path_cat(dir, ent->d_name);
string fn = path_cat(dir, dname);
struct stat st;
int statret = lstat(fn.c_str(), &st);
SYSPATH(fn, sysfn);
struct STATBUF st;
int statret = LSTAT(sysfn, &st);
if (statret == -1) {
LOGERR("wipedir: cant stat " << (fn) << ", errno " << (errno) << "\n" );
LOGSYSERR("wipedir", "stat", fn);
goto out;
}
if (S_ISDIR(st.st_mode)) {
@ -86,8 +131,8 @@ int wipedir(const string& dir, bool selfalso, bool recurse)
remaining++;
}
} else {
if (unlink(fn.c_str()) < 0) {
LOGERR("wipedir: cant unlink " << (fn) << ", errno " << (errno) << "\n" );
if (UNLINK(sysfn) < 0) {
LOGSYSERR("wipedir", "unlink", fn);
goto out;
}
}
@ -95,74 +140,14 @@ int wipedir(const string& dir, bool selfalso, bool recurse)
ret = remaining;
if (selfalso && ret == 0) {
if (rmdir(dir.c_str()) < 0) {
LOGERR("wipedir: rmdir(" << (dir) << ") failed, errno " << (errno) << "\n" );
if (RMDIR(sysdir) < 0) {
LOGSYSERR("wipedir", "rmdir", dir);
ret = -1;
}
}
out:
out:
if (d)
closedir(d);
CLOSEDIR(d);
return ret;
}
#else // FILEUT_TEST
#include <stdio.h>
#include <stdlib.h>
#include "wipedir.h"
using namespace std;
static const char *thisprog;
static int op_flags;
#define OPT_MOINS 0x1
#define OPT_r 0x2
#define OPT_s 0x4
static char usage [] =
"wipedir [-r -s] topdir\n"
" -r : recurse\n"
" -s : also delete topdir\n"
;
static void
Usage(void)
{
fprintf(stderr, "%s: usage:\n%s", thisprog, usage);
exit(1);
}
int main(int argc, const char **argv)
{
thisprog = argv[0];
argc--; argv++;
while (argc > 0 && **argv == '-') {
(*argv)++;
if (!(**argv))
/* Cas du "adb - core" */
Usage();
while (**argv)
switch (*(*argv)++) {
case 'r': op_flags |= OPT_r; break;
case 's': op_flags |= OPT_s; break;
default: Usage(); break;
}
b1: argc--; argv++;
}
if (argc != 1)
Usage();
string dir = *argv++;argc--;
bool topalso = ((op_flags&OPT_s) != 0);
bool recurse = ((op_flags&OPT_r) != 0);
int cnt = wipedir(dir, topalso, recurse);
printf("wipedir returned %d\n", cnt);
exit(0);
}
#endif