From 6b0fd3878da1948b8f77387ee335bebe08b39787 Mon Sep 17 00:00:00 2001 From: dockes Date: Mon, 5 Dec 2005 14:09:16 +0000 Subject: [PATCH] create personal config if it does not exist --- src/common/rclconfig.cpp | 45 ++++++++++++++++++++++++++-- src/lib/Makefile | 6 ++-- src/mk/localdefs | 2 +- src/mk/localdefs.in | 2 +- src/utils/copyfile.cpp | 64 ++++++++++++++++++++++++++++++++++++++++ src/utils/copyfile.h | 12 ++++++++ 6 files changed, 125 insertions(+), 6 deletions(-) create mode 100644 src/utils/copyfile.cpp create mode 100644 src/utils/copyfile.h diff --git a/src/common/rclconfig.cpp b/src/common/rclconfig.cpp index fdff84b5..88f28227 100644 --- a/src/common/rclconfig.cpp +++ b/src/common/rclconfig.cpp @@ -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 +#include #include +#include +#include #include @@ -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) { diff --git a/src/lib/Makefile b/src/lib/Makefile index 8082055c..88e09ec1 100644 --- a/src/lib/Makefile +++ b/src/lib/Makefile @@ -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) diff --git a/src/mk/localdefs b/src/mk/localdefs index f1856491..6b822a44 100644 --- a/src/mk/localdefs +++ b/src/mk/localdefs @@ -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\" diff --git a/src/mk/localdefs.in b/src/mk/localdefs.in index 47343ebc..176f7272 100644 --- a/src/mk/localdefs.in +++ b/src/mk/localdefs.in @@ -5,5 +5,5 @@ XAPIANCXXFLAGS=@XAPIANCXXFLAGS@ LIBICONV=@LIBICONV@ INCICONV=@INCICONV@ -LOCALCXXFLAGS = $(INCICONV) $(XAPIANCXXFLAGS) +LOCALCXXFLAGS = $(INCICONV) $(XAPIANCXXFLAGS) -DRECOLL_PREFIX=\"@prefix@\" diff --git a/src/utils/copyfile.cpp b/src/utils/copyfile.cpp new file mode 100644 index 00000000..e34f9bf9 --- /dev/null +++ b/src/utils/copyfile.cpp @@ -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 +#include +#include + +#include +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; +} diff --git a/src/utils/copyfile.h b/src/utils/copyfile.h new file mode 100644 index 00000000..1f0fb601 --- /dev/null +++ b/src/utils/copyfile.h @@ -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 + +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_ */