Skip to content

Commit

Permalink
tools,common,msg: Use O_BINARY for win32 compatibility
Browse files Browse the repository at this point in the history
Unless O_BINARY is passed when opening files, Windows will translate
newlines and handle CTRL+z (Alt+026) as EOF, which can abruptly
end file reads.

We'll define O_BINARY as 0 on other platforms and explicitly use
O_BINARY when expecting binary files.

Signed-off-by: Lucian Petrut <[email protected]>
  • Loading branch information
petrutlucian94 committed Oct 22, 2020
1 parent 8e10578 commit 0c57edc
Show file tree
Hide file tree
Showing 17 changed files with 40 additions and 32 deletions.
6 changes: 3 additions & 3 deletions src/common/buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1685,7 +1685,7 @@ void buffer::list::decode_base64(buffer::list& e)

ssize_t buffer::list::pread_file(const char *fn, uint64_t off, uint64_t len, std::string *error)
{
int fd = TEMP_FAILURE_RETRY(::open(fn, O_RDONLY|O_CLOEXEC));
int fd = TEMP_FAILURE_RETRY(::open(fn, O_RDONLY|O_CLOEXEC|O_BINARY));
if (fd < 0) {
int err = errno;
std::ostringstream oss;
Expand Down Expand Up @@ -1745,7 +1745,7 @@ ssize_t buffer::list::pread_file(const char *fn, uint64_t off, uint64_t len, std

int buffer::list::read_file(const char *fn, std::string *error)
{
int fd = TEMP_FAILURE_RETRY(::open(fn, O_RDONLY|O_CLOEXEC));
int fd = TEMP_FAILURE_RETRY(::open(fn, O_RDONLY|O_CLOEXEC|O_BINARY));
if (fd < 0) {
int err = errno;
std::ostringstream oss;
Expand Down Expand Up @@ -1812,7 +1812,7 @@ ssize_t buffer::list::recv_fd(int fd, size_t len)

int buffer::list::write_file(const char *fn, int mode)
{
int fd = TEMP_FAILURE_RETRY(::open(fn, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, mode));
int fd = TEMP_FAILURE_RETRY(::open(fn, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC|O_BINARY, mode));
if (fd < 0) {
int err = errno;
cerr << "bufferlist::write_file(" << fn << "): failed to open file: "
Expand Down
6 changes: 3 additions & 3 deletions src/common/safe_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ int safe_write_file(const char *base, const char *file,

snprintf(fn, sizeof(fn), "%s/%s", base, file);
snprintf(tmp, sizeof(tmp), "%s/%s.tmp", base, file);
fd = open(tmp, O_WRONLY|O_CREAT|O_TRUNC, mode);
fd = open(tmp, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, mode);
if (fd < 0) {
ret = errno;
return -ret;
Expand All @@ -262,7 +262,7 @@ int safe_write_file(const char *base, const char *file,
return ret;
}

fd = open(base, O_RDONLY);
fd = open(base, O_RDONLY|O_BINARY);
if (fd < 0) {
ret = -errno;
return ret;
Expand All @@ -281,7 +281,7 @@ int safe_read_file(const char *base, const char *file,
int fd, len;

snprintf(fn, sizeof(fn), "%s/%s", base, file);
fd = open(fn, O_RDONLY);
fd = open(fn, O_RDONLY|O_BINARY);
if (fd < 0) {
return -errno;
}
Expand Down
7 changes: 7 additions & 0 deletions src/include/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,11 @@ static inline int ceph_sock_errno() {
#endif
}

// Needed on Windows when handling binary files. Without it, line
// endings will be replaced and certain characters can be treated as
// EOF.
#ifndef O_BINARY
#define O_BINARY 0
#endif

#endif /* !CEPH_COMPAT_H */
2 changes: 1 addition & 1 deletion src/include/denc.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class DencDumper {
::snprintf(fn, sizeof(fn),
ENCODE_STRINGIFY(ENCODE_DUMP_PATH) "/%s__%d.%x", name,
getpid(), i++);
int fd = ::open(fn, O_WRONLY|O_TRUNC|O_CREAT|O_CLOEXEC, 0644);
int fd = ::open(fn, O_WRONLY|O_TRUNC|O_CREAT|O_CLOEXEC|O_BINARY, 0644);
if (fd < 0) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/include/encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ WRITE_FLTTYPE_ENCODER(double, uint64_t, le64)
break; \
char fn[PATH_MAX]; \
snprintf(fn, sizeof(fn), ENCODE_STRINGIFY(ENCODE_DUMP_PATH) "/%s__%d.%x", #cl, getpid(), i++); \
int fd = ::open(fn, O_WRONLY|O_TRUNC|O_CREAT|O_CLOEXEC, 0644); \
int fd = ::open(fn, O_WRONLY|O_TRUNC|O_CREAT|O_CLOEXEC|O_BINARY, 0644); \
if (fd >= 0) { \
::ceph::bufferlist sub; \
sub.substr_of(bl, pre_off, bl.length() - pre_off); \
Expand Down
2 changes: 1 addition & 1 deletion src/msg/Message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ void Message::encode(uint64_t features, int crcflags, bool skip_header_crc)
snprintf(fn, sizeof(fn), ENCODE_STRINGIFY(ENCODE_DUMP) "/%s__%d.%x",
abi::__cxa_demangle(typeid(*this).name(), 0, 0, &status),
getpid(), i++);
int fd = ::open(fn, O_WRONLY|O_TRUNC|O_CREAT|O_CLOEXEC, 0644);
int fd = ::open(fn, O_WRONLY|O_TRUNC|O_CREAT|O_CLOEXEC|O_BINARY, 0644);
if (fd >= 0) {
bl.write_fd(fd);
::close(fd);
Expand Down
2 changes: 1 addition & 1 deletion src/rbd_replay/Replayer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ void Replayer::run(const std::string& replay_file) {
m_rbd = new librbd::RBD();
map<thread_id_t, Worker*> workers;

int fd = open(replay_file.c_str(), O_RDONLY);
int fd = open(replay_file.c_str(), O_RDONLY|O_BINARY);
if (fd < 0) {
std::cerr << "Failed to open " << replay_file << ": "
<< cpp_strerror(errno) << std::endl;
Expand Down
3 changes: 2 additions & 1 deletion src/rbd_replay/rbd-replay-prep.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// This code assumes that IO IDs and timestamps are related monotonically.
// In other words, (a.id < b.id) == (a.timestamp < b.timestamp) for all IOs a and b.

#include "include/compat.h"
#include "common/errno.h"
#include "rbd_replay/ActionTypes.h"
#include <babeltrace/babeltrace.h>
Expand Down Expand Up @@ -198,7 +199,7 @@ class Processor {

struct bt_iter *bt_itr = bt_ctf_get_iter(itr);

int fd = open(output_file_name.c_str(), O_WRONLY | O_CREAT | O_EXCL, 0644);
int fd = open(output_file_name.c_str(), O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0644);
ASSERT_EXIT(fd >= 0, "Error opening output file " << output_file_name <<
": " << cpp_strerror(errno));
BOOST_SCOPE_EXIT( (fd) ) {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/ceph-dencoder/ceph_dencoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ int main(int argc, const char **argv)
cerr << "expecting filename" << std::endl;
exit(1);
}
int fd = ::open(*i, O_WRONLY|O_CREAT|O_TRUNC, 0644);
int fd = ::open(*i, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
if (fd < 0) {
cerr << "error opening " << *i << " for write: " << cpp_strerror(errno) << std::endl;
exit(1);
Expand Down
4 changes: 2 additions & 2 deletions src/tools/ceph_monstore_tool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class TraceIter {
MonitorDBStore::TransactionRef t;
public:
explicit TraceIter(string fname) : fd(-1), idx(-1) {
fd = ::open(fname.c_str(), O_RDONLY);
fd = ::open(fname.c_str(), O_RDONLY|O_BINARY);
t.reset(new MonitorDBStore::Transaction);
}
bool valid() {
Expand Down Expand Up @@ -879,7 +879,7 @@ int main(int argc, char **argv) {

int fd = STDOUT_FILENO;
if (!outpath.empty()){
fd = ::open(outpath.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0666);
fd = ::open(outpath.c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0666);
if (fd < 0) {
std::cerr << "error opening output file: "
<< cpp_strerror(errno) << std::endl;
Expand Down
4 changes: 2 additions & 2 deletions src/tools/cephfs/Dumper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ int Dumper::dump(const char *dump_file)

cout << "journal is " << start << "~" << len << std::endl;

int fd = ::open(dump_file, O_WRONLY|O_CREAT|O_TRUNC, 0644);
int fd = ::open(dump_file, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
if (fd >= 0) {
// include an informative header
uuid_d fsid = monc->get_fsid();
Expand Down Expand Up @@ -213,7 +213,7 @@ int Dumper::undump(const char *dump_file, bool force)
derr << "recover_journal failed, try to get header from dump file " << dendl;
}

int fd = ::open(dump_file, O_RDONLY);
int fd = ::open(dump_file, O_RDONLY|O_BINARY);
if (fd < 0) {
r = errno;
derr << "couldn't open " << dump_file << ": " << cpp_strerror(r) << dendl;
Expand Down
12 changes: 6 additions & 6 deletions src/tools/rados/rados.cc
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ static int dump_data(std::string const &filename, bufferlist const &data)
if (filename == "-") {
fd = STDOUT_FILENO;
} else {
fd = TEMP_FAILURE_RETRY(::open(filename.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0644));
fd = TEMP_FAILURE_RETRY(::open(filename.c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644));
if (fd < 0) {
int err = errno;
cerr << "failed to open file: " << cpp_strerror(err) << std::endl;
Expand All @@ -471,7 +471,7 @@ static int do_get(IoCtx& io_ctx, const char *objname, const char *outfile, unsig
if (strcmp(outfile, "-") == 0) {
fd = STDOUT_FILENO;
} else {
fd = TEMP_FAILURE_RETRY(::open(outfile, O_WRONLY|O_CREAT|O_TRUNC, 0644));
fd = TEMP_FAILURE_RETRY(::open(outfile, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644));
if (fd < 0) {
int err = errno;
cerr << "failed to open file: " << cpp_strerror(err) << std::endl;
Expand Down Expand Up @@ -568,7 +568,7 @@ static int do_put(IoCtx& io_ctx,
int ret = 0;
int fd = STDIN_FILENO;
if (!stdio)
fd = open(infile, O_RDONLY);
fd = open(infile, O_RDONLY|O_BINARY);
if (fd < 0) {
cerr << "error reading input file " << infile << ": " << cpp_strerror(errno) << std::endl;
return 1;
Expand Down Expand Up @@ -629,7 +629,7 @@ static int do_append(IoCtx& io_ctx,
int ret = 0;
int fd = STDIN_FILENO;
if (!stdio)
fd = open(infile, O_RDONLY);
fd = open(infile, O_RDONLY|O_BINARY);
if (fd < 0) {
cerr << "error reading input file " << infile << ": " << cpp_strerror(errno) << std::endl;
return 1;
Expand Down Expand Up @@ -3855,7 +3855,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts,
if (nargs.size() < 2 || std::string(nargs[1]) == "-") {
file_fd = STDOUT_FILENO;
} else {
file_fd = open(nargs[1], O_WRONLY|O_CREAT|O_TRUNC, 0666);
file_fd = open(nargs[1], O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0666);
if (file_fd < 0) {
cerr << "Error opening '" << nargs[1] << "': "
<< cpp_strerror(file_fd) << std::endl;
Expand Down Expand Up @@ -3904,7 +3904,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts,
if (filename == "-") {
file_fd = STDIN_FILENO;
} else {
file_fd = open(filename.c_str(), O_RDONLY);
file_fd = open(filename.c_str(), O_RDONLY|O_BINARY);
if (file_fd < 0) {
cerr << "Error opening '" << filename << "': "
<< cpp_strerror(file_fd) << std::endl;
Expand Down
4 changes: 2 additions & 2 deletions src/tools/rbd/action/Export.cc
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ int do_export_diff(librbd::Image& image, const char *fromsnapname,
if (strcmp(path, "-") == 0)
fd = STDOUT_FILENO;
else
fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0644);
fd = open(path, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0644);
if (fd < 0)
return -errno;

Expand Down Expand Up @@ -565,7 +565,7 @@ static int do_export(librbd::Image& image, const char *path, bool no_progress,
if (to_stdout) {
fd = STDOUT_FILENO;
} else {
fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0644);
fd = open(path, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0644);
if (fd < 0) {
return -errno;
}
Expand Down
4 changes: 2 additions & 2 deletions src/tools/rbd/action/Import.cc
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ int do_import_diff(librados::Rados &rados, librbd::Image &image,
if (strcmp(path, "-") == 0) {
fd = STDIN_FILENO;
} else {
fd = open(path, O_RDONLY);
fd = open(path, O_RDONLY|O_BINARY);
if (fd < 0) {
r = -errno;
std::cerr << "rbd: error opening " << path << std::endl;
Expand Down Expand Up @@ -843,7 +843,7 @@ static int do_import(librados::Rados &rados, librbd::RBD &rbd,
fd = STDIN_FILENO;
size = 1ULL << order;
} else {
if ((fd = open(path, O_RDONLY)) < 0) {
if ((fd = open(path, O_RDONLY|O_BINARY)) < 0) {
r = -errno;
std::cerr << "rbd: error opening " << path << std::endl;
goto done2;
Expand Down
4 changes: 2 additions & 2 deletions src/tools/rbd/action/Journal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ static int do_export_journal(librados::IoCtx& io_ctx,
if (to_stdout) {
fd = STDOUT_FILENO;
} else {
fd = open(path.c_str(), O_WRONLY | O_CREAT | O_EXCL, 0644);
fd = open(path.c_str(), O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0644);
if (fd < 0) {
r = -errno;
std::cerr << "rbd: error creating " << path << std::endl;
Expand Down Expand Up @@ -920,7 +920,7 @@ static int do_import_journal(librados::IoCtx& io_ctx,
if (from_stdin) {
fd = STDIN_FILENO;
} else {
if ((fd = open(path.c_str(), O_RDONLY)) < 0) {
if ((fd = open(path.c_str(), O_RDONLY|O_BINARY)) < 0) {
r = -errno;
std::cerr << "rbd: error opening " << path << std::endl;
return r;
Expand Down
6 changes: 3 additions & 3 deletions src/tools/rbd/action/MergeDiff.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,15 @@ static int do_merge_diff(const char *first, const char *second,
if (first_stdin) {
fd = STDIN_FILENO;
} else {
fd = open(first, O_RDONLY);
fd = open(first, O_RDONLY|O_BINARY);
if (fd < 0) {
r = -errno;
std::cerr << "rbd: error opening " << first << std::endl;
goto done;
}
}

sd = open(second, O_RDONLY);
sd = open(second, O_RDONLY|O_BINARY);
if (sd < 0) {
r = -errno;
std::cerr << "rbd: error opening " << second << std::endl;
Expand All @@ -191,7 +191,7 @@ static int do_merge_diff(const char *first, const char *second,
if (strcmp(path, "-") == 0) {
pd = 1;
} else {
pd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0644);
pd = open(path, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0644);
if (pd < 0) {
r = -errno;
std::cerr << "rbd: error create " << path << std::endl;
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rbd/action/MirrorPool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ int execute_peer_bootstrap_import(

int fd = STDIN_FILENO;
if (token_path != "-") {
fd = open(token_path.c_str(), O_RDONLY);
fd = open(token_path.c_str(), O_RDONLY|O_BINARY);
if (fd < 0) {
r = -errno;
std::cerr << "rbd: error opening " << token_path << std::endl;
Expand Down

0 comments on commit 0c57edc

Please sign in to comment.