szaszm commented on a change in pull request #772:
URL: https://github.com/apache/nifi-minifi-cpp/pull/772#discussion_r420232627



##########
File path: nanofi/src/core/file_utils.c
##########
@@ -28,114 +28,222 @@
 #include "core/string_utils.h"
 #include "core/file_utils.h"
 
-#ifdef _MSC_VER
+#ifdef WIN32
 #ifndef PATH_MAX
 #define PATH_MAX 260
 #endif
 #endif
 
+#ifdef WIN32
+#define stat _stat
+#define mkdir _mkdir
+#endif
+
 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);
+  struct stat dir_stat;
+  if (stat(path, &dir_stat) < 0) {
+    return -1;
+  }
+#ifdef WIN32
+  return dir_stat.st_mode & S_IFDIR;
+#else
+  return S_ISDIR(dir_stat.st_mode);
+#endif
 }
 
-const char * get_separator(int force_posix)
-{
+const char * get_separator(int force_posix) {
 #ifdef WIN32
-    if (!force_posix) {
-        return "\\";
-    }
+  if (!force_posix) {
+    return "\\";
+  }
 #endif
-    return "/";
+  return "/";
 }
 
 char * concat_path(const char * parent, const char * child) {
-    char * path = (char *)malloc((strlen(parent) + strlen(child) + 2) * 
sizeof(char));
-    strcpy(path, parent);
-    const char * sep = get_separator(0);
-    strcat(path, sep);
-    strcat(path, child);
-    return path;
+  char * path = (char *) malloc((strlen(parent) + strlen(child) + 2) * 
sizeof(char));
+  strcpy(path, parent);
+  const char * sep = get_separator(0);
+  strcat(path, sep);
+  strcat(path, child);
+  return path;
 }
 
+#ifndef WIN32
 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;
+  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;
-        }
-        char * path = concat_path(dir_path, entry_name);
-        remove_directory(path);
-        free(path);
+  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;
     }
