file.c 13 KB

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