sceIo problems

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

Moderators: cheriff, TyRaNiD

Post Reply
mplacona
Posts: 28
Joined: Tue Aug 07, 2007 7:07 am

sceIo problems

Post by mplacona »

Hello guys,

Following up my last post here about copying an entire folder, I started to write my functions to do the job.

It compiles fine, but when I run it on the PSP, it freezes and shut down.

I'm trying to do a basic copy txt file and paste. The idea is to copy all the file contents and create a new text file with the contents.

It's very strange, 'cause basically it's kind of creating the files, but is corrupting the whole thing. And also, it is creating it as a binary. Is there any way to do it in a normal way, like "cloning" the file, and without corrupting it?

Here's my code. Is thre anything wrong there?

Code: Select all

void copy(){
char write_buffer[128*1024];

//Opens the file
int target=sceIoOpen("ms0:/test.txt", PSP_O_WRONLY, 0777);
int newFile=sceIoOpen("ms0:/test1.txt", PSP_O_APPEND|PSP_O_CREAT, 0777);

//Creates a buffer for the file contents
char* data;

//Reads the contents
sceIoRead(target, &data, sizeof(data));

sprintf(write_buffer, data);
sceIoWrite(newFile, write_buffer, sizeof(write_buffer));

sceIoClose(target);
sceIoClose(newFile);
}
Cheers
flatmush
Posts: 28
Joined: Tue Aug 07, 2007 9:15 am
Location: Here
Contact:

Post by flatmush »

OK, that is the most fudged code I can imagine. Why are you reading from a file you opened for writing, then appending it to another file. You are then attempting to read the data into an uninitialized array. Then you proceed to copy data for no reason using string functions which makes no sense. Also sizeof doesn't return the size of the array, it returns the size of the pointer which is 4 bytes.

What you are looking for is something like this:

Code: Select all

int fileCopy(char* inSrc, char* inDest) {
     SceUID tempSrc = sceIoOpen(inSrc, PSP_O_RDONLY, 0777);
     if(!tempSrc)
          return 0;
     SceUID tempDest = sceIoOpen(inDest, PSP_O_WRONLY | PSP_O_CREAT, 0777);
     if(!tempDest) {
          sceIoClose(tempSrc);
          return 0;
     }

     char tempData[1024];
     int tempLen = sceIoRead(tempSrc, tempData, 1024);

     while(tempLen) {
          sceIoWrite(tempDest, tempData, tempLen);
          tempLen = sceIoRead(tempSrc, tempData, 1024);
     }

     sceIoClose(tempDest);
     sceIoClose(tempSrc);

     return 1;
} 
I haven't tested that or checked it works, but I can't see any reason why it wouldn't. You will also notice that this streams the data across, so it should be able to copy files of any size. You could improve on the code further by making it recursive, and maybe using a funky for loop instead of that while.
mplacona
Posts: 28
Joined: Tue Aug 07, 2007 7:07 am

Yes

Post by mplacona »

Hi flatmush, you're right! The code was a bit messed up, and your code was great, it worked straight away. I just did some changes and it's fine now.

I was now wondering if it's possible to copy data from the PSP to a PC via USB. I know that the mass storage devices have some kind of limitations to do the job the other way round, but do you think I can do something in C to do that?

Thank you very much for your code, and soryy for mine [:(], but as I said, I'm still starting in the C development.

Cheers
flatmush
Posts: 28
Joined: Tue Aug 07, 2007 9:15 am
Location: Here
Contact:

Post by flatmush »

OK, that isn't my area of expertise, tyranid would be better suited to answer this question, but the usb driver is only programmed to act as an external storage device and the interface is on/off style I think. So basically you would have to write your own usb driver, and possibly write a driver for the pc if there isn't already one that does what you want.
It's probably not worth the effort.

Oh, and I decided to actually read the pspiofilemgr.h and I updated the filecopy slightly to make it a little better.

Code: Select all

int fileCopy(char* inSrc, char* inDest) {
     SceUID tempSrc = sceIoOpen(inSrc, PSP_O_RDONLY, 0777);
     if(!tempSrc)
          return -1;
     SceUID tempDest = sceIoOpen(inDest, PSP_O_WRONLY | PSP_O_CREAT, 0777);
     if(!tempDest) {
          sceIoClose(tempSrc);
          return -2;
     }

     char tempData[1024];
     int tempRead = sceIoRead(tempSrc, tempData, 1024);
     int tempWritten;

     while(tempRead > 0) {
          tempWritten = sceIoWrite(tempDest, tempData, tempRead);
          if(tempWritten != tempRead) {
               sceIoClose(tempDest);
               sceIoClose(tempSrc);
               sceIoRemove(inDest);
               return -3;
          }
          tempRead = sceIoRead(tempSrc, tempData, 1024);

     }

     sceIoClose(tempDest);
     sceIoClose(tempSrc);

     return 1;
} 
Question_dev
Posts: 88
Joined: Fri Aug 24, 2007 8:23 pm

Post by Question_dev »

is it also possible to copy dirs with it ?

Code: Select all

int fileCopy(char* inSrc, char* inDest) { 
     SceUID tempSrc = sceIoOpen(inSrc, PSP_O_RDONLY, 0777); 
     if(!tempSrc) 
          return -1; 
     SceUID tempDest = sceIoOpen(inDest, PSP_O_WRONLY | PSP_O_CREAT, 0777); 
     if(!tempDest) { 
          sceIoClose(tempSrc); 
          return -2; 
     } 

     char tempData[1024]; 
     int tempRead = sceIoRead(tempSrc, tempData, 1024); 
     int tempWritten; 

     while(tempRead > 0) { 
          tempWritten = sceIoWrite(tempDest, tempData, tempRead); 
          if(tempWritten != tempRead) { 
               sceIoClose(tempDest); 
               sceIoClose(tempSrc); 
               sceIoRemove(inDest); 
               return -3; 
          } 
          tempRead = sceIoRead(tempSrc, tempData, 1024); 

     } 

     sceIoClose(tempDest); 
     sceIoClose(tempSrc); 

     return 1; 
} 
moonlight
Posts: 567
Joined: Wed Oct 26, 2005 7:46 pm

Post by moonlight »

Btw, I wouldn't place a 128*1024 array in the stack...
Post Reply