be/src/load/message_body_sink.cpp
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include "load/message_body_sink.h" |
19 | | |
20 | | // IWYU pragma: no_include <bthread/errno.h> |
21 | | #include <errno.h> // IWYU pragma: keep |
22 | | #include <fcntl.h> |
23 | | #include <glog/logging.h> |
24 | | #include <string.h> |
25 | | #include <unistd.h> |
26 | | |
27 | | #include <ostream> |
28 | | |
29 | | namespace doris { |
30 | | |
31 | 1 | MessageBodyFileSink::~MessageBodyFileSink() { |
32 | 1 | if (_fd >= 0) { |
33 | 0 | close(_fd); |
34 | 0 | } |
35 | 1 | } |
36 | | |
37 | 1 | Status MessageBodyFileSink::open() { |
38 | 1 | _fd = ::open(_path.data(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
39 | 1 | if (_fd < 0) { |
40 | 0 | char errmsg[64]; |
41 | 0 | LOG(WARNING) << "fail to open file, file=" << _path |
42 | 0 | << ", errmsg=" << strerror_r(errno, errmsg, 64); |
43 | 0 | return Status::InternalError("fail to open file"); |
44 | 0 | } |
45 | 1 | return Status::OK(); |
46 | 1 | } |
47 | | |
48 | 1 | Status MessageBodyFileSink::append(const char* data, size_t size) { |
49 | 1 | auto written = ::write(_fd, data, size); |
50 | 1 | if (written == size) { |
51 | 1 | return Status::OK(); |
52 | 1 | } |
53 | 0 | char errmsg[64]; |
54 | 0 | LOG(WARNING) << "fail to write, file=" << _path << ", error=" << strerror_r(errno, errmsg, 64); |
55 | 0 | return Status::InternalError("fail to write file"); |
56 | 1 | } |
57 | | |
58 | 1 | Status MessageBodyFileSink::finish() { |
59 | 1 | if (::close(_fd) < 0) { |
60 | 0 | std::stringstream ss; |
61 | 0 | char errmsg[64]; |
62 | 0 | LOG(WARNING) << "fail to write, file=" << _path |
63 | 0 | << ", error=" << strerror_r(errno, errmsg, 64); |
64 | 0 | _fd = -1; |
65 | 0 | return Status::InternalError("fail to close file"); |
66 | 0 | } |
67 | 1 | _fd = -1; |
68 | 1 | return Status::OK(); |
69 | 1 | } |
70 | | |
71 | 0 | void MessageBodyFileSink::cancel(const std::string& reason) { |
72 | 0 | unlink(_path.data()); |
73 | 0 | } |
74 | | |
75 | | } // namespace doris |