Ethanlm commented on a change in pull request #3366: URL: https://github.com/apache/storm/pull/3366#discussion_r669080574
########## File path: storm-core/src/native/worker-launcher/impl/utils/file-utils.c ########## @@ -0,0 +1,182 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define FILE_BUFFER_INCREMENT (128*1024) + +#include <sys/types.h> +#include <sys/stat.h> +#include <errno.h> +#include <fcntl.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "worker-launcher.h" +#include "file-utils.h" + +/** + * Read the contents of the specified file into an allocated buffer and return + * the contents as a NUL-terminated string. NOTE: The file contents must not + * contain a NUL character or the result will appear to be truncated. + * + * Returns a pointer to the allocated, NUL-terminated string or NULL on error. + */ +char* read_file_to_string(const char* filename) { + char* buff = NULL; + int rc = -1; + int fd = open(filename, O_RDONLY); + if (fd < 0) { + fprintf(ERRORFILE, "Error opening %s : %s\n", filename, strerror(errno)); + goto cleanup; + } + + struct stat filestat; + if (fstat(fd, &filestat) != 0) { + fprintf(ERRORFILE, "Error examining %s : %s\n", filename, strerror(errno)); + goto cleanup; + } + + size_t buff_size = FILE_BUFFER_INCREMENT; + if (S_ISREG(filestat.st_mode)) { + buff_size = filestat.st_size + 1; // +1 for terminating NUL + } + buff = malloc(buff_size); + if (buff == NULL) { + fprintf(ERRORFILE, "Unable to allocate %ld bytes\n", buff_size); + goto cleanup; + } + + int bytes_left = buff_size; + char* cp = buff; + int bytes_read; + while ((bytes_read = read(fd, cp, bytes_left)) > 0) { + cp += bytes_read; + bytes_left -= bytes_read; + if (bytes_left == 0) { + buff_size += FILE_BUFFER_INCREMENT; + bytes_left += FILE_BUFFER_INCREMENT; + buff = realloc(buff, buff_size); + if (buff == NULL) { + fprintf(ERRORFILE, "Unable to allocate %ld bytes\n", buff_size); + goto cleanup; + } + } + } + if (bytes_left < 0) { Review comment: I think this is not needed as `bytes_left` will always be not less than `bytes_read`. will remove -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@storm.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org