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
cd08e5c7
Commit
cd08e5c7
authored
Mar 03, 2015
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fs/io/FileOutputStream: use class FileDescriptor
parent
dd4beea4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
19 deletions
+20
-19
FileOutputStream.cxx
src/fs/io/FileOutputStream.cxx
+14
-17
FileOutputStream.hxx
src/fs/io/FileOutputStream.hxx
+6
-2
No files found.
src/fs/io/FileOutputStream.cxx
View file @
cd08e5c7
...
@@ -111,14 +111,14 @@ FileOutputStream::Cancel()
...
@@ -111,14 +111,14 @@ FileOutputStream::Cancel()
/**
/**
* Open a file using Linux's O_TMPFILE for writing the given file.
* Open a file using Linux's O_TMPFILE for writing the given file.
*/
*/
static
int
static
bool
OpenTempFile
(
Path
path
)
OpenTempFile
(
FileDescriptor
&
fd
,
Path
path
)
{
{
const
auto
directory
=
path
.
GetDirectoryName
();
const
auto
directory
=
path
.
GetDirectoryName
();
if
(
directory
.
IsNull
())
if
(
directory
.
IsNull
())
return
-
1
;
return
false
;
return
OpenFile
(
directory
,
O_TMPFILE
|
O_WRONLY
,
0666
);
return
fd
.
Open
(
directory
.
c_str
()
,
O_TMPFILE
|
O_WRONLY
,
0666
);
}
}
#endif
/* HAVE_LINKAT */
#endif
/* HAVE_LINKAT */
...
@@ -128,15 +128,13 @@ FileOutputStream::FileOutputStream(Path _path, Error &error)
...
@@ -128,15 +128,13 @@ FileOutputStream::FileOutputStream(Path _path, Error &error)
{
{
#ifdef HAVE_LINKAT
#ifdef HAVE_LINKAT
/* try Linux's O_TMPFILE first */
/* try Linux's O_TMPFILE first */
fd
=
OpenTempFile
(
path
);
is_tmpfile
=
OpenTempFile
(
fd
,
path
);
is_tmpfile
=
fd
>=
0
;
if
(
!
is_tmpfile
)
{
if
(
!
is_tmpfile
)
{
#endif
#endif
/* fall back to plain POSIX */
/* fall back to plain POSIX */
fd
=
OpenFile
(
path
,
if
(
!
fd
.
Open
(
path
.
c_str
(),
O_WRONLY
|
O_CREAT
|
O_TRUNC
,
O_WRONLY
|
O_CREAT
|
O_TRUNC
,
0666
);
0666
))
if
(
fd
<
0
)
error
.
FormatErrno
(
"Failed to create %s"
,
path
.
c_str
());
error
.
FormatErrno
(
"Failed to create %s"
,
path
.
c_str
());
#ifdef HAVE_LINKAT
#ifdef HAVE_LINKAT
}
}
...
@@ -148,7 +146,7 @@ FileOutputStream::Write(const void *data, size_t size, Error &error)
...
@@ -148,7 +146,7 @@ FileOutputStream::Write(const void *data, size_t size, Error &error)
{
{
assert
(
IsDefined
());
assert
(
IsDefined
());
ssize_t
nbytes
=
write
(
fd
,
data
,
size
);
ssize_t
nbytes
=
fd
.
Write
(
data
,
size
);
if
(
nbytes
<
0
)
{
if
(
nbytes
<
0
)
{
error
.
FormatErrno
(
"Failed to write to %s"
,
path
.
c_str
());
error
.
FormatErrno
(
"Failed to write to %s"
,
path
.
c_str
());
return
false
;
return
false
;
...
@@ -172,18 +170,18 @@ FileOutputStream::Commit(Error &error)
...
@@ -172,18 +170,18 @@ FileOutputStream::Commit(Error &error)
/* hard-link the temporary file to the final path */
/* hard-link the temporary file to the final path */
char
fd_path
[
64
];
char
fd_path
[
64
];
snprintf
(
fd_path
,
sizeof
(
fd_path
),
"/proc/self/fd/%d"
,
fd
);
snprintf
(
fd_path
,
sizeof
(
fd_path
),
"/proc/self/fd/%d"
,
fd
.
Get
());
if
(
linkat
(
AT_FDCWD
,
fd_path
,
AT_FDCWD
,
path
.
c_str
(),
if
(
linkat
(
AT_FDCWD
,
fd_path
,
AT_FDCWD
,
path
.
c_str
(),
AT_SYMLINK_FOLLOW
)
<
0
)
{
AT_SYMLINK_FOLLOW
)
<
0
)
{
error
.
FormatErrno
(
"Failed to commit %s"
,
path
.
c_str
());
error
.
FormatErrno
(
"Failed to commit %s"
,
path
.
c_str
());
close
(
fd
);
fd
.
Close
(
);
return
false
;
return
false
;
}
}
}
}
#endif
#endif
bool
success
=
close
(
fd
)
==
0
;
bool
success
=
fd
.
Close
();
fd
=
-
1
;
if
(
!
success
)
if
(
!
success
)
error
.
FormatErrno
(
"Failed to commit %s"
,
path
.
c_str
());
error
.
FormatErrno
(
"Failed to commit %s"
,
path
.
c_str
());
...
@@ -195,8 +193,7 @@ FileOutputStream::Cancel()
...
@@ -195,8 +193,7 @@ FileOutputStream::Cancel()
{
{
assert
(
IsDefined
());
assert
(
IsDefined
());
close
(
fd
);
fd
.
Close
();
fd
=
-
1
;
#ifdef HAVE_LINKAT
#ifdef HAVE_LINKAT
if
(
!
is_tmpfile
)
if
(
!
is_tmpfile
)
...
...
src/fs/io/FileOutputStream.hxx
View file @
cd08e5c7
...
@@ -25,6 +25,10 @@
...
@@ -25,6 +25,10 @@
#include "fs/AllocatedPath.hxx"
#include "fs/AllocatedPath.hxx"
#include "Compiler.h"
#include "Compiler.h"
#ifndef WIN32
#include "system/FileDescriptor.hxx"
#endif
#include <assert.h>
#include <assert.h>
#ifdef WIN32
#ifdef WIN32
...
@@ -39,7 +43,7 @@ class FileOutputStream final : public OutputStream {
...
@@ -39,7 +43,7 @@ class FileOutputStream final : public OutputStream {
#ifdef WIN32
#ifdef WIN32
HANDLE
handle
;
HANDLE
handle
;
#else
#else
int
fd
;
FileDescriptor
fd
;
#endif
#endif
#ifdef HAVE_LINKAT
#ifdef HAVE_LINKAT
...
@@ -64,7 +68,7 @@ public:
...
@@ -64,7 +68,7 @@ public:
#ifdef WIN32
#ifdef WIN32
return
handle
!=
INVALID_HANDLE_VALUE
;
return
handle
!=
INVALID_HANDLE_VALUE
;
#else
#else
return
fd
>=
0
;
return
fd
.
IsDefined
()
;
#endif
#endif
}
}
...
...
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