MCPcopy Create free account
hub / github.com/coreutils/coreutils / ofile_open

Function ofile_open

src/split.c:1135–1206  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1133 If so, it's probably best to close each file when finished with it. */
1134
1135static bool
1136ofile_open (of_t *files, idx_t i_check, idx_t nfiles)
1137{
1138 bool file_limit = false;
1139
1140 if (files[i_check].ofd <= OFD_NEW)
1141 {
1142 int fd;
1143 idx_t i_reopen = i_check ? i_check - 1 : nfiles - 1;
1144
1145 /* Another process could have opened a file in between the calls to
1146 close and open, so we should keep trying until open succeeds or
1147 we've closed all of our files. */
1148 while (true)
1149 {
1150 if (files[i_check].ofd == OFD_NEW)
1151 fd = create (files[i_check].of_name);
1152 else /* OFD_APPEND */
1153 {
1154 /* Attempt to append to previously opened file.
1155 We use O_NONBLOCK to support writing to fifos,
1156 where the other end has closed because of our
1157 previous close. In that case we'll immediately
1158 get an error, rather than waiting indefinitely.
1159 In specialized cases the consumer can keep reading
1160 from the fifo, terminating on conditions in the data
1161 itself, or perhaps never in the case of 'tail -f'.
1162 I.e., for fifos it is valid to attempt this reopen.
1163
1164 We don't handle the filter_command case here, as create()
1165 will exit if there are not enough files in that case.
1166 I.e., we don't support restarting filters, as that would
1167 put too much burden on users specifying --filter commands. */
1168 fd = open (files[i_check].of_name,
1169 O_WRONLY | O_BINARY | O_APPEND | O_NONBLOCK);
1170 }
1171
1172 if (0 <= fd)
1173 break;
1174
1175 if (!(errno == EMFILE || errno == ENFILE))
1176 error (EXIT_FAILURE, errno, "%s", quotef (files[i_check].of_name));
1177
1178 file_limit = true;
1179
1180 /* Search backwards for an open file to close. */
1181 while (files[i_reopen].ofd < 0)
1182 {
1183 i_reopen = i_reopen ? i_reopen - 1 : nfiles - 1;
1184 /* No more open files to close, exit with E[NM]FILE. */
1185 if (i_reopen == i_check)
1186 error (EXIT_FAILURE, errno, "%s",
1187 quotef (files[i_check].of_name));
1188 }
1189
1190 if (fclose (files[i_reopen].ofile) != 0)
1191 error (EXIT_FAILURE, errno, "%s", quotef (files[i_reopen].of_name));
1192 files[i_reopen].ofile = NULL;

Callers 1

lines_rrFunction · 0.85

Calls 1

createFunction · 0.85

Tested by

no test coverage detected