file.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /** @file file.c
  2. * @brief Functions for working with files.
  3. *
  4. * This library was created to exclude glibc's file stream operations like
  5. * fopen(), fgets() etc. These functions use glibc internal buffer. To work
  6. * with buffer glibc has its own fflush() function and special behaviour while
  7. * fclose(). It brings a problems with stream file objects and system file
  8. * descriptors while fork(). The file streams and system file descriptors can't
  9. * be used interchangeably. So faux file library uses standard system file
  10. * operations like open(), read() and emulate some usefull stream function like
  11. * getline(). The faux file object has own buffer and doesn't use glibc's one.
  12. * The faux_file_close() doesn't lseek() file descriptor as fclose() can do.
  13. * You can use faux file object and standard file operation in the same time.
  14. * The only thing to remember is internal buffer that can contain already
  15. * readed bytes.
  16. */
  17. #include <stdlib.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <fcntl.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <assert.h>
  24. #include <errno.h>
  25. #include "private.h"
  26. #include "faux/faux.h"
  27. #include "faux/file.h"
  28. #include "faux/str.h"
  29. /** @brief Create file object using existent fd.
  30. *
  31. * Create file object and link it to existent file descriptor.
  32. *
  33. * @param [in] fd Already opened file descriptor.
  34. * @return Allocated and initialized file object or NULL on error.
  35. */
  36. faux_file_t *faux_file_fdopen(int fd)
  37. {
  38. struct stat stat_struct = {};
  39. faux_file_t *f = NULL;
  40. // Before object creation check is fd valid.
  41. // Try to get stat().
  42. if (fstat(fd, &stat_struct) < 0)
  43. return NULL; // Illegal fd
  44. f = faux_zmalloc(sizeof(*f));
  45. assert(f);
  46. if (!f)
  47. return NULL;
  48. // Init
  49. f->fd = fd;
  50. f->buf_size = FAUX_FILE_CHUNK_SIZE;
  51. f->buf = faux_zmalloc(f->buf_size);
  52. assert(f->buf);
  53. if (!f->buf) {
  54. faux_free(f);
  55. return NULL;
  56. }
  57. f->len = 0;
  58. f->eof = BOOL_FALSE;
  59. return f;
  60. }
  61. /** @brief Create file object and open correspondent file.
  62. *
  63. * Function opens specified file using flags and create file object based
  64. * on this opened file. The object must be freed and file must be closed
  65. * later by the faux_file_close().
  66. *
  67. * @warning The faux_file_close() must be executed later to free file object.
  68. *
  69. * @param [in] pathname File name.
  70. * @param [in] flags Flags to open file (like O_RDONLY etc).
  71. * @param [in] mode File permissions if file will be created.
  72. * @return File object or NULL on error.
  73. */
  74. faux_file_t *faux_file_open(const char *pathname, int flags, mode_t mode)
  75. {
  76. int fd = -1;
  77. assert(pathname);
  78. if (!pathname)
  79. return NULL;
  80. fd = open(pathname, flags, mode);
  81. if (fd < 0)
  82. return NULL;
  83. return faux_file_fdopen(fd);
  84. }
  85. /** @brief Closes file and frees file object.
  86. *
  87. * Function closes previously opened (by faux_file_open() or faux_file_fdopen())
  88. * file and frees file object structures.
  89. *
  90. * @param [in] f File object to close and free.
  91. * @return BOOL_TRUE - success, BOOL_FALSE - error
  92. */
  93. bool_t faux_file_close(faux_file_t *f)
  94. {
  95. int fd = -1;
  96. if (!f)
  97. return BOOL_FALSE;
  98. fd = f->fd;
  99. faux_free(f->buf);
  100. faux_free(f);
  101. if (close(fd) < 0)
  102. return BOOL_FALSE;
  103. return BOOL_TRUE;
  104. }
  105. /** @brief Returns file descriptor from file object.
  106. *
  107. * Works like fileno() function for stream objects.
  108. *
  109. * @param [in] f File object.
  110. * @return Linked file descriptor.
  111. */
  112. int faux_file_fileno(faux_file_t *f)
  113. {
  114. assert(f);
  115. if (!f)
  116. return -1;
  117. return f->fd;
  118. }
  119. /** @brief Returns EOF flag.
  120. *
  121. * @param [in] f File object
  122. * @return BOOL_TRUE if it's end of file and BOOL_FALSE else.
  123. */
  124. bool_t faux_file_eof(const faux_file_t *f)
  125. {
  126. assert(f);
  127. if (!f)
  128. return BOOL_FALSE;
  129. return f->eof;
  130. }
  131. /** @brief Service static function to take away data block from internal buffer.
  132. *
  133. * Returns allocated data block in a form of C-string i.e. adds '\0' at the end
  134. * of data. Additionally function can drop some bytes from internal buffer.
  135. * It's usefull when it's necessary to get text string from the buffer and
  136. * drop trailing end of line.
  137. *
  138. * @warning Returned pointer must be freed by faux_str_free() later.
  139. *
  140. * @param [in] f File object.
  141. * @param [in] bytes_get Number of bytes to get from internal buffer.
  142. * @param [in] bytes_drop Number of bytes to drop. Actually
  143. * (bytes_drop + bytes_get) bytes will be removed from internal buffer.
  144. * @return Allocated string (with trailing '\0') with data to get.
  145. */
  146. static char *faux_file_takeaway(faux_file_t *f,
  147. size_t bytes_get, size_t bytes_drop)
  148. {
  149. size_t remove_len = 0;
  150. char *line = NULL;
  151. assert(f);
  152. if (!f)
  153. return NULL;
  154. remove_len = bytes_get + bytes_drop;
  155. // Try to take away more bytes than buffer contain
  156. if ((remove_len > f->len) || (0 == remove_len))
  157. return NULL;
  158. line = faux_zmalloc(bytes_get + 1); // One extra byte for '\0'
  159. assert(line);
  160. if (!line)
  161. return NULL; // Memory problems
  162. memcpy(line, f->buf, bytes_get);
  163. // Remove block from the internal buffer
  164. f->len = f->len - remove_len;
  165. memmove(f->buf, f->buf + remove_len, f->len);
  166. return line;
  167. }
  168. /** @brief Service static function to get all data from buf as single C-string.
  169. *
  170. * Gets all the data from internal buffer as a single C-string (i.e. ends with
  171. * '\0'). This data will be removed from internal buffer.
  172. *
  173. * @warning Returned pointer must be freed by faux_str_free() later.
  174. *
  175. * @param [in] f File object.
  176. * @return Allocated string (with trailing '\0') with data to get.
  177. */
  178. static char *faux_file_takeaway_rest(faux_file_t *f)
  179. {
  180. assert(f);
  181. if (!f)
  182. return NULL;
  183. return faux_file_takeaway(f, f->len, 0);
  184. }
  185. /** @brief Universal static function to get line from buf as single C-string.
  186. *
  187. * Universal function for faux_file_takeaway(), faux_file_takeway_raw()
  188. * implementation.
  189. *
  190. * @warning Returned pointer must be freed by faux_str_free() later.
  191. *
  192. * @param [in] f File object.
  193. * @param [in] raw Boolean flag.
  194. * BOOL_TRUE - include trailing EOL to resulting string,
  195. * BOOL_FALSE - don't include
  196. * @return Allocated string (with trailing '\0') with line.
  197. */
  198. static char *faux_file_takeaway_line_internal(faux_file_t *f, bool_t raw)
  199. {
  200. char *find = NULL;
  201. const char *eol = "\n\r";
  202. size_t line_len = 0;
  203. assert(f);
  204. if (!f)
  205. return NULL;
  206. // Search buffer for EOL
  207. find = faux_str_charsn(f->buf, eol, f->len);
  208. if (!find)
  209. return NULL; // End of line is not found
  210. line_len = find - f->buf;
  211. if (raw) {
  212. // Takeaway line with trailing EOL.
  213. return faux_file_takeaway(f, line_len + 1, 0);
  214. } else {
  215. // Takeaway line without trailing EOL. So drop one last byte
  216. return faux_file_takeaway(f, line_len, 1);
  217. }
  218. }
  219. /** @brief Service static function to get raw line from buf as single C-string.
  220. *
  221. * Gets line (data ends with EOL) from internal buffer as a single C-string
  222. * (i.e. ends with '\0'). The resulting line will contain trailing EOL.
  223. *
  224. * @warning Returned pointer must be freed by faux_str_free() later.
  225. *
  226. * @param [in] f File object.
  227. * @return Allocated string (with trailing '\0') with line.
  228. */
  229. static char *faux_file_takeaway_line_raw(faux_file_t *f)
  230. {
  231. return faux_file_takeaway_line_internal(f, BOOL_TRUE);
  232. }
  233. /** @brief Service static function to get line from buf as single C-string.
  234. *
  235. * Gets line (data ends with EOL) from internal buffer as a single C-string
  236. * (i.e. ends with '\0'). The resulting line will not contain trailing EOL but
  237. * EOL will be removed from internal buffer together with line.
  238. *
  239. * @warning Returned pointer must be freed by faux_str_free() later.
  240. *
  241. * @param [in] f File object.
  242. * @return Allocated string (with trailing '\0') with line.
  243. */
  244. static char *faux_file_takeaway_line(faux_file_t *f)
  245. {
  246. return faux_file_takeaway_line_internal(f, BOOL_FALSE);
  247. }
  248. /** @brief Service static function to enlarge internal buffer.
  249. *
  250. * The initial size of internal buffer is 128 bytes. Each function execution
  251. * enlarges buffer by chunk of 128 bytes.
  252. *
  253. * @param [in] f File objects.
  254. * @return 0 - success, < 0 - error
  255. */
  256. static int faux_file_enlarge_buffer(faux_file_t *f)
  257. {
  258. size_t new_size = 0;
  259. char *new_buf = NULL;
  260. assert(f);
  261. if (!f)
  262. return -1;
  263. new_size = f->buf_size + FAUX_FILE_CHUNK_SIZE;
  264. new_buf = realloc(f->buf, new_size);
  265. assert(new_buf);
  266. if (!new_buf)
  267. return -1;
  268. // NULLify newly allocated memory
  269. faux_bzero(new_buf + f->buf_size, new_size - f->buf_size);
  270. f->buf = new_buf;
  271. f->buf_size = new_size;
  272. return 0;
  273. }
  274. /** @brief Universal static function to read line from file.
  275. *
  276. * Function for implementation faux_file_getline_raw() and faux_file_getline().
  277. *
  278. * @warning Returned pointer must be freed by faux_str_free() later.
  279. *
  280. * @param [in] f File object.
  281. * @param [in] raw
  282. * BOOL_TRUE - raw mode (with trailing EOL)
  283. * BOOL_FALSE - without trailing EOL
  284. * @return Line pointer or NULL on error.
  285. */
  286. char *faux_file_getline_internal(faux_file_t *f, bool_t raw)
  287. {
  288. ssize_t bytes_readed = 0;
  289. assert(f);
  290. if (!f)
  291. return NULL;
  292. do {
  293. char *find = NULL;
  294. // May be buffer already contain line
  295. if (raw) { // raw mode
  296. find = faux_file_takeaway_line_raw(f);
  297. } else { // without trailing EOL
  298. find = faux_file_takeaway_line(f);
  299. }
  300. if (find)
  301. return find;
  302. if (f->buf_size == f->len) { // Buffer is full but doesn't contain line
  303. if (faux_file_enlarge_buffer(f) < 0) // Make buffer larger
  304. return NULL; // Memory problem
  305. }
  306. // Read new data from file
  307. do {
  308. bytes_readed = read(f->fd, f->buf + f->len, f->buf_size - f->len);
  309. if ((bytes_readed < 0) && (errno != EINTR))
  310. return NULL; // Some file error
  311. } while (bytes_readed < 0); // i.e. EINTR
  312. f->len += bytes_readed;
  313. } while (bytes_readed > 0);
  314. // EOF (here bytes_readed == 0)
  315. f->eof = BOOL_TRUE;
  316. // The last line can be without eol. Consider it as a line too
  317. return faux_file_takeaway_rest(f);
  318. }
  319. /** @brief Read raw line from file.
  320. *
  321. * Raw line is a line with trailing EOL included.
  322. * Actually function searches for line within internal buffer. If line is not
  323. * found then function reads new data from file and searches for the line again.
  324. * The last line in file (without trailing EOL) is considered as line too.
  325. *
  326. * @warning Returned pointer must be freed by faux_str_free() later.
  327. *
  328. * @param [in] f File object.
  329. * @return Line pointer or NULL on error.
  330. */
  331. char *faux_file_getline_raw(faux_file_t *f)
  332. {
  333. return faux_file_getline_internal(f, BOOL_TRUE);
  334. }
  335. /** @brief Read line from file.
  336. *
  337. * Actually function searches for line within internal buffer. If line is not
  338. * found then function reads new data from file and searches for the line again.
  339. * The last line in file (without trailing EOL) is considered as line too.
  340. *
  341. * @warning Returned pointer must be freed by faux_str_free() later.
  342. *
  343. * @param [in] f File object.
  344. * @return Line pointer or NULL on error.
  345. */
  346. char *faux_file_getline(faux_file_t *f)
  347. {
  348. return faux_file_getline_internal(f, BOOL_FALSE);
  349. }
  350. /** @brief Writes data to file.
  351. *
  352. * The system write() can be interrupted by signal or can write less bytes
  353. * than specified. This function will continue to write data until all data
  354. * will be written or error occured.
  355. *
  356. * @param [in] f File object.
  357. * @param [in] buf Buffer to write.
  358. * @param [in] n Number of bytes to write.
  359. * @return Number of bytes written or < 0 on error.
  360. */
  361. ssize_t faux_file_write(faux_file_t *f, const void *buf, size_t n)
  362. {
  363. assert(f);
  364. if (!f)
  365. return -1;
  366. return faux_write(f->fd, buf, n);
  367. }
  368. /** @brief Writes data block to file.
  369. *
  370. * See faux_write_block() for documentation.
  371. *
  372. * @param [in] f File object.
  373. * @param [in] buf Buffer to write.
  374. * @param [in] n Number of bytes to write.
  375. * @return Number of bytes written or < 0 on error.
  376. */
  377. ssize_t faux_file_write_block(faux_file_t *f, const void *buf, size_t n)
  378. {
  379. assert(f);
  380. if (!f)
  381. return -1;
  382. return faux_write_block(f->fd, buf, n);
  383. }
  384. /** @brief Read data from file.
  385. *
  386. * See faux_read() for documentation.
  387. *
  388. * @param [in] f File object.
  389. * @param [in] buf Buffer.
  390. * @param [in] n Number of bytes.
  391. * @return Number of bytes readed or < 0 on error.
  392. */
  393. ssize_t faux_file_read(faux_file_t *f, void *buf, size_t n)
  394. {
  395. assert(f);
  396. if (!f)
  397. return -1;
  398. // TODO: Read buffer first
  399. return faux_read(f->fd, buf, n);
  400. }
  401. /** @brief Read data block from file.
  402. *
  403. * See faux_read_block() for documentation.
  404. *
  405. * @param [in] f File object.
  406. * @param [in] buf Buffer.
  407. * @param [in] n Number of bytes.
  408. * @return Number of bytes readed or < 0 on error.
  409. */
  410. ssize_t faux_file_read_block(faux_file_t *f, void *buf, size_t n)
  411. {
  412. assert(f);
  413. if (!f)
  414. return -1;
  415. // TODO: Read buffer first
  416. return faux_read_block(f->fd, buf, n);
  417. }