file.c 13 KB

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