~hikiko/nux/arb-srgba-shader

« back to all changes in this revision

Viewing changes to NuxCore/FileManager/NFileManagerPS3.cpp

  • Committer: Neil Jagdish Patel
  • Date: 2010-09-01 19:25:37 UTC
  • Revision ID: neil.patel@canonical.com-20100901192537-mfz7rm6q262pewg6
Import and build NuxCore

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "NKernel.h"
 
2
 
 
3
NAMESPACE_BEGIN
 
4
 
 
5
// Choose the size so it is a power of 2. Example (size-1)= 11111111.
 
6
const t_int  NPS3SerialFileReader::sBufferSize = 1024;
 
7
 
 
8
NPS3SerialFileReader::NPS3SerialFileReader(t_int InFileDescriptor, NOutputDevice& InError, t_int InSize)
 
9
:   m_FileDescriptor    (InFileDescriptor)
 
10
,   m_Error             (InError)
 
11
,   m_FileSize          (InSize)
 
12
,   m_FilePos           (0)
 
13
,   m_BufferBase        (0)
 
14
,   m_BufferCount       (0)
 
15
{
 
16
    m_Buffer = new BYTE[sBufferSize];
 
17
}
 
18
NPS3SerialFileReader::~NPS3SerialFileReader()
 
19
{
 
20
    INL_SAFE_DELETE_ARRAY(m_Buffer);
 
21
    if(m_FileDescriptor)
 
22
    {
 
23
        Close();
 
24
    }
 
25
}
 
26
 
 
27
bool NPS3SerialFileReader::Precache(t_int PrecacheOffset, t_int PrecacheSize)
 
28
{
 
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))
 
31
    {
 
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);
 
36
        t_u64 Count = 0;
 
37
        //GTotalBytesReadViaFileManager += m_BufferCount;
 
38
        cellFsRead(m_FileDescriptor, m_Buffer, m_BufferCount, &Count);
 
39
        if(Count != m_BufferCount)
 
40
        {
 
41
            m_ErrorCode = 1;
 
42
            m_Error.LogFunction(TEXT("ReadFile failed: Count=%i BufferCount=%i"), Count, m_BufferCount);
 
43
        }
 
44
    }
 
45
    return TRUE;
 
46
}
 
47
 
 
48
t_s64 NPS3SerialFileReader::Seek(t_s64 InPos, NSerializer::SeekPos seekpos)
 
49
{
 
50
    nuxAssert(InPos >= 0);
 
51
    nuxAssert(InPos <= m_FileSize);
 
52
 
 
53
    Flush();
 
54
    // Because we precache our reads, we must perform Seek accordingly.
 
55
 
 
56
    t_s64 pos = m_FilePos;
 
57
    t_u64 filepos = 0;
 
58
 
 
59
    // Set the file pointer to m_FilePos.
 
60
    if(cellFsLseek(m_FileDescriptor, pos, CELL_FS_SEEK_SET, &filepos) == 0)
 
61
    {
 
62
        m_ErrorCode = 1;
 
63
        m_Error.LogFunction(TEXT("[NPS3SerialFileReader::Seek] Seek to %i has failed."), InPos);
 
64
    }
 
65
 
 
66
    // Now the file pointer is current with what we have read so far.
 
67
    pos = InPos;
 
68
    filepos = 0;
 
69
    if(cellFsLseek(m_FileDescriptor, pos, (seekpos == SeekStart) ? CELL_FS_SEEK_SET : (seekpos == SeekCurrent) ? CELL_FS_SEEK_CUR : CELL_FS_SEEK_END, &filepos) == 0)
 
70
    {
 
71
        m_ErrorCode = 1;
 
72
        m_Error.LogFunction(TEXT("[NPS3SerialFileReader::Seek] Seek to %i has failed."), InPos);
 
73
    }
 
74
    m_FilePos   = filepos;
 
75
    m_BufferBase  = 0;
 
76
    m_BufferCount = 0;
 
77
    Precache(m_FilePos, sBufferSize);
 
78
    return filepos;
 
79
}
 
80
 
 
81
t_s64 NPS3SerialFileReader::Tell()
 
82
{
 
83
    //     Flush();
 
84
    //     LARGE_INTEGER pos;
 
85
    //     LARGE_INTEGER filepos;
 
86
    //     pos.QuadPart = 0;
 
87
    //     ::SetFilePointerEx(m_FileDescriptor, pos, &filepos, FILE_CURRENT);
 
88
    //     return filepos.QuadPart;
 
89
 
 
90
    return m_FilePos;
 
91
}
 
