Sfoglia il codice sorgente

base: faux_isdir()

Serj Kalichev 3 anni fa
parent
commit
7de9bcbab8
2 ha cambiato i file con 25 aggiunte e 5 eliminazioni
  1. 24 5
      faux/base/fs.c
  2. 1 0
      faux/faux.h

+ 24 - 5
faux/base/fs.c

@@ -15,6 +15,29 @@
 
 #include "faux/str.h"
 
+
+/** @brief If given path is directory.
+ *
+ * @param [in] path Filesystem path.
+ * @return 0 - success, < 0 on error.
+ */
+bool_t faux_isdir(const char *path)
+{
+	struct stat statbuf = {};
+
+	assert(path);
+	if (!path)
+		return BOOL_FALSE;
+
+	if (lstat(path, &statbuf) < 0)
+		return BOOL_FALSE;
+
+	if (S_ISDIR(statbuf.st_mode))
+		return BOOL_TRUE;
+
+	return BOOL_FALSE;
+}
+
 /** @brief Removes filesystem objects recursively.
  *
  * Function can remove file or directory (recursively).
@@ -24,7 +47,6 @@
  */
 int faux_rm(const char *path)
 {
-	struct stat statbuf = {};
 	DIR *dir = NULL;
 	struct dirent *dir_entry = NULL;
 
@@ -32,11 +54,8 @@ int faux_rm(const char *path)
 	if (!path)
 		return -1;
 
-	if (lstat(path, &statbuf) < 0)
-		return -1;
-
 	// Common file (not dir)
-	if (!S_ISDIR(statbuf.st_mode))
+	if (!faux_isdir(path))
 		return unlink(path);
 
 	// Directory

+ 1 - 0
faux/faux.h

@@ -85,6 +85,7 @@ ssize_t faux_write_block(int fd, const void *buf, size_t n);
 size_t faux_read_block(int fd, void *buf, size_t n);
 
 // Filesystem
+bool_t faux_isdir(const char *path);
 int faux_rm(const char *path);
 char *faux_expand_tilde(const char *path);