[SOLVED] Recursive delete...

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

[SOLVED] Recursive delete...

Post by ne0h »

Hi,
I've find this recursive function to delete a directory, but when I try to delete a big dir (PSP) , it was deleted all the MS!!
I've try to modify it but it's not perfect!
I think there are a basically solution, but I'haven't find it!
What's wrong in this function?
It's a possible bug of sceIoRmDir or sceIoDelete funct?

Code: Select all

void RmDir(char *dir)
{
 SceIoDirent dirent;
 int fd;
 char fullname[512];
 fd = sceIoDopen(dir);
 memset(&dirent, 0, sizeof dirent);
 while (sceIoDread(fd, &dirent) > 0)
 {
  sprintf(fullname, "%s/%s", dir, dirent.d_name);

   if ((FIO_S_IFREG & (dirent.d_stat.st_mode & FIO_S_IFMT)) == 0)
      {
             RmDir(fullname);
             sceIoRmdir(fullname);
         }
  else
      {
      sceIoRemove(fullname);
      }
  }
 sceIoDclose(fd);
 sceIoRmdir(dir);
}
I think the problem is in the string fullname and in it's allocate ( not perfect english I think)
Last edited by ne0h on Sat Jun 14, 2008 12:43 am, edited 2 times in total.
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

What you call a big dir?

A directory with lots of files in it, or a directory with a really long name?
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

Oh come on ! your wonderful function is not even slightly recursive. Is it so hard to think by yourself instead of gibbering ?
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

This function is not my, I've searched and I've find this!
I call big dir a directory with a lot of dir and files
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

This function is not my, I've searched and I've find this!
I call big dir a directory with a lot of dir and files
hilde, I haven't understand your post!
Explain your problem please!
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

Hint: Your function is named RmDir().

Internally it's calling recursiveDelete()

Spot the difference.

That's what hlide meant by it not being recursive.

If you want to rename functions, ensure you rename all of them!
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

Excuse me,
I've writed bad the fuct ( the old func is named recursivedelete, but in my prog i've changed the name to RmDir)
However thanks but this is not the error
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

ne0h wrote:Excuse me,
I've writed bad the fuct ( the old func is named recursivedelete, but in my prog i've changed the name to RmDir)
However thanks but this is not the error
if so, why to remove a directory twice ? kinda fishy your excuse...

Code: Select all

