file.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. struct stat stat_struct = {};
  38. faux_file_t *f = NULL;
  39. // Before object creation check is fd valid.
  40. // Try to get stat().
  41. if (fstat(fd, &stat_struct) < 0)
  42. return NULL; // Illegal fd
  43. f = faux_zmalloc(sizeof(*f));
  44. assert(f);
  45. if (!f)
  46. return NULL;
  47. // Init
  48. f->fd = fd;
  49. f->buf_size = FAUX_FILE_CHUNK_SIZE;
  50. f->buf = faux_zmalloc(f->buf_size);
  51. assert(f->buf);
  52. if (!f->buf) {
  53. faux_free(f);
  54. return NULL;
  55. }
  56. f->len = 0;
  57. f->eof = BOOL_FALSE;
  58. return f;
  59. }
  60. /** @brief Create file object and open correspondent file.
  61. *
  62. * Function opens specified file using flags and create file object based
  63. * on this opened file. The object must be freed and file must be closed
  64. * later by the faux_file_close().
  65. *
  66. * @warning The faux_file_close() must be executed later to free file object.
  67. *
  68. * @param [in] pathname File name.
  69. * @param [in] flags Flags to open file (like O_RDONLY etc).
  70. * @param [in] mode File permissions if file will be created.
  71. * @return File object or NULL on error.
  72. */
  73. faux_file_t *faux_file_open(const char *pathname, int flags, mode_t mode) {
  74. int fd = -1;
  75. assert(pathname);
  76. if (!pathname)
  77. return NULL;
  78. fd = open(pathname, flags, mode);
  79. if (fd < 0)
  80. return NULL;
  81. return faux_file_fdopen(fd);
  82. }
  83. /** @brief Closes file and frees file object.
  84. *
  85. * Function closes previously opened (by faux_file_open() or faux_file_fdopen())
  86. * file and frees file object structures.
  87. *
  88. * @param [in] f File object to close and free.
  89. * @return 0 - success, < 0 - error
  90. */
  91. int faux_file_close(faux_file_t *f) {
  92. int fd = -1;
  93. assert(f);
  94. if (!f)
  95. return -1;
  96. fd = f->fd;
  97. faux_free(f->buf);
  98. faux_free(f);
  99. return close(fd);
  100. }
  101. /** @brief Returns file descriptor from file object.
  102. *
  103. * Works like fileno() function for stream objects.
  104. *
  105. * @param [in] f File object.
  106. * @return Linked file descriptor.
  107. */
  108. int faux_file_fileno(faux_file_t *f) {
  109. assert(f);
  110. if (!f)
  111. return -1;
  112. return f->fd;
  113. }
  114. /** @brief Returns EOF flag.
  115. *
  116. * @param [in] f File object
  117. * @return BOOL_TRUE if it's end of file and BOOL_FALSE else.
  118. */
  119. bool_t faux_file_eof(const faux_file_t *f) {
  120. assert(f);
  121. if (!f)
  122. return BOOL_FALSE;
  123. return f->eof;
  124. }
  125. /** @brief Service static function to take away data block from internal buffer.
  126. *
  127. * Returns allocated data block in a form of C-string i.e. adds '\0' at the end
  128. * of data. Additionally function can drop some bytes from internal buffer.
  129. * It's usefull when it's necessary to get text string from the buffer and
  130. * drop trailing end of line.
  131. *
  132. * @warning Returned pointer must be freed by faux_str_free() later.
  133. *
  134. * @param [in] f File object.
  135. * @param [in] bytes_get Number of bytes to get from internal buffer.
  136. * @param [in] bytes_drop Number of bytes to drop. Actually
  137. * (bytes_drop + bytes_get) bytes will be removed from internal buffer.
  138. * @return Allocated string (with trailing '\0') with data to get.
  139. */
  140. static char *faux_file_takeaway(faux_file_t *f,
  141. size_t bytes_get, size_t bytes_drop) {
  142. size_t remove_len = 0;
  143. char *line = NULL;
  144. assert(f);
  145. if (!f)
  146. return NULL;
  147. remove_len = bytes_get + bytes_drop;
  148. // Try to take away more bytes than buffer contain
  149. if ((remove_len > f->len) || (0 == remove_len))
  150. return NULL;
  151. line = faux_zmalloc(bytes_get + 1); // One extra byte for '\0'
  152. assert(line);
  153. if (!line)
  154. return NULL; // Memory problems
  155. memcpy(line, f->buf, bytes_get);
  156. // Remove block from the internal buffer
  157. f->len = f->len - remove_len;
  158. memmove(f->buf, f->buf + remove_len, f->len);
  159. return line;
  160. }
  161. /** @brief Service static function to get all data from buf as single C-string.
  162. *
  163. * Gets all the data from internal buffer as a single C-string (i.e. ends with
  164. * '\0'). This data will be removed from internal buffer.
  165. *
  166. * @warning Returned pointer must be freed by faux_str_free() later.
  167. *
  168. * @param [in] f File object.
  169. * @return Allocated string (with trailing '\0') with data to get.
  170. */
  171. static char *faux_file_takeaway_rest(faux_file_t *f) {
  172. assert(f);
  173. if (!f)
  174. return NULL;
  175. return faux_file_takeaway(f, f->len, 0);
  176. }
  177. /** @brief Service static function to get line from buf as single C-string.
  178. *
  179. * Gets line (data ends with EOL) from internal buffer as a single C-string
  180. * (i.e. ends with '\0'). The resulting line will not contain trailing EOL but
  181. * EOL will be removed from internal buffer together with line.
  182. *
  183. * @warning Returned pointer must be freed by faux_str_free() later.
  184. *
  185. * @param [in] f File object.
  186. * @return Allocated string (with trailing '\0') with line.
  187. */
  188. static char *faux_file_takeaway_line(faux_file_t *f) {
  189. char *find = NULL;
  190. const char *eol = "\n\r";
  191. size_t line_len = 0;
  192. assert(f);
  193. if (!f)
  194. return NULL;
  195. // Search buffer for EOL
  196. find = faux_str_charsn(f->buf, eol, f->len);
  197. if (!find)
  198. return NULL; // End of line is not found
  199. line_len = find - f->buf;
  200. // Takeaway line without trailing EOL. So drop one last byte
  201. return faux_file_takeaway(f, line_len, 1);
  202. }
  203. /** @brief Service static function to enlarge internal buffer.
  204. *
  205. * The initial size of internal buffer is 128 bytes. Each function execution
  206. * enlarges buffer by chunk of 128 bytes.
  207. *
  208. * @param [in] f File objects.
  209. * @return 0 - success, < 0 - error
  210. */
  211. static int faux_file_enlarge_buffer(faux_file_t *f) {
  212. size_t new_size = 0;
  213. char *new_buf = NULL;
  214. assert(f);
  215. if (!f)
  216. return -1;
  217. new_size = f->buf_size + FAUX_FILE_CHUNK_SIZE;
  218. new_buf = realloc(f->buf, new_size);
  219. assert(new_buf);
  220. if (!new_buf)
  221. return -1;
  222. // NULLify newly allocated memory
  223. faux_bzero(new_buf + f->buf_size, new_size - f->buf_size);
  224. f->buf = new_buf;
  225. f->buf_size = new_size;
  226. return 0;
  227. }
  228. /** @brief Read line from file.
  229. *
  230. * Actually function searches for line within internal buffer. If line is not
  231. * found then function reads new data from file and searches for the line again.
  232. * The last line in file (without trailing EOL) is considered as line too.
  233. *
  234. * @warning Returned pointer must be freed by faux_str_free() later.
  235. *
  236. * @param [in] f File object.
  237. * @return Line pointer or NULL on error.
  238. */
  239. char *faux_file_getline(faux_file_t *f) {
  240. ssize_t bytes_readed = 0;
  241. assert(f);
  242. if (!f)
  243. return NULL;
  244. do {
  245. char *find = NULL;
  246. // May be buffer already contain line
  247. find = faux_file_takeaway_line(f);
  248. if (find)
  249. return find;
  250. if (f->buf_size == f->len) { // Buffer is full but doesn't contain line
  251. if (faux_file_enlarge_buffer(f) < 0) // Make buffer larger
  252. return NULL; // Memory problem
  253. }
  254. // Read new data from file
  255. do {
  256. bytes_readed = read(f->fd, f->buf + f->len, f->buf_size - f->len);
  257. if ((bytes_readed < 0) && (errno != EINTR))
  258. return NULL; // Some file error
  259. } while (bytes_readed < 0); // i.e. EINTR
  260. f->len += bytes_readed;
  261. } while (bytes_readed > 0);
  262. // EOF (here bytes_readed == 0)
  263. f->eof = BOOL_TRUE;
  264. // The last line can be without eol. Consider it as a line too
  265. return faux_file_takeaway_rest(f);
  266. }
  267. /** @brief Writes data to file.
  268. *
  269. * The system write() can be interrupted by signal or can write less bytes
  270. * than specified. This function will continue to write data until all data
  271. * will be written or error occured.
  272. *
  273. * @param [in] f File object.
  274. * @param [in] buf Buffer to write.
  275. * @param [in] n Number of bytes to write.
  276. * @return Number of bytes written or NULL on error.
  277. */
  278. ssize_t faux_file_write(faux_file_t *f, const void *buf, size_t n) {
  279. ssize_t bytes_written = 0;
  280. size_t left = n;
  281. const void *data = buf;
  282. assert(f);
  283. assert(buf);
  284. if (!f || !buf)
  285. return -1;
  286. if (0 == n)
  287. return 0;
  288. do {
  289. bytes_written = write(f->fd, data, left);
  290. if (bytes_written < 0) {
  291. if (EINTR == errno)
  292. continue;
  293. return -1;
  294. }
  295. if (0 == bytes_written) // Insufficient space
  296. return -1;
  297. data += bytes_written;
  298. left = left - bytes_written;
  299. } while (left > 0);
  300. return n;
  301. }