forked from google/ced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_collaborator.cc
95 lines (83 loc) · 2.75 KB
/
io_collaborator.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2017 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "temp_file.h"
#include "wrap_syscall.h"
#include "buffer.h"
class IOCollaborator final : public AsyncCollaborator {
public:
IOCollaborator(const Buffer* buffer);
void Push(const EditNotification& notification) override;
EditResponse Pull() override;
private:
absl::Mutex mu_;
const Buffer* const buffer_;
int attributes_;
int fd_;
ID last_char_id_ GUARDED_BY(mu_);
AnnotatedString last_saved_ GUARDED_BY(mu_);
};
IOCollaborator::IOCollaborator(const Buffer* buffer)
: AsyncCollaborator("io", absl::Milliseconds(100), absl::Milliseconds(500)),
buffer_(buffer),
last_char_id_(AnnotatedString::Begin()) {
fd_ = WrapSyscall("open", [this]() {
return open(buffer_->filename().string().c_str(), O_RDONLY);
});
struct stat st;
WrapSyscall("fstat", [&]() { return fstat(fd_, &st); });
attributes_ = st.st_mode;
}
void IOCollaborator::Push(const EditNotification& notification) {
if (!notification.fully_loaded) return;
absl::MutexLock lock(&mu_);
if (last_saved_.SameContentIdentity(notification.content)) return;
NamedTempFile tmp;
int fd = WrapSyscall("open", [&]() {
return open(tmp.filename().c_str(), O_WRONLY | O_CREAT, attributes_);
});
try {
auto str = notification.content.Render();
WrapSyscall("write", [&]() { return write(fd, str.data(), str.length()); });
} catch (...) {
close(fd);
throw;
}
close(fd);
WrapSyscall("rename", [&]() {
return rename(tmp.filename().c_str(), buffer_->filename().string().c_str());
});
}
EditResponse IOCollaborator::Pull() {
static constexpr const int kChunkSize = 4096;
char buf[kChunkSize];
const int n = WrapSyscall(
"read", [this, &buf]() { return read(fd_, buf, sizeof(buf)); });
EditResponse r;
if (n != sizeof(buf)) {
r.done = true;
r.become_loaded = true;
close(fd_);
fd_ = 0;
}
absl::MutexLock lock(&mu_);
last_char_id_ = AnnotatedString::MakeRawInsert(
&r.content_updates, site(), std::string(buf, n), last_char_id_,
AnnotatedString::End());
return r;
}
IMPL_COLLABORATOR(IOCollaborator, buffer) { return !buffer->synthetic(); }