92
 
 
93
t_s64 NPS3SerialFileReader::GetFileSize()
 
94
{
 
95
    return m_FileSize;
 
96
}
 
97
 
 
98
bool NPS3SerialFileReader::Close()
 
99
{
 
100
    if(m_FileDescriptor)
 
101
    {
 
102
        cellFsClose(m_FileDescriptor);
 
103
    }
 
104
    m_FileDescriptor = 0;
 
105
    return !m_ErrorCode;
 
106
}
 
107
 
 
108
void NPS3SerialFileReader::Serialize(void* Dest, t_u64 Length)
 
109
{
 
110
    nuxAssert(Dest);
 
111
    while(Length > 0)
 
112
    {
 
113
        t_int DataSize = Min<t_s64>(Length, m_BufferBase + m_BufferCount - m_FilePos);
 
114
        if(DataSize == 0)
 
115
        {
 
116
            if(Length >= sBufferSize)
 
117
            {
 
118
                t_u64 Count=0;
 
119
                //GTotalBytesReadViaFileManager += Length;              
 
120
                cellFsRead(m_FileDescriptor, Dest, Length, &Count);
 
121
                if(Count!=Length)
 
122
                {
 
123
                    m_ErrorCode = 1;
 
124
                    m_Error.LogFunction(TEXT("[NPS3SerialFileReader::Serialize] ReadFile failed: Read Requested=%i Read Count=%i"), Length, Count);
 
125
                }
 
126
                m_FilePos += Length;
 
127
                m_BufferBase += Length;
 
128
                return;
 
129
            }
 
130
            Precache(m_FilePos, t_s32_max);
 
131
            DataSize = Min<t_s64>(Length, m_BufferBase + m_BufferCount - m_FilePos);
 
132
            if(DataSize <= 0)
 
133
            {
 
134
                m_ErrorCode = 1;
 
135
                m_Error.LogFunction(TEXT("ReadFile beyond EOF %i+%i/%i"), m_FilePos, Length, m_FileSize);
 
136
            }
 
137
            if(m_ErrorCode)
 
138
                return;
 
139
        }
 
140
        Memcpy(Dest, m_Buffer + m_FilePos - m_BufferBase, DataSize);
 
141
        m_FilePos   += DataSize;
 
142
        Length      -= DataSize;
 
143
        Dest        = (BYTE*)Dest + DataSize;
 
144
    }
 
145
}
 
146
//////////////////////////////////////////////////////////////////////////
 
147
// Choose the size so it is a power of 2. Example (size-1)= 11111111.
 
148
const t_int  NPS3SerialFileWriter::sBufferSize = 4096;
 
149
 
 
150
NPS3SerialFileWriter::NPS3SerialFileWriter(t_int InFileDescriptor, NOutputDevice& InError, t_int InPos)
 
151
:   m_FileDescriptor(InFileDescriptor)
 
152
,   m_Error         (InError)
 
153
,   m_Pos           (InPos)
 
154
,   m_BufferCount   (0)
 
155
{
 
156
    m_Buffer = new BYTE[sBufferSize];
 
157
}
 
158
 
 
159
NPS3SerialFileWriter::~NPS3SerialFileWriter()
 
160
{
 
161
    INL_SAFE_DELETE_ARRAY(m_Buffer);
 
162
    if(m_FileDescriptor)
 
163
        Close();
 
164
    m_FileDescriptor = 0;
 
165
}
 
166
 
 
167
t_s64 NPS3SerialFileWriter::Seek(t_s64 InPos, NSerializer::SeekPos seekpos)
 
168
{
 
169
    Flush();
 
170
    t_s64 pos = InPos;
 
171
    t_u64 filepos = 0;
 
172
    if(cellFsLseek(m_FileDescriptor, pos, (seekpos == SeekStart) ? CELL_FS_SEEK_SET : (seekpos == SeekCurrent) ? CELL_FS_SEEK_CUR : CELL_FS_SEEK_END, &filepos) == 0)
 
173
    {
 
174
        m_ErrorCode = 1;
 
175
        m_Error.LogFunction(TEXT("SeekFailed"));
 
176
    }
 
177
    m_Pos = filepos;
 
178
    return filepos;
 
179
}
 
180
 
 
181
t_s64 NPS3SerialFileWriter::Tell()
 
182
{
 
183
    Flush();
 
184
    t_s64 pos = 0;
 
185
    t_u64 filepos = 0;
 
186
    cellFsLseek(m_FileDescriptor, pos, CELL_FS_SEEK_CUR, &filepos);
 
187
    return filepos;
 
188
}
 
