• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KIO

  • kio
  • kio
knfsshare.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 Copyright (c) 2004 Jan Schaefer <j_schaef@informatik.uni-kl.de>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
17*/
18
19#include "knfsshare.h"
20
21#include <QSet>
22#include <QtCore/QFile>
23#include <QtCore/QMutableStringListIterator>
24#include <QtCore/QTextIStream>
25
26#include <kdirwatch.h>
27#include <kdebug.h>
28#include <kconfig.h>
29#include <kconfiggroup.h>
30#include <kglobal.h>
31
32class KNFSShare::KNFSSharePrivate
33{
34public:
35 KNFSSharePrivate( KNFSShare *parent );
36
37 void _k_slotFileChange(const QString&);
38
39 bool readExportsFile();
40 bool findExportsFile();
41
42 KNFSShare *q;
43 QSet<QString> sharedPaths;
44 QString exportsFile;
45};
46
47KNFSShare::KNFSSharePrivate::KNFSSharePrivate( KNFSShare *parent )
48 : q(parent)
49{
50 if (findExportsFile())
51 readExportsFile();
52}
53
60bool KNFSShare::KNFSSharePrivate::findExportsFile()
61{
62 KConfig knfsshare("knfsshare");
63 KConfigGroup config(&knfsshare, "General");
64 exportsFile = config.readPathEntry("exportsFile", QString());
65
66 if ( QFile::exists(exportsFile) )
67 return true;
68
69 if ( QFile::exists("/etc/exports") )
70 exportsFile = "/etc/exports";
71 else {
72 kDebug(7000) << "Could not find exports file! /etc/exports doesn't exist. Configure it in share/config/knfsshare, [General], exportsFile=....";
73 return false;
74 }
75
76 config.writeEntry("exportsFile",exportsFile);
77 return true;
78}
79
84bool KNFSShare::KNFSSharePrivate::readExportsFile()
85{
86 QFile f(exportsFile);
87
88 //kDebug(7000) << exportsFile;
89
90 if (!f.open(QIODevice::ReadOnly)) {
91 kError() << "KNFSShare: Could not open" << exportsFile;
92 return false;
93 }
94
95 sharedPaths.clear();
96
97 QTextStream s( &f );
98
99 bool continuedLine = false; // is true if the line before ended with a backslash
100 QString completeLine;
101
102 while ( !s.atEnd() )
103 {
104 QString currentLine = s.readLine().trimmed();
105
106 if (continuedLine) {
107 completeLine += currentLine;
108 continuedLine = false;
109 }
110 else
111 completeLine = currentLine;
112
113 // is the line continued in the next line ?
114 if ( completeLine.endsWith(QLatin1Char('\\')) )
115 {
116 continuedLine = true;
117 // remove the ending backslash
118 completeLine.chop(1);
119 continue;
120 }
121
122 // comments or empty lines
123 if (completeLine.startsWith(QLatin1Char('#')) || completeLine.isEmpty())
124 {
125 continue;
126 }
127
128 QString path;
129
130 // Handle quotation marks
131 if ( completeLine[0] == QLatin1Char('\"') ) {
132 int i = completeLine.indexOf(QLatin1Char('"'), 1);
133 if (i == -1) {
134 kError() << "KNFSShare: Parse error: Missing quotation mark:" << completeLine;
135 continue;
136 }
137 path = completeLine.mid(1,i-1);
138
139 } else { // no quotation marks
140 int i = completeLine.indexOf(QLatin1Char(' '));
141 if (i == -1)
142 i = completeLine.indexOf(QLatin1Char('\t'));
143
144 if (i == -1)
145 path = completeLine;
146 else
147 path = completeLine.left(i);
148
149 }
150
151 //kDebug(7000) << "KNFSShare: Found path: " << path;
152
153 if (!path.isEmpty()) {
154 // normalize path
155 if ( !path.endsWith(QLatin1Char('/')) )
156 path += QLatin1Char('/');
157
158 sharedPaths.insert(path);
159 }
160 }
161
162 return true;
163}
164
165KNFSShare::KNFSShare()
166 : d(new KNFSSharePrivate(this))
167{
168 if (QFile::exists(d->exportsFile)) {
169 KDirWatch::self()->addFile(d->exportsFile);
170 connect(KDirWatch::self(), SIGNAL(dirty(QString)),this,
171 SLOT(_k_slotFileChange(QString)));
172 }
173}
174
175KNFSShare::~KNFSShare()
176{
177 // This is not needed, we're exiting the process anyway, and KDirWatch is already deleted.
178 //if (QFile::exists(d->exportsFile)) {
179 // KDirWatch::self()->removeFile(d->exportsFile);
180 //}
181 delete d;
182}
183
184
185bool KNFSShare::isDirectoryShared( const QString & path ) const
186{
187 if( path.isEmpty())
188 return false;
189 QString fixedPath = path;
190 if ( path[path.length()-1] != '/' )
191 fixedPath += '/';
192
193 return d->sharedPaths.contains(fixedPath);
194}
195
196QStringList KNFSShare::sharedDirectories() const
197{
198 return d->sharedPaths.values();
199}
200
201QString KNFSShare::exportsPath() const
202{
203 return d->exportsFile;
204}
205
206
207
208void KNFSShare::KNFSSharePrivate::_k_slotFileChange( const QString & path )
209{
210 if (path == exportsFile)
211 readExportsFile();
212
213 emit q->changed();
214}
215
216KNFSShare* KNFSShare::instance()
217{
218 K_GLOBAL_STATIC(KNFSShare, _instance)
219
220 return _instance;
221}
222
223#include "knfsshare.moc"
224
KConfigGroup
KConfig
KDirWatch::addFile
void addFile(const QString &file)
KDirWatch::self
static KDirWatch * self()
KNFSShare
Similar functionality like KFileShare, but works only for NFS and do not need any suid script.
Definition: knfsshare.h:34
KNFSShare::exportsPath
QString exportsPath() const
Returns the path to the used exports file, or null if no exports file was found.
Definition: knfsshare.cpp:201
KNFSShare::instance
static KNFSShare * instance()
Returns the one and only instance of KNFSShare.
Definition: knfsshare.cpp:216
KNFSShare::~KNFSShare
virtual ~KNFSShare()
KNFSShare destructor.
Definition: knfsshare.cpp:175
KNFSShare::isDirectoryShared
bool isDirectoryShared(const QString &path) const
Whether or not the given path is shared by NFS.
Definition: knfsshare.cpp:185
KNFSShare::sharedDirectories
QStringList sharedDirectories() const
Returns a list of all directories shared by NFS.
Definition: knfsshare.cpp:196
QSet
f
static quint32 f(DES_KEY *key, quint32 r, char *subkey)
Definition: des.cpp:378
K_GLOBAL_STATIC
#define K_GLOBAL_STATIC(TYPE, NAME)
kDebug
#define kDebug
kconfig.h
kconfiggroup.h
kdebug.h
kdirwatch.h
kglobal.h
knfsshare.h
config
KSharedConfigPtr config()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal