2013-01-20 11:19:53 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 The Music Player Daemon Project
|
2013-01-20 11:19:53 +01:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* 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 "config.h"
|
|
|
|
#include "FileSystem.hxx"
|
2013-10-17 21:59:35 +02:00
|
|
|
#include "AllocatedPath.hxx"
|
2013-10-17 22:00:01 +02:00
|
|
|
#include "Limits.hxx"
|
2016-08-15 22:13:38 +02:00
|
|
|
#include "system/Error.hxx"
|
2013-01-20 11:19:53 +01:00
|
|
|
|
|
|
|
#include <errno.h>
|
2016-08-15 22:13:38 +02:00
|
|
|
#include <fcntl.h>
|
2013-01-20 11:19:53 +01:00
|
|
|
|
2016-12-04 19:59:33 +01:00
|
|
|
void
|
|
|
|
RenameFile(Path oldpath, Path newpath)
|
|
|
|
{
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2016-12-04 19:59:33 +01:00
|
|
|
if (!MoveFileEx(oldpath.c_str(), newpath.c_str(),
|
|
|
|
MOVEFILE_REPLACE_EXISTING))
|
|
|
|
throw MakeLastError("Failed to rename file");
|
|
|
|
#else
|
|
|
|
if (rename(oldpath.c_str(), newpath.c_str()) < 0)
|
|
|
|
throw MakeErrno("Failed to rename file");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-10-17 21:59:35 +02:00
|
|
|
AllocatedPath
|
|
|
|
ReadLink(Path path)
|
2013-01-20 11:19:53 +01:00
|
|
|
{
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2013-01-20 11:19:53 +01:00
|
|
|
(void)path;
|
|
|
|
errno = EINVAL;
|
2013-10-17 21:59:35 +02:00
|
|
|
return AllocatedPath::Null();
|
2013-01-20 11:19:53 +01:00
|
|
|
#else
|
|
|
|
char buffer[MPD_PATH_MAX];
|
|
|
|
ssize_t size = readlink(path.c_str(), buffer, MPD_PATH_MAX);
|
2013-01-21 18:56:40 +01:00
|
|
|
if (size < 0)
|
2013-10-17 21:59:35 +02:00
|
|
|
return AllocatedPath::Null();
|
2013-10-17 22:03:58 +02:00
|
|
|
if (size_t(size) >= MPD_PATH_MAX) {
|
2013-01-20 11:19:53 +01:00
|
|
|
errno = ENOMEM;
|
2013-10-17 21:59:35 +02:00
|
|
|
return AllocatedPath::Null();
|
2013-01-20 11:19:53 +01:00
|
|
|
}
|
|
|
|
buffer[size] = '\0';
|
2013-10-17 21:59:35 +02:00
|
|
|
return AllocatedPath::FromFS(buffer);
|
2013-01-20 11:19:53 +01:00
|
|
|
#endif
|
|
|
|
}
|
2016-08-15 22:13:38 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
TruncateFile(Path path)
|
|
|
|
{
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2016-08-15 22:13:38 +02:00
|
|
|
HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, nullptr,
|
|
|
|
TRUNCATE_EXISTING, FILE_ATTRIBUTE_NORMAL,
|
|
|
|
nullptr);
|
|
|
|
if (h == INVALID_HANDLE_VALUE)
|
|
|
|
throw FormatLastError("Failed to truncate %s", path.c_str());
|
|
|
|
|
|
|
|
CloseHandle(h);
|
|
|
|
#else
|
2017-08-10 19:40:47 +02:00
|
|
|
UniqueFileDescriptor fd;
|
|
|
|
if (!fd.Open(path.c_str(), O_WRONLY|O_TRUNC))
|
2016-08-15 22:13:38 +02:00
|
|
|
throw FormatErrno("Failed to truncate %s", path.c_str());
|
|
|
|
#endif
|
|
|
|
}
|
2016-08-15 22:25:15 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
RemoveFile(Path path)
|
|
|
|
{
|
2017-12-12 10:22:20 +01:00
|
|
|
#ifdef _WIN32
|
2016-08-15 22:25:15 +02:00
|
|
|
if (!DeleteFile(path.c_str()))
|
|
|
|
throw FormatLastError("Failed to delete %s", path.c_str());
|
|
|
|
#else
|
|
|
|
if (unlink(path.c_str()) < 0)
|
|
|
|
throw FormatErrno("Failed to delete %s", path.c_str());
|
|
|
|
#endif
|
|
|
|
}
|