aboutsummaryrefslogtreecommitdiff
path: root/oscl/oscl/osclio/src/oscl_file_native.cpp
blob: 368b47506910a204d92ed65a151a55304c7fdc64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/* ------------------------------------------------------------------
 * Copyright (C) 1998-2009 PacketVideo
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 * -------------------------------------------------------------------
 */
/*! \file oscl_file_native.cpp
    \brief This file contains file io APIs
*/

#include "oscl_file_native.h"
#include "oscl_stdstring.h"
#include "oscl_utf8conv.h"
#include "oscl_int64_utils.h"

#ifdef ENABLE_SHAREDFD_PLAYBACK
#undef LOG_TAG
#define LOG_TAG "OsclNativeFile"
#include <utils/Log.h>
#endif
#include "oscl_mem.h"
#include "oscl_file_types.h"
#include "oscl_file_handle.h"


OsclNativeFile::OsclNativeFile()
{
    iOpenFileHandle = false;
    iMode = 0;

    iFile = 0;
#ifdef ENABLE_SHAREDFD_PLAYBACK
    iSharedFd = -1;
#endif

}

OsclNativeFile::~OsclNativeFile()
{
}

int32  OsclNativeFile::Open(const OsclFileHandle& aHandle, uint32 mode
                            , const OsclNativeFileParams& params
                            , Oscl_FileServer& fileserv)
{
    //open with an external file handle

    OSCL_UNUSED_ARG(fileserv);

    iMode = mode;
    iOpenFileHandle = true;

    {
        OSCL_UNUSED_ARG(params);
        //Just save the open file handle
        iFile = aHandle.Handle();
    }

    return 0;
}

static void OpenModeToString(uint32 mode, char mode_str[4])
{
    uint32 index = 0;

    if (mode & Oscl_File::MODE_READWRITE)
    {
        if (mode & Oscl_File::MODE_APPEND)
        {
            mode_str[index++] = 'a';
            mode_str[index++] = '+';
        }
        else
        {
            mode_str[index++] = 'w';
            mode_str[index++] = '+';
        }
    }
    else if (mode & Oscl_File::MODE_APPEND)
    {
        mode_str[index++] = 'a';
        mode_str[index++] = '+';
    }
    else if (mode & Oscl_File::MODE_READ)
    {
        mode_str[index++] = 'r';
    }
    else if (mode & Oscl_File::MODE_READ_PLUS)
    {
        mode_str[index++] = 'r';
        mode_str[index++] = '+';
    }

    if (mode & Oscl_File::MODE_TEXT)
    {
        mode_str[index++] = 't';
    }
    else
    {
        mode_str[index++] = 'b';
    }

    mode_str[index++] = '\0';
}

int32 OsclNativeFile::OpenFileOrSharedFd(
    const char *filename, const char *openmode)
{
#ifdef ENABLE_SHAREDFD_PLAYBACK
    int fd;
    long long offset;
    long long len;
    if (sscanf(filename, "sharedfd://%d:%lld:%lld", &fd, &offset, &len) == 3)
    {
        iSharedFd = fd;
        iSharedFilePosition = 0;
        iSharedFileOffset = offset;
        long long size = lseek64(iSharedFd, 0, SEEK_END);
        lseek64(iSharedFd, 0, SEEK_SET);
        size -= offset;
        iSharedFileSize = size < len ? size : len;
    }
    else
#endif
    {
        if ((iFile = fopen(filename, openmode)) == NULL)
        {
            return -1;
        }
    }

    return 0;
}

int32 OsclNativeFile::Open(const oscl_wchar *filename, uint32 mode
                           , const OsclNativeFileParams& params
                           , Oscl_FileServer& fileserv)
{
    iMode = mode;
    iOpenFileHandle = false;

    {
        OSCL_UNUSED_ARG(fileserv);
        OSCL_UNUSED_ARG(params);

        if (!filename || *filename == '\0') return -1; // Null string not supported in fopen, error out

        char openmode[4];

        OpenModeToString(mode, openmode);

#ifdef _UNICODE
        oscl_wchar convopenmode[4];
        if (0 == oscl_UTF8ToUnicode(openmode, oscl_strlen(openmode), convopenmode, 4))
        {
            return -1;
        }

        if ((iFile = _wfopen(filename, convopenmode)) == NULL)
        {
            return -1;
        }
#else
        //Convert to UTF8
        char convfilename[OSCL_IO_FILENAME_MAXLEN];
        if (0 == oscl_UnicodeToUTF8(filename, oscl_strlen(filename), convfilename, OSCL_IO_FILENAME_MAXLEN))
        {
            return -1;
        }
        return OpenFileOrSharedFd(convfilename, openmode);
#endif
    }

}

int32 OsclNativeFile::Open(const char *filename, uint32 mode
                           , const OsclNativeFileParams& params
                           , Oscl_FileServer& fileserv)
{
    iMode = mode;
    iOpenFileHandle = false;

    OSCL_UNUSED_ARG(fileserv);
    OSCL_UNUSED_ARG(params);

    if (!filename || *filename == '\0') return -1; // Null string not supported in fopen, error out

    char openmode[4];

    OpenModeToString(mode, openmode);

    return OpenFileOrSharedFd(filename, openmode);

}

TOsclFileOffset OsclNativeFile::Size()
{
    //this is the default for platforms with no
    //native size query.
    //Just do seek to end, tell, then seek back.
    TOsclFileOffset curPos = Tell();
    if (curPos >= 0
            && Seek(0, Oscl_File::SEEKEND) == 0)
    {
        TOsclFileOffset endPos = Tell();
        if (Seek(curPos, Oscl_File::SEEKSET) == 0)
        {
            return endPos;
        }
        else
        {
            return (-1);
        }
    }
    return (-1);
}

