create personal config if it does not exist
This commit is contained in:
parent
658ffb4aa3
commit
6b0fd3878d
@ -1,8 +1,11 @@
|
||||
#ifndef lint
|
||||
static char rcsid[] = "@(#$Id: rclconfig.cpp,v 1.15 2005-12-02 16:17:29 dockes Exp $ (C) 2004 J.F.Dockes";
|
||||
static char rcsid[] = "@(#$Id: rclconfig.cpp,v 1.16 2005-12-05 14:09:16 dockes Exp $ (C) 2004 J.F.Dockes";
|
||||
#endif
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
@ -11,11 +14,44 @@ static char rcsid[] = "@(#$Id: rclconfig.cpp,v 1.15 2005-12-02 16:17:29 dockes E
|
||||
#include "conftree.h"
|
||||
#include "debuglog.h"
|
||||
#include "smallut.h"
|
||||
#include "pathut.h"
|
||||
#include "copyfile.h"
|
||||
|
||||
#ifndef NO_NAMESPACES
|
||||
using namespace std;
|
||||
#endif /* NO_NAMESPACES */
|
||||
|
||||
static const char *configfiles[] = {"recoll.conf", "mimemap", "mimeconf"};
|
||||
static int ncffiles = sizeof(configfiles) / sizeof(char *);
|
||||
|
||||
static bool createConfig(string &reason)
|
||||
{
|
||||
const char *cprefix = getenv("RECOLL_PREFIX");
|
||||
if (cprefix == 0)
|
||||
cprefix = RECOLL_PREFIX;
|
||||
string prefix(cprefix);
|
||||
path_cat(prefix, "share/recoll/examples");
|
||||
|
||||
string recolldir = path_tildexpand("~/.recoll");
|
||||
if (mkdir(recolldir.c_str(), 0755) < 0) {
|
||||
reason += string("mkdir(") + recolldir + ") failed: " +
|
||||
strerror(errno);
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < ncffiles; i++) {
|
||||
string src = prefix;
|
||||
path_cat(src, string(configfiles[i]));
|
||||
string dst = recolldir;
|
||||
path_cat(dst, string(configfiles[i]));
|
||||
if (!copyfile(src.c_str(), dst.c_str(), reason)) {
|
||||
LOGERR(("Copyfile failed: %s\n", reason.c_str()));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
RclConfig::RclConfig()
|
||||
: m_ok(false), conf(0), mimemap(0), mimeconf(0), stopsuffixes(0)
|
||||
|
||||
@ -37,6 +73,11 @@ RclConfig::RclConfig()
|
||||
string cfilename = confdir;
|
||||
path_cat(cfilename, "recoll.conf");
|
||||
|
||||
if (access(confdir.c_str(), 0) != 0 || access(cfilename.c_str(), 0) != 0) {
|
||||
if (!createConfig(reason))
|
||||
return;
|
||||
}
|
||||
|
||||
// Open readonly here so as not to casually create a config file
|
||||
conf = new ConfTree(cfilename.c_str(), true);
|
||||
if (conf == 0 ||
|
||||
@ -177,7 +218,7 @@ string RclConfig::getMimeIconName(const string &mtype)
|
||||
}
|
||||
|
||||
// Look up an executable filter.
|
||||
// We look in RECOLL_FILTERSDIR, filtersdir param, RECOLL_CONFDIR, then
|
||||
// We look in RECOLL_FILTERSDIR, filtersdir param, then
|
||||
// let the system use the PATH
|
||||
string find_filter(RclConfig *conf, const string &icmd)
|
||||
{
|
||||
|
||||
@ -13,7 +13,7 @@ OBJS = base64.o conftree.o csguess.o debuglog.o \
|
||||
mimehandler.o mimeparse.o mimetype.o myhtmlparse.o pathhash.o pathut.o \
|
||||
rclconfig.o rcldb.o rclinit.o readfile.o smallut.o \
|
||||
textsplit.o transcode.o \
|
||||
unacpp.o unac.o docseq.o sortseq.o
|
||||
unacpp.o unac.o docseq.o sortseq.o copyfile.o
|
||||
|
||||
SRCS = ../utils/conftree.cpp ../index/csguess.cpp ../utils/debuglog.cpp \
|
||||
../utils/execmd.cpp ../utils/idfile.cpp ../utils/md5.cpp \
|
||||
@ -27,7 +27,7 @@ SRCS = ../utils/conftree.cpp ../index/csguess.cpp ../utils/debuglog.cpp \
|
||||
../utils/base64.cpp ../utils/readfile.cpp ../utils/smallut.cpp \
|
||||
../common/textsplit.cpp ../utils/transcode.cpp \
|
||||
../common/unacpp.cpp ../unac/unac.c ../query/history.cpp \
|
||||
../query/docseq.cpp ../query/sortseq.cpp
|
||||
../query/docseq.cpp ../query/sortseq.cpp ../utils/copyfile.cpp
|
||||
|
||||
librcl.a : $(OBJS)
|
||||
ar ru librcl.a $(OBJS)
|
||||
@ -103,6 +103,8 @@ md5.o : ../utils/md5.cpp
|
||||
$(CXX) $(CXXFLAGS) -c $<
|
||||
unacpp.o : ../common/unacpp.cpp
|
||||
$(CXX) $(CXXFLAGS) -c $<
|
||||
copyfile.o : ../utils/copyfile.cpp
|
||||
$(CXX) $(CXXFLAGS) -c $<
|
||||
|
||||
depend: alldeps.stamp
|
||||
alldeps.stamp : $(SRCS)
|
||||
|
||||
@ -5,5 +5,5 @@ XAPIANCXXFLAGS=-I/usr/local/include
|
||||
LIBICONV=-L/usr/local/lib -liconv
|
||||
INCICONV=-I/usr/local/include
|
||||
|
||||
LOCALCXXFLAGS = $(INCICONV) $(XAPIANCXXFLAGS)
|
||||
LOCALCXXFLAGS = $(INCICONV) $(XAPIANCXXFLAGS) -DRECOLL_PREFIX=\"/usr/local\"
|
||||
|
||||
|
||||
@ -5,5 +5,5 @@ XAPIANCXXFLAGS=@XAPIANCXXFLAGS@
|
||||
LIBICONV=@LIBICONV@
|
||||
INCICONV=@INCICONV@
|
||||
|
||||
LOCALCXXFLAGS = $(INCICONV) $(XAPIANCXXFLAGS)
|
||||
LOCALCXXFLAGS = $(INCICONV) $(XAPIANCXXFLAGS) -DRECOLL_PREFIX=\"@prefix@\"
|
||||
|
||||
|
||||
64
src/utils/copyfile.cpp
Normal file
64
src/utils/copyfile.cpp
Normal file
@ -0,0 +1,64 @@
|
||||
#ifndef lint
|
||||
static char rcsid[] = "@(#$Id: copyfile.cpp,v 1.1 2005-12-05 14:09:16 dockes Exp $ (C) 2005 J.F.Dockes";
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <string>
|
||||
using std::string;
|
||||
|
||||
#include "debuglog.h"
|
||||
|
||||
#define CPBSIZ 8192
|
||||
#define COPYFILE_NOERRUNLINK 1
|
||||
|
||||
bool copyfile(const char *src, const char *dst, string &reason, int flags = 0)
|
||||
{
|
||||
int sfd = -1;
|
||||
int dfd = -1;
|
||||
bool ret = false;
|
||||
char buf[CPBSIZ];
|
||||
|
||||
reason.erase();
|
||||
|
||||
LOGDEB(("copyfile: %s to %s\n", src, dst));
|
||||
|
||||
if ((sfd = open(src, O_RDONLY)) < 0) {
|
||||
reason += string("open ") + src + ": " + strerror(errno);
|
||||
goto out;
|
||||
}
|
||||
|
||||
if ((dfd = open(dst, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) {
|
||||
reason += string("open/creat ") + dst + ": " + strerror(errno);
|
||||
// If we fail because of an open/truncate error, we do not want to unlink
|
||||
// the file, we might succeed...
|
||||
flags |= COPYFILE_NOERRUNLINK;
|
||||
goto out;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
int didread;
|
||||
didread = read(sfd, buf, CPBSIZ);
|
||||
if (didread < 0) {
|
||||
reason += string("read src ") + src + ": " + strerror(errno);
|
||||
goto out;
|
||||
}
|
||||
if (didread == 0)
|
||||
break;
|
||||
if (write(dfd, buf, didread) != didread) {
|
||||
reason += string("write dst ") + src + ": " + strerror(errno);
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
ret = true;
|
||||
out:
|
||||
if (ret == false && !(flags©FILE_NOERRUNLINK))
|
||||
unlink(dst);
|
||||
if (sfd >= 0)
|
||||
close(sfd);
|
||||
if (dfd >= 0)
|
||||
close(dfd);
|
||||
return ret;
|
||||
}
|
||||
12
src/utils/copyfile.h
Normal file
12
src/utils/copyfile.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef _COPYFILE_H_INCLUDED_
|
||||
#define _COPYFILE_H_INCLUDED_
|
||||
/* @(#$Id: copyfile.h,v 1.1 2005-12-05 14:09:16 dockes Exp $ (C) 2004 J.F.Dockes */
|
||||
|
||||
#include <string>
|
||||
|
||||
enum CopyfileFlags {COPYFILE_NONE = 0, COPYFILE_NOERRUNLINK = 1};
|
||||
|
||||
extern bool copyfile(const char *src, const char *dst, std::string &reason,
|
||||
int flags = 0);
|
||||
|
||||
#endif /* _COPYFILE_H_INCLUDED_ */
|
||||
Loading…
x
Reference in New Issue
Block a user