2014-02-18 15:54:40 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Olivier Goffart <ogoffart@owncloud.com>
|
|
|
|
*
|
|
|
|
* 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
|
2016-10-25 12:00:07 +03:00
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2014-02-18 15:54:40 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2015-05-20 22:42:08 +03:00
|
|
|
#include "config.h"
|
|
|
|
|
2014-02-18 15:54:40 +04:00
|
|
|
#include <QString>
|
|
|
|
#include <ctime>
|
2015-05-20 22:42:08 +03:00
|
|
|
#include <QCryptographicHash>
|
2015-10-09 14:02:02 +03:00
|
|
|
#include <QFileInfo>
|
2014-02-18 15:54:40 +04:00
|
|
|
|
2014-07-11 00:58:58 +04:00
|
|
|
#include <owncloudlib.h>
|
|
|
|
|
2015-02-25 12:51:05 +03:00
|
|
|
class QFile;
|
|
|
|
class QFileInfo;
|
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
namespace OCC {
|
2014-02-18 15:54:40 +04:00
|
|
|
|
2015-06-29 19:56:09 +03:00
|
|
|
/**
|
2015-06-26 18:07:47 +03:00
|
|
|
* \addtogroup libsync
|
|
|
|
* @{
|
2014-02-18 15:54:40 +04:00
|
|
|
*/
|
|
|
|
|
2015-06-26 18:07:47 +03:00
|
|
|
/**
|
2015-06-29 19:56:09 +03:00
|
|
|
* @brief This file contains file system helper
|
2015-06-26 18:07:47 +03:00
|
|
|
*/
|
2014-02-18 15:54:40 +04:00
|
|
|
namespace FileSystem {
|
|
|
|
|
2015-06-29 19:56:09 +03:00
|
|
|
/**
|
|
|
|
* @brief compare two files with given filename and return true if they have the same content
|
2015-06-26 18:07:47 +03:00
|
|
|
*/
|
2014-02-18 15:54:40 +04:00
|
|
|
bool fileEquals(const QString &fn1, const QString &fn2);
|
|
|
|
|
2015-06-29 19:56:09 +03:00
|
|
|
/**
|
|
|
|
* @brief Mark the file as hidden (only has effects on windows)
|
2015-06-26 18:07:47 +03:00
|
|
|
*/
|
2014-07-11 00:58:58 +04:00
|
|
|
void OWNCLOUDSYNC_EXPORT setFileHidden(const QString &filename, bool hidden);
|
2014-02-18 15:54:40 +04:00
|
|
|
|
2015-10-29 16:10:11 +03:00
|
|
|
/**
|
|
|
|
* @brief Marks the file as read-only.
|
|
|
|
*
|
|
|
|
* On linux this either revokes all 'w' permissions or restores permissions
|
|
|
|
* according to the umask.
|
|
|
|
*/
|
|
|
|
void OWNCLOUDSYNC_EXPORT setFileReadOnly(const QString &filename, bool readonly);
|
|
|
|
|
2015-11-23 15:23:19 +03:00
|
|
|
/**
|
|
|
|
* @brief Marks the file as read-only.
|
|
|
|
*
|
|
|
|
* It's like setFileReadOnly(), but weaker: if readonly is false and the user
|
|
|
|
* already has write permissions, no change to the permissions is made.
|
|
|
|
*
|
|
|
|
* This means that it will preserve explicitly set rw-r--r-- permissions even
|
|
|
|
* when the umask is 0002. (setFileReadOnly() would adjust to rw-rw-r--)
|
|
|
|
*/
|
|
|
|
void OWNCLOUDSYNC_EXPORT setFileReadOnlyWeak(const QString &filename, bool readonly);
|
|
|
|
|
2016-11-23 13:05:41 +03:00
|
|
|
/**
|
|
|
|
* @brief Try to set permissions so that other users on the local machine can not
|
|
|
|
* go into the folder.
|
|
|
|
*/
|
|
|
|
void OWNCLOUDSYNC_EXPORT setFolderMinimumPermissions(const QString &filename);
|
|
|
|
|
2015-06-19 14:00:19 +03:00
|
|
|
/** convert a "normal" windows path into a path that can be 32k chars long. */
|
2015-07-03 17:25:10 +03:00
|
|
|
QString OWNCLOUDSYNC_EXPORT longWinPath(const QString &inpath);
|
2014-09-04 13:20:41 +04:00
|
|
|
|
2015-06-29 19:56:09 +03:00
|
|
|
/**
|
|
|
|
* @brief Get the mtime for a filepath
|
2014-09-04 13:20:41 +04:00
|
|
|
*
|
|
|
|
* Use this over QFileInfo::lastModified() to avoid timezone related bugs. See
|
|
|
|
* owncloud/core#9781 for details.
|
|
|
|
*/
|
2015-02-25 12:51:05 +03:00
|
|
|
time_t OWNCLOUDSYNC_EXPORT getModTime(const QString &filename);
|
2014-09-04 13:20:41 +04:00
|
|
|
|
2017-02-07 21:31:55 +03:00
|
|
|
bool OWNCLOUDSYNC_EXPORT setModTime(const QString &filename, time_t modTime);
|
2014-02-18 15:54:40 +04:00
|
|
|
|
2015-06-29 19:56:09 +03:00
|
|
|
/**
|
|
|
|
* @brief Get the size for a file
|
2015-02-12 13:02:56 +03:00
|
|
|
*
|
|
|
|
* Use this over QFileInfo::size() to avoid bugs with lnk files on Windows.
|
|
|
|
* See https://bugreports.qt.io/browse/QTBUG-24831.
|
|
|
|
*/
|
|
|
|
qint64 OWNCLOUDSYNC_EXPORT getSize(const QString &filename);
|
2015-02-25 12:51:05 +03:00
|
|
|
|
2015-06-29 19:56:09 +03:00
|
|
|
/**
|
|
|
|
* @brief Checks whether a file exists.
|
2015-02-25 12:51:05 +03:00
|
|
|
*
|
|
|
|
* Use this over QFileInfo::exists() and QFile::exists() to avoid bugs with lnk
|
|
|
|
* files, see above.
|
|
|
|
*/
|
2015-10-09 14:02:02 +03:00
|
|
|
bool OWNCLOUDSYNC_EXPORT fileExists(const QString &filename, const QFileInfo & = QFileInfo());
|
2015-02-12 13:02:56 +03:00
|
|
|
|
2015-06-29 19:56:09 +03:00
|
|
|
/**
|
|
|
|
* @brief Rename the file \a originFileName to \a destinationFileName.
|
2015-03-11 12:51:36 +03:00
|
|
|
*
|
|
|
|
* It behaves as QFile::rename() but handles .lnk files correctly on Windows.
|
|
|
|
*/
|
|
|
|
bool OWNCLOUDSYNC_EXPORT rename(const QString &originFileName,
|
|
|
|
const QString &destinationFileName,
|
|
|
|
QString *errorString = NULL);
|
2015-05-07 12:41:29 +03:00
|
|
|
|
2015-06-29 19:56:09 +03:00
|
|
|
/**
|
2015-10-05 06:20:09 +03:00
|
|
|
* @brief Check if \a fileName has changed given previous size and mtime
|
2015-06-26 18:07:47 +03:00
|
|
|
*
|
2015-05-07 12:41:29 +03:00
|
|
|
* Nonexisting files are covered through mtime: they have an mtime of -1.
|
2015-06-26 18:07:47 +03:00
|
|
|
*
|
2015-06-29 19:56:09 +03:00
|
|
|
* @return true if the file's mtime or size are not what is expected.
|
2015-05-07 12:41:29 +03:00
|
|
|
*/
|
2016-06-03 14:28:44 +03:00
|
|
|
bool OWNCLOUDSYNC_EXPORT fileChanged(const QString &fileName,
|
|
|
|
qint64 previousSize,
|
|
|
|
time_t previousMtime);
|
2015-05-07 12:41:29 +03:00
|
|
|
|
2015-06-29 19:56:09 +03:00
|
|
|
/**
|
|
|
|
* @brief Like !fileChanged() but with verbose logging if the file *did* change.
|
2015-05-07 12:41:29 +03:00
|
|
|
*/
|
|
|
|
bool verifyFileUnchanged(const QString &fileName,
|
|
|
|
qint64 previousSize,
|
|
|
|
time_t previousMtime);
|
|
|
|
|
2015-06-29 19:56:09 +03:00
|
|
|
/**
|
|
|
|
* @brief renames a file, overriding the target if it exists
|
2015-06-26 18:07:47 +03:00
|
|
|
*
|
2015-05-07 12:41:29 +03:00
|
|
|
* Rename the file \a originFileName to \a destinationFileName, and
|
|
|
|
* overwrite the destination if it already exists - as long as the
|
|
|
|
* destination file has the expected \a destinationSize and
|
|
|
|
* \a destinationMtime.
|
2015-06-26 18:07:47 +03:00
|
|
|
*
|
2015-05-07 12:41:29 +03:00
|
|
|
* If the destination file does not exist, the given size and mtime are
|
|
|
|
* ignored.
|
2014-02-18 17:05:29 +04:00
|
|
|
*/
|
2015-05-07 12:41:29 +03:00
|
|
|
bool renameReplace(const QString &originFileName,
|
|
|
|
const QString &destinationFileName,
|
|
|
|
qint64 destinationSize,
|
|
|
|
time_t destinationMtime,
|
2014-02-18 17:05:29 +04:00
|
|
|
QString *errorString);
|
|
|
|
|
2015-06-29 19:56:09 +03:00
|
|
|
/**
|
2015-05-07 12:41:29 +03:00
|
|
|
* Rename the file \a originFileName to \a destinationFileName, and
|
|
|
|
* overwrite the destination if it already exists - without extra checks.
|
|
|
|
*/
|
|
|
|
bool uncheckedRenameReplace(const QString &originFileName,
|
|
|
|
const QString &destinationFileName,
|
|
|
|
QString *errorString);
|
|
|
|
|
2016-01-05 13:58:18 +03:00
|
|
|
/**
|
|
|
|
* Removes a file.
|
|
|
|
*
|
|
|
|
* Equivalent to QFile::remove(), except on Windows, where it will also
|
|
|
|
* successfully remove read-only files.
|
|
|
|
*/
|
|
|
|
bool OWNCLOUDSYNC_EXPORT remove(const QString &fileName, QString *errorString = 0);
|
|
|
|
|
2015-06-29 19:56:09 +03:00
|
|
|
/**
|
2015-03-12 15:18:08 +03:00
|
|
|
* Replacement for QFile::open(ReadOnly) followed by a seek().
|
|
|
|
* This version sets a more permissive sharing mode on Windows.
|
2015-01-14 14:35:28 +03:00
|
|
|
*
|
2015-10-05 06:20:09 +03:00
|
|
|
* Warning: The resulting file may have an empty fileName and be unsuitable for use
|
2015-03-12 15:18:08 +03:00
|
|
|
* with QFileInfo! Calling seek() on the QFile with >32bit signed values will fail!
|
2015-01-14 14:35:28 +03:00
|
|
|
*/
|
2015-03-12 15:18:08 +03:00
|
|
|
bool openAndSeekFileSharedRead(QFile *file, QString *error, qint64 seek);
|
2015-01-14 14:35:28 +03:00
|
|
|
|
2015-02-11 14:40:48 +03:00
|
|
|
#ifdef Q_OS_WIN
|
2015-06-29 19:56:09 +03:00
|
|
|
/**
|
2015-02-11 14:40:48 +03:00
|
|
|
* Returns the file system used at the given path.
|
|
|
|
*/
|
|
|
|
QString fileSystemForPath(const QString &path);
|
|
|
|
#endif
|
|
|
|
|
2015-05-27 15:59:28 +03:00
|
|
|
QByteArray OWNCLOUDSYNC_EXPORT calcMd5(const QString &fileName);
|
|
|
|
QByteArray OWNCLOUDSYNC_EXPORT calcSha1(const QString &fileName);
|
2015-05-12 16:48:13 +03:00
|
|
|
#ifdef ZLIB_FOUND
|
2015-05-27 15:59:28 +03:00
|
|
|
QByteArray OWNCLOUDSYNC_EXPORT calcAdler32(const QString &fileName);
|
2015-05-12 16:48:13 +03:00
|
|
|
#endif
|
|
|
|
|
2015-12-22 15:02:02 +03:00
|
|
|
/**
|
|
|
|
* Returns a file name based on \a fn that's suitable for a conflict.
|
|
|
|
*/
|
|
|
|
QString OWNCLOUDSYNC_EXPORT makeConflictFileName(const QString &fn, const QDateTime &dt);
|
|
|
|
|
2016-04-29 17:14:18 +03:00
|
|
|
/**
|
|
|
|
* Returns true when a file is locked. (Windows only)
|
|
|
|
*/
|
|
|
|
bool OWNCLOUDSYNC_EXPORT isFileLocked(const QString &fileName);
|
2015-06-26 18:07:47 +03:00
|
|
|
}
|
|
|
|
|
2015-06-29 19:56:09 +03:00
|
|
|
/** @} */
|
2015-06-26 18:07:47 +03:00
|
|
|
}
|