Files
android
doc
m4
python
scripts
src
AudioCompress
android
archive
client
command
config
db
decoder
encoder
event
filter
fs
io
AllocatedPath.cxx
AllocatedPath.hxx
Charset.cxx
Charset.hxx
CheckFile.cxx
CheckFile.hxx
Config.cxx
Config.hxx
DirectoryReader.cxx
DirectoryReader.hxx
Domain.cxx
Domain.hxx
FileInfo.hxx
FileSystem.cxx
FileSystem.hxx
Glob.hxx
Limits.hxx
NarrowPath.hxx
Path.cxx
Path.hxx
Path2.cxx
StandardDirectory.cxx
StandardDirectory.hxx
Traits.cxx
Traits.hxx
haiku
input
java
lib
mixer
neighbor
net
output
pcm
player
playlist
protocol
queue
sticker
storage
system
tag
thread
unix
util
win32
zeroconf
AudioConfig.cxx
AudioConfig.hxx
AudioFormat.cxx
AudioFormat.hxx
AudioParser.cxx
AudioParser.hxx
BulkEdit.hxx
CheckAudioFormat.cxx
CheckAudioFormat.hxx
Chrono.hxx
CommandLine.cxx
CommandLine.hxx
Compiler.h
DetachedSong.cxx
DetachedSong.hxx
IOThread.cxx
IOThread.hxx
IcyMetaDataParser.cxx
IcyMetaDataParser.hxx
Idle.cxx
Idle.hxx
IdleFlags.cxx
IdleFlags.hxx
Instance.cxx
Instance.hxx
Listen.cxx
Listen.hxx
LocateUri.cxx
LocateUri.hxx
Log.cxx
Log.hxx
LogBackend.cxx
LogBackend.hxx
LogInit.cxx
LogInit.hxx
LogLevel.hxx
LogV.hxx
Main.cxx
Main.hxx
Mapper.cxx
Mapper.hxx
MixRampInfo.hxx
MusicBuffer.cxx
MusicBuffer.hxx
MusicChunk.cxx
MusicChunk.hxx
MusicPipe.cxx
MusicPipe.hxx
Partition.cxx
Partition.hxx
Permission.cxx
Permission.hxx
PlaylistDatabase.cxx
PlaylistDatabase.hxx
PlaylistError.cxx
PlaylistError.hxx
PlaylistFile.cxx
PlaylistFile.hxx
PlaylistPrint.cxx
PlaylistPrint.hxx
PlaylistSave.cxx
PlaylistSave.hxx
ReplayGainConfig.cxx
ReplayGainConfig.hxx
ReplayGainInfo.cxx
ReplayGainInfo.hxx
SongFilter.cxx
SongFilter.hxx
SongLoader.cxx
SongLoader.hxx
SongPrint.cxx
SongPrint.hxx
SongSave.cxx
SongSave.hxx
SongUpdate.cxx
StateFile.cxx
StateFile.hxx
Stats.cxx
Stats.hxx
TagArchive.cxx
TagArchive.hxx
TagFile.cxx
TagFile.hxx
TagPrint.cxx
TagPrint.hxx
TagSave.cxx
TagSave.hxx
TagStream.cxx
TagStream.hxx
TimePrint.cxx
TimePrint.hxx
check.h
ls.cxx
ls.hxx
notify.cxx
notify.hxx
open.h
poison.h
systemd
test
win32
.gitignore
AUTHORS
COPYING
INSTALL
Makefile.am
NEWS
README.md
autogen.sh
configure.ac
mpd.svg
valgrind.suppressions
mpd/src/fs/FileSystem.cxx
2016-08-15 22:13:38 +02:00

69 lines
1.8 KiB
C++

/*
* Copyright 2003-2016 The Music Player Daemon Project
* 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"
#include "AllocatedPath.hxx"
#include "Limits.hxx"
#include "system/Error.hxx"
#include <errno.h>
#include <fcntl.h>
AllocatedPath
ReadLink(Path path)
{
#ifdef WIN32
(void)path;
errno = EINVAL;
return AllocatedPath::Null();
#else
char buffer[MPD_PATH_MAX];
ssize_t size = readlink(path.c_str(), buffer, MPD_PATH_MAX);
if (size < 0)
return AllocatedPath::Null();
if (size_t(size) >= MPD_PATH_MAX) {
errno = ENOMEM;
return AllocatedPath::Null();
}
buffer[size] = '\0';
return AllocatedPath::FromFS(buffer);
#endif
}
void
TruncateFile(Path path)
{
#ifdef WIN32
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
int fd = open_cloexec(path.c_str(), O_WRONLY|O_TRUNC, 0);
if (fd < 0)
throw FormatErrno("Failed to truncate %s", path.c_str());
close(fd);
#endif
}