+    char * path = concat_path(dir_path, entry_name);
+    remove_directory(path);
+    free(path);
+  }
+  rmdir(dir_path);
+
+  closedir(d);
+}
+#else
+void remove_directory(const char * dir_path) {
+  HANDLE hFind;
+  WIN32_FIND_DATA fd;
 
-    rmdir(dir_path);
-    closedir(d);
+  hFind = FindFirstFile(dir_path, &fd);
+  if (hFind == INVALID_HANDLE_VALUE) {
+    return;
+  }
+
+  if (fd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) {
+    DeleteFile(dir_path);
+    FindClose(hFind);
+    return;
+  }
+
+  char * path = concat_path(dir_path, "*");
+  HANDLE hFind1;
+  if ((hFind1 = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
+    do {
+      char * entry_name = fd.cFileName;
+      if (!strcmp(entry_name, ".") || !strcmp(entry_name, "..")) continue;
+      char * entry_path = concat_path(dir_path, entry_name);
+      remove_directory(entry_path);
+      free(entry_path);
+    }while (FindNextFile(hFind1, &fd));
+  }
+  RemoveDirectory(dir_path);
+  FindClose(hFind);
+  FindClose(hFind1);
+  free(path);
 }
+#endif
 
 int make_dir(const char * path) {
-    if (!path) return -1;
+  if (!path)
+    return -1;
 
-    errno = 0;
-    int ret = mkdir(path, 
S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
-    if (ret == 0) {
-        return 0;
-    }
+  errno = 0;
+#ifdef WIN32
+  int ret = mkdir(path);
+  char path_sep = '\\';
+#else
+  int ret = mkdir(path,
+  S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
+  char path_sep = '/';
+#endif

Review comment:
       `path_sep` could be initialized via `get_separator` to avoid duplicated 
logic. (Although it's quite unlikely that path separators are going to change 
anytime soon.)
   Also, if the second argument of unix `mkdir` is wrapped to a new line, it 
should use continuation indentation.
   
   ```suggestion
   #ifdef WIN32
     const int ret = mkdir(path);
   #else
     const int ret = mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | 
S_IXGRP | S_IROTH | S_IXOTH);
   #endif /* WIN32 */
     const char path_sep = get_separator(0)[0];
   ```

##########
File path: nanofi/src/core/file_utils.c
##########
@@ -28,114 +28,222 @@
 #include "core/string_utils.h"
 #include "core/file_utils.h"
 
-#ifdef _MSC_VER
+#ifdef WIN32
 #ifndef PATH_MAX
 #define PATH_MAX 260
 #endif
 #endif
 
+#ifdef WIN32
+#define stat _stat
+#define mkdir _mkdir
+#endif
+
 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);
+  struct stat dir_stat;
+  if (stat(path, &dir_stat) < 0) {
+    return -1;
+  }
+#ifdef WIN32
+  return dir_stat.st_mode & S_IFDIR;
+#else
+  return S_ISDIR(dir_stat.st_mode);
+#endif
 }
 
-const char * get_separator(int force_posix)
-{
+const char * get_separator(int force_posix) {
 #ifdef WIN32
-    if (!force_posix) {
-        return "\\";
-    }
+  if (!force_posix) {
+    return "\\";
+  }
 #endif
-    return "/";
+  return "/";
 }
 
 char * concat_path(const char * parent, const char * child) {
-    char * path = (char *)malloc((strlen(parent) + strlen(child) + 2) * 
sizeof(char));
-    strcpy(path, parent);
-    const char * sep = get_separator(0);
-    strcat(path, sep);
-    strcat(path, child);
-    return path;
+  char * path = (char *) malloc((strlen(parent) + strlen(child) + 2) * 
sizeof(char));
+  strcpy(path, parent);
+  const char * sep = get_separator(0);
+  strcat(path, sep);
+  strcat(path, child);
+  return path;
 }
 
+#ifndef WIN32
 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;
+  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;
-        }
-        char * path = concat_path(dir_path, entry_name);
-        remove_directory(path);
-        free(path);
+  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;
     }
+    char * path = concat_path(dir_path, entry_name);
+    remove_directory(path);
+    free(path);
+  }
+  rmdir(dir_path);
+
+  closedir(d);
+}
+#else
+void remove_directory(const char * dir_path) {
+  HANDLE hFind;
+  WIN32_FIND_DATA fd;
 
-    rmdir(dir_path);
-    closedir(d);
+  hFind = FindFirstFile(dir_path, &fd);
+  if (hFind == INVALID_HANDLE_VALUE) {
+    return;
+  }
+
+  if (fd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) {
+    DeleteFile(dir_path);
+    FindClose(hFind);
+    return;
+  }
+
+  char * path = concat_path(dir_path, "*");
+  HANDLE hFind1;
+  if ((hFind1 = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
+    do {
+      char * entry_name = fd.cFileName;
+      if (!strcmp(entry_name, ".") || !strcmp(entry_name, "..")) continue;
+      char * entry_path = concat_path(dir_path, entry_name);
+      remove_directory(entry_path);
+      free(entry_path);
+    }while (FindNextFile(hFind1, &fd));
+  }
+  RemoveDirectory(dir_path);
+  FindClose(hFind);
+  FindClose(hFind1);
+  free(path);
 }
+#endif
 
 int make_dir(const char * path) {
-    if (!path) return -1;
+  if (!path)
+    return -1;
 
-    errno = 0;
-    int ret = mkdir(path, 
S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
-    if (ret == 0) {
-        return 0;
-    }
+  errno = 0;
+#ifdef WIN32
+  int ret = mkdir(path);
+  char path_sep = '\\';
+#else
+  int ret = mkdir(path,
+  S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
+  char path_sep = '/';
+#endif
+  if (ret == 0) {
+    return 0;
+  }
 
-    switch (errno) {
-    case ENOENT: {
-        char * found = strrchr(path, '/');
-        if (!found) {
-            return -1;
-        }
-        int len = found - path;
-        char * dir = calloc(len + 1, sizeof(char));
-        strncpy(dir, path, len);
-        dir[len] = '\0';
-        int res = make_dir(dir);
-        free(dir);
-        if (res < 0) {
-            return -1;
-        }
-        return mkdir(path, 
S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
+  switch (errno) {
+  case ENOENT: {
+    char * found = strrchr(path, path_sep);
+    if (!found) {
+      return -1;
     }
-    case EEXIST: {
-        if (is_directory(path)) {
-            return 0;
-        }
-        return -1;
+    int len = found - path;
+    char * dir = calloc(len + 1, sizeof(char));
+    strncpy(dir, path, len);
+    dir[len] = '\0';

Review comment:
       Nothing important, just a few small things here:
   - `sizeof(char)` is defined to be 1 by the standard (i.e. on all platforms)
   - `calloc` initializes the allocated storage to zero, so the last line is 
redundant. Alternatively, `(char*)malloc(len+1)` could be used, leaving the 
last line intact.
   - While implicit `void*` to other pointer conversion is valid C (but not 
valid C++), I think it's good practice to not rely on this and add an explicit 
cast to the appropriate type, as done in every other allocation in the file.
   ```suggestion
       const ptrdiff_t len = found - path;
       char * const dir = (char*)malloc(len + 1);
       strncpy(dir, path, len);
       dir[len] = '\0';
   ```

##########
File path: nanofi/src/core/file_utils.c
##########
@@ -28,114 +28,222 @@
 #include "core/string_utils.h"
 #include "core/file_utils.h"
 
-#ifdef _MSC_VER
+#ifdef WIN32
 #ifndef PATH_MAX
 #define PATH_MAX 260
 #endif
 #endif
 
+#ifdef WIN32
+#define stat _stat
+#define mkdir _mkdir
+#endif
+
 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);
+  struct stat dir_stat;
+  if (stat(path, &dir_stat) < 0) {
+    return -1;
+  }
+#ifdef WIN32
+  return dir_stat.st_mode & S_IFDIR;
+#else
+  return S_ISDIR(dir_stat.st_mode);
+#endif
 }
 
-const char * get_separator(int force_posix)
-{
+const char * get_separator(int force_posix) {
 #ifdef WIN32
-    if (!force_posix) {
-        return "\\";
-    }
+  if (!force_posix) {
+    return "\\";
+  }
 #endif
-    return "/";
+  return "/";
 }
 
 char * concat_path(const char * parent, const char * child) {
-    char * path = (char *)malloc((strlen(parent) + strlen(child) + 2) * 
sizeof(char));
-    strcpy(path, parent);
-    const char * sep = get_separator(0);
-    strcat(path, sep);
-    strcat(path, child);
-    return path;
+  char * path = (char *) malloc((strlen(parent) + strlen(child) + 2) * 
sizeof(char));
+  strcpy(path, parent);
+  const char * sep = get_separator(0);
+  strcat(path, sep);
+  strcat(path, child);
+  return path;
 }
 
+#ifndef WIN32
 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;
+  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;
-        }
-        char * path = concat_path(dir_path, entry_name);
-        remove_directory(path);
-        free(path);
+  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;
     }
+    char * path = concat_path(dir_path, entry_name);
+    remove_directory(path);
+    free(path);
+  }
+  rmdir(dir_path);
+
+  closedir(d);
+}
+#else
+void remove_directory(const char * dir_path) {
+  HANDLE hFind;
+  WIN32_FIND_DATA fd;
 
-    rmdir(dir_path);
-    closedir(d);
+  hFind = FindFirstFile(dir_path, &fd);
+  if (hFind == INVALID_HANDLE_VALUE) {
+    return;
+  }
+
+  if (fd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) {
+    DeleteFile(dir_path);
+    FindClose(hFind);
+    return;
+  }
+
+  char * path = concat_path(dir_path, "*");
+  HANDLE hFind1;
+  if ((hFind1 = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
+    do {
+      char * entry_name = fd.cFileName;
+      if (!strcmp(entry_name, ".") || !strcmp(entry_name, "..")) continue;
+      char * entry_path = concat_path(dir_path, entry_name);
+      remove_directory(entry_path);
+      free(entry_path);
+    }while (FindNextFile(hFind1, &fd));
+  }
+  RemoveDirectory(dir_path);
+  FindClose(hFind);
+  FindClose(hFind1);
+  free(path);
 }
+#endif
 
 int make_dir(const char * path) {
-    if (!path) return -1;
+  if (!path)
+    return -1;
 
-    errno = 0;
-    int ret = mkdir(path, 
S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
-    if (ret == 0) {
-        return 0;
-    }
+  errno = 0;
+#ifdef WIN32
+  int ret = mkdir(path);
+  char path_sep = '\\';
+#else
+  int ret = mkdir(path,
+  S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
+  char path_sep = '/';
+#endif
+  if (ret == 0) {
+    return 0;
+  }
 
-    switch (errno) {
-    case ENOENT: {
-        char * found = strrchr(path, '/');
-        if (!found) {
-            return -1;
-        }
-        int len = found - path;
-        char * dir = calloc(len + 1, sizeof(char));
-        strncpy(dir, path, len);
-        dir[len] = '\0';
-        int res = make_dir(dir);
-        free(dir);
-        if (res < 0) {
-            return -1;
-        }
-        return mkdir(path, 
S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
+  switch (errno) {
+  case ENOENT: {
+    char * found = strrchr(path, path_sep);
+    if (!found) {
+      return -1;
     }
-    case EEXIST: {
-        if (is_directory(path)) {
-            return 0;
-        }
-        return -1;
+    int len = found - path;
+    char * dir = calloc(len + 1, sizeof(char));
+    strncpy(dir, path, len);
+    dir[len] = '\0';
+    int res = make_dir(dir);
+    free(dir);
+    if (res < 0) {
+      return -1;
     }
-    default:
-        return -1;
+#ifdef WIN32
+    return mkdir(path);
+#else
+    return mkdir(path,
+    S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
+#endif
+  }
+  case EEXIST: {
+    int ret = is_directory(path);
+    if (ret <= 0) {
+      return -1;
     }
+    return 0;
+  }
+  default:
+    return -1;
+  }
 }
 
 char * get_current_working_directory() {
-    char * cwd = (char *)malloc(PATH_MAX * sizeof(char));
-    memset(cwd, 0, PATH_MAX);
-    #ifdef WIN32
-    if (_getcwd(cwd, PATH_MAX) != NULL)
-        return cwd;
-    #else
-    if (getcwd(cwd, PATH_MAX) != NULL) {
-        return cwd;
-    }
-    #endif
-    free(cwd);
+  char * cwd = (char *) malloc(PATH_MAX * sizeof(char));
+  memset(cwd, 0, PATH_MAX);
+#ifdef WIN32
+  if (_getcwd(cwd, PATH_MAX) != NULL)
+  return cwd;
+#else
+  if (getcwd(cwd, PATH_MAX) != NULL) {
+    return cwd;
+  }
+#endif
+  free(cwd);
+  return NULL;
+}
+
+char * create_temp_directory(char * format) {
+#ifndef WIN32
+  if (!mkdtemp(format)) {
     return NULL;
+  }
+  return copystr(format);
+#else
+  char * temp_dir_path = (char *)malloc(PATH_MAX);
+  memset(temp_dir_path, 0, PATH_MAX);
+  DWORD ret = GetTempPath(PATH_MAX, temp_dir_path);
+  if (ret <= PATH_MAX && ret != 0) {
+    CIDGenerator gen;
+    char uuid[37];
+    gen.implementation_ = CUUID_DEFAULT_IMPL;
+    generate_uuid(&gen, uuid);
+    uuid[36] = '\0';
+    strcat(temp_dir_path, ".");
+    strcat(temp_dir_path, uuid);
+    if (make_dir(temp_dir_path) < 0) {
+      return NULL;
+    }
+    return temp_dir_path;
+  }

Review comment:
       1. If the returned temp path is exactly `PATH_MAX` (which I assume is 
the same as `MAX_PATH` as referred to by 
[MSDN](https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppatha)),
 then it doesn't fit in the buffer with the null terminator.
   2. If the returned temp path is near `PATH_MAX` in length, then the `strcat` 
calls may cause buffer overflow. I think we may want to check for this and 
report failure if we can not concat the UUID to the path and still fit in 
`PATH_MAX`.
   3. I suggest using the 'A' versions of the WINAPI functions to avoid 
dependency on the UNICODE macro.
   ```suggestion
     const size_t uuid_strlen = 37;
     const size_t max_temp_path_len = PATH_MAX - uuid_strlen - 2;  // 2: "." 
and '\0'
     char temp_dir_path[PATH_MAX + 1] = {0};
     const DWORD ret = GetTempPathA(PATH_MAX + 1, temp_dir_path);
     if (ret <= max_temp_path_len && ret != 0) {
       CIDGenerator gen;
       char uuid[uuid_strlen];
       gen.implementation_ = CUUID_DEFAULT_IMPL;
       generate_uuid(&gen, uuid);
       uuid[uuid_strlen - 1] = '\0';
       strcat(temp_dir_path, ".");
       strcat(temp_dir_path, uuid);
       if (make_dir(temp_dir_path) < 0) {
         return NULL;
       }
       return copystr(temp_dir_path);
     }
   ```

##########
File path: nanofi/src/core/file_utils.c
##########
@@ -28,114 +28,222 @@
 #include "core/string_utils.h"
 #include "core/file_utils.h"
 
-#ifdef _MSC_VER
+#ifdef WIN32
 #ifndef PATH_MAX
 #define PATH_MAX 260
 #endif
 #endif
 
+#ifdef WIN32
+#define stat _stat
+#define mkdir _mkdir
+#endif
+
 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);
+  struct stat dir_stat;
+  if (stat(path, &dir_stat) < 0) {
+    return -1;
+  }
+#ifdef WIN32
+  return dir_stat.st_mode & S_IFDIR;
+#else
+  return S_ISDIR(dir_stat.st_mode);
+#endif
 }
 
-const char * get_separator(int force_posix)
-{
+const char * get_separator(int force_posix) {
 #ifdef WIN32
-    if (!force_posix) {
-        return "\\";
-    }
+  if (!force_posix) {
+    return "\\";
+  }
 #endif
-    return "/";
+  return "/";
 }
 
 char * concat_path(const char * parent, const char * child) {
-    char * path = (char *)malloc((strlen(parent) + strlen(child) + 2) * 
sizeof(char));
-    strcpy(path, parent);
-    const char * sep = get_separator(0);
-    strcat(path, sep);
-    strcat(path, child);
-    return path;
+  char * path = (char *) malloc((strlen(parent) + strlen(child) + 2) * 
sizeof(char));
+  strcpy(path, parent);
+  const char * sep = get_separator(0);
+  strcat(path, sep);
+  strcat(path, child);
+  return path;
 }
 
+#ifndef WIN32
 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;
+  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;
-        }
-        char * path = concat_path(dir_path, entry_name);
-        remove_directory(path);
-        free(path);
+  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;
     }
+    char * path = concat_path(dir_path, entry_name);
+    remove_directory(path);
+    free(path);
+  }
+  rmdir(dir_path);
+
+  closedir(d);
+}
+#else
+void remove_directory(const char * dir_path) {
+  HANDLE hFind;
+  WIN32_FIND_DATA fd;
 
-    rmdir(dir_path);
-    closedir(d);
+  hFind = FindFirstFile(dir_path, &fd);
+  if (hFind == INVALID_HANDLE_VALUE) {
+    return;
+  }
+
+  if (fd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) {
+    DeleteFile(dir_path);
+    FindClose(hFind);
+    return;
+  }
+
+  char * path = concat_path(dir_path, "*");
+  HANDLE hFind1;
+  if ((hFind1 = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
+    do {
+      char * entry_name = fd.cFileName;
+      if (!strcmp(entry_name, ".") || !strcmp(entry_name, "..")) continue;
+      char * entry_path = concat_path(dir_path, entry_name);
+      remove_directory(entry_path);
+      free(entry_path);
+    }while (FindNextFile(hFind1, &fd));
+  }
+  RemoveDirectory(dir_path);
+  FindClose(hFind);
+  FindClose(hFind1);
+  free(path);
 }
+#endif
 
 int make_dir(const char * path) {
-    if (!path) return -1;
+  if (!path)
+    return -1;
 
-    errno = 0;
-    int ret = mkdir(path, 
S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
-    if (ret == 0) {
-        return 0;
-    }
+  errno = 0;
+#ifdef WIN32
+  int ret = mkdir(path);
+  char path_sep = '\\';
+#else
+  int ret = mkdir(path,
+  S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
+  char path_sep = '/';
+#endif
+  if (ret == 0) {
+    return 0;
+  }
 
-    switch (errno) {
-    case ENOENT: {
-        char * found = strrchr(path, '/');
-        if (!found) {
-            return -1;
-        }
-        int len = found - path;
-        char * dir = calloc(len + 1, sizeof(char));
-        strncpy(dir, path, len);
-        dir[len] = '\0';
-        int res = make_dir(dir);
-        free(dir);
-        if (res < 0) {
-            return -1;
-        }
-        return mkdir(path, 
S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
+  switch (errno) {
+  case ENOENT: {
+    char * found = strrchr(path, path_sep);
+    if (!found) {
+      return -1;
     }
-    case EEXIST: {
-        if (is_directory(path)) {
-            return 0;
-        }
-        return -1;
+    int len = found - path;
+    char * dir = calloc(len + 1, sizeof(char));
+    strncpy(dir, path, len);
+    dir[len] = '\0';
+    int res = make_dir(dir);
+    free(dir);
+    if (res < 0) {
+      return -1;
     }
-    default:
-        return -1;
+#ifdef WIN32
+    return mkdir(path);
+#else
+    return mkdir(path,
+    S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);

Review comment:
       I think this shouldn't be wrapped, or if wrapped, should use 
continuation indentation.
   ```suggestion
       return mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | 
S_IROTH | S_IXOTH);
   ```




----------------------------------------------------------------
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:
us...@infra.apache.org


Reply via email to