Coverage Report

Created: 2025-08-26 06:34

/src/jq/src/jv_file.c
Line
Count
Source (jump to first uncovered line)
1
#include <sys/stat.h>
2
3
#include <errno.h>
4
#include <fcntl.h>
5
#include <stdio.h>
6
#include <stdlib.h>
7
#include <string.h>
8
#include <unistd.h>
9
#include "jv.h"
10
#include "jv_unicode.h"
11
12
6.40k
jv jv_load_file(const char* filename, int raw) {
13
6.40k
  struct stat sb;
14
6.40k
  int fd = open(filename, O_RDONLY);
15
6.40k
  if (fd == -1) {
16
0
    return jv_invalid_with_msg(jv_string_fmt("Could not open %s: %s",
17
0
                                             filename,
18
0
                                             strerror(errno)));
19
0
  }
20
6.40k
  if (fstat(fd, &sb) == -1 || S_ISDIR(sb.st_mode)) {
21
0
    close(fd);
22
0
    return jv_invalid_with_msg(jv_string_fmt("Could not open %s: %s",
23
0
                                             filename,
24
0
                                             "It's a directory"));
25
0
  }
26
6.40k
  FILE* file = fdopen(fd, "r");
27
6.40k
  struct jv_parser* parser = NULL;
28
6.40k
  jv data;
29
6.40k
  if (!file) {
30
0
    close(fd);
31
0
    return jv_invalid_with_msg(jv_string_fmt("Could not open %s: %s",
32
0
                                             filename,
33
0
                                             strerror(errno)));
34
0
  }
35
6.40k
  if (raw) {
36
3.20k
    data = jv_string("");
37
3.20k
  } else {
38
3.20k
    data = jv_array();
39
3.20k
    parser = jv_parser_new(0);
40
3.20k
  }
41
42
  // To avoid mangling UTF-8 multi-byte sequences that cross the end of our read
43
  // buffer, we need to be able to read the remainder of a sequence and add that
44
  // before appending.
45
6.40k
  const int max_utf8_len = 4;
46
6.40k
  char buf[4096+max_utf8_len];
47
42.7k
  while (!feof(file) && !ferror(file)) {
48
37.9k
    size_t n = fread(buf, 1, sizeof(buf)-max_utf8_len, file);
49
37.9k
    int len = 0;
50
51
37.9k
    if (n == 0)
52
45
      continue;
53
37.8k
    if (jvp_utf8_backtrack(buf+(n-1), buf, &len) && len > 0 &&
54
37.8k
        !feof(file) && !ferror(file)) {
55
1.42k
      n += fread(buf+n, 1, len, file);
56
1.42k
    }
57
58
37.8k
    if (raw) {
59
19.9k
      data = jv_string_append_buf(data, buf, n);
60
19.9k
    } else {
61
17.8k
      jv_parser_set_buf(parser, buf, n, !feof(file));
62
17.8k
      jv value;
63
3.05M
      while (jv_is_valid((value = jv_parser_next(parser))))
64
3.03M
        data = jv_array_append(data, value);
65
17.8k
      if (jv_invalid_has_msg(jv_copy(value))) {
66
1.60k
        jv_free(data);
67
1.60k
        data = value;
68
1.60k
        break;
69
1.60k
      }
70
17.8k
    }
71
37.8k
  }
72
6.40k
  if (!raw)
73
3.20k
      jv_parser_free(parser);
74
6.40k
  int badread = ferror(file);
75
6.40k
  if (fclose(file) != 0 || badread) {
76
0
    jv_free(data);
77
0
    return jv_invalid_with_msg(jv_string_fmt("Error reading from %s",
78
0
                                             filename));
79
0
  }
80
6.40k
  return data;
81
6.40k
}