strncpy(dest, src, size);
}
-long SS_recursive_folder_creater(const char *path, const mode_t mode)
-{
- int ret = 0;
- int offset = 0;
- char temppath[MAX_PATH] = { '\0' };
+long SS_recursive_folder_creater(const char *path, const mode_t mode) {
- LOGL(LOG_SSENGINE, "path: %s\n", path);
+ if (path == 0) {
+ LOGL(LOG_SSENGINE, "Bad path param value \n");
+ return -E_SS_BAD_PARAMS;
+ }
- if ((offset = strlen(path)) == 0) // For counting back until the '/' delimiter
- return -1; //if from some reason we got to the end return error!!!.
+ if (path[0] == '\0') {
+ LOGL(LOG_SSENGINE, "Path empty \n");
+ return -E_SS_FILENAMELENERROR;
+ }
- while (path[offset] != '/') // get to the next '/' place
- offset--;
+ char input_path[MAX_PATH] = {'\0'};
+ char temppath[MAX_PATH] = {'\0'};
- strncpy(temppath, path, offset); // copy one depth below till and without the char '/'
- LOGL(LOG_SSENGINE, " temppath: %s\n", temppath);
- ret = mkdir(temppath, mode);
- LOGL(LOG_SSENGINE, " mkdir result: %d errno: %d\n", ret, errno);
+ strncpy(input_path, path, MAX_PATH);
- if (ret == 0 || ((ret == -1) && (errno == EEXIST))) {
- return 0; //meaning the depth creation is success.
- } else if ((ret == -1) && (errno == ENOENT)) {
- if ((ret = SS_recursive_folder_creater(temppath, mode)) == 0)
- ret = mkdir(temppath, mode);
- return ret;
- } else {
- return -1;
+ if (input_path[0] == '/') {
+ temppath[0] = '/';
+ }
+
+ char *path_token;
+ char *path_pos = input_path;
+
+ int temppath_current_len = (temppath[0] == '/');
+
+ while((path_token = strtok_r(path_pos, "/", &path_pos))) {
+
+ int path_token_len = strnlen(path_token, MAX_PATH-temppath_current_len);
+ strncat(temppath, path_token, path_token_len);
+ temppath_current_len += path_token_len;
+
+ int mkd_res = mkdir(temppath, mode);
+
+ if(mkd_res != 0 && errno != EEXIST) {
+ LOGL(LOG_SSENGINE,"cannot create dir %s\n system error: %d error: %s\n", temppath, errno, strerror(errno));
+ return -E_SS_CANNOT_CREATE_DIRECTORY;
+ }
+
+ if(path_token_len < (MAX_PATH-1)) {
+ strcat(temppath, "/");
+ }
}
+ return 0;
}
long