[vlc-commits] [Git][videolan/vlc][master] 5 commits: fs: add vlc_mkdir_parent function that correspond to 'mkdir -p'

Steve Lhomme (@robUx4) gitlab at videolan.org
Thu Mar 21 10:12:10 UTC 2024



Steve Lhomme pushed to branch master at VideoLAN / VLC


Commits:
2c853dd8 by Gabriel LT at 2024-03-21T09:50:58+00:00
fs: add vlc_mkdir_parent function that correspond to 'mkdir -p'

- - - - -
d5a785d8 by Gabriel LT at 2024-03-21T09:50:58+00:00
config: replace local recursive mkdir implementation by vlc_mkdir_parent

- - - - -
b4653b28 by Gabriel LT at 2024-03-21T09:50:58+00:00
preparser/art: replace local recursive mkdir implementation by vlc_mkdir_parent

- - - - -
7aa3b6d6 by Gabriel LT at 2024-03-21T09:50:58+00:00
fsstorage: replace local recursive mkdir implementation by vlc_mkdir_parent

- - - - -
5412294f by Gabriel LT at 2024-03-21T09:50:58+00:00
gui: replace local recursive mkdir implementation by vlc_mkdir_parent

- - - - -


9 changed files:

- include/vlc_fs.h
- modules/gui/skins2/src/theme_loader.cpp
- modules/misc/addons/fsstorage.c
- src/Makefile.am
- src/config/file.c
- src/libvlccore.sym
- src/meson.build
- + src/misc/filesystem.c
- src/preparser/art.c


Changes:

=====================================
include/vlc_fs.h
=====================================
@@ -275,6 +275,16 @@ VLC_API void vlc_rewinddir( vlc_DIR *dir );
  */
 VLC_API int vlc_mkdir(const char *dirname, mode_t mode);
 
+/**
+ * Creates a directory and parent directories as needed.
+ *
+ * @param dirname a UTF-8 string containing the name of the directory to
+ *        be created.
+ * @param mode directory permissions
+ * @return 0 on success, -1 on error (see errno).
+ */
+VLC_API int vlc_mkdir_parent(const char *dirname, mode_t mode);
+
 /**
  * Determines the current working directory.
  *


=====================================
modules/gui/skins2/src/theme_loader.cpp
=====================================
@@ -69,45 +69,11 @@ static inline std::string sFromLocale( const std::string &rLocale )
  */
 static int makedir( const char *newdir )
 {
-    char *p, *buffer = strdup( newdir );
-    int  len = strlen( buffer );
-
-    if( len <= 0 )
-    {
-        free( buffer );
-        return 0;
-    }
-
-    if( buffer[len-1] == '/' )
-    {
-        buffer[len-1] = '\0';
-    }
-
-    if( vlc_mkdir( buffer, 0775 ) == 0 )
-    {
-        free( buffer );
+    int ret = vlc_mkdir_parent(newdir, 0775);
+    if (ret == 0)
         return 1;
-    }
-
-    p = buffer + 1;
-    while( 1 )
-    {
-        char hold;
-
-        while( *p && *p != '\\' && *p != '/' ) p++;
-        hold = *p;
-        *p = 0;
-        if( ( vlc_mkdir( buffer, 0775 ) == -1 ) && ( errno == ENOENT ) )
-        {
-            fprintf( stderr, "couldn't create directory %s\n", buffer );
-            free( buffer );
-            return 0;
-        }
-        if( hold == 0 ) break;
-        *p++ = hold;
-    }
-    free( buffer );
-    return 1;
+    fprintf(stderr, "couldn't create directory %s\n", newdir);
+    return 0;
 }
 
 bool ThemeLoader::load( const std::string &fileName )


=====================================
modules/misc/addons/fsstorage.c
=====================================
@@ -347,36 +347,9 @@ static int List( addons_finder_t *p_finder )
 }
 
 static int recursive_mkdir( vlc_object_t *p_this, const char *psz_dirname )
