Simplified r_mkdir()
This commit is contained in:
		
							
								
								
									
										3
									
								
								thumbs.c
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								thumbs.c
									
									
									
									
									
								
							@@ -92,7 +92,8 @@ void tns_cache_write(Imlib_Image im, const char *filepath, bool force)
 | 
				
			|||||||
		{
 | 
							{
 | 
				
			||||||
			if ((dirend = strrchr(cfile, '/')) != NULL) {
 | 
								if ((dirend = strrchr(cfile, '/')) != NULL) {
 | 
				
			||||||
				*dirend = '\0';
 | 
									*dirend = '\0';
 | 
				
			||||||
				err = r_mkdir(cfile);
 | 
									if ((err = r_mkdir(cfile)) == -1)
 | 
				
			||||||
 | 
										error(0, errno, "%s", cfile);
 | 
				
			||||||
				*dirend = '/';
 | 
									*dirend = '/';
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			if (err == 0) {
 | 
								if (err == 0) {
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										44
									
								
								util.c
									
									
									
									
									
								
							
							
						
						
									
										44
									
								
								util.c
									
									
									
									
									
								
							@@ -181,37 +181,23 @@ char* r_readdir(r_dir_t *rdir)
 | 
				
			|||||||
	return NULL;
 | 
						return NULL;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int r_mkdir(const char *path)
 | 
					int r_mkdir(char *path)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	char *dir, *d;
 | 
						char c, *s = path;
 | 
				
			||||||
	struct stat stats;
 | 
						struct stat st;
 | 
				
			||||||
	int err = 0;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (*path == '\0')
 | 
						while (*s != '\0') {
 | 
				
			||||||
 | 
							if (*s == '/') {
 | 
				
			||||||
 | 
								s++;
 | 
				
			||||||
 | 
								continue;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							for (; *s != '\0' && *s != '/'; s++);
 | 
				
			||||||
 | 
							c = *s;
 | 
				
			||||||
 | 
							*s = '\0';
 | 
				
			||||||
 | 
							if (mkdir(path, 0755) == -1)
 | 
				
			||||||
 | 
								if (errno != EEXIST || stat(path, &st) == -1 || !S_ISDIR(st.st_mode))
 | 
				
			||||||
				return -1;
 | 
									return -1;
 | 
				
			||||||
 | 
							*s = c;
 | 
				
			||||||
	if (stat(path, &stats) == 0)
 | 
					 | 
				
			||||||
		return S_ISDIR(stats.st_mode) ? 0 : -1;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	d = dir = (char*) emalloc(strlen(path) + 1);
 | 
					 | 
				
			||||||
	strcpy(dir, path);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	while (d != NULL && err == 0) {
 | 
					 | 
				
			||||||
		d = strchr(d + 1, '/');
 | 
					 | 
				
			||||||
		if (d != NULL)
 | 
					 | 
				
			||||||
			*d = '\0';
 | 
					 | 
				
			||||||
		if (access(dir, F_OK) < 0 && errno == ENOENT) {
 | 
					 | 
				
			||||||
			if (mkdir(dir, 0755) < 0) {
 | 
					 | 
				
			||||||
				error(0, errno, "%s", dir);
 | 
					 | 
				
			||||||
				err = -1;
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
		} else if (stat(dir, &stats) < 0 || !S_ISDIR(stats.st_mode)) {
 | 
						return 0;
 | 
				
			||||||
			err = -1;
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		if (d != NULL)
 | 
					 | 
				
			||||||
			*d = '/';
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	free(dir);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return err;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										2
									
								
								util.h
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								util.h
									
									
									
									
									
								
							@@ -74,6 +74,6 @@ void size_readable(float*, const char**);
 | 
				
			|||||||
int r_opendir(r_dir_t*, const char*);
 | 
					int r_opendir(r_dir_t*, const char*);
 | 
				
			||||||
int r_closedir(r_dir_t*);
 | 
					int r_closedir(r_dir_t*);
 | 
				
			||||||
char* r_readdir(r_dir_t*);
 | 
					char* r_readdir(r_dir_t*);
 | 
				
			||||||
int r_mkdir(const char *);
 | 
					int r_mkdir(char*);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif /* UTIL_H */
 | 
					#endif /* UTIL_H */
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user