~ubuntu-branches/ubuntu/trusty/znc/trusty

« back to all changes in this revision

Viewing changes to src/FileUtils.cpp

  • Committer: Package Import Robot
  • Author(s): Patrick Matthäi
  • Date: 2013-11-08 13:13:58 UTC
  • mfrom: (34.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20131108131358-o90mlo1evvu27z8h
Tags: 1.2-1
* New upstream release.
  Closes: #728786
  - Remove merged patch 01-spelling-error.
  - Remove merged patch 02-CVE-2013-2130.
  - License has been changed to Apache-2.0.
  - Disable new tests, because they require an internet connection.
  - Add new znc-extra module modules_online.
  - Remove AUTHORS file.
* Bump Standards-Version to 3.9.5 (no changes needed).
* Don't explicitly request xz compression - dpkg 1.17 does this by default.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 2004-2012  See the AUTHORS file for details.
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or modify it
5
 
 * under the terms of the GNU General Public License version 2 as published
6
 
 * by the Free Software Foundation.
 
2
 * Copyright (C) 2004-2013 ZNC, see the NOTICE file for details.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
7
15
 */
8
16
 
9
17
#include <znc/FileUtils.h>
10
18
#include <znc/ExecSock.h>
11
19
#include <znc/Utils.h>
12
 
#include <znc/ZNCDebug.h>
13
 
#include <errno.h>
14
20
#include <fcntl.h>
15
21
#include <pwd.h>
16
22
#include <sys/stat.h>
198
204
}
199
205
 
200
206
bool CFile::Move(const CString& sOldFileName, const CString& sNewFileName, bool bOverwrite) {
201
 
        if ((!bOverwrite) && (CFile::Exists(sNewFileName))) {
202
 
                errno = EEXIST;
203
 
                return false;
 
207
        if (CFile::Exists(sNewFileName)) {
 
208
                if (!bOverwrite) {
 
209
                        errno = EEXIST;
 
210
                        return false;
 
211
                }
 
212
#ifdef _WIN32
 
213
                // Windows (or maybe cygwin?) sometimes fails to rename() file over an existing file
 
214
                // We don't want to remove the old file before moving new file over, for the case if the following rename() will fail for any reason
 
215
                CString sTempFile = sNewFileName
 
216
                        + CUtils::FormatTime(time(NULL), "_%Y%m%d-%H%M%S_", "UTC")
 
217
                        + CString::RandomString(10).MD5(); // this file shouldn't exist :)
 
218
                bool result = rename(sNewFileName.c_str(), sTempFile.c_str()) == 0;
 
219
                result &&= rename(sOldFileName.c_str(), sNewFileName.c_str()) == 0;
 
220
                result &&= Delete(sTempFile); // ok, renamed the file successfully, it's safe to remove backup of old file now
 
221
                return result;
 
222
#endif
204
223
        }
205
224
 
206
225
        return (rename(sOldFileName.c_str(), sNewFileName.c_str()) == 0);
243
262
        OldFile.Close();
244
263
        NewFile.Close();
245
264
 
 
265
        struct stat st;
 
266
        GetInfo(sOldFileName, st);
 
267
        Chmod(sNewFileName, st.st_mode);
 
268
 
246
269
        return true;
247
270
}
248
271