Implementing std::filesystem::remove_all() for AndroidMonitor filesystem for continuous integration and buildRefactoring Android fragmentsFilesystem-dependant unit-testing in goStyling android widgets programmaticallyCaching in PHP using the filesystemJava (Android) abstract class correct implementationFetching filesystem directory listings on a serverValidating FileSystem StructureFilesystem search class in C#Implementation of an OkHttp singleton for Android

The use of multiple foreign keys on same column in SQL Server

Why is Minecraft giving an OpenGL error?

Why is consensus so controversial in Britain?

Smoothness of finite-dimensional functional calculus

How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?

Today is the Center

What's the output of a record cartridge playing an out-of-speed record

Is it possible to do 50 km distance without any previous training?

Fencing style for blades that can attack from a distance

How can I make a cone from a cube and view the cube with different angles?

What are these boxed doors outside store fronts in New York?

Adding span tags within wp_list_pages list items

What defenses are there against being summoned by the Gate spell?

Why are electrically insulating heatsinks so rare? Is it just cost?

What do the dots in this tr command do: tr .............A-Z A-ZA-Z <<< "JVPQBOV" (with 13 dots)

What variety is this tomato with long, milky green branches?

Are the number of citations and number of published articles the most important criteria for a tenure promotion?

Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?

"You are your self first supporter", a more proper way to say it

Languages that we cannot (dis)prove to be Context-Free

To string or not to string

What is the offset in a seaplane's hull?

How to format long polynomial?

Is it important to consider tone, melody, and musical form while writing a song?



Implementing std::filesystem::remove_all() for Android


Monitor filesystem for continuous integration and buildRefactoring Android fragmentsFilesystem-dependant unit-testing in goStyling android widgets programmaticallyCaching in PHP using the filesystemJava (Android) abstract class correct implementationFetching filesystem directory listings on a serverValidating FileSystem StructureFilesystem search class in C#Implementation of an OkHttp singleton for Android






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1












$begingroup$


I needed to use some c++ code on both standard Linux and the Android variant, and the code will use filesystem library but it isn't yet in the ndk.

So I'm making a one to use on Android

It will be also usable on Linux because working with files and dirs go through the linux api which is the same on android



So, this is my implementation of the remove_all() function.

It reports errors in int variable and may be wrapped later with a function that will translate expected errno to proper c++ err_code.

It can't delete a dir that contains subdir with files exceeding the file descriptors limit for current process.

Also it can take the whole stack if called recursively too many times.

My filesystem implementation uses a thread local variable (int filesystem_errno) to store errors if needed.



size_t remove_all(const filesystem::path& p, int& err = filesystem_errno)
O_DIRECTORY);
if (dir_fd < 0)

err = errno;
return 0;

if (p.native().length() > PATH_MAX)

close(dir_fd);
err = ENAMETOOLONG;
return 0;

const int BUFF_SIZE = 1024;
char buffer[BUFF_SIZE];
char subdir[PATH_MAX];
int base_dir_len = strlen(p.c_str());
memcpy(subdir,p.c_str(),base_dir_len);
if (subdir[base_dir_len - 1] != '/')

subdir[base_dir_len] = '/';
++base_dir_len;

char *subdir_ptr = subdir;
subdir_ptr += base_dir_len;
struct linux_dirent

unsigned long inod;
off64_t off;
unsigned short len;
unsigned char type;
char name[];
;
struct linux_dirent32

unsigned long inod;
off_t off;
unsigned short len;
char name[];
;
linux_dirent *ldir_ptr;
linux_dirent32 *ldir_ptr32;
size_t fd_count = 0;
while(true)

int nread = syscall(SYS_getdents64,dir_fd,buffer,BUFF_SIZE);
if (!nread)
break;
if (nread == -1)

err = errno;
close(dir_fd);
return fd_count;

for (int pos = 0; pos < nread; pos += ldir_ptr->len)

ldir_ptr = (linux_dirent*)(buffer + pos);
// skip . and ..
if (ldir_ptr->name[0] == '.')

if (ldir_ptr->name[1] == 0)
continue;
else if (ldir_ptr->name[1] == '.')
if (ldir_ptr->name[2] == 0)
continue;


int subdir_len = strlen(ldir_ptr->name);
if ((subdir_len + base_dir_len) > PATH_MAX)

close(dir_fd);
err = ENAMETOOLONG;
return fd_count;
memcpy(subdir_ptr,ldir_ptr->name,subdir_len);
subdir_ptr[subdir_len] = 0;

if (ldir_ptr->type == DT_DIR)

