5
// Choose the size so it is a power of 2. Example (size-1)= 11111111.
6
const t_int NPS3SerialFileReader::sBufferSize = 1024;
8
NPS3SerialFileReader::NPS3SerialFileReader(t_int InFileDescriptor, NOutputDevice& InError, t_int InSize)
9
: m_FileDescriptor (InFileDescriptor)
16
m_Buffer = new BYTE[sBufferSize];
18
NPS3SerialFileReader::~NPS3SerialFileReader()
20
INL_SAFE_DELETE_ARRAY(m_Buffer);
27
bool NPS3SerialFileReader::Precache(t_int PrecacheOffset, t_int PrecacheSize)
29
// Only pre-cache at current position and avoid work if pre-caching same offset twice.
30
if((m_FilePos == PrecacheOffset) && (!m_BufferBase || !m_BufferCount || m_BufferBase != m_FilePos))
32
m_BufferBase = m_FilePos;
33
// (sBufferSize - 1) contains only '1', i.e 1111111111.
34
// So (m_FilePos & (sBufferSize-1)) is equal to m_FilePos if m_FilePos <= (sBufferSize-1).
35
m_BufferCount = Min<t_s64>(Min<t_s64>(PrecacheSize, (t_int)(sBufferSize - (m_FilePos & (sBufferSize-1)))), m_FileSize - m_FilePos);
37
//GTotalBytesReadViaFileManager += m_BufferCount;
38
cellFsRead(m_FileDescriptor, m_Buffer, m_BufferCount, &Count);
39
if(Count != m_BufferCount)
42
m_Error.LogFunction(TEXT("ReadFile failed: Count=%i BufferCount=%i"), Count, m_BufferCount);
48
t_s64 NPS3SerialFileReader::Seek(t_s64 InPos, NSerializer::SeekPos seekpos)
50
nuxAssert(InPos >= 0);
51
nuxAssert(InPos <= m_FileSize);
54
// Because we precache our reads, we must perform Seek accordingly.
56
t_s64 pos = m_FilePos;
59
// Set the file pointer to m_FilePos.
60
if(cellFsLseek(m_FileDescriptor, pos, CELL_FS_SEEK_SET, &filepos) == 0)
63
m_Error.LogFunction(TEXT("[NPS3SerialFileReader::Seek] Seek to %i has failed."), InPos);
66
// Now the file pointer is current with what we have read so far.
69
if(cellFsLseek(m_FileDescriptor, pos, (seekpos == SeekStart) ? CELL_FS_SEEK_SET : (seekpos == SeekCurrent) ? CELL_FS_SEEK_CUR : CELL_FS_SEEK_END, &filepos) == 0)
72
m_Error.LogFunction(TEXT("[NPS3SerialFileReader::Seek] Seek to %i has failed."), InPos);
77
Precache(m_FilePos, sBufferSize);
81
t_s64 NPS3SerialFileReader::Tell()
85
// LARGE_INTEGER filepos;
87
// ::SetFilePointerEx(m_FileDescriptor, pos, &filepos, FILE_CURRENT);
88
// return filepos.QuadPart;
93
t_s64 NPS3SerialFileReader::GetFileSize()
98
bool NPS3SerialFileReader::Close()
102
cellFsClose(m_FileDescriptor);
104
m_FileDescriptor = 0;
108
void NPS3SerialFileReader::Serialize(void* Dest, t_u64 Length)
113
t_int DataSize = Min<t_s64>(Length, m_BufferBase + m_BufferCount - m_FilePos);
116
if(Length >= sBufferSize)
119
//GTotalBytesReadViaFileManager += Length;
120
cellFsRead(m_FileDescriptor, Dest, Length, &Count);
124
m_Error.LogFunction(TEXT("[NPS3SerialFileReader::Serialize] ReadFile failed: Read Requested=%i Read Count=%i"), Length, Count);
127
m_BufferBase += Length;
130
Precache(m_FilePos, t_s32_max);
131
DataSize = Min<t_s64>(Length, m_BufferBase + m_BufferCount - m_FilePos);
135
m_Error.LogFunction(TEXT("ReadFile beyond EOF %i+%i/%i"), m_FilePos, Length, m_FileSize);
140
Memcpy(Dest, m_Buffer + m_FilePos - m_BufferBase, DataSize);
141
m_FilePos += DataSize;
143
Dest = (BYTE*)Dest + DataSize;
146
//////////////////////////////////////////////////////////////////////////
147
// Choose the size so it is a power of 2. Example (size-1)= 11111111.
148
const t_int NPS3SerialFileWriter::sBufferSize = 4096;
150
NPS3SerialFileWriter::NPS3SerialFileWriter(t_int InFileDescriptor, NOutputDevice& InError, t_int InPos)
151
: m_FileDescriptor(InFileDescriptor)
156
m_Buffer = new BYTE[sBufferSize];
159
NPS3SerialFileWriter::~NPS3SerialFileWriter()
161
INL_SAFE_DELETE_ARRAY(m_Buffer);
164
m_FileDescriptor = 0;
167
t_s64 NPS3SerialFileWriter::Seek(t_s64 InPos, NSerializer::SeekPos seekpos)
172
if(cellFsLseek(m_FileDescriptor, pos, (seekpos == SeekStart) ? CELL_FS_SEEK_SET : (seekpos == SeekCurrent) ? CELL_FS_SEEK_CUR : CELL_FS_SEEK_END, &filepos) == 0)
175
m_Error.LogFunction(TEXT("SeekFailed"));
181
t_s64 NPS3SerialFileWriter::Tell()
186
cellFsLseek(m_FileDescriptor, pos, CELL_FS_SEEK_CUR, &filepos);
190
bool NPS3SerialFileWriter::Close()
193
if(m_FileDescriptor && !cellFsClose(m_FileDescriptor))
196
m_Error.LogFunction(TEXT("WriteFailed"));
198
m_FileDescriptor = 0;
202
t_s64 NPS3SerialFileWriter::GetFileSize()
205
if(cellFsFstat(m_FileDescriptor, &sb) != CELL_FS_SUCCEEDED)
207
m_Error.LogFunction(TEXT("[NPS3SerialFileWriter::GetFileSize] Failed."));
213
void NPS3SerialFileWriter::Serialize(void* V, t_u64 Length)
219
while(Length > (FreeSpace = sBufferSize - m_BufferCount))
221
// m_Buffer is Full. Write it to the file.
222
Memcpy(m_Buffer + m_BufferCount, V, FreeSpace);
223
m_BufferCount += FreeSpace;
225
V = (BYTE*)V + FreeSpace;
230
Memcpy(m_Buffer + m_BufferCount, V, Length);
231
m_BufferCount += Length; // Count the number of Characters stored in m_Buffer.
234
void NPS3SerialFileWriter::Flush()
236
//GTotalBytesWrittenViaFileManager += m_BufferCount;
240
CellFsErrno error = cellFsWrite(m_FileDescriptor, m_Buffer, m_BufferCount, &Result);
241
if(error != CELL_FS_SUCCEEDED)
244
m_Error.LogFunction(TEXT("[NPS3SerialFileWriter::Flush] Write error."));
249
//////////////////////////////////////////////////////////////////////////
250
INL_IMPLEMENT_GLOBAL_OBJECT(NFileManagerPS3);
252
void NFileManagerPS3::Constructor()
256
void NFileManagerPS3::Destructor()
260
NSerializer* NFileManagerPS3::CreateFileReader(const TCHAR* Filename, DWORD Flags, NOutputDevice& Error)
263
if(cellFsOpen(TCHAR_TO_ANSI(Filename), CELL_FS_O_RDWR, &FileDesc, NULL, 0) != CELL_FS_SUCCEEDED)
265
if(Flags & NSerializer::OutputErrorIfFail)
267
nuxError(TEXT("Failed to read file: %s"), Filename);
273
if(cellFsFstat(FileDesc, &sb) != CELL_FS_SUCCEEDED)
275
cellFsClose(FileDesc);
278
return new NPS3SerialFileReader(FileDesc, Error, sb.st_size);
281
NSerializer* NFileManagerPS3::CreateFileWriter(const TCHAR* Filename,
283
NOutputDevice& Error)
285
if(Flags & NSerializer::OverWriteReadOnly)
287
cellFsChmod(TCHAR_TO_ANSI(Filename), CELL_FS_S_IRUSR|CELL_FS_S_IWUSR);
291
if((Flags & NSerializer::Read) && (Flags & NSerializer::Write))
293
ModeFlags |= CELL_FS_O_RDWR;
295
else if(Flags & NSerializer::Read)
297
ModeFlags |= CELL_FS_O_RDONLY;
299
else if(Flags & NSerializer::Write)
301
ModeFlags |= CELL_FS_O_WRONLY;
304
ModeFlags |= (Flags & NSerializer::Append) ? CELL_FS_O_APPEND : 0;
305
ModeFlags |= (Flags & NSerializer::NoOverWrite) ? CELL_FS_O_EXCL /*fail if the file already exist*/: CELL_FS_O_CREAT /*create the file if it does not exist*/;
308
if(cellFsOpen(TCHAR_TO_ANSI(Filename), ModeFlags, &FileDesc, NULL, 0) != CELL_FS_SUCCEEDED)
310
if(Flags & NSerializer::OutputErrorIfFail)
312
nuxError(TEXT("[NFileManagerPS3::CreateFileWriter] Failed to create file %s."), Filename);
317
if(Flags & NSerializer::Append)
319
cellFsLseek(FileDesc, 0, CELL_FS_SEEK_END, &Pos);
322
if(cellFsFstat(FileDesc, &sb) != CELL_FS_SUCCEEDED)
324
cellFsClose(FileDesc);
327
return new NPS3SerialFileWriter(FileDesc, Error, sb.st_size);
330
t_s64 NFileManagerPS3::FileSize(const TCHAR* Filename)
333
if(cellFsStat(TCHAR_TO_ANSI(Filename), &sb) != CELL_FS_SUCCEEDED)
335
nuxDebugMsg(TEXT("[NFileManagerPS3::FileSize] Can't get file size"));
338
if(sb.st_mode & CELL_FS_S_IFDIR)
340
// This is a directory
343
if(sb.st_mode & CELL_FS_S_IFWHT)
345
// The type is unknown
348
if(sb.st_mode & CELL_FS_S_IFLNK)
350
// This is a symbolic link
356
bool NFileManagerPS3::FileExist(const TCHAR* Filename)
359
if(cellFsStat(TCHAR_TO_ANSI(Filename), &sb) != CELL_FS_SUCCEEDED)
366
int NFileManagerPS3::Copy(const TCHAR* DestFile,
367
const TCHAR* SrcFile,
368
bool OverWriteExisting,
369
bool OverWriteReadOnly,
370
NFileTransferMonitor* Monitor)
376
bool NFileManagerPS3::Delete(const TCHAR* Filename, bool OverWriteReadOnly)
378
if(OverWriteReadOnly)
380
cellFsChmod(TCHAR_TO_ANSI(Filename), CELL_FS_S_IRUSR|CELL_FS_S_IWUSR);
382
if(cellFsUnlink(TCHAR_TO_ANSI(Filename)) != CELL_FS_SUCCEEDED)
384
nuxDebugMsg(TEXT("[NFileManagerPS3::Delete] Error deleting file '%s'."), Filename);
390
bool NFileManagerPS3::IsReadOnly(const TCHAR* Filename)
393
if(cellFsStat(TCHAR_TO_ANSI(Filename), &sb) != CELL_FS_SUCCEEDED)
395
nuxDebugMsg(TEXT("[NFileManagerPS3::IsReadOnly] Error reading file status '%s'."), Filename);
398
if((sb.st_mode & CELL_FS_S_IRUSR) && !(sb.st_mode & CELL_FS_S_IWUSR))
405
bool NFileManagerPS3::IsDirectory(const TCHAR* DirectoryName)
408
if(cellFsStat(TCHAR_TO_ANSI(DirectoryName), &sb) != CELL_FS_SUCCEEDED)
410
nuxDebugMsg(TEXT("[NFileManagerPS3::IsDirectory] Error reading file status '%s'."), DirectoryName);
413
if(sb.st_mode & CELL_FS_S_IFDIR)
420
bool NFileManagerPS3::IsHidden(const TCHAR* Filename)
426
@return TRUE is the file exist.
428
bool NFileManagerPS3::GetFileAttribute(const TCHAR* Filename, bool& isDirectory, bool& isReadOnly, bool& isHidden, t_s64& Size)
436
if(cellFsStat(TCHAR_TO_ANSI(Filename), &sb) != CELL_FS_SUCCEEDED)
441
if(sb.st_mode & CELL_FS_S_IFDIR)
445
if((sb.st_mode & CELL_FS_S_IRUSR) && !(sb.st_mode & CELL_FS_S_IWUSR))
453
bool NFileManagerPS3::Move(const TCHAR* Dest, const TCHAR* Src, bool OverWriteExisting, bool OverWriteReadOnly, NFileTransferMonitor* Monitor)
459
bool NFileManagerPS3::MakeDirectory(const TCHAR* Path, bool CreateCompletePath)
461
if(CreateCompletePath)
463
return NFileManagerGeneric::MakeDirectory(Path, CreateCompletePath);
466
CellFsErrno error = cellFsMkdir(TCHAR_TO_ANSI(Path),
467
CELL_FS_S_IRUSR|CELL_FS_S_IWUSR|CELL_FS_S_IXUSR|
468
CELL_FS_S_IRGRP|CELL_FS_S_IWGRP|CELL_FS_S_IXGRP|
469
CELL_FS_S_IROTH|CELL_FS_S_IWOTH|CELL_FS_S_IXOTH);
471
// CELL_FS_EEXIST = -2147418092 = 0x80010014
472
if((error != CELL_FS_SUCCEEDED) && (error != CELL_FS_EEXIST))
474
nuxDebugMsg(TEXT("[NFileManagerPS3::MakeDirectory] Error creating directory '%s'."), Path);
480
bool NFileManagerPS3::DeleteDirectory(const TCHAR* Path, bool DeleteContentFirst)
482
// if(DeleteContentFirst)
484
// return NFileManagerGeneric::DeleteDirectory(Path, DeleteContentFirst);
486
// if((::RemoveDirectory(Path) == 0) && (::GetLastError() != ERROR_FILE_NOT_FOUND))
488
// nuxDebugMsg(TEXT("[NFileManagerWindows::DeleteDirectory] Error deleting directory '%s' (GetLastError: %d)"), Path, ::GetLastError());