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;
$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;
c++ android file-system linux c++17
$endgroup$
add a comment |
$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;
c++ android file-system linux c++17
$endgroup$
add a comment |
$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;
c++ android file-system linux c++17
$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
c++ android file-system linux c++17
edited 6 mins ago
Deduplicator
11.8k1950
11.8k1950
asked 54 mins ago
prog511prog511
61
61
add a comment |
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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