Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
2bca3cd2
Commit
2bca3cd2
authored
Aug 15, 2016
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fs/FileSystem: add TruncateFile()
parent
22a353b8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
0 deletions
+29
-0
FileSystem.cxx
src/fs/FileSystem.cxx
+22
-0
FileSystem.hxx
src/fs/FileSystem.hxx
+7
-0
No files found.
src/fs/FileSystem.cxx
View file @
2bca3cd2
...
...
@@ -21,8 +21,10 @@
#include "FileSystem.hxx"
#include "AllocatedPath.hxx"
#include "Limits.hxx"
#include "system/Error.hxx"
#include <errno.h>
#include <fcntl.h>
AllocatedPath
ReadLink
(
Path
path
)
...
...
@@ -44,3 +46,23 @@ ReadLink(Path path)
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
}
src/fs/FileSystem.hxx
View file @
2bca3cd2
...
...
@@ -105,6 +105,13 @@ StatFile(Path file, struct stat &buf, bool follow_symlinks = true)
#endif
/**
* Truncate a file that exists already. Throws std::system_error on
* error.
*/
void
TruncateFile
(
Path
path
);
/**
* Wrapper for unlink() that uses #Path names.
*/
static
inline
bool
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment