summaryrefslogtreecommitdiffstats
path: root/filesharing/advanced/nfs/nfsfile.cpp
blob: 79fc732ec575026e50d68ee03b4e9eb781a3b376 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
  Copyright (c) 2004 Jan Schaefer <j_schaef@informatik.uni-kl.de>

  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
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program 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 General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/

#include <pwd.h>
#include <time.h>
#include <unistd.h>

#include <qfileinfo.h>
#include <qfile.h>
#include <qtextstream.h>
#include <qstringlist.h>

#include <kdebug.h>
#include <kmessagebox.h>
#include <klocale.h>
#include <knfsshare.h>
#include <ktempfile.h>
#include <kprocio.h>

#include "nfsfile.h"

NFSFile::NFSFile(const KURL & url, bool readonly)
{
  _lines.setAutoDelete(true);
  _entries.setAutoDelete(false);
  _url = url;
  _readonly = readonly;
}

NFSFile::~NFSFile()
{
}

void NFSFile::addEntry(NFSEntry *entry)
{
  _lines.append(entry);
  _entries.append(entry);
}

void NFSFile::removeEntry(NFSEntry *entry)
{
  _entries.remove(entry);
  _lines.remove(entry);
}

bool NFSFile::hasEntry(NFSEntry *entry)
{
  return 0 < _entries.contains(entry);
}


NFSEntry* NFSFile::getEntryByPath(const QString & path)
{
  QString testPath = path.stripWhiteSpace();
  if ( testPath[testPath.length()-1] != '/' )
       testPath += '/';
  
  for (NFSEntry* entry = _entries.first(); entry; entry = _entries.next())
  {
    if (entry->path()==testPath)
       return entry;
  }

  return 0L;
}

bool NFSFile::removeEntryByPath(const QString & path) {
  NFSEntry* entry = getEntryByPath(path);
  if (!entry) 
      return false;
      
  removeEntry(entry);      
  return true;
}

EntryIterator NFSFile::getEntries()
{
  return EntryIterator(_entries);
}




bool NFSFile::load()
{
  QFile f(_url.path());

  if ( !f.open(IO_ReadOnly) ) {
    kdError() << "NFSFile::load: Could not open " << _url.path() << endl;
    return false;
  }
  
  _entries.clear();
  _lines.clear();

  QTextStream s( &f );
    
  bool continuedLine = false; // is true if the line before ended with a backslash
  QString completeLine;
  
  while ( !s.eof() )
  {
    QString currentLine = s.readLine().stripWhiteSpace();

    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;
    }
      
    // empty lines
    if (completeLine.isEmpty()) {
      _lines.append(new NFSEmptyLine()); 
      continue;
    }
    
    // comments
    if ('#' == completeLine[0]) {
      _lines.append(new NFSComment(completeLine));
      continue;
    }
    
    QString path;
    QString hosts;
    
    // Handle quotation marks
    if ( completeLine[0] == '"' ) {
      int i = completeLine.find('"',1);
      if (i == -1) {
        kdError() << "NFSFile: Parse error: Missing quotation mark: " 
                  << completeLine << endl;   
        continue;
      }
      path = completeLine.mid(1,i-1);
      hosts = completeLine.mid(i+1);
      
    } else { // no quotation marks
      int i = completeLine.find(' ');
      if (i == -1)
          i = completeLine.find('\t');
      
      if (i == -1) 
        path = completeLine;
      else {
        path = completeLine.left(i);
        hosts = completeLine.mid(i+1).stripWhiteSpace();
      }          
    }        
    
    // normalize path
    if ( path[path.length()-1] != '/' )
            path += '/';
    
    kdDebug(5009) << "KNFSShare: Found path: '" << path << "'" << endl;
    NFSEntry *entry = new NFSEntry(path);
    QStringList hostList = QStringList::split(' ', hosts);
    
    if (hostList.isEmpty()) {
        NFSHost* host = new NFSHost("*");          
        entry->addHost(host);
    } else {
      QStringList::iterator it;
      for ( it = hostList.begin(); it != hostList.end(); ++it ) {
         NFSHost* host = new NFSHost((*it).stripWhiteSpace());          
         entry->addHost(host);
         kdDebug(5009) << "KNFSShare: Found host: " << (*it) << " name='"
                  << host->name << "'" << endl;
      }          
    }
    
    kdDebug(5009) << "KNFSShare: Found hosts: " << hosts << "'" << endl;
    this->addEntry(entry);
  }

  f.close();
  

  return true;

}

void NFSFile::saveTo(QTextStream * stream) {
  QPtrListIterator<NFSLine> it(_lines);
  
  NFSLine *line;
  while ( (line = it.current()) != 0 ) {
    ++it;
    *stream << line->toString() << endl;
  }
}

bool NFSFile::saveTo(const QString& fileName) {
    QFile file(fileName);
    if (!file.open(IO_WriteOnly))
        return false;
        
    QTextStream stream(&file);        
    saveTo(&stream);
    file.close();
    return true;
}

bool NFSFile::save()
{
  if (QFileInfo(_url.path()).isWritable() ) {
    saveTo(_url.path());
  } else
  {
  
    KTempFile tempFile;
    saveTo(tempFile.name());
    tempFile.close();
    tempFile.setAutoDelete( true );

    KProcIO proc;

    QString command = QString("cp %1 %2")
        .arg(KProcess::quote( tempFile.name() ))
        .arg(KProcess::quote( _url.path() ));

    if (restartNFSServer)
      command +=";exportfs -ra";

    if (!QFileInfo(_url.path()).isWritable() )
      proc<<"kdesu" << "-d" << "-c"<<command;

    if (!proc.start(KProcess::Block, true)) {
      return false;
    }      


  }    
  return true;
}