-{/* stolen from config_CreateDir() */
-    if( !psz_dirname || !*psz_dirname ) return -1;
-
-    if( vlc_mkdir( psz_dirname, 0700 ) == 0 )
+{
+    if (vlc_mkdir_parent(psz_dirname, 0700) == 0)
         return 0;
-
-    switch( errno )
-    {
-        case EEXIST:
-            return 0;
-
-        case ENOENT:
-        {
-            /* Let's try to create the parent directory */
-            char psz_parent[strlen( psz_dirname ) + 1], *psz_end;
-            strcpy( psz_parent, psz_dirname );
-
-            psz_end = strrchr( psz_parent, DIR_SEP_CHAR );
-            if( psz_end && psz_end != psz_parent )
-            {
-                *psz_end = '\0';
-                if( recursive_mkdir( p_this, psz_parent ) == 0 )
-                {
-                    if( !vlc_mkdir( psz_dirname, 0700 ) )
-                        return 0;
-                }
-            }
-        }
-    }
-
     msg_Warn( p_this, "could not create %s: %m", psz_dirname );
     return -1;
 }


=====================================
src/Makefile.am
=====================================
@@ -385,6 +385,7 @@ libvlccore_la_SOURCES = \
 	misc/mtime.c \
 	misc/frame.c \
 	misc/fifo.c \
+	misc/filesystem.c \
 	misc/fourcc.c \
 	misc/fourcc_list.h \
 	misc/es_format.c \


=====================================
src/config/file.c
=====================================
@@ -277,34 +277,8 @@ int config_LoadConfigFile( libvlc_int_t *p_this )
  *****************************************************************************/
 static int config_CreateDir( libvlc_int_t *p_this, char *psz_dirname )
 {
-    if( !psz_dirname || !*psz_dirname ) return -1;
-
-    if( vlc_mkdir( psz_dirname, 0700 ) == 0 )
+    if (vlc_mkdir_parent(psz_dirname, 0700) == 0)
         return 0;
-
-    switch( errno )
-    {
-        case EEXIST:
-            return 0;
-
-        case ENOENT:
-        {
-            /* Let's try to create the parent directory */
-            char *psz_end = strrchr( psz_dirname, DIR_SEP_CHAR );
-            if( psz_end && psz_end != psz_dirname )
-            {
-                *psz_end = '\0';
-                int res = config_CreateDir( p_this, psz_dirname );
-                *psz_end = DIR_SEP_CHAR;
-                if( res == 0 )
-                {
-                    if( !vlc_mkdir( psz_dirname, 0700 ) )
-                        return 0;
-                }
-            }
-        }
-    }
-
     msg_Warn( p_this, "could not create %s: %s", psz_dirname,
               vlc_strerror_c(errno) );
     return -1;


=====================================
src/libvlccore.sym
=====================================
@@ -459,6 +459,7 @@ utf8_fprintf
 vlc_loaddir
 vlc_lstat
 vlc_mkdir
+vlc_mkdir_parent
 vlc_mkstemp
 vlc_open
 vlc_openat


=====================================
src/meson.build
=====================================
@@ -236,6 +236,7 @@ libvlccore_sources_base = files(
     'misc/mtime.c',
     'misc/frame.c',
     'misc/fifo.c',
+    'misc/filesystem.c',
     'misc/fourcc.c',
     'misc/fourcc_list.h',
     'misc/es_format.c',


=====================================
src/misc/filesystem.c
=====================================
@@ -0,0 +1,56 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*****************************************************************************
+ * filesystem.c: filesystem helpers
+ *****************************************************************************
+ * Copyright © 2024 VLC authors, VideoLAN and Videolabs
+ *
+ * Authors: Gabriel Lafond Thenaille <gabriel at videolabs.io>
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_fs.h>
+
+/**
+ * Create all directories in the given path if missing.
+ */
+int vlc_mkdir_parent(const char *dirname, mode_t mode)
+{
+    int ret = vlc_mkdir(dirname, mode);
+    if (ret == 0 || errno == EEXIST) {
+        return 0;
+    } else if (errno != ENOENT) {
+        return -1;
+    }
+
+    char *path = strdup(dirname);
+    if (path == NULL) {
+        return -1;
+    }
+
+    char *ptr = path + 1;
+    while (*ptr) {
+        ptr = strchr(ptr, DIR_SEP_CHAR);
+        if (ptr == NULL) {
+            break;
+        }
+        *ptr = '\0';
+        if (vlc_mkdir(path, mode) != 0) {
+            if (errno != EEXIST) {
+                free(path);
+                return -1;
+            }
+        }
+        *ptr = DIR_SEP_CHAR;
+        ptr++;
+    }
+    ret = vlc_mkdir(path, mode);
+    if (errno == EEXIST) {
+        ret = 0;
+    }
+    free(path);
+    return ret;
+}


=====================================
src/preparser/art.c
=====================================
@@ -40,19 +40,7 @@
 
 static void ArtCacheCreateDir( char *psz_dir )
 {
-    char * psz = psz_dir;
-
-    while( *psz )
-    {
-        while( *psz && *psz != DIR_SEP_CHAR) psz++;
-        if( !*psz ) break;
-        *psz = 0;
-        if( !EMPTY_STR( psz_dir ) )
-            vlc_mkdir( psz_dir, 0700 );
-        *psz = DIR_SEP_CHAR;
-        psz++;
-    }
-    vlc_mkdir( psz_dir, 0700 );
+    vlc_mkdir_parent(psz_dir, 0700);
 }
 
 static char* ArtCacheGetDirPath( const char *psz_arturl, const char *psz_artist,



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/908d899866e099775fa8bb09f3b7a8687daf6374...5412294fdaeeee147659cb2c5f9891ee22ce1e07

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/908d899866e099775fa8bb09f3b7a8687daf6374...5412294fdaeeee147659cb2c5f9891ee22ce1e07
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list