How to implement MakeSureDirectoryPathExists() in esp-idf?

hasukuma
Posts: 4
Joined: Tue Jul 11, 2023 3:45 am

How to implement MakeSureDirectoryPathExists() in esp-idf?

Postby hasukuma » Tue Jul 11, 2023 4:05 am

Hello there!

I just started a serious hobby project with ESP32 using esp-idf. This project uses SD Card for storing various data points that are organized in nested directories, and I would like to write a method similar to MakeSureDirectoryPathExists(). Basically I am looking for a function that will create all non-existing sub-directories in a directory path.

Let us say my SD Card is mounted as /sdcard, and I already have a directory in it viz., "/sdcard/data", so if I call this method with "/sdcard/data/devices/sensor1/data.csv", it should create sub-folders "devices" and "sensor1" (under respective parent directories of course). How can I do that in esp-idf? Since mine is a new project I am free to use any available library or filesystem that can make it easier to implement this function.

Thanks in advance!

hasukuma
Posts: 4
Joined: Tue Jul 11, 2023 3:45 am

Re: How to implement MakeSureDirectoryPathExists() in esp-idf?

Postby hasukuma » Tue Jul 11, 2023 12:06 pm

Ok... this is what I came up with. I just got mkdir() working as expected including the /sdcard mount point in the directory path.
  1.  
  2. void MyESPApp::MakesureDirectoryPathExists(char *filePath)
  3. {
  4.     char *fullPath = filePath;
  5.     filePath++; // Skip the leading path delimiter
  6.     while (*filePath && *filePath != '/')
  7.         filePath++; // skip mountpoint
  8.  
  9.     while (*filePath)
  10.     {
  11.         if (*filePath == '/')
  12.         {
  13.             *filePath = '\0';
  14.             mkdir(fullPath, 0755);
  15.             *filePath = '/';
  16.         }
  17.  
  18.         filePath++;
  19.     }
  20. }
  21.  

hasukuma
Posts: 4
Joined: Tue Jul 11, 2023 3:45 am

Re: How to implement MakeSureDirectoryPathExists() in esp-idf?

Postby hasukuma » Tue Jul 11, 2023 12:54 pm

Modified the code to call mkdir only if the directory does not exist.
  1. void ESPApp::MakesureDirectoryPathExists(char *filePath)
  2. {
  3.     char *directory = filePath;
  4.  
  5.     filePath++; // Skip the leading path delimiter
  6.     while (*filePath && *filePath != '/')
  7.         filePath++; // skip mountpoint
  8.  
  9.     while (*filePath)
  10.     {
  11.         if (*filePath == '/')
  12.         {
  13.             *filePath = '\0';
  14.  
  15.             //mkdir(fullPath, 0755);
  16.  
  17.             struct stat s;
  18.             bool directory_exists = stat(directory, &s) == 0;
  19.             if (!directory_exists)
  20.             {
  21.                 if (mkdir(directory, 0775) != 0)
  22.                 {
  23.                     ESP_LOGE(TAG, "mkdir failed with error: %s\n", strerror(errno));
  24.                 }
  25.             }
  26.  
  27.             *filePath = '/';
  28.         }
  29.  
  30.         filePath++;
  31.     }
  32. }

MicroController
Posts: 1708
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: How to implement MakeSureDirectoryPathExists() in esp-idf?

Postby MicroController » Wed Jul 12, 2023 7:06 am

hasukuma wrote:
Tue Jul 11, 2023 12:54 pm
Modified the code to call mkdir only if the directory does not exist.
Not needed because mkdir will check the same thing again (and yield errno EEXIST if it does).

Who is online

Users browsing this forum: Baidu [Spider], MicroController and 79 guests