MCPcopy Create free account
hub / github.com/apache/trafficserver / load_from_file

Method load_from_file

src/proxy/http/HttpBodyFactory.cc:1089–1156  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1087}
1088
1089int
1090HttpBodyTemplate::load_from_file(char *dir, char *file)
1091{
1092 int fd, status;
1093 int64_t bytes_read;
1094 struct stat stat_buf;
1095 char path[MAXPATHLEN + 1];
1096 char *new_template_buffer;
1097 int64_t new_byte_count;
1098
1099 ////////////////////////////////////
1100 // ensure this is actually a file //
1101 ////////////////////////////////////
1102
1103 snprintf(path, sizeof(path), "%s/%s", dir, file);
1104 // coverity[fs_check_call]
1105 status = stat(path, &stat_buf);
1106 if (status != 0) {
1107 return 0;
1108 }
1109 if (!S_ISREG(stat_buf.st_mode)) {
1110 return 0;
1111 }
1112
1113 ///////////////////
1114 // open the file //
1115 ///////////////////
1116
1117 // coverity[toctou]
1118 fd = open(path, O_RDONLY);
1119 if (fd < 0) {
1120 return 0;
1121 }
1122
1123 ////////////////////////////////////////
1124 // read in the template file contents //
1125 ////////////////////////////////////////
1126
1127 new_byte_count = stat_buf.st_size;
1128 new_template_buffer = static_cast<char *>(ats_malloc(new_byte_count + 1));
1129 bytes_read = read(fd, new_template_buffer, new_byte_count);
1130 new_template_buffer[new_byte_count] = '\0';
1131 close(fd);
1132
1133 ///////////////////////////
1134 // check for read errors //
1135 ///////////////////////////
1136
1137 if (bytes_read != new_byte_count) {
1138 Warning("reading template file '%s', got %" PRId64 " bytes instead of %" PRId64 " (%s)", path, bytes_read, new_byte_count,
1139 (strerror(errno) ? strerror(errno) : "unknown error"));
1140 ats_free(new_template_buffer);
1141 return 0;
1142 }
1143
1144 Dbg(dbg_ctl_body_factory, " read %" PRId64 " bytes from '%s'", new_byte_count, path);
1145
1146 /////////////////////////////////

Callers 2

Calls 4

ats_mallocFunction · 0.85
ats_freeFunction · 0.85
statClass · 0.70
readFunction · 0.50

Tested by

no test coverage detected