size_t deleted_dirs = 0;
if (!rmdir(subdir)) // empty dir
++deleted_dirs;
else if (!(deleted_dirs = filesystem::remove_all(subdir,err)))

close(dir_fd);
return fd_count;

fd_count += deleted_dirs;

else if (ldir_ptr->type != DT_UNKNOWN)

if(unlink(subdir))

err = errno;
close(dir_fd);
return fd_count;

++fd_count;

else

struct stat st;
if (stat(subdir,&st))

err = errno;
close(dir_fd);
return fd_count;

if (filesystem::is_directory(st))

size_t deleted_dirs = 0;
if (!rmdir(subdir))
++deleted_dirs;
else if (!(deleted_dirs = filesystem::remove_all(subdir,err)))

close(dir_fd);
return fd_count;

fd_count += deleted_dirs;

else

if (unlink(subdir))

err = errno;
close(dir_fd);
return fd_count;

++fd_count;





close(dir_fd);
if (rmdir(p.c_str()))

err = errno;
return fd_count;

err = 0;
++fd_count;
return fd_count;










share|improve this question











$endgroup$


















    1












    $begingroup$


    I needed to use some c++ code on both standard Linux and the Android variant, and the code will use filesystem library but it isn't yet in the ndk.

    So I'm making a one to use on Android

    It will be also usable on Linux because working with files and dirs go through the linux api which is the same on android



    So, this is my implementation of the remove_all() function.

    It reports errors in int variable and may be wrapped later with a function that will translate expected errno to proper c++ err_code.

    It can't delete a dir that contains subdir with files exceeding the file descriptors limit for current process.

    Also it can take the whole stack if called recursively too many times.

    My filesystem implementation uses a thread local variable (int filesystem_errno) to store errors if needed.



    size_t remove_all(const filesystem::path& p, int& err = filesystem_errno)
    O_DIRECTORY);
    if (dir_fd < 0)

    err = errno;
    return 0;

    if (p.native().length() > PATH_MAX)

    close(dir_fd);
    err = ENAMETOOLONG;
    return 0;

    const int BUFF_SIZE = 1024;
    char buffer[BUFF_SIZE];
    char subdir[PATH_MAX];
    int base_dir_len = strlen(p.c_str());
    memcpy(subdir,p.c_str(),base_dir_len);
    if (subdir[base_dir_len - 1] != '/')

    subdir[base_dir_len] = '/';
    ++base_dir_len;

    char *subdir_ptr = subdir;
    subdir_ptr += base_dir_len;
    struct linux_dirent

    unsigned long inod;
    off64_t off;
    unsigned short len;
    unsigned char type;
    char name[];
    ;
    struct linux_dirent32

    unsigned long inod;
    off_t off;
    unsigned short len;
    char name[];
    ;
    linux_dirent *ldir_ptr;
    linux_dirent32 *ldir_ptr32;
    size_t fd_count = 0;
    while(true)

    int nread = syscall(SYS_getdents64,dir_fd,buffer,BUFF_SIZE);
    if (!nread)
    break;
    if (nread == -1)

    err = errno;
    close(dir_fd);
    return fd_count;

    for (int pos = 0; pos < nread; pos += ldir_ptr->len)

    ldir_ptr = (linux_dirent*)(buffer + pos);
    // skip . and ..
    if (ldir_ptr->name[0] == '.')

    if (ldir_ptr->name[1] == 0)
    continue;
    else if (ldir_ptr->name[1] == '.')
    if (ldir_ptr->name[2] == 0)
    continue;


    int subdir_len = strlen(ldir_ptr->name);
    if ((subdir_len + base_dir_len) > PATH_MAX)

    close(dir_fd);
    err = ENAMETOOLONG;
    return fd_count;
    memcpy(subdir_ptr,ldir_ptr->name,subdir_len);
    subdir_ptr[subdir_len] = 0;

    if (ldir_ptr->type == DT_DIR)

    size_t deleted_dirs = 0;
    if (!rmdir(subdir)) // empty dir
    ++deleted_dirs;
    else if (!(deleted_dirs = filesystem::remove_all(subdir,err)))

    close(dir_fd);
    return fd_count;

    fd_count += deleted_dirs;

    else if (ldir_ptr->type != DT_UNKNOWN)

    if(unlink(subdir))

    err = errno;
    close(dir_fd);
    return fd_count;

    ++fd_count;

    else

    struct stat st;
    if (stat(subdir,&st))

    err = errno;
    close(dir_fd);
    return fd_count;

    if (filesystem::is_directory(st))

    size_t deleted_dirs = 0;
    if (!rmdir(subdir))
    ++deleted_dirs;
    else if (!(deleted_dirs = filesystem::remove_all(subdir,err)))

    close(dir_fd);
    return fd_count;

    fd_count += deleted_dirs;

    else

    if (unlink(subdir))

    err = errno;
    close(dir_fd);
    return fd_count;

    ++fd_count;





    close(dir_fd);
    if (rmdir(p.c_str()))

    err = errno;
    return fd_count;

    err = 0;
    ++fd_count;
    return fd_count;










    share|improve this question











    $endgroup$














      1












      1








      1





      $begingroup$


      I needed to use some c++ code on both standard Linux and the Android variant, and the code will use filesystem library but it isn't yet in the ndk.

      So I'm making a one to use on Android

      It will be also usable on Linux because working with files and dirs go through the linux api which is the same on android



      So, this is my implementation of the remove_all() function.

      It reports errors in int variable and may be wrapped later with a function that will translate expected errno to proper c++ err_code.

      It can't delete a dir that contains subdir with files exceeding the file descriptors limit for current process.

      Also it can take the whole stack if called recursively too many times.

      My filesystem implementation uses a thread local variable (int filesystem_errno) to store errors if needed.



      size_t remove_all(const filesystem::path& p, int& err = filesystem_errno)
      O_DIRECTORY);
      if (dir_fd < 0)

      err = errno;
      return 0;

      if (p.native().length() > PATH_MAX)

      close(dir_fd);
      err = ENAMETOOLONG;
      return 0;

      const int BUFF_SIZE = 1024;
      char buffer[BUFF_SIZE];
      char subdir[PATH_MAX];
      int base_dir_len = strlen(p.c_str());
      memcpy(subdir,p.c_str(),base_dir_len);
      if (subdir[base_dir_len - 1] != '/')

      subdir[base_dir_len] = '/';
      ++base_dir_len;

      char *subdir_ptr = subdir;
      subdir_ptr += base_dir_len;
      struct linux_dirent

      unsigned long inod;
      off64_t off;
      unsigned short len;
      unsigned char type;
      char name[];
      ;
      struct linux_dirent32

      unsigned long inod;
      off_t off;
      unsigned short len;
      char name[];
      ;
      linux_dirent *ldir_ptr;
      linux_dirent32 *ldir_ptr32;
      size_t fd_count = 0;
      while(true)

      int nread = syscall(SYS_getdents64,dir_fd,buffer,BUFF_SIZE);
      if (!nread)
      break;
      if (nread == -1)

      err = errno;
      close(dir_fd);
      return fd_count;

      for (int pos = 0; pos < nread; pos += ldir_ptr->len)

      ldir_ptr = (linux_dirent*)(buffer + pos);
      // skip . and ..
      if (ldir_ptr->name[0] == '.')

      if (ldir_ptr->name[1] == 0)
      continue;
      else if (ldir_ptr->name[1] == '.')
      if (ldir_ptr->name[2] == 0)
      continue;


      int subdir_len = strlen(ldir_ptr->name);
      if ((subdir_len + base_dir_len) > PATH_MAX)

      close(dir_fd);
      err = ENAMETOOLONG;
      return fd_count;
      memcpy(subdir_ptr,ldir_ptr->name,subdir_len);
      subdir_ptr[subdir_len] = 0;

      if (ldir_ptr->type == DT_DIR)

      size_t deleted_dirs = 0;
      if (!rmdir(subdir)) // empty dir
      ++deleted_dirs;
      else if (!(deleted_dirs = filesystem::remove_all(subdir,err)))

      close(dir_fd);
      return fd_count;

      fd_count += deleted_dirs;

      else if (ldir_ptr->type != DT_UNKNOWN)

      if(unlink(subdir))

      err = errno;
      close(dir_fd);
      return fd_count;

      ++fd_count;

      else

      struct stat st;
      if (stat(subdir,&st))

      err = errno;
      close(dir_fd);
      return fd_count;

      if (filesystem::is_directory(st))

      size_t deleted_dirs = 0;
      if (!rmdir(subdir))
      ++deleted_dirs;
      else if (!(deleted_dirs = filesystem::remove_all(subdir,err)))

      close(dir_fd);
      return fd_count;

      fd_count += deleted_dirs;

      else

      if (unlink(subdir))

      err = errno;
      close(dir_fd);
      return fd_count;

      ++fd_count;





      close(dir_fd);
      if (rmdir(p.c_str()))

      err = errno;
      return fd_count;

      err = 0;
      ++fd_count;
      return fd_count;










      share|improve this question











      $endgroup$




      I needed to use some c++ code on both standard Linux and the Android variant, and the code will use filesystem library but it isn't yet in the ndk.

      So I'm making a one to use on Android

      It will be also usable on Linux because working with files and dirs go through the linux api which is the same on android



      So, this is my implementation of the remove_all() function.

      It reports errors in int variable and may be wrapped later with a function that will translate expected errno to proper c++ err_code.

      It can't delete a dir that contains subdir with files exceeding the file descriptors limit for current process.

      Also it can take the whole stack if called recursively too many times.

      My filesystem implementation uses a thread local variable (int filesystem_errno) to store errors if needed.



      size_t remove_all(const filesystem::path& p, int& err = filesystem_errno)
      O_DIRECTORY);
      if (dir_fd < 0)

      err = errno;
      return 0;

      if (p.native().length() > PATH_MAX)

      close(dir_fd);
      err = ENAMETOOLONG;
      return 0;

      const int BUFF_SIZE = 1024;
      char buffer[BUFF_SIZE];
      char subdir[PATH_MAX];
      int base_dir_len = strlen(p.c_str());
      memcpy(subdir,p.c_str(),base_dir_len);
      if (subdir[base_dir_len - 1] != '/')

      subdir[base_dir_len] = '/';
      ++base_dir_len;

      char *subdir_ptr = subdir;
      subdir_ptr += base_dir_len;
      struct linux_dirent

      unsigned long inod;
      off64_t off;
      unsigned short len;
      unsigned char type;
      char name[];
      ;
      struct linux_dirent32

      unsigned long inod;
      off_t off;
      unsigned short len;
      char name[];
      ;
      linux_dirent *ldir_ptr;
      linux_dirent32 *ldir_ptr32;
      size_t fd_count = 0;
      while(true)

      int nread = syscall(SYS_getdents64,dir_fd,buffer,BUFF_SIZE);
      if (!nread)
      break;
      if (nread == -1)

      err = errno;
      close(dir_fd);
      return fd_count;

      for (int pos = 0; pos < nread; pos += ldir_ptr->len)

      ldir_ptr = (linux_dirent*)(buffer + pos);
      // skip . and ..
      if (ldir_ptr->name[0] == '.')

      if (ldir_ptr->name[1] == 0)
      continue;
      else if (ldir_ptr->name[1] == '.')
      if (ldir_ptr->name[2] == 0)
      continue;


      int subdir_len = strlen(ldir_ptr->name);
      if ((subdir_len + base_dir_len) > PATH_MAX)

      close(dir_fd);
      err = ENAMETOOLONG;
      return fd_count;
      memcpy(subdir_ptr,ldir_ptr->name,subdir_len);
      subdir_ptr[subdir_len] = 0;

      if (ldir_ptr->type == DT_DIR)

      size_t deleted_dirs = 0;
      if (!rmdir(subdir)) // empty dir
      ++deleted_dirs;
      else if (!(deleted_dirs = filesystem::remove_all(subdir,err)))

      close(dir_fd);
      return fd_count;

      fd_count += deleted_dirs;

      else if (ldir_ptr->type != DT_UNKNOWN)

      if(unlink(subdir))

      err = errno;
      close(dir_fd);
      return fd_count;

      ++fd_count;

      else

      struct stat st;
      if (stat(subdir,&st))

      err = errno;
      close(dir_fd);
      return fd_count;

      if (filesystem::is_directory(st))

      size_t deleted_dirs = 0;
      if (!rmdir(subdir))
      ++deleted_dirs;
      else if (!(deleted_dirs = filesystem::remove_all(subdir,err)))

      close(dir_fd);
      return fd_count;

      fd_count += deleted_dirs;

      else

      if (unlink(subdir))

      err = errno;
      close(dir_fd);
      return fd_count;

      ++fd_count;





      close(dir_fd);
      if (rmdir(p.c_str()))

      err = errno;
      return fd_count;

      err = 0;
      ++fd_count;
      return fd_count;







      c++ android file-system linux c++17






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 6 mins ago









      Deduplicator

      11.8k1950




      11.8k1950










      asked 54 mins ago









      prog511prog511

      61




      61




















          0






          active

          oldest

          votes












          Your Answer





          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216986%2fimplementing-stdfilesystemremove-all-for-android%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216986%2fimplementing-stdfilesystemremove-all-for-android%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

          格濟夫卡 參考資料 导航菜单51°3′40″N 34°2′21″E / 51.06111°N 34.03917°E / 51.06111; 34.03917ГезівкаПогода в селі 编辑或修订