189
 
 
190
bool NPS3SerialFileWriter::Close()
 
191
{
 
192
    Flush();
 
193
    if(m_FileDescriptor && !cellFsClose(m_FileDescriptor))
 
194
    {
 
195
        m_ErrorCode = 1;
 
196
        m_Error.LogFunction(TEXT("WriteFailed"));
 
197
    }
 
198
    m_FileDescriptor = 0;
 
199
    return !m_ErrorCode;
 
200
}
 
201
 
 
202
t_s64 NPS3SerialFileWriter::GetFileSize()
 
203
{
 
204
    CellFsStat sb;
 
205
    if(cellFsFstat(m_FileDescriptor, &sb) != CELL_FS_SUCCEEDED)
 
206
    {
 
207
        m_Error.LogFunction(TEXT("[NPS3SerialFileWriter::GetFileSize] Failed."));
 
208
        return -1;
 
209
    }
 
210
    return sb.st_size;
 
211
}
 
212
 
 
213
void NPS3SerialFileWriter::Serialize(void* V, t_u64 Length)
 
214
{
 
215
    nuxAssert(V);
 
216
 
 
217
    m_Pos += Length;
 
218
    t_int FreeSpace;
 
219
    while(Length > (FreeSpace = sBufferSize - m_BufferCount))
 
220
    {
 
221
        // m_Buffer is Full. Write it to the file.
 
222
        Memcpy(m_Buffer + m_BufferCount, V, FreeSpace);
 
223
        m_BufferCount   += FreeSpace;
 
224
        Length          -= FreeSpace;
 
225
        V               = (BYTE*)V + FreeSpace;
 
226
        Flush();
 
227
    }
 
228
    if(Length)
 
229
    {
 
230
        Memcpy(m_Buffer + m_BufferCount, V, Length);
 
231
        m_BufferCount += Length; // Count the number of Characters stored in m_Buffer.
 
232
    }
 
233
}
 
234
void NPS3SerialFileWriter::Flush()
 
235
{
 
236
    //GTotalBytesWrittenViaFileManager += m_BufferCount;
 
237
    if(m_BufferCount)
 
238
    {
 
239
        t_u64 Result = 0;
 
240
        CellFsErrno error = cellFsWrite(m_FileDescriptor, m_Buffer, m_BufferCount, &Result);
 
241
        if(error != CELL_FS_SUCCEEDED)
 
242
        {
 
243
            m_ErrorCode = 1;
 
244
            m_Error.LogFunction(TEXT("[NPS3SerialFileWriter::Flush] Write error."));
 
245
        }
 
246
    }
 
247
    m_BufferCount = 0;
 
248
}
 
249
//////////////////////////////////////////////////////////////////////////
 
250
INL_IMPLEMENT_GLOBAL_OBJECT(NFileManagerPS3);
 
251
 
 
252
void NFileManagerPS3::Constructor()
 
253
{
 
254
}
 
255
 
 
256
void NFileManagerPS3::Destructor()
 
257
{
 
258
}
 
259
 
 
260
NSerializer* NFileManagerPS3::CreateFileReader(const TCHAR* Filename, DWORD Flags, NOutputDevice& Error)
 
261
{
 
262
    t_int FileDesc;
 
263
    if(cellFsOpen(TCHAR_TO_ANSI(Filename), CELL_FS_O_RDWR, &FileDesc, NULL, 0) != CELL_FS_SUCCEEDED)
 
264
    {
 
265
        if(Flags & NSerializer::OutputErrorIfFail)
 
266
        {
 
267
            nuxError(TEXT("Failed to read file: %s"), Filename);
 
268
        }
 
269
        return NULL;
 
270
    }
 
271
 
 
272
    CellFsStat sb;
 
273
    if(cellFsFstat(FileDesc, &sb) != CELL_FS_SUCCEEDED)
 
274
    {
 
275
        cellFsClose(FileDesc);
 
276
        return NULL;
 
277
    }
 
278
    return new NPS3SerialFileReader(FileDesc, Error, sb.st_size);
 
279
}
 
280
 
 
281
NSerializer* NFileManagerPS3::CreateFileWriter(const TCHAR* Filename,
 
282
                                               DWORD Flags,
 
283
                                               NOutputDevice& Error)
 
