file.c 12 KB

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