bakaid commented on a change in pull request #604: MINIFICPP-926 Create nanofi 
tailfile processor for tailing file by configurable chunk size
URL: https://github.com/apache/nifi-minifi-cpp/pull/604#discussion_r300648363
 
 

 ##########
 File path: nanofi/src/core/file_utils.c
 ##########
 @@ -0,0 +1,171 @@
+/**
+ *
+ * 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.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <dirent.h>
+
+#include "api/nanofi.h"
+#include "api/ecu.h"
+#include "core/string_utils.h"
+#include "core/file_utils.h"
+
+flow_file_info log_aggregate(const char * file_path, char delim, int 
curr_offset, nifi_instance * instance, standalone_processor * proc) {
+    flow_file_info ff_info;
+    memset(&ff_info, 0, sizeof(ff_info));
+
+    if (!file_path) {
+        return ff_info;
+    }
+
+    char buff[MAX_BYTES_READ + 1];
+    memset(buff, 0, MAX_BYTES_READ+1);
+    errno = 0;
+    FILE * fp = fopen(file_path, "rb");
+    if (!fp) {
+        printf("Cannot open file: {file: %s, reason: %s}\n", file_path, 
strerror(errno));
+        return ff_info;
+    }
+    fseek(fp, curr_offset, SEEK_SET);
+
+    flow_file_list ffl;
+    memset(&ffl, 0, sizeof(ffl));
+    int bytes_read = 0;
+    while ((bytes_read = fread(buff, 1, MAX_BYTES_READ, fp)) > 0) {
+        buff[bytes_read] = '\0';
+        struct token_list tokens = tokenize_string_tailfile(buff, delim);
+        if (tokens.total_bytes > 0) {
+            curr_offset += tokens.total_bytes;
+            fseek(fp, curr_offset, SEEK_SET);
+        }
+        memset(buff, 0, MAX_BYTES_READ+1);
+
+        token_node * head;
+        for (head = tokens.head; head && head->data; head = head->next) {
+            flow_file_record * ffr = write_to_flow_file(head->data, instance, 
proc);
+            char offset[256] = {0};
+            snprintf(offset, sizeof(offset), "%d", curr_offset);
+            add_attribute(ffr, "file offset", offset, strlen(offset));
+            add_flow_file_record(&ffl, ffr);
+        }
+        ff_info.total_bytes += tokens.total_bytes;
+        free_all_tokens(&tokens);
+    }
+    fclose(fp);
+    ff_info.ff_list = ffl;
+    return ff_info;
+}
+
+int is_directory(const char * path) {
+    struct stat dir_stat;
+    if (stat(path, &dir_stat) < 0) {
+        return 0;
+    }
+    return S_ISDIR(dir_stat.st_mode);
+}
+
+const char * concat_path(const char * parent, const char * child) {
+    char * path = (char *)calloc(strlen(parent) + strlen(child) + 1, 
sizeof(char));
+    strcpy(path, parent);
+    strcat(path, "/");
+    strcat(path, child);
+    return path;
+}
+
+void remove_directory(const char * dir_path) {
+
+    if (!is_directory(dir_path)) {
+        if (unlink(dir_path) == -1) {
+            printf("Could not remove file %s\n", dir_path);
+        }
+        return;
+    }
+
+    uint64_t path_len = strlen(dir_path);
+    struct dirent * dir;
+    DIR * d = opendir(dir_path);
+
+    while ((dir = readdir(d)) != NULL) {
+        char * entry_name = dir->d_name;
+        if (!strcmp(entry_name, ".") || !strcmp(entry_name, "..")) {
+            continue;
+        }
+        const char * path = concat_path(dir_path, entry_name);
+        remove_directory(path);
+    }
+
+    rmdir(dir_path);
+    closedir(d);
+}
+
+size_t file_size(const char * path) {
+    if (!path) return 0;
+
+    FILE * fp = fopen(path, "rb");
+    if (!fp) return 0;
+    int result = fseek(fp, 0, SEEK_END);
+    if (result != 0) {
+        fclose(fp);
+        return 0;
 
 Review comment:
   I think with the current usage it poses no problem, but an error condition 
being equal to a valid empty file can be misleading in the future.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to