Skip to content

Commit

Permalink
Made safe writing of files optional
Browse files Browse the repository at this point in the history
Since the safe (atomic) writing of files is unfortunately causing
problems on network drives and in Dropbox folders, there is now an
option to disable it entirely.

Users should be aware that disabling it will result in partially written
files in the event of Tiled crashing while saving or when there is
insufficient space available. When overwriting, the previous contents of
the file will become unavailable, so backups should be made.

Closes #1402
Closes #1404
  • Loading branch information
bjorn committed Jan 3, 2017
1 parent f63db45 commit 8276633
Show file tree
Hide file tree
Showing 21 changed files with 288 additions and 79 deletions.
2 changes: 2 additions & 0 deletions src/libtiled/libtiled.pro
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ SOURCES += compression.cpp \
plugin.cpp \
pluginmanager.cpp \
properties.cpp \
savefile.cpp \
staggeredrenderer.cpp \
tile.cpp \
tileanimationdriver.cpp \
Expand Down Expand Up @@ -76,6 +77,7 @@ HEADERS += compression.h \
plugin.h \
pluginmanager.h \
properties.h \
savefile.h \
staggeredrenderer.h \
terrain.h \
tile.h \
Expand Down
2 changes: 2 additions & 0 deletions src/libtiled/libtiled.qbs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ DynamicLibrary {
"pluginmanager.h",
"properties.cpp",
"properties.h",
"savefile.cpp",
"savefile.h",
"staggeredrenderer.cpp",
"staggeredrenderer.h",
"tile.cpp",
Expand Down
23 changes: 14 additions & 9 deletions src/libtiled/mapwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "mapobject.h"
#include "imagelayer.h"
#include "objectgroup.h"
#include "savefile.h"
#include "tile.h"
#include "tilelayer.h"
#include "tileset.h"
Expand All @@ -44,7 +45,6 @@
#include <QBuffer>
#include <QCoreApplication>
#include <QDir>
#include <QSaveFile>
#include <QXmlStreamWriter>

using namespace Tiled;
Expand Down Expand Up @@ -73,7 +73,7 @@ class MapWriterPrivate
void writeTileset(const Tileset &tileset, QIODevice *device,
const QString &path);

bool openFile(QIODevice *file);
bool openFile(SaveFile *file);

QString mError;
Map::LayerDataFormat mLayerDataFormat;
Expand Down Expand Up @@ -107,7 +107,7 @@ MapWriterPrivate::MapWriterPrivate()
{
}

bool MapWriterPrivate::openFile(QIODevice *file)
bool MapWriterPrivate::openFile(SaveFile *file)
{
if (!file->open(QIODevice::WriteOnly | QIODevice::Text)) {
mError = tr("Could not open file for writing.");
Expand Down Expand Up @@ -677,13 +677,13 @@ void MapWriter::writeMap(const Map *map, QIODevice *device,

bool MapWriter::writeMap(const Map *map, const QString &fileName)
{
QSaveFile file(fileName);
SaveFile file(fileName);
if (!d->openFile(&file))
return false;

writeMap(map, &file, QFileInfo(fileName).absolutePath());
writeMap(map, file.device(), QFileInfo(fileName).absolutePath());

if (file.error() != QFile::NoError) {
if (file.error() != QFileDevice::NoError) {
d->mError = file.errorString();
return false;
}
Expand All @@ -704,13 +704,18 @@ void MapWriter::writeTileset(const Tileset &tileset, QIODevice *device,

bool MapWriter::writeTileset(const Tileset &tileset, const QString &fileName)
{
QFile file(fileName);
SaveFile file(fileName);
if (!d->openFile(&file))
return false;

writeTileset(tileset, &file, QFileInfo(fileName).absolutePath());
writeTileset(tileset, file.device(), QFileInfo(fileName).absolutePath());

if (file.error() != QFile::NoError) {
if (file.error() != QFileDevice::NoError) {
d->mError = file.errorString();
return false;
}

if (!file.commit()) {
d->mError = file.errorString();
return false;
}
Expand Down
64 changes: 64 additions & 0 deletions src/libtiled/savefile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* savefile.cpp
* Copyright 2017, Thorbjørn Lindeijer <bjorn@lindeijer.nl>
*
* This file is part of libtiled.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "savefile.h"

#include <QFile>
#include <QSaveFile>

namespace Tiled {

bool SaveFile::mSafeSavingEnabled = true;

SaveFile::SaveFile(const QString &name)
{
if (mSafeSavingEnabled)
mFileDevice.reset(new QSaveFile(name));
else
mFileDevice.reset(new QFile(name));
}

bool SaveFile::commit()
{
if (auto saveFile = qobject_cast<QSaveFile*>(mFileDevice.data()))
return saveFile->commit();

return mFileDevice->error() == QFileDevice::NoError;
}

bool SaveFile::safeSavingEnabled()
{
return mSafeSavingEnabled;
}

void SaveFile::setSafeSavingEnabled(bool enabled)
{
mSafeSavingEnabled = enabled;
}

} // namespace Tiled
89 changes: 89 additions & 0 deletions src/libtiled/savefile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* savefile.h
* Copyright 2017, Thorbjørn Lindeijer <bjorn@lindeijer.nl>
*
* This file is part of libtiled.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef SAVEFILE_H
#define SAVEFILE_H

#include "tiled_global.h"

#include <QFileDevice>
#include <QScopedPointer>
#include <QString>

namespace Tiled {

/**
* A wrapper around QSaveFile and QFile. Allows safe writing of files to be
* turned off globally.
*/
class TILEDSHARED_EXPORT SaveFile
{
public:
SaveFile(const QString &name);

QFileDevice* device() const;

bool open(QIODevice::OpenMode mode);
bool commit();

QFileDevice::FileError error() const;
QString errorString() const;

static bool safeSavingEnabled();
static void setSafeSavingEnabled(bool enabled);

private:
QScopedPointer<QFileDevice> mFileDevice;

static bool mSafeSavingEnabled;
};


inline QFileDevice *SaveFile::device() const
{
return mFileDevice.data();
}

inline bool SaveFile::open(QIODevice::OpenMode mode)
{
return mFileDevice->open(mode);
}

inline QFileDevice::FileError SaveFile::error() const
{
return mFileDevice->error();
}

inline QString SaveFile::errorString() const
{
return mFileDevice->errorString();
}

} // namespace Tiled

#endif // SAVEFILE_H
21 changes: 11 additions & 10 deletions src/plugins/csv/csvplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@
#include "csvplugin.h"

#include "map.h"
#include "savefile.h"
#include "tile.h"
#include "tilelayer.h"

#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QSaveFile>

using namespace Tiled;
using namespace Csv;
Expand All @@ -43,39 +42,41 @@ bool CsvPlugin::write(const Map *map, const QString &fileName)

// Traverse all tile layers
uint currentLayer = 0u;
foreach (const Layer *layer, map->layers()) {
for (const Layer *layer : map->layers()) {
if (layer->layerType() != Layer::TileLayerType)
continue;

const TileLayer *tileLayer = static_cast<const TileLayer*>(layer);

QSaveFile file(layerPaths.at(currentLayer));
SaveFile file(layerPaths.at(currentLayer));

if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
mError = tr("Could not open file for writing.");
return false;
}

auto device = file.device();

// Write out tiles either by ID or their name, if given. -1 is "empty"
for (int y = 0; y < tileLayer->height(); ++y) {
for (int x = 0; x < tileLayer->width(); ++x) {
if (x > 0)
file.write(",", 1);
device->write(",", 1);

const Cell &cell = tileLayer->cellAt(x, y);
const Tile *tile = cell.tile();
if (tile && tile->hasProperty(QLatin1String("name"))) {
file.write(tile->property(QLatin1String("name")).toString().toUtf8());
device->write(tile->property(QLatin1String("name")).toString().toUtf8());
} else {
const int id = tile ? tile->id() : -1;
file.write(QByteArray::number(id));
device->write(QByteArray::number(id));
}
}

file.write("\n", 1);
device->write("\n", 1);
}

if (file.error() != QFile::NoError) {
if (file.error() != QFileDevice::NoError) {
mError = file.errorString();
return false;
}
Expand Down Expand Up @@ -105,7 +106,7 @@ QStringList CsvPlugin::outputFiles(const Tiled::Map *map, const QString &fileNam
const QString path = fileInfo.path();

// Loop layers to calculate the path for the exported file
foreach (const Layer *layer, map->layers()) {
for (const Layer *layer : map->layers()) {
if (layer->layerType() != Layer::TileLayerType)
continue;

Expand Down
12 changes: 6 additions & 6 deletions src/plugins/defold/defoldplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@

#include "tokendefines.h"

#include <QSaveFile>
#include <QTextStream>

#include "layer.h"
#include "map.h"
#include "mapobject.h"
#include "objectgroup.h"
#include "savefile.h"
#include "tile.h"
#include "tilelayer.h"

#include <QTextStream>

#include <cmath>

namespace Defold {
Expand Down Expand Up @@ -119,15 +119,15 @@ bool DefoldPlugin::write(const Tiled::Map *map, const QString &fileName)
map_h["tile_set"] = "";

QString result = replaceTags(QLatin1String(map_t), map_h);
QSaveFile mapFile(fileName);
Tiled::SaveFile mapFile(fileName);
if (!mapFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
mError = tr("Could not open file for writing.");
return false;
}
QTextStream stream(&mapFile);
QTextStream stream(mapFile.device());
stream << result;

if (mapFile.error() != QSaveFile::NoError) {
if (mapFile.error() != QFileDevice::NoError) {
mError = mapFile.errorString();
return false;
}
Expand Down
Loading

0 comments on commit 8276633

Please sign in to comment.