void RmDir(char *dir) 
{ 
 SceIoDirent dirent; 
 int fd; 
 char fullname[512]; 
 fd = sceIoDopen(dir); 
 memset(&dirent, 0, sizeof dirent); 
 while (sceIoDread(fd, &dirent) > 0) 
 { 
  sprintf(fullname, "%s/%s", dir, dirent.d_name); 

   if ((FIO_S_IFREG & (dirent.d_stat.st_mode & FIO_S_IFMT)) == 0) 
      { 
             RmDir(fullname); 
             >>>sceIoRmdir&#40;fullname&#41;;<<< 
         &#125; 
  else 
      &#123; 
      sceIoRemove&#40;fullname&#41;; 
      &#125; 
  &#125; 
 sceIoDclose&#40;fd&#41;; 
 >>>sceIoRmdir&#40;dir&#41;;<<< 
&#125; 
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

yes, it's a error but this is not my problem
Last edited by ne0h on Fri Jun 13, 2008 2:49 am, edited 1 time in total.
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

You need to watch recursive functions - their greatest fault is the amount of stack space they consume. Look at ONE thing in the function:

Code: Select all

 char fullname&#91;512&#93;; 
Every recursion will consume that much stack space. If a dir has a thousand files, it will consume a thousand times the stack space. Make sure you have a BIG stack when using functions like this.
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

Ok, this is not the error but I've try to resolve it yet:

Code: Select all

int Rmdir&#40;char *dir&#41;
&#123;
    SceIoDirent dirent;
    int fd;
    fd = sceIoDopen&#40;dir&#41;;
    memset&#40;&dirent, 0, sizeof dirent&#41;;
    char *fullname=&#40;char*&#41;malloc&#40;1&#41;;
    free&#40;fullname&#41;;
    while &#40;sceIoDread&#40;fd, &dirent&#41; > 0&#41;
    &#123;
        fullname=&#40;char*&#41;malloc&#40;strlen&#40;dir&#41;+strlen&#40;dirent.d_name&#41;+1&#41;;
        sprintf&#40;fullname, "%s/%s", dir, dirent.d_name&#41;;
        if &#40;&#40;FIO_S_IFREG & &#40;dirent.d_stat.st_mode & FIO_S_IFMT&#41;&#41; == 0&#41;
        &#123;
             Rmdir&#40;fullname&#41;;
             sceIoRmdir&#40;fullname&#41;;
        &#125;
        else
        &#123;
             sceIoRemove&#40;fullname&#41;;
        &#125;
    &#125;
    sceIoDclose&#40;fd&#41;;
    sceIoRmdir&#40;dir&#41;;
    return 0;
&#125;
I've writed it speedly, I think is not perfect! ( For memory use )
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

RmDir("ms0:/blabla") will call :
>RmDir("ms0:/blabla/bloblo");
>>sceIoRmdir("ms0:/blabla/bloblo"); // just after sceIoDclose(fd);
>sceIoRmdir("ms0:/blabla/bloblo"); // just after RmDir(fullname);
>sceIoRmdir("ms0:/blabla"); // just after sceIoDclose(fd);

Now, who's the idiot ?
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

What? Excuse me, maybe I'm a idiot but I haven't understand your post!
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

not a surprise
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

hlide, if I want delete a folder named "FOLDER", I think if I don't write the final sceIoRmDir the dir will be not deleted
J.F., I think the memory it's not the problem, because if I try yet to delete a big directory the PSP crash!
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

well i guess C is not your forte
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

bah
Last edited by ne0h on Fri Jun 13, 2008 2:47 am, edited 2 times in total.
Question_dev
Posts: 88
Joined: Fri Aug 24, 2007 8:23 pm

Post by Question_dev »

I know a site where you can find a del dir example.
http://www.pspcoding.co.nr

Goto Coding Examples -> C/C++ Examples

Hope this helps.
Cu
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

But what's wrong in my function?
I want know the bug if is possible!
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

Yeah, maybe I've resolved!

Code: Select all

int Rmdir&#40;char *dir&#41;
&#123;
    SceIoDirent dirent;
    int fd;
    fd = sceIoDopen&#40;dir&#41;;
    memset&#40;&dirent, 0, sizeof dirent&#41;;
    char *fullname=&#40;char*&#41;malloc&#40;1&#41;;
    free&#40;fullname&#41;;
    while &#40;sceIoDread&#40;fd, &dirent&#41; > 0&#41;
    &#123;
        fullname=&#40;char*&#41;malloc&#40;strlen&#40;dir&#41;+strlen&#40;dirent.d_name&#41;+1&#41;;
        sprintf&#40;fullname, "%s/%s", dir, dirent.d_name&#41;;
        if &#40;&#40;FIO_S_IFREG & &#40;dirent.d_stat.st_mode & FIO_S_IFMT&#41;&#41; == 0&#41;
        &#123;
             Rmdir&#40;fullname&#41;;
             sceIoRmdir&#40;fullname&#41;;
        &#125;
        else
        &#123;
             sceIoRemove&#40;fullname&#41;;
--------->memset&#40;&dirent, 0, sizeof dirent&#41;;
        &#125;
    &#125;
    sceIoDclose&#40;fd&#41;;
    sceIoRmdir&#40;dir&#41;;
    return 0;
&#125;
Last edited by ne0h on Fri Jun 13, 2008 2:12 am, edited 1 time in total.
Question_dev
Posts: 88
Joined: Fri Aug 24, 2007 8:23 pm

Post by Question_dev »

Compare the two.

If you can't find it by yourself, then you need to learn c.

Also, ever heard about google? "c delete dir"

Cu
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

... I've already resolved!
I've posted before read the other example!
Thanks, Question_dev!
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

"I've already resolved! " is your motto ? it sounds quite repetive to my ears... and unnecessary to say
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

I've writed this funct but this uses all the RAM!!!!
Why?

Code: Select all

int Rmdir&#40;char *dir&#41;
&#123;
    SceIoDirent dirent;
    int fd;
    fd = sceIoDopen&#40;dir&#41;;
    memset&#40;&dirent, 0, sizeof dirent&#41;;
    while &#40;sceIoDread&#40;fd, &dirent&#41; > 0&#41;
    &#123;
        char fullname&#91;strlen&#40;dir&#41;+strlen&#40;dirent.d_name&#41;+1&#93;;
        sprintf&#40;fullname, "%s/%s", dir, dirent.d_name&#41;;
        if &#40;&#40;FIO_S_IFREG & &#40;dirent.d_stat.st_mode & FIO_S_IFMT&#41;&#41; == 0&#41;
        &#123;
             Rmdir&#40;fullname&#41;;
             sceIoRmdir&#40;fullname&#41;;
        &#125;
        else
        &#123;
             sceIoRemove&#40;fullname&#41;;
             memset&#40;&dirent, 0, sizeof dirent&#41;;
        &#125;
    &#125;
    sceIoDclose&#40;fd&#41;;
    sceIoRmdir&#40;dir&#41;;
    return 0;
&#125;
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

Thanks!...
Last edited by ne0h on Fri Jun 13, 2008 2:43 am, edited 1 time in total.
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

I lol'd at this thread.
@ne0h, make sure you learn some C coding before saying hlide is an idiot and does not know C and is more stupid than you. I believe hlide has a lot more C experience than you.
Also, it's not "I've writed", writed does not exists.
I'ts I've written or I wrote. (at least this is what I know from my english lol)

@Question Dev, maybe google will not be his best friend for coding lol and I think you are not the right person to say "SEARCH" as you were like him a few months ago.

Edit, don't bump your posts please. Wait for an answer if you even deserve one.
Image
Upgrade your PSP
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

bah...
I have understand what hlide intend to say yet...
Last edited by ne0h on Fri Jun 13, 2008 2:50 am, edited 1 time in total.
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

It's I haven't understood. (irregular verb)
he meant you were trying to delete the same directory after it was deleted.

Code: Select all

             RmDir&#40;fullname&#41;;
             sceIoRmdir&#40;fullname&#41;; 
you run your RmDir function to delete the folder then you run sony's function to delete the folder, again.

Do you understand?
Image
Upgrade your PSP
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

yes, yes, I have understand...
Pirata Nervo
Posts: 409
Joined: Tue Oct 09, 2007 4:22 am

Post by Pirata Nervo »

ok, now how do you know your function is using all the ram?
Image
Upgrade your PSP
Post Reply