~vcs-imports-ii/znc/master

« back to all changes in this revision

Viewing changes to FileUtils.cpp

  • Committer: Uli Schlachter
  • Date: 2011-03-31 16:00:07 UTC
  • Revision ID: git-v1:70358cabdd7b0b45992eb209a75cef2c5d86fa52
CFile: Add an error flag

Whenever an error happens with some file operation, the error flag is now set.
This is especially interesting for tracking errors from Write().

Signed-off-by: Uli Schlachter <psychon@znc.in>

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 
27
27
CFile::CFile() {
28
28
        m_iFD = -1;
 
29
        ResetError();
29
30
}
30
31
 
31
32
CFile::CFile(const CString& sLongName) {
32
33
        m_iFD = -1;
33
34
 
 
35
        ResetError();
34
36
        SetFileName(sLongName);
35
37
}
36
38
 
167
169
//
168
170
// Functions to manipulate the file on the filesystem
169
171
//
170
 
bool CFile::Delete() { return CFile::Delete(m_sLongName); }
 
172
bool CFile::Delete() {
 
173
        if (CFile::Delete(m_sLongName))
 
174
                return true;
 
175
        m_bHadError = true;
 
176
        return false;
 
177
}
 
178
 
171
179
bool CFile::Move(const CString& sNewFileName, bool bOverwrite) {
172
 
        return CFile::Move(m_sLongName, sNewFileName, bOverwrite);
 
180
        if (CFile::Move(m_sLongName, sNewFileName, bOverwrite))
 
181
                return true;
 
182
        m_bHadError = true;
 
183
        return false;
173
184
}
174
185
 
175
186
bool CFile::Copy(const CString& sNewFileName, bool bOverwrite) {
176
 
        return CFile::Copy(m_sLongName, sNewFileName, bOverwrite);
 
187
        if (CFile::Copy(m_sLongName, sNewFileName, bOverwrite))
 
188
                return true;
 
189
        m_bHadError = true;
 
190
        return false;
177
191
}
178
192
 
179
193
bool CFile::Delete(const CString& sFileName) {
235
249
        if (m_iFD == -1) {
236
250
                return false;
237
251
        }
238
 
        return (fchmod(m_iFD, mode) == 0);
 
252
        if (fchmod(m_iFD, mode) != 0) {
 
253
                m_bHadError = true;
 
254
                return false;
 
255
        }
 
256
        return true;
239
257
}
240
258
 
241
259
bool CFile::Chmod(const CString& sFile, mode_t mode) {
248
266
                return true;
249
267
        }
250
268
 
 
269
        m_bHadError = true;
 
270
 
251
271
        return false;
252
272
}
253
273
 
257
277
                return true;
258
278
        }
259
279
 
 
280
        m_bHadError = true;
 
281
 
260
282
        return false;
261
283
}
262
284
 
263
285
bool CFile::Sync() {
264
 
        return (m_iFD != -1 && fsync(m_iFD) == 0);
 
286
        if (m_iFD != -1 && fsync(m_iFD) == 0)
 
287
                return true;
 
288
        m_bHadError = true;
 
289
        return false;
265
290
}
266
291
 
267
292
bool CFile::Open(const CString& sFileName, int iFlags, mode_t iMode) {
271
296
 
272
297
bool CFile::Open(int iFlags, mode_t iMode) {
273
298
        if (m_iFD != -1) {
 
299
                m_bHadError = true;
274
300
                return false;
275
301
        }
276
302
 
282
308
        iMode |= O_BINARY;
283
309
 
284
310
        m_iFD = open(m_sLongName.c_str(), iFlags, iMode);
285
 
        if (m_iFD < 0)
 
311
        if (m_iFD < 0) {
 
312
                m_bHadError = true;
286
313
                return false;
 
314
        }
287
315
 
288
316
        /* Make sure this FD isn't given to childs */
289
317
        SetFdCloseOnExec(m_iFD);
296
324
                return -1;
297
325
        }
298
326
 
299
 
        return read(m_iFD, pszBuffer, iBytes);
 
327
        int res = read(m_iFD, pszBuffer, iBytes);
 
328
        if (res != iBytes)
 
329
                m_bHadError = true;
 
330
        return res;
300
331
}
301
332
 
302
333
bool CFile::ReadLine(CString& sData, const CString & sDelimiter) {
366
397
                return -1;
367
398
        }
368
399
 
369
 
        return write(m_iFD, pszBuffer, iBytes);
 
400
        u_int res = write(m_iFD, pszBuffer, iBytes);
 
401
        if (res != iBytes)
 
402
                m_bHadError = true;
 
403
        return res;
370
404
}
371
405
 
372
406
int CFile::Write(const CString & sData) {
375
409
void CFile::Close() {
376
410
        if (m_iFD >= 0) {
377
411
                if (close(m_iFD) < 0) {
 
412
                        m_bHadError = true;
378
413
                        DEBUG("CFile::Close(): close() failed with ["
379
414
                                        << strerror(errno) << "]");
380
415
                }