int32 OsclNativeFile::Close()
{
    int32 closeret = 0;

    {
        if (iOpenFileHandle)
            closeret = Flush();
        else if (iFile != NULL)
        {
            closeret = fclose(iFile);
            iFile = NULL;
        }
#ifdef ENABLE_SHAREDFD_PLAYBACK
        else if (iSharedFd >= 0)
        {
            // we don't need to, and in fact MUST NOT, close mSharedFd here,
            // since it might still be shared by another OsclFileNative, and
            // will be closed by the playerdriver when we're done with it.
            closeret = 0;
        }
#endif
        else
        {
            return -1; //Linux Porting : Fix 1
        }
    }

    return closeret;
}


uint32 OsclNativeFile::Read(OsclAny *buffer, uint32 size, uint32 numelements)
{
#ifdef ENABLE_SHAREDFD_PLAYBACK
    if (iSharedFd >= 0)
    {
        // restore position, no locking is needed because all access to the
        // shared filedescriptor is done by the same thread.
        lseek64(iSharedFd, iSharedFilePosition + iSharedFileOffset, SEEK_SET);
        uint32 count = size * numelements;
        if (iSharedFilePosition + count > iSharedFileSize)
        {
            count = iSharedFileSize - iSharedFilePosition;
        }
        ssize_t numread = read(iSharedFd, buffer, count);
        // unlock
        long long curpos = lseek64(iSharedFd, 0, SEEK_CUR);
        if (curpos >= 0)
        {
            iSharedFilePosition = curpos - iSharedFileOffset;
        }
        if (numread < 0)
        {
            return numread;
        }
        return numread / size;
    }
#endif

    if (iFile)
    {
        return fread(buffer, OSCL_STATIC_CAST(int32, size), OSCL_STATIC_CAST(int32, numelements), iFile);
    }
    return 0;
}

bool OsclNativeFile::HasAsyncRead()
{
    return false;//not supported.
}

int32 OsclNativeFile::ReadAsync(OsclAny*buffer, uint32 size, uint32 numelements, OsclAOStatus& status)
{
    OSCL_UNUSED_ARG(buffer);
    OSCL_UNUSED_ARG(size);
    OSCL_UNUSED_ARG(numelements);
    OSCL_UNUSED_ARG(status);
    return -1;//not supported
}

void OsclNativeFile::ReadAsyncCancel()
{
}

uint32 OsclNativeFile::GetReadAsyncNumElements()
{
    return 0;//not supported
}



uint32 OsclNativeFile::Write(const OsclAny *buffer, uint32 size, uint32 numelements)
{
#ifdef ENABLE_SHAREDFD_PLAYBACK
    if (iSharedFd >= 0)
        return 0;
#endif
    if (iFile)
    {
        return fwrite(buffer, OSCL_STATIC_CAST(int32, size), OSCL_STATIC_CAST(int32, numelements), iFile);
    }
    return 0;
}

int32 OsclNativeFile::Seek(TOsclFileOffset offset, Oscl_File::seek_type origin)
{

    {
#ifdef ENABLE_SHAREDFD_PLAYBACK
        if (iSharedFd >= 0)
        {
            int newpos = iSharedFilePosition;
            if (origin == Oscl_File::SEEKCUR) newpos = newpos + offset;
            else if (origin == Oscl_File::SEEKSET) newpos = offset;
            else if (origin == Oscl_File::SEEKEND) newpos = iSharedFileSize + offset;
            if (newpos < 0)
                return EINVAL;
            if (newpos > iSharedFileSize) // is this valid?
                newpos = iSharedFileSize;
            iSharedFilePosition = newpos;
            return 0;
        }
#endif
        if (iFile)
        {
            int32 seekmode = SEEK_CUR;

            if (origin == Oscl_File::SEEKCUR)
                seekmode = SEEK_CUR;
            else if (origin == Oscl_File::SEEKSET)
                seekmode = SEEK_SET;
            else if (origin == Oscl_File::SEEKEND)
                seekmode = SEEK_END;
#if OSCL_HAS_LARGE_FILE_SUPPORT
            return fseeko(iFile, offset, seekmode);
#else
            return fseek(iFile, offset, seekmode);
#endif
        }
    }
    return -1;
}


TOsclFileOffset OsclNativeFile::Tell()
{
    TOsclFileOffset result = -1;
#ifdef ENABLE_SHAREDFD_PLAYBACK
    if (iSharedFd >= 0)
        return iSharedFilePosition;
#endif
    if (iFile)
    {
#if OSCL_HAS_LARGE_FILE_SUPPORT
        result = ftello(iFile);
#else
        result = ftell(iFile);
#endif
    }
    return result;
}



int32 OsclNativeFile::Flush()
{
#ifdef ENABLE_SHAREDFD_PLAYBACK
    if (iSharedFd >= 0)
        return iSharedFilePosition >= iSharedFileSize;
#endif
    if (iFile)
        return fflush(iFile);
    return EOF;
}



int32 OsclNativeFile::EndOfFile()
{
#ifdef ENABLE_SHAREDFD_PLAYBACK
    if (iSharedFd >= 0)
        return iSharedFilePosition >= iSharedFileSize;
#endif
    if (iFile)
        return feof(iFile);
    return 0;
}


int32 OsclNativeFile::GetError()
{
//FIXME ENABLE_SHAREDFD_PLAYBACK
    if (iFile)
        return ferror(iFile);
    return 0;
}