file.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 0 - success, < 0 - error
  92. */
  93. int faux_file_close(faux_file_t *f)
  94. {
  95. int fd = -1;
  96. assert(f);
  97. if (!f)
  98. return -1;
  99. fd = f->fd;
  100. faux_free(f->buf);
  101. faux_free(f);
  102. return close(fd);
  103. }
  104. /** @brief Returns file descriptor from file object.
  105. *
  106. * Works like fileno() function for stream objects.
  107. *
  108. * @param [in] f File object.
  109. * @return Linked file descriptor.
  110. */
  111. int faux_file_fileno(faux_file_t *f)
  112. {
  113. assert(f);
  114. if (!f)
  115. return -1;
  116. return f->fd;
  117. }
  118. /** @brief Returns EOF flag.
  119. *
  120. * @param [in] f File object
  121. * @return BOOL_TRUE if it's end of file and BOOL_FALSE else.
  122. */
  123. bool_t faux_file_eof(const faux_file_t *f)
  124. {
  125. assert(f);
  126. if (!f)
  127. return BOOL_FALSE;
  128. return f->eof;
  129. }
  130. /** @brief Service static function to take away data block from internal buffer.
  131. *
  132. * Returns allocated data block in a form of C-string i.e. adds '\0' at the end
  133. * of data. Additionally function can drop some bytes from internal buffer.
  134. * It's usefull when it's necessary to get text string from the buffer and
  135. * drop trailing end of line.
  136. *
  137. * @warning Returned pointer must be freed by faux_str_free() later.
  138. *
  139. * @param [in] f File object.
  140. * @param [in] bytes_get Number of bytes to get from internal buffer.
  141. * @param [in] bytes_drop Number of bytes to drop. Actually
  142. * (bytes_drop + bytes_get) bytes will be removed from internal buffer.
  143. * @return Allocated string (with trailing '\0') with data to get.
  144. */
  145. static char *faux_file_takeaway(faux_file_t *f,
  146. size_t bytes_get, size_t bytes_drop)
  147. {
  148. size_t remove_len = 0;
  149. char *line = NULL;
  150. assert(f);
  151. if (!f)
  152. return NULL;
  153. remove_len = bytes_get + bytes_drop;
  154. // Try to take away more bytes than buffer contain
  155. if ((remove_len > f->len) || (0 == remove_len))
  156. return NULL;
  157. line = faux_zmalloc(bytes_get + 1); // One extra byte for '\0'
  158. assert(line);
  159. if (!line)
  160. return NULL; // Memory problems
  161. memcpy(line, f->buf, bytes_get);
  162. // Remove block from the internal buffer
  163. f->len = f->len - remove_len;
  164. memmove(f->buf, f->buf + remove_len, f->len);
  165. return line;
  166. }
  167. /** @brief Service static function to get all data from buf as single C-string.
  168. *
  169. * Gets all the data from internal buffer as a single C-string (i.e. ends with
  170. * '\0'). This data will be removed from internal buffer.
  171. *
  172. * @warning Returned pointer must be freed by faux_str_free() later.
  173. *
  174. * @param [in] f File object.
  175. * @return Allocated string (with trailing '\0') with data to get.
  176. */
  177. static char *faux_file_takeaway_rest(faux_file_t *f)
  178. {
  179. assert(f);
  180. if (!f)
  181. return NULL;
  182. return faux_file_takeaway(f, f->len, 0);
  183. }
  184. /** @brief Universal static function to get line from buf as single C-string.
  185. *
  186. * Universal function for faux_file_takeaway(), faux_file_takeway_raw()
  187. * implementation.
  188. *
  189. * @warning Returned pointer must be freed by faux_str_free() later.
  190. *
  191. * @param [in] f File object.
  192. * @param [in] raw Boolean flag.
  193. * BOOL_TRUE - include trailing EOL to resulting string,
  194. * BOOL_FALSE - don't include
  195. * @return Allocated string (with trailing '\0') with line.
  196. */
  197. static char *faux_file_takeaway_line_internal(faux_file_t *f, bool_t raw)
  198. {
  199. char *find = NULL;
  200. const char *eol = "\n\r";
  201. size_t line_len = 0;
  202. assert(f);
  203. if (!f)
  204. return NULL;
  205. // Search buffer for EOL
  206. find = faux_str_charsn(f->buf, eol, f->len);
  207. if (!find)
  208. return NULL; // End of line is not found
  209. line_len = find - f->buf;
  210. if (raw) {
  211. // Takeaway line with trailing EOL.
  212. return faux_file_takeaway(f, line_len + 1, 0);
  213. } else {
  214. // Takeaway line without trailing EOL. So drop one last byte
  215. return faux_file_takeaway(f, line_len, 1);
  216. }
  217. }
  218. /** @brief Service static function to get raw line from buf as single C-string.
  219. *
  220. * Gets line (data ends with EOL) from internal buffer as a single C-string
  221. * (i.e. ends with '\0'). The resulting line will contain trailing EOL.
  222. *
  223. * @warning Returned pointer must be freed by faux_str_free() later.
  224. *
  225. * @param [in] f File object.
  226. * @return Allocated string (with trailing '\0') with line.
  227. */
  228. static char *faux_file_takeaway_line_raw(faux_file_t *f)
  229. {
  230. return faux_file_takeaway_line_internal(f, BOOL_TRUE);
  231. }
  232. /** @brief Service static function to get line from buf as single C-string.
  233. *
  234. * Gets line (data ends with EOL) from internal buffer as a single C-string
  235. * (i.e. ends with '\0'). The resulting line will not contain trailing EOL but
  236. * EOL will be removed from internal buffer together with line.
  237. *
  238. * @warning Returned pointer must be freed by faux_str_free() later.
  239. *
  240. * @param [in] f File object.
  241. * @return Allocated string (with trailing '\0') with line.
  242. */
  243. static char *faux_file_takeaway_line(faux_file_t *f)
  244. {
  245. return faux_file_takeaway_line_internal(f, BOOL_FALSE);
  246. }
  247. /** @brief Service static function to enlarge internal buffer.
  248. *
  249. * The initial size of internal buffer is 128 bytes. Each function execution
  250. * enlarges buffer by chunk of 128 bytes.
  251. *
  252. * @param [in] f File objects.
  253. * @return 0 - success, < 0 - error
  254. */
  255. static int faux_file_enlarge_buffer(faux_file_t *f)
  256. {
  257. size_t new_size = 0;
  258. char *new_buf = NULL;
  259. assert(f);
  260. if (!f)
  261. return -1;
  262. new_size = f->buf_size + FAUX_FILE_CHUNK_SIZE;
  263. new_buf = realloc(f->buf, new_size);
  264. assert(new_buf);
  265. if (!new_buf)
  266. return -1;
  267. // NULLify newly allocated memory
  268. faux_bzero(new_buf + f->buf_size, new_size - f->buf_size);
  269. f->buf = new_buf;
  270. f->buf_size = new_size;
  271. return 0;
  272. }
  273. /** @brief Universal static function to read line from file.
  274. *
  275. * Function for implementation faux_file_getline_raw() and faux_file_getline().
  276. *
  277. * @warning Returned pointer must be freed by faux_str_free() later.
  278. *
  279. * @param [in] f File object.
  280. * @param [in] raw
  281. * BOOL_TRUE - raw mode (with trailing EOL)
  282. * BOOL_FALSE - without trailing EOL
  283. * @return Line pointer or NULL on error.
  284. */
  285. char *faux_file_getline_internal(faux_file_t *f, bool_t raw)
  286. {
  287. ssize_t bytes_readed = 0;
  288. assert(f);
  289. if (!f)
  290. return NULL;
  291. do {
  292. char *find = NULL;
  293. // May be buffer already contain line
  294. if (raw) { // raw mode
  295. find = faux_file_takeaway_line_raw(f);
  296. } else { // without trailing EOL
  297. find = faux_file_takeaway_line(f);
  298. }
  299. if (find)
  300. return find;
  301. if (f->buf_size == f->len) { // Buffer is full but doesn't contain line
  302. if (faux_file_enlarge_buffer(f) < 0) // Make buffer larger
  303. return NULL; // Memory problem
  304. }
  305. // Read new data from file
  306. do {
  307. bytes_readed = read(f->fd, f->buf + f->len, f->buf_size - f->len);
  308. if ((bytes_readed < 0) && (errno != EINTR))
  309. return NULL; // Some file error
  310. } while (bytes_readed < 0); // i.e. EINTR
  311. f->len += bytes_readed;
  312. } while (bytes_readed > 0);
  313. // EOF (here bytes_readed == 0)
  314. f->eof = BOOL_TRUE;
  315. // The last line can be without eol. Consider it as a line too
  316. return faux_file_takeaway_rest(f);
  317. }
  318. /** @brief Read raw line from file.
  319. *
  320. * Raw line is a line with trailing EOL included.
  321. * Actually function searches for line within internal buffer. If line is not
  322. * found then function reads new data from file and searches for the line again.
  323. * The last line in file (without trailing EOL) is considered as line too.
  324. *
  325. * @warning Returned pointer must be freed by faux_str_free() later.
  326. *
  327. * @param [in] f File object.
  328. * @return Line pointer or NULL on error.
  329. */
  330. char *faux_file_getline_raw(faux_file_t *f)
  331. {
  332. return faux_file_getline_internal(f, BOOL_TRUE);
  333. }
  334. /** @brief Read line from file.
  335. *
  336. * Actually function searches for line within internal buffer. If line is not
  337. * found then function reads new data from file and searches for the line again.
  338. * The last line in file (without trailing EOL) is considered as line too.
  339. *
  340. * @warning Returned pointer must be freed by faux_str_free() later.
  341. *
  342. * @param [in] f File object.
  343. * @return Line pointer or NULL on error.
  344. */
  345. char *faux_file_getline(faux_file_t *f)
  346. {
  347. return faux_file_getline_internal(f, BOOL_FALSE);
  348. }
  349. /** @brief Writes data to file.
  350. *
  351. * The system write() can be interrupted by signal or can write less bytes
  352. * than specified. This function will continue to write data until all data
  353. * will be written or error occured.
  354. *
  355. * @param [in] f File object.
  356. * @param [in] buf Buffer to write.
  357. * @param [in] n Number of bytes to write.
  358. * @return Number of bytes written or < 0 on error.
  359. */
  360. ssize_t faux_file_write(faux_file_t *f, const void *buf, size_t n)
  361. {
  362. assert(f);
  363. if (!f)
  364. return -1;
  365. return faux_write(f->fd, buf, n);
  366. }
  367. /** @brief Writes data block to file.
  368. *
  369. * See faux_write_block() for documentation.
  370. *
  371. * @param [in] f File object.
  372. * @param [in] buf Buffer to write.
  373. * @param [in] n Number of bytes to write.
  374. * @return Number of bytes written or < 0 on error.
  375. */
  376. ssize_t faux_file_write_block(faux_file_t *f, const void *buf, size_t n)
  377. {
  378. assert(f);
  379. if (!f)
  380. return -1;
  381. return faux_write_block(f->fd, buf, n);
  382. }
  383. /** @brief Read data from file.
  384. *
  385. * See faux_read() for documentation.
  386. *
  387. * @param [in] f File object.
  388. * @param [in] buf Buffer.
  389. * @param [in] n Number of bytes.
  390. * @return Number of bytes readed or < 0 on error.
  391. */
  392. ssize_t faux_file_read(faux_file_t *f, void *buf, size_t n)
  393. {
  394. assert(f);
  395. if (!f)
  396. return -1;
  397. // TODO: Read buffer first
  398. return faux_read(f->fd, buf, n);
  399. }
  400. /** @brief Read data block from file.
  401. *
  402. * See faux_read_block() for documentation.
  403. *
  404. * @param [in] f File object.
  405. * @param [in] buf Buffer.
  406. * @param [in] n Number of bytes.
  407. * @return Number of bytes readed or < 0 on error.
  408. */
  409. ssize_t faux_file_read_block(faux_file_t *f, void *buf, size_t n)
  410. {
  411. assert(f);
  412. if (!f)
  413. return -1;
  414. // TODO: Read buffer first
  415. return faux_read_block(f->fd, buf, n);
  416. }