284
{
 
285
    if(Flags & NSerializer::OverWriteReadOnly)
 
286
    {
 
287
        cellFsChmod(TCHAR_TO_ANSI(Filename), CELL_FS_S_IRUSR|CELL_FS_S_IWUSR);
 
288
    }
 
289
 
 
290
    DWORD  ModeFlags  = 0;
 
291
    if((Flags & NSerializer::Read) && (Flags & NSerializer::Write))
 
292
    {
 
293
        ModeFlags |= CELL_FS_O_RDWR;
 
294
    }
 
295
    else if(Flags & NSerializer::Read)
 
296
    {
 
297
        ModeFlags |= CELL_FS_O_RDONLY;
 
298
    }
 
299
    else if(Flags & NSerializer::Write)
 
300
    {
 
301
        ModeFlags |= CELL_FS_O_WRONLY;
 
302
    }
 
303
 
 
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*/;
 
306
 
 
307
    t_int FileDesc = 0;
 
308
    if(cellFsOpen(TCHAR_TO_ANSI(Filename), ModeFlags, &FileDesc, NULL, 0) != CELL_FS_SUCCEEDED)
 
309
    {
 
310
        if(Flags & NSerializer::OutputErrorIfFail)
 
311
        {
 
312
            nuxError(TEXT("[NFileManagerPS3::CreateFileWriter] Failed to create file %s."), Filename);
 
313
        }
 
314
        return NULL;
 
315
    }
 
316
    t_u64 Pos = 0;
 
317
    if(Flags & NSerializer::Append)
 
318
    {
 
319
        cellFsLseek(FileDesc, 0, CELL_FS_SEEK_END, &Pos);
 
320
    }
 
321
    CellFsStat sb;
 
322
    if(cellFsFstat(FileDesc, &sb) != CELL_FS_SUCCEEDED)
 
323
    {
 
324
        cellFsClose(FileDesc);
 
325
        return NULL;
 
326
    }
 
327
    return new NPS3SerialFileWriter(FileDesc, Error, sb.st_size);
 
328
}
 
329
 
 
330
t_s64 NFileManagerPS3::FileSize(const TCHAR* Filename)
 
331
{
 
332
    CellFsStat sb;
 
333
    if(cellFsStat(TCHAR_TO_ANSI(Filename), &sb) != CELL_FS_SUCCEEDED)
 
334
    {
 
335
        nuxDebugMsg(TEXT("[NFileManagerPS3::FileSize] Can't get file size"));
 
336
        return 0;
 
337
    }
 
338
    if(sb.st_mode & CELL_FS_S_IFDIR)
 
339
    {
 
340
        // This is a directory
 
341
        return 0;
 
342
    }
 
343
    if(sb.st_mode & CELL_FS_S_IFWHT)
 
344
    {
 
345
        // The type is unknown
 
346
        //return 0;
 
347
    }
 
348
    if(sb.st_mode & CELL_FS_S_IFLNK)
 
349
    {
 
350
        // This is a symbolic link
 
351
        //return 0;
 
352
    }
 
353
    return sb.st_size;
 
354
}
 
355
 
 
356
bool NFileManagerPS3::FileExist(const TCHAR* Filename)
 
357
{
 
358
    CellFsStat sb;
 
359
    if(cellFsStat(TCHAR_TO_ANSI(Filename), &sb) != CELL_FS_SUCCEEDED)
 
360
    {
 
361
        return false;
 
362
    }
 
363
    return true;
 
364
}
 
365
 
 
366
int NFileManagerPS3::Copy(const TCHAR* DestFile,
 
367
                                const TCHAR* SrcFile,
 
368
                                bool OverWriteExisting,
 
369
                                bool OverWriteReadOnly,
 
370
                                NFileTransferMonitor* Monitor)
 
371
{
 
372
    nuxAssert(0);
 
373
    return false;
 
374
}
 
375
 
 
376
bool NFileManagerPS3::Delete(const TCHAR* Filename, bool OverWriteReadOnly)
 
377
{
 
378
    if(OverWriteReadOnly)
 
379
    {
 
380
        cellFsChmod(TCHAR_TO_ANSI(Filename), CELL_FS_S_IRUSR|CELL_FS_S_IWUSR);
 
381
    }
 
382
    if(cellFsUnlink(TCHAR_TO_ANSI(Filename)) != CELL_FS_SUCCEEDED)
 
383
    {
 
384
        nuxDebugMsg(TEXT("[NFileManagerPS3::Delete] Error deleting file '%s'."), Filename);
 
385
        return false;
 
386
    }
 
387
    return true;
 
388
}
 
389
 
 
390
bool NFileManagerPS3::IsReadOnly(const TCHAR* Filename)
 
