robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
38986188ba
master
1/// @file fileio.c 2/// 3/// Buffered reading/writing to a file. Unlike fileio.c this is not dealing with 4/// Nvim structures for buffer, with autocommands, etc: just fopen/fread/fwrite 5/// replacement. 6 7#include <assert.h> 8#include <fcntl.h> 9#include <stdbool.h> 10#include <stddef.h> 11#include <string.h> 12#include <uv.h> 13 14#include "auto/config.h" 15#include "nvim/log.h" 16#include "nvim/macros_defs.h" 17#include "nvim/memory.h" 18#include "nvim/os/fileio.h" 19#include "nvim/os/fs.h" 20#include "nvim/os/os_defs.h" 21#include "nvim/types_defs.h" 22 23#ifdef HAVE_SYS_UIO_H 24# include <sys/uio.h> 25#endif 26 27#include "os/fileio.c.generated.h" // IWYU pragma: keep 28 29/// Open file 30/// 31/// @param[out] ret_fp Address where information needed for reading from or 32/// writing to a file is saved 33/// @param[in] fname File name to open. 34/// @param[in] flags Flags, @see FileOpenFlags. Currently reading from and 35/// writing to the file at once is not supported, so either 36/// kFileWriteOnly or kFileReadOnly is required. 37/// @param[in] mode Permissions for the newly created file (ignored if flags 38/// does not have kFileCreate\*). 39/// 40/// @return Error code, or 0 on success. @see os_strerror() 41int file_open (FileDescriptor * const ret_fp ,const char * const fname ,const int flags , 42const int mode ) 43FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT 44{ 45int os_open_flags = 0 ; 46TriState wr = kNone ; 47#define FLAG (flags ,flag ,fcntl_flags ,wrval ,cond ) \ 48 do { \ 49 if (flags & flag) { \ 50 os_open_flags |= fcntl_flags; \ 51 assert(cond); \ 52 if (wrval != kNone) { \ 53 wr = wrval; \ 54 } \ 55 } \ 56 } while (0) 57FLAG (flags ,kFileWriteOnly ,O_WRONLY ,kTrue , true); 58FLAG (flags ,kFileCreateOnly ,O_CREAT |O_EXCL |O_WRONLY ,kTrue , true); 59FLAG (flags ,kFileCreate ,O_CREAT |O_WRONLY ,kTrue , !(flags & kFileCreateOnly )); 60FLAG (flags ,kFileTruncate ,O_TRUNC |O_WRONLY ,kTrue , 61 !(flags & kFileCreateOnly )); 62FLAG (flags ,kFileAppend ,O_APPEND |O_WRONLY ,kTrue , 63 !(flags & kFileCreateOnly )); 64FLAG (flags ,kFileReadOnly ,O_RDONLY ,kFalse ,wr != kTrue ); 65#ifdef O_NOFOLLOW 66FLAG (flags ,kFileNoSymlink ,O_NOFOLLOW ,kNone , true); 67FLAG (flags ,kFileMkDir ,O_CREAT |O_WRONLY ,kTrue , !(flags & kFileCreateOnly )); 68#endif 69#undef FLAG 70// wr is used for kFileReadOnly flag, but on 71// QB:neovim-qb-slave-ubuntu-12-04-64bit it still errors out with 72// `error: variable ‘wr’ set but not used [-Werror=unused-but-set-variable]` 73 (void )wr ; 74 75if (flags & kFileMkDir ) { 76int mkdir_ret = os_file_mkdir ((char * )fname ,0755 ); 77if (mkdir_ret < 0 ) { 78return mkdir_ret ; 79 } 80 } 81 82const int fd = os_open (fname ,os_open_flags ,mode ); 83 84if (fd < 0 ) { 85return fd ; 86 } 87return file_open_fd (ret_fp ,fd ,flags ); 88} 89 90/// Wrap file descriptor with FileDescriptor structure 91/// 92/// @warning File descriptor wrapped like this must not be accessed by other 93/// means. 94/// 95/// @param[out] ret_fp Address where information needed for reading from or 96/// writing to a file is saved 97/// @param[in] fd File descriptor to wrap. 98/// @param[in] flags Flags, @see FileOpenFlags. Currently reading from and 99/// writing to the file at once is not supported, so either 100/// FILE_WRITE_ONLY or FILE_READ_ONLY is required. 101/// 102/// @return Error code (@see os_strerror()) or 0. Currently always returns 0. 103int file_open_fd (FileDescriptor * const ret_fp ,const int fd ,const int flags ) 104FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT 105{ 106ret_fp -> wr = !!(flags & (kFileCreate 107 |kFileCreateOnly 108 |kFileTruncate 109 |kFileAppend 110 |kFileWriteOnly )); 111ret_fp -> non_blocking = !!(flags & kFileNonBlocking ); 112// Non-blocking writes not supported currently. 113assert (!ret_fp -> wr || !ret_fp -> non_blocking ); 114ret_fp -> fd = fd ; 115ret_fp -> eof = false; 116ret_fp -> buffer = alloc_block (); 117ret_fp -> read_pos = ret_fp -> buffer ; 118ret_fp -> write_pos = ret_fp -> buffer ; 119ret_fp -> bytes_read = 0 ; 120return 0 ; 121} 122 123/// Opens standard input as a FileDescriptor. 124int file_open_stdin (FileDescriptor * fp ) 125FUNC_ATTR_WARN_UNUSED_RESULT 126{ 127int error = file_open_fd (fp ,os_open_stdin_fd (),kFileReadOnly |kFileNonBlocking ); 128if (error != 0 ) { 129ELOG ("failed to open stdin: %s" ,os_strerror (error )); 130 } 131return error ; 132} 133 134/// opens buffer for reading 135void file_open_buffer (FileDescriptor * ret_fp ,char * data ,size_t len ) 136{ 137ret_fp -> wr = false; 138ret_fp -> non_blocking = false; 139ret_fp -> fd = -1 ; 140ret_fp -> eof = true; 141ret_fp -> buffer = NULL ;// we don't take ownership 142ret_fp -> read_pos = data ; 143ret_fp -> write_pos = data + len ; 144ret_fp -> bytes_read = 0 ; 145} 146 147/// Close file and free its buffer 148/// 149/// @param[in,out] fp File to close. 150/// @param[in] do_fsync If true, use fsync() to write changes to disk. 151/// 152/// @return 0 or error code. 153int file_close (FileDescriptor * const fp ,const bool do_fsync ) 154FUNC_ATTR_NONNULL_ALL 155{ 156if (fp -> fd < 0 ) { 157return 0 ; 158 } 159 160const int flush_error = (do_fsync ?file_fsync (fp ) :file_flush (fp )); 161const int close_error = os_close (fp -> fd ); 162free_block (fp -> buffer ); 163if (close_error != 0 ) { 164return close_error ; 165 } 166return flush_error ; 167} 168 169/// Flush file modifications to disk and run fsync() 170/// 171/// @param[in,out] fp File to work with. 172/// 173/// @return 0 or error code. 174int file_fsync (FileDescriptor * const fp ) 175FUNC_ATTR_NONNULL_ALL 176{ 177if (!fp -> wr ) { 178return 0 ; 179 } 180const int flush_error = file_flush (fp ); 181if (flush_error != 0 ) { 182return flush_error ; 183 } 184const int fsync_error = os_fsync (fp -> fd ); 185if (fsync_error != UV_EINVAL 186&& fsync_error != UV_EROFS 187// fsync not supported on this storage. 188&& fsync_error != UV_ENOTSUP ) { 189return fsync_error ; 190 } 191return 0 ; 192} 193 194/// Flush file modifications to disk 195/// 196/// @param[in,out] fp File to work with. 197/// 198/// @return 0 or error code. 199int file_flush (FileDescriptor * fp ) 200FUNC_ATTR_NONNULL_ALL 201{ 202if (!fp -> wr ) { 203return 0 ; 204 } 205 206ptrdiff_t to_write = fp -> write_pos - fp -> read_pos ; 207if (to_write == 0 ) { 208return 0 ; 209 } 210const ptrdiff_t wres = os_write (fp -> fd ,fp -> read_pos , (size_t )to_write , 211fp -> non_blocking ); 212fp -> read_pos = fp -> write_pos = fp -> buffer ; 213if (wres != to_write ) { 214return (wres >=0 ) ?UV_EIO : (int )wres ; 215 } 216return 0 ; 217} 218 219/// Read from file 220/// 221/// @param[in,out] fp File to work with. 222/// @param[out] ret_buf Buffer to read to. Must not be NULL. 223/// @param[in] size Number of bytes to read. Buffer must have at least ret_buf 224/// bytes. 225/// 226/// @return error_code (< 0) or number of bytes read. 227ptrdiff_t file_read (FileDescriptor * const fp ,char * const ret_buf ,const size_t size ) 228FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT 229{ 230assert (!fp -> wr ); 231size_t from_buffer = MIN ((size_t )(fp -> write_pos - fp -> read_pos ),size ); 232memcpy (ret_buf ,fp -> read_pos ,from_buffer ); 233 234char * buf = ret_buf + from_buffer ; 235size_t read_remaining = size - from_buffer ; 236if (!read_remaining ) { 237fp -> bytes_read += from_buffer ; 238fp -> read_pos += from_buffer ; 239return (ptrdiff_t )from_buffer ; 240 } 241 242// at this point, we have consumed all of an existing buffer. restart from the beginning 243fp -> read_pos = fp -> write_pos = fp -> buffer ; 244 245#ifdef HAVE_READV 246bool called_read = false; 247while (read_remaining ) { 248// Allow only at most one os_read[v] call. 249if (fp -> eof || (called_read && fp -> non_blocking )) { 250break ; 251 } 252// If there is readv() syscall, then take an opportunity to populate 253// both target buffer and RBuffer at once, … 254struct iovec iov []= { 255 { .iov_base = buf , .iov_len = read_remaining }, 256 { .iov_base = fp -> write_pos , 257 .iov_len = ARENA_BLOCK_SIZE }, 258 }; 259const ptrdiff_t r_ret = os_readv (fp -> fd ,& fp -> eof ,iov , 260ARRAY_SIZE (iov ),fp -> non_blocking ); 261if (r_ret > 0 ) { 262if (r_ret > (ptrdiff_t )read_remaining ) { 263fp -> write_pos += (size_t )(r_ret - (ptrdiff_t )read_remaining ); 264read_remaining = 0 ; 265 }else { 266buf += r_ret ; 267read_remaining -= (size_t )r_ret ; 268 } 269 }else if (r_ret < 0 ) { 270return r_ret ; 271 } 272called_read = true; 273 } 274#else 275if (fp -> eof ) { 276// already eof, cannot read more 277 }else if (read_remaining >=ARENA_BLOCK_SIZE ) { 278// …otherwise leave fp->buffer empty and populate only target buffer, 279// because filtering information through rbuffer will be more syscalls. 280const ptrdiff_t r_ret = os_read (fp -> fd ,& fp -> eof ,buf ,read_remaining , 281fp -> non_blocking ); 282if (r_ret >=0 ) { 283read_remaining -= (size_t )r_ret ; 284 }else if (r_ret < 0 ) { 285return r_ret ; 286 } 287 }else { 288const ptrdiff_t r_ret = os_read (fp -> fd ,& fp -> eof , 289fp -> write_pos , 290ARENA_BLOCK_SIZE ,fp -> non_blocking ); 291if (r_ret < 0 ) { 292return r_ret ; 293 }else { 294fp -> write_pos += r_ret ; 295size_t to_copy = MIN ((size_t )r_ret ,read_remaining ); 296memcpy (buf ,fp -> read_pos ,to_copy ); 297fp -> read_pos += to_copy ; 298read_remaining -= to_copy ; 299 } 300 } 301#endif 302 303fp -> bytes_read += (size - read_remaining ); 304return (ptrdiff_t )(size - read_remaining ); 305} 306 307/// try to read already buffered data in place 308/// 309/// @return NULL if enough data is not available 310/// valid pointer to chunk of "size". pointer becomes invalid in the next "file_read" call! 311char * file_try_read_buffered (FileDescriptor * const fp ,const size_t size ) 312FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT 313{ 314if ((size_t )(fp -> write_pos - fp -> read_pos ) >=size ) { 315char * ret = fp -> read_pos ; 316fp -> read_pos += size ; 317fp -> bytes_read += size ; 318return ret ; 319 } 320return NULL ; 321} 322 323/// Write to a file 324/// 325/// @param[in] fd File descriptor to write to. 326/// @param[in] buf Data to write. May be NULL if size is zero. 327/// @param[in] size Amount of bytes to write. 328/// 329/// @return Number of bytes written or libuv error code (< 0). 330ptrdiff_t file_write (FileDescriptor * const fp ,const char * const buf ,const size_t size ) 331FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ARG (1 ) 332{ 333assert (fp -> wr ); 334// includes the trivial case of size==0 335if (size < file_space (fp )) { 336memcpy (fp -> write_pos ,buf ,size ); 337fp -> write_pos += size ; 338return (ptrdiff_t )size ; 339 } 340 341// TODO(bfredl): just as for reading, use iovec to combine fp->buffer with buf 342int status = file_flush (fp ); 343if (status < 0 ) { 344return status ; 345 } 346 347if (size < ARENA_BLOCK_SIZE ) { 348memcpy (fp -> write_pos ,buf ,size ); 349fp -> write_pos += size ; 350return (ptrdiff_t )size ; 351 } 352 353const ptrdiff_t wres = os_write (fp -> fd ,buf ,size , 354fp -> non_blocking ); 355return (wres != (ptrdiff_t )size && wres >=0 ) ?UV_EIO :wres ; 356} 357 358/// Skip some bytes 359/// 360/// This is like `fseek(fp, size, SEEK_CUR)`, but actual implementation simply 361/// reads to the buffer and discards the result. 362ptrdiff_t file_skip (FileDescriptor * const fp ,const size_t size ) 363FUNC_ATTR_NONNULL_ALL 364{ 365assert (!fp -> wr ); 366size_t from_buffer = MIN ((size_t )(fp -> write_pos - fp -> read_pos ),size ); 367size_t skip_remaining = size - from_buffer ; 368if (skip_remaining == 0 ) { 369fp -> read_pos += from_buffer ; 370fp -> bytes_read += from_buffer ; 371return (ptrdiff_t )from_buffer ; 372 } 373 374fp -> read_pos = fp -> write_pos = fp -> buffer ; 375bool called_read = false; 376while (skip_remaining > 0 ) { 377// Allow only at most one os_read[v] call. 378if (fp -> eof || (called_read && fp -> non_blocking )) { 379break ; 380 } 381const ptrdiff_t r_ret = os_read (fp -> fd ,& fp -> eof ,fp -> buffer ,ARENA_BLOCK_SIZE , 382fp -> non_blocking ); 383if (r_ret < 0 ) { 384return r_ret ; 385 }else if ((size_t )r_ret > skip_remaining ) { 386fp -> read_pos = fp -> buffer + skip_remaining ; 387fp -> write_pos = fp -> buffer + r_ret ; 388fp -> bytes_read += size ; 389return (ptrdiff_t )size ; 390 } 391skip_remaining -= (size_t )r_ret ; 392called_read = true; 393 } 394 395fp -> bytes_read += size - skip_remaining ; 396return (ptrdiff_t )(size - skip_remaining ); 397}