Skip to content

Commit

Permalink
fix #3839
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Nov 17, 2020
1 parent 661b789 commit a71956d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
5 changes: 5 additions & 0 deletions include/swoole_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ typedef struct stat FileStatus;
class File {
private:
int fd_;
int flags_;
std::string path_;

public:
Expand All @@ -50,21 +51,25 @@ class File {

explicit File(int fd) {
fd_ = fd;
flags_ = 0;
}

File(int fd, const std::string &path) {
fd_ = fd;
path_ = path;
flags_ = 0;
}

File(const std::string &path, int oflags) {
fd_ = ::open(path.c_str(), oflags);
path_ = path;
flags_ = oflags;
}

File(const std::string &path, int oflags, int mode) {
fd_ = ::open(path.c_str(), oflags, mode);
path_ = path;
flags_ = oflags;
}

~File() {
Expand Down
2 changes: 1 addition & 1 deletion src/coroutine/system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ std::shared_ptr<String> System::read_file(const char *file, bool lock) {

ssize_t System::write_file(const char *file, char *buf, size_t length, bool lock, int flags) {
ssize_t retval = -1;
uint16_t file_flags = flags | O_CREAT | O_WRONLY;
int file_flags = flags | O_CREAT | O_WRONLY;
swoole::coroutine::async([&]() {
File _file(file, file_flags, 0644);
if (!_file.ready()) {
Expand Down
7 changes: 6 additions & 1 deletion src/os/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ bool file_put_contents(const std::string &filename, const char *content, size_t
size_t File::write_all(const void *data, size_t len) {
size_t written_bytes = 0;
while (written_bytes < len) {
ssize_t n = pwrite((char *) data + written_bytes, len - written_bytes, written_bytes);
ssize_t n;
if (flags_ & APPEND) {
n = write((char *) data + written_bytes, len - written_bytes);
} else {
n = pwrite((char *) data + written_bytes, len - written_bytes, written_bytes);
}
if (n > 0) {
written_bytes += n;
} else if (n == 0) {
Expand Down

0 comments on commit a71956d

Please sign in to comment.