| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#define _GNU_SOURCE |
| 6 |
|
| 7 |
|
| 8 |
#include <aws/common/environment.h> |
| 9 |
#include <aws/common/file.h> |
| 10 |
#include <aws/common/logging.h> |
| 11 |
#include <aws/common/string.h> |
| 12 |
#include <aws/common/system_info.h> |
| 13 |
#include <dirent.h> |
| 14 |
#include <errno.h> |
| 15 |
#include <fcntl.h> |
| 16 |
#include <inttypes.h> |
| 17 |
#include <pwd.h> |
| 18 |
#include <stdio.h> |
| 19 |
#include <sys/stat.h> |
| 20 |
#include <unistd.h> |
| 21 |
|
| 22 |
|
| 23 |
#ifndef O_DIRECT |
| 24 |
# define O_DIRECT 0 |
| 25 |
#endif |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
static const size_t AWS_FILE_MAX_READ_CHUNK = 0x7ffff000; |
| 34 |
|
| 35 |
int aws_file_path_read_from_offset_direct_io_with_chunk_size( |
| 36 |
const struct aws_string *file_path, |
| 37 |
uint64_t offset, |
| 38 |
size_t max_read_length, |
| 39 |
size_t max_chunk_size, |
| 40 |
struct aws_byte_buf *output_buf, |
| 41 |
size_t *out_actual_read) { |
| 42 |
if (max_chunk_size > AWS_FILE_MAX_READ_CHUNK) { |
| 43 |
|
| 44 |
max_chunk_size = AWS_FILE_MAX_READ_CHUNK; |
| 45 |
} |
| 46 |
|
| 47 |
if (O_DIRECT == 0) { |
| 48 |
AWS_LOGF_ERROR(AWS_LS_COMMON_GENERAL, "O_DIRECT is not supported on this platform"); |
| 49 |
return aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION); |
| 50 |
} |
| 51 |
size_t available_len = aws_sub_size_saturating(output_buf->capacity, output_buf->len); |
| 52 |
size_t length = aws_min_size(available_len, max_read_length); |
| 53 |
if (length == 0) { |
| 54 |
return AWS_OP_SUCCESS; |
| 55 |
} |
| 56 |
|
| 57 |
size_t page_size = aws_system_info_page_size(); |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
if (offset % page_size != 0 || (uintptr_t)(output_buf->buffer + output_buf->len) % page_size != 0) { |
| 65 |
AWS_LOGF_ERROR( |
| 66 |
AWS_LS_COMMON_GENERAL, |
| 67 |
"aws_file_path_read_from_offset_direct_io: offset %" PRIu64 |
| 68 |
", buffer pointer %p must all be aligned to page size %zu", |
| 69 |
offset, |
| 70 |
(void *)(output_buf->buffer + output_buf->len), |
| 71 |
page_size); |
| 72 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 73 |
} |
| 74 |
|
| 75 |
int rt_code = AWS_OP_ERR; |
| 76 |
int fd = open(aws_string_c_str(file_path), O_RDONLY | O_DIRECT); |
| 77 |
if (fd == -1) { |
| 78 |
int errno_value = errno; |
| 79 |
AWS_LOGF_ERROR( |
| 80 |
AWS_LS_COMMON_GENERAL, |
| 81 |
"Failed to open file %s for reading with O_DIRECT, errno: %d", |
| 82 |
aws_string_c_str(file_path), |
| 83 |
errno_value); |
| 84 |
aws_translate_and_raise_io_error(errno_value); |
| 85 |
goto cleanup; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
if (lseek(fd, (off_t)offset, SEEK_SET) == -1) { |
| 90 |
int errno_value = errno; |
| 91 |
AWS_LOGF_ERROR( |
| 92 |
AWS_LS_COMMON_GENERAL, |
| 93 |
"Failed to seek to position %llu in file %s, errno: %d", |
| 94 |
(unsigned long long)offset, |
| 95 |
aws_string_c_str(file_path), |
| 96 |
errno_value); |
| 97 |
aws_translate_and_raise_io_error(errno_value); |
| 98 |
goto cleanup; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
size_t total_bytes_read = 0; |
| 103 |
size_t remaining_length = length; |
| 104 |
uint8_t *current_buffer = output_buf->buffer + output_buf->len; |
| 105 |
|
| 106 |
while (remaining_length > 0) { |
| 107 |
size_t chunk_size = aws_min_size(remaining_length, max_chunk_size); |
| 108 |
|
| 109 |
ssize_t bytes_read = read(fd, current_buffer, chunk_size); |
| 110 |
if (bytes_read == -1) { |
| 111 |
int errno_value = errno; |
| 112 |
AWS_LOGF_ERROR( |
| 113 |
AWS_LS_COMMON_GENERAL, |
| 114 |
"Failed to read %zu bytes from file %s, errno: %d", |
| 115 |
chunk_size, |
| 116 |
aws_string_c_str(file_path), |
| 117 |
errno_value); |
| 118 |
aws_translate_and_raise_io_error(errno_value); |
| 119 |
goto cleanup; |
| 120 |
} |
| 121 |
|
| 122 |
if (bytes_read == 0) { |
| 123 |
|
| 124 |
break; |
| 125 |
} |
| 126 |
|
| 127 |
total_bytes_read += (size_t)bytes_read; |
| 128 |
current_buffer += bytes_read; |
| 129 |
remaining_length -= (size_t)bytes_read; |
| 130 |
|
| 131 |
|
| 132 |
if ((size_t)bytes_read < chunk_size) { |
| 133 |
break; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
*out_actual_read = total_bytes_read; |
| 138 |
output_buf->len += total_bytes_read; |
| 139 |
rt_code = AWS_OP_SUCCESS; |
| 140 |
cleanup: |
| 141 |
if (fd != -1) { |
| 142 |
close(fd); |
| 143 |
} |
| 144 |
return rt_code; |
| 145 |
} |
| 146 |
|
| 147 |
int aws_file_path_read_from_offset_direct_io( |
| 148 |
const struct aws_string *file_path, |
| 149 |
uint64_t offset, |
| 150 |
size_t max_read_length, |
| 151 |
struct aws_byte_buf *output_buf, |
| 152 |
size_t *out_actual_read) { |
| 153 |
|
| 154 |
if (O_DIRECT == 0) { |
| 155 |
AWS_LOGF_ERROR(AWS_LS_COMMON_GENERAL, "O_DIRECT is not supported on this platform"); |
| 156 |
return aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION); |
| 157 |
} |
| 158 |
|
| 159 |
return aws_file_path_read_from_offset_direct_io_with_chunk_size( |
| 160 |
file_path, offset, max_read_length, AWS_FILE_MAX_READ_CHUNK, output_buf, out_actual_read); |
| 161 |
} |
| 162 |
|
| 163 |
int aws_file_path_write_to_offset_direct_io( |
| 164 |
const struct aws_string *file_path, |
| 165 |
uint64_t offset, |
| 166 |
struct aws_byte_cursor data) { |
| 167 |
|
| 168 |
if (O_DIRECT == 0) { |
| 169 |
AWS_LOGF_ERROR(AWS_LS_COMMON_GENERAL, "O_DIRECT is not supported on this platform"); |
| 170 |
return aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION); |
| 171 |
} |
| 172 |
|
| 173 |
if (data.len == 0) { |
| 174 |
return AWS_OP_SUCCESS; |
| 175 |
} |
| 176 |
|
| 177 |
size_t page_size = aws_system_info_page_size(); |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
if (offset % page_size != 0 || (uintptr_t)data.ptr % page_size != 0 || data.len % page_size != 0) { |
| 184 |
AWS_LOGF_ERROR( |
| 185 |
AWS_LS_COMMON_GENERAL, |
| 186 |
"aws_file_path_write_to_offset_direct_io: offset %" PRIu64 |
| 187 |
", buffer pointer %p, and data.len %zu must all be aligned to page size %zu", |
| 188 |
offset, |
| 189 |
(void *)data.ptr, |
| 190 |
data.len, |
| 191 |
page_size); |
| 192 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 193 |
} |
| 194 |
|
| 195 |
int rt_code = AWS_OP_ERR; |
| 196 |
int fd = open(aws_string_c_str(file_path), O_WRONLY | O_DIRECT); |
| 197 |
if (fd == -1) { |
| 198 |
int errno_value = errno; |
| 199 |
AWS_LOGF_ERROR( |
| 200 |
AWS_LS_COMMON_GENERAL, |
| 201 |
"Failed to open file %s for writing with O_DIRECT, errno: %d", |
| 202 |
aws_string_c_str(file_path), |
| 203 |
errno_value); |
| 204 |
aws_translate_and_raise_io_error(errno_value); |
| 205 |
goto cleanup; |
| 206 |
} |
| 207 |
|
| 208 |
if (lseek(fd, (off_t)offset, SEEK_SET) == -1) { |
| 209 |
int errno_value = errno; |
| 210 |
AWS_LOGF_ERROR( |
| 211 |
AWS_LS_COMMON_GENERAL, |
| 212 |
"Failed to seek to position %llu in file %s, errno: %d", |
| 213 |
(unsigned long long)offset, |
| 214 |
aws_string_c_str(file_path), |
| 215 |
errno_value); |
| 216 |
aws_translate_and_raise_io_error(errno_value); |
| 217 |
goto cleanup; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
size_t total_written = 0; |
| 222 |
while (total_written < data.len) { |
| 223 |
size_t chunk_size = aws_min_size(data.len - total_written, AWS_FILE_MAX_READ_CHUNK); |
| 224 |
ssize_t written = write(fd, data.ptr + total_written, chunk_size); |
| 225 |
if (written == -1) { |
| 226 |
int errno_value = errno; |
| 227 |
AWS_LOGF_ERROR( |
| 228 |
AWS_LS_COMMON_GENERAL, |
| 229 |
"Failed to write %zu bytes to file %s with O_DIRECT, errno: %d", |
| 230 |
chunk_size, |
| 231 |
aws_string_c_str(file_path), |
| 232 |
errno_value); |
| 233 |
aws_translate_and_raise_io_error(errno_value); |
| 234 |
goto cleanup; |
| 235 |
} |
| 236 |
total_written += (size_t)written; |
| 237 |
} |
| 238 |
|
| 239 |
rt_code = AWS_OP_SUCCESS; |
| 240 |
cleanup: |
| 241 |
if (fd != -1) { |
| 242 |
close(fd); |
| 243 |
} |
| 244 |
return rt_code; |
| 245 |
} |
| 246 |
|
| 247 |
bool aws_file_direct_io_is_supported(void) { |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
return O_DIRECT != 0; |
| 252 |
} |