You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
220 lines
5.1 KiB
220 lines
5.1 KiB
15 years ago
|
/* This file is part of the KDE project
|
||
|
Copyright (c) 2004 Jan Schaefer <j_schaef@informatik.uni-kl.de>
|
||
|
|
||
|
This library is free software; you can redistribute it and/or
|
||
|
modify it under the terms of the GNU Library General Public
|
||
|
License version 2 as published by the Free Software Foundation.
|
||
|
|
||
|
This library 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
|
||
|
Library General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU Library General Public License
|
||
|
along with this library; see the file COPYING.LIB. If not, write to
|
||
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
Boston, MA 02110-1301, USA.
|
||
|
*/
|
||
|
|
||
14 years ago
|
#include <tqdict.h>
|
||
|
#include <tqfile.h>
|
||
13 years ago
|
#include <tqtextstream.h>
|
||
15 years ago
|
|
||
|
#include <kdirwatch.h>
|
||
|
#include <kstaticdeleter.h>
|
||
|
#include <kdebug.h>
|
||
12 years ago
|
#include <tdeconfig.h>
|
||
15 years ago
|
|
||
|
#include "knfsshare.h"
|
||
|
|
||
|
class KNFSSharePrivate
|
||
|
{
|
||
|
public:
|
||
|
KNFSSharePrivate();
|
||
|
|
||
|
bool readExportsFile();
|
||
14 years ago
|
bool findExportsFile();
|
||
15 years ago
|
|
||
14 years ago
|
TQDict<bool> sharedPaths;
|
||
|
TQString exportsFile;
|
||
15 years ago
|
};
|
||
|
|
||
|
KNFSSharePrivate::KNFSSharePrivate()
|
||
|
{
|
||
14 years ago
|
if (findExportsFile())
|
||
15 years ago
|
readExportsFile();
|
||
|
}
|
||
|
|
||
|
/**
|
||
14 years ago
|
* Try to find the nfs config file path
|
||
12 years ago
|
* First tries the tdeconfig, then checks
|
||
15 years ago
|
* several well-known paths
|
||
|
* @return wether an 'exports' file was found.
|
||
|
**/
|
||
14 years ago
|
bool KNFSSharePrivate::findExportsFile() {
|
||
12 years ago
|
TDEConfig config("knfsshare");
|
||
15 years ago
|
config.setGroup("General");
|
||
|
exportsFile = config.readPathEntry("exportsFile");
|
||
|
|
||
14 years ago
|
if ( TQFile::exists(exportsFile) )
|
||
15 years ago
|
return true;
|
||
|
|
||
14 years ago
|
if ( TQFile::exists("/etc/exports") )
|
||
15 years ago
|
exportsFile = "/etc/exports";
|
||
|
else {
|
||
|
kdDebug(7000) << "KNFSShare: Could not found exports file!" << endl;
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
config.writeEntry("exportsFile",exportsFile);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Reads all paths from the exports file
|
||
|
* and fills the sharedPaths dict with the values
|
||
|
*/
|
||
|
bool KNFSSharePrivate::readExportsFile() {
|
||
14 years ago
|
TQFile f(exportsFile);
|
||
15 years ago
|
|
||
|
kdDebug(7000) << "KNFSShare::readExportsFile " << exportsFile << endl;
|
||
|
|
||
|
if (!f.open(IO_ReadOnly)) {
|
||
|
kdError() << "KNFSShare: Could not open " << exportsFile << endl;
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
|
||
|
sharedPaths.clear();
|
||
|
|
||
14 years ago
|
TQTextStream s( &f );
|
||
15 years ago
|
|
||
|
bool continuedLine = false; // is true if the line before ended with a backslash
|
||
14 years ago
|
TQString completeLine;
|
||
15 years ago
|
|
||
|
while ( !s.eof() )
|
||
|
{
|
||
14 years ago
|
TQString currentLine = s.readLine().stripWhiteSpace();
|
||
15 years ago
|
|
||
|
if (continuedLine) {
|
||
|
completeLine += currentLine;
|
||
|
continuedLine = false;
|
||
|
}
|
||
|
else
|
||
|
completeLine = currentLine;
|
||
|
|
||
|
// is the line continued in the next line ?
|
||
|
if ( completeLine[completeLine.length()-1] == '\\' )
|
||
|
{
|
||
|
continuedLine = true;
|
||
|
// remove the ending backslash
|
||
|
completeLine.truncate( completeLine.length()-1 );
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
// comments or empty lines
|
||
|
if (completeLine.isEmpty() ||
|
||
|
'#' == completeLine[0])
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString path;
|
||
15 years ago
|
|
||
|
// Handle quotation marks
|
||
|
if ( completeLine[0] == '"' ) {
|
||
13 years ago
|
int i = completeLine.find('"',1);
|
||
15 years ago
|
if (i == -1) {
|
||
|
kdError() << "KNFSShare: Parse error: Missing quotation mark: " << completeLine << endl;
|
||
|
continue;
|
||
|
}
|
||
|
path = completeLine.mid(1,i-1);
|
||
|
|
||
|
} else { // no quotation marks
|
||
13 years ago
|
int i = completeLine.find(' ');
|
||
15 years ago
|
if (i == -1)
|
||
13 years ago
|
i = completeLine.find('\t');
|
||
15 years ago
|
|
||
|
if (i == -1)
|
||
|
path = completeLine;
|
||
|
else
|
||
|
path = completeLine.left(i);
|
||
|
|
||
|
}
|
||
|
|
||
|
kdDebug(7000) << "KNFSShare: Found path: " << path << endl;
|
||
|
|
||
|
// normalize path
|
||
|
if ( path[path.length()-1] != '/' )
|
||
|
path += '/';
|
||
|
|
||
|
bool b = true;
|
||
|
sharedPaths.insert(path,&b);
|
||
|
}
|
||
|
|
||
|
f.close();
|
||
|
|
||
|
return true;
|
||
|
|
||
|
}
|
||
|
|
||
|
KNFSShare::KNFSShare() {
|
||
|
d = new KNFSSharePrivate();
|
||
14 years ago
|
if (TQFile::exists(d->exportsFile)) {
|
||
15 years ago
|
KDirWatch::self()->addFile(d->exportsFile);
|
||
14 years ago
|
connect(KDirWatch::self(), TQT_SIGNAL(dirty (const TQString&)),this,
|
||
|
TQT_SLOT(slotFileChange(const TQString&)));
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
|
KNFSShare::~KNFSShare() {
|
||
14 years ago
|
if (TQFile::exists(d->exportsFile)) {
|
||
15 years ago
|
KDirWatch::self()->removeFile(d->exportsFile);
|
||
|
}
|
||
|
delete d;
|
||
|
}
|
||
|
|
||
|
|
||
14 years ago
|
bool KNFSShare::isDirectoryShared( const TQString & path ) const {
|
||
|
TQString fixedPath = path;
|
||
15 years ago
|
if ( path[path.length()-1] != '/' )
|
||
|
fixedPath += '/';
|
||
|
|
||
13 years ago
|
return d->sharedPaths.find(fixedPath) != 0;
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
TQStringList KNFSShare::sharedDirectories() const {
|
||
|
TQStringList result;
|
||
|
TQDictIterator<bool> it(d->sharedPaths);
|
||
15 years ago
|
for( ; it.current(); ++it )
|
||
|
result << it.currentKey();
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString KNFSShare::exportsPath() const {
|
||
15 years ago
|
return d->exportsFile;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
14 years ago
|
void KNFSShare::slotFileChange( const TQString & path ) {
|
||
15 years ago
|
if (path == d->exportsFile)
|
||
|
d->readExportsFile();
|
||
|
|
||
|
emit changed();
|
||
|
}
|
||
|
|
||
|
KNFSShare* KNFSShare::_instance = 0L;
|
||
|
static KStaticDeleter<KNFSShare> ksdNFSShare;
|
||
|
|
||
|
KNFSShare* KNFSShare::instance() {
|
||
|
if (! _instance )
|
||
|
_instance = ksdNFSShare.setObject(_instance, new KNFSShare());
|
||
|
|
||
|
return _instance;
|
||
|
}
|
||
|
|
||
|
#include "knfsshare.moc"
|
||
|
|