391
{
 
392
    CellFsStat sb;
 
393
    if(cellFsStat(TCHAR_TO_ANSI(Filename), &sb) != CELL_FS_SUCCEEDED)
 
394
    {
 
395
        nuxDebugMsg(TEXT("[NFileManagerPS3::IsReadOnly] Error reading file status '%s'."), Filename);
 
396
        return false;
 
397
    }
 
398
    if((sb.st_mode & CELL_FS_S_IRUSR) && !(sb.st_mode & CELL_FS_S_IWUSR))
 
399
    {
 
400
        return true;
 
401
    }
 
402
    return false;
 
403
}
 
404
 
 
405
bool NFileManagerPS3::IsDirectory(const TCHAR* DirectoryName)
 
406
{
 
407
    CellFsStat sb;
 
408
    if(cellFsStat(TCHAR_TO_ANSI(DirectoryName), &sb) != CELL_FS_SUCCEEDED)
 
409
    {
 
410
        nuxDebugMsg(TEXT("[NFileManagerPS3::IsDirectory] Error reading file status '%s'."), DirectoryName);
 
411
        return false;
 
412
    }
 
413
    if(sb.st_mode & CELL_FS_S_IFDIR)
 
414
    {
 
415
        return true;
 
416
    }
 
417
    return false;
 
418
}
 
419
 
 
420
bool NFileManagerPS3::IsHidden(const TCHAR* Filename)
 
421
{
 
422
    return false;
 
423
}
 
424
 
 
425
/*!
 
426
    @return TRUE is the file exist.
 
427
*/
 
428
bool NFileManagerPS3::GetFileAttribute(const TCHAR* Filename, bool& isDirectory, bool& isReadOnly, bool& isHidden, t_s64& Size)
 
429
{
 
430
    isDirectory = false;
 
431
    isReadOnly = false;
 
432
    isHidden = false;
 
433
    Size = 0;
 
434
 
 
435
    CellFsStat sb;
 
436
    if(cellFsStat(TCHAR_TO_ANSI(Filename), &sb) != CELL_FS_SUCCEEDED)
 
437
    {
 
438
        return false;
 
439
    }
 
440
 
 
441
    if(sb.st_mode & CELL_FS_S_IFDIR)
 
442
    {
 
443
        isDirectory = true;
 
444
    }
 
445
    if((sb.st_mode & CELL_FS_S_IRUSR) && !(sb.st_mode & CELL_FS_S_IWUSR))
 
446
    {
 
447
        isReadOnly = true;
 
448
    }
 
449
    Size = sb.st_mode;
 
450
    return true;
 
451
}
 
452
 
 
453
bool NFileManagerPS3::Move(const TCHAR* Dest, const TCHAR* Src, bool OverWriteExisting, bool OverWriteReadOnly, NFileTransferMonitor* Monitor)
 
454
{
 
455
    nuxAssert(0);
 
456
    return false;
 
457
}
 
458
 
 
459
bool NFileManagerPS3::MakeDirectory(const TCHAR* Path, bool CreateCompletePath)
 
460
{
 
461
    if(CreateCompletePath)
 
462
    {
 
463
        return NFileManagerGeneric::MakeDirectory(Path, CreateCompletePath);
 
464
    }
 
465
 
 
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);
 
470
 
 
471
    // CELL_FS_EEXIST = -2147418092 = 0x80010014
 
472
    if((error != CELL_FS_SUCCEEDED) && (error != CELL_FS_EEXIST))
 
473
    {
 
474
        nuxDebugMsg(TEXT("[NFileManagerPS3::MakeDirectory] Error creating directory '%s'."), Path);
 
475
        return INL_ERROR;
 
476
    }
 
477
    return INL_OK;
 
478
}
 
479
 
 
480
bool NFileManagerPS3::DeleteDirectory(const TCHAR* Path, bool DeleteContentFirst)
 
481
{
 
482
//     if(DeleteContentFirst)
 
483
//     {
 
484
//         return NFileManagerGeneric::DeleteDirectory(Path, DeleteContentFirst);
 
485
//     }
 
486
//     if((::RemoveDirectory(Path) == 0) && (::GetLastError() != ERROR_FILE_NOT_FOUND))
 
487
//     {
 
488
//         nuxDebugMsg(TEXT("[NFileManagerWindows::DeleteDirectory] Error deleting directory '%s' (GetLastError: %d)"), Path, ::GetLastError());
 
489
//         return false;
 
490
//     }
 
491
    return true;
 
492
}
 
493
 
 
494
NAMESPACE_END
 
495