summaryrefslogtreecommitdiffstats
path: root/libvideoeditor/osal/src/M4OSA_FileExtra.c
blob: bcac97c75b40cc38d430c4f46bfb5f851771888f (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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*
 * Copyright (C) 2004-2011 NXP Software
 * Copyright (C) 2011 The Android Open Source Project
 *
 * 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         M4OSA_FileExtra.c
* @brief        File extra for Android
* @note         This file implements a set of basic functions to handle file
*               itself.
************************************************************************
*/

#include "M4OSA_Debug.h"
#include "M4OSA_FileCommon.h"
#include "M4OSA_FileCommon_priv.h"
#include "M4OSA_FileExtra.h"
#include "M4OSA_FileReader.h"
#include "M4OSA_FileWriter.h"

#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/statfs.h>



/**
 ************************************************************************
 * @brief      This function deletes the provided URL.
 * @note
 * @param      pUrl: (IN) URL of the file to delete
 * @param      fileModeAccess: (IN) File mode access
 * @return     M4NO_ERROR: there is no error
 * @return     M4ERR_ALLOC: there is no more memory available
 * @return     M4ERR_PARAMETER: at least one parameter is NULL
 * @return     M4ERR_NOT_IMPLEMENTED: the URL does not match with the supported
 *             file
 ************************************************************************
*/
M4OSA_ERR M4OSA_fileExtraDelete(const M4OSA_Char* pUrl)
{

    M4OSA_Int32  err;
#ifdef UTF_CONVERSION
    M4OSA_Void* tempConversionBuf;
    M4OSA_UInt32 tempConversionSize = 1000;    /*size of the decoded buffer,
                                                 can be increase if necessary*/
#endif /* UTF_CONVERSION */

    M4OSA_TRACE1_1("M4OSA_fileExtraDelete\t\tM4OSA_Char* %s", pUrl);
    M4OSA_DEBUG_IF2(M4OSA_NULL == pUrl, M4ERR_PARAMETER,
                                   "M4OSA_fileExtraDelete: pUrl is M4OSA_NULL");

#ifdef UTF_CONVERSION
    /*FB: to test the UTF16->UTF8 conversion into Video Artist*/
    /*Convert the URL from UTF16 to UTF8*/
    tempConversionBuf = (M4OSA_Char*)M4OSA_32bitAlignedMalloc(tempConversionSize +1, 0,
                                                 (M4OSA_Char*)"conversion buf");
    if(tempConversionBuf == M4OSA_NULL)
    {
        M4OSA_TRACE1_0("Error when allocating conversion buffer\n");
        return M4ERR_PARAMETER;
    }
    M4OSA_ToUTF8_OSAL((M4OSA_Void*)pUrl, tempConversionBuf, &tempConversionSize);
    ((M4OSA_Char*)tempConversionBuf)[tempConversionSize ] = '\0';

    printf("remove file %s\n", tempConversionBuf);

    /*Open the converted path*/
    err = remove (tempConversionBuf);
    /*Free the temporary decoded buffer*/
    free(tempConversionBuf);
#else
    err = remove((const char *)pUrl);
#endif /* UTF_CONVERSION */

    if(-1 == err)
    {
        M4OSA_DEBUG(M4ERR_PARAMETER,
                          "M4OSA_fileExtraDelete: Cannot remove the input url");
        return M4ERR_PARAMETER;
    }

    return M4NO_ERROR;
}

/**
 ************************************************************************
 * @brief      This function copies the file located by 'pSrcUrl' to 'pDstUrl'.
 * @note
 * @param      pSrcUrl: (IN) source URL
 * @param      pDstUrl: (IN) Destination URL
 * @return     M4NO_ERROR: there is no error
 * @return     M4ERR_ALLOC: there is no more memory available
 * @return     M4ERR_PARAMETER: at least one parameter is NULL
 * @return     M4ERR_NOT_IMPLEMENTED: the URL does not match with the supported
 *             file
 ************************************************************************
*/
M4OSA_ERR M4OSA_fileExtraCopy(M4OSA_Char* pSrcUrl, M4OSA_Char* pDstUrl)
{
    M4OSA_Context pInputFileContext    = M4OSA_NULL;
    M4OSA_Context pOutputFileContext= M4OSA_NULL;

    M4OSA_ERR       err;
    M4OSA_UInt32    uiSizeRead = BUFFER_COPY_SIZE;
    M4OSA_MemAddr32 copy_buffer;

    M4OSA_TRACE1_2("M4OSA_fileExtraDelete\t\tM4OSA_Char* %s\tM4OSA_Char* %s",
                                                              pSrcUrl, pDstUrl);
    M4OSA_DEBUG_IF2(M4OSA_NULL == pDstUrl, M4ERR_PARAMETER,
                                  "M4OSA_fileExtraCopy: pDstUrl is M4OSA_NULL");
    M4OSA_DEBUG_IF2(M4OSA_NULL == pSrcUrl, M4ERR_PARAMETER,
                                  "M4OSA_fileExtraCopy: pSrcUrl is M4OSA_NULL");

    /* Open input file */
    err = M4OSA_fileReadOpen(&pInputFileContext, pSrcUrl, M4OSA_kFileRead);
    if(M4NO_ERROR != err)
    {
        M4OSA_DEBUG(err, "M4OSA_fileExtraCopy: M4OSA_fileReadOpen");
        return err;
    }

    /* Open output file */
    err = M4OSA_fileWriteOpen(&pOutputFileContext, pDstUrl,
                        M4OSA_kFileWrite|M4OSA_kFileCreate);
    if(M4NO_ERROR != err)
    {
        M4OSA_DEBUG(err, "M4OSA_fileExtraCopy: M4OSA_fileWriteOpen");
        return err;
    }

    /* Allocate buffer */
    copy_buffer = M4OSA_32bitAlignedMalloc(BUFFER_COPY_SIZE, M4OSA_FILE_EXTRA,
                               (M4OSA_Char*)"M4OSA_fileExtraCopy: copy buffer");
    if(M4OSA_NULL == copy_buffer)
    {
        M4OSA_DEBUG(M4ERR_ALLOC, "M4OSA_fileExtraCopy");
        return M4ERR_ALLOC;
    }

    /* Copy input file to output file using copy buffer */
    while (1)
    {
        /* Load data into copy buffer */
        err = M4OSA_fileReadData(pInputFileContext,
                                      (M4OSA_MemAddr8)copy_buffer, &uiSizeRead);
        if(M4NO_ERROR == err)
        {
            /* Write data to output file */
            err = M4OSA_fileWriteData(pOutputFileContext,
                                       (M4OSA_MemAddr8)copy_buffer, uiSizeRead);
            if(M4NO_ERROR != err)
            {
                break;
            }
        }
        else if (M4WAR_NO_DATA_YET == err)
        {
            /* no more data to copy, end of file reached */
            err = M4OSA_fileWriteData(pOutputFileContext,
                                       (M4OSA_MemAddr8)copy_buffer, uiSizeRead);
            break;
        }
        else
        {
            break; /* an error occur */
        }
    }

    /* Free copy buffer */
    free(copy_buffer);

    err = M4OSA_fileWriteClose(pOutputFileContext);
    if(M4NO_ERROR != err)
    {
        M4OSA_DEBUG(err, "M4OSA_fileExtraCopy: M4OSA_fileWriteClose");
    }

    err = M4OSA_fileReadClose(pInputFileContext);
    if(M4NO_ERROR != err)
    {
        M4OSA_DEBUG(err, "M4OSA_fileExtraCopy: M4OSA_fileReadClose");
    }

    return err;
}


/**
 ************************************************************************
 * @brief      This function renames the 'pSrcUrl' to 'pDstUrl'.
 * @note
 * @param      pSrcUrl: (IN) source URL
 * @param      pDstUrl: (IN) Destination URL
 * @return     M4NO_ERROR: there is no error
 * @return     M4ERR_ALLOC: there is no more memory available
 * @return     M4ERR_PARAMETER: at least one parameter is NULL
 * @return     M4ERR_NOT_IMPLEMENTED: the URL does not match with the supported
 *             file
 ************************************************************************
*/
M4OSA_ERR M4OSA_fileExtraRename(M4OSA_Char* pSrcUrl, M4OSA_Char* pDstUrl)
{
    M4OSA_ERR    err;
    M4OSA_Int32 iValue;
    M4OSA_Char*    pSrcFilename = M4OSA_NULL;
    M4OSA_Char*    pDstFilename = M4OSA_NULL;

    M4OSA_TRACE1_2("M4OSA_fileExtraRename\t\tM4OSA_Char* %s\tM4OSA_Char* %s",
                                                              pSrcUrl, pDstUrl);

    M4OSA_DEBUG_IF2(M4OSA_NULL == pDstUrl, M4ERR_PARAMETER,
                                "M4OSA_fileExtraRename: pSrcUrl is M4OSA_NULL");
    M4OSA_DEBUG_IF2(M4OSA_NULL == pSrcUrl, M4ERR_PARAMETER,
                                "M4OSA_fileExtraRename: pDstUrl is M4OSA_NULL");

    err = M4OSA_fileCommonGetFilename(pSrcUrl, &pSrcFilename);
    if(M4NO_ERROR != err)
    {
        M4OSA_DEBUG(err, "M4OSA_fileExtraRename: M4OSA_fileCommonGetFilename");
        return err;
    }

    err = M4OSA_fileCommonGetFilename(pDstUrl, &pDstFilename);
    if(M4NO_ERROR != err)
    {
        free(pSrcFilename);
        M4OSA_DEBUG(err, "M4OSA_fileExtraRename: M4OSA_fileCommonGetFilename");
        return err;
    }

    /* Rename file */
    iValue = rename((const char *)pSrcFilename, (const char *)pDstFilename);
    if (0 != iValue)
    {
    /*
        What error code shall be returned ? From MSDN:
        Each of these functions returns 0 if it is successful. On an error, the
        function  returns a nonzero value and sets errno to one of the following
        values:
        - EACCES: File or directory specified by newname already exists or could
        not be created (invalid path); or oldname is a directory and newname
        specifies a different path.
        - ENOENT: File or path specified by oldname not found.
        - EINVAL: Name contains invalid characters.
        For other possible return values, see _doserrno, _errno, syserrlist, and
            _sys_nerr. */
        M4OSA_DEBUG(M4ERR_PARAMETER, "M4OSA_fileExtraRename: rename failed");
        return M4ERR_PARAMETER;
    }

    free(pDstFilename);
    free(pSrcFilename);

    return M4NO_ERROR;
}



/**
 ************************************************************************
 * @brief      This function changes the current directory to the specified new
 *             directory 'url'.
 * @note
 * @param      pUrl: (IN) Directory to which current directory to be changed
 * @return     M4NO_ERROR: there is no error
 * @return     M4ERR_ALLOC: there is no more memory available
 * @return     M4ERR_PARAMETER: at least one parameter is NULL
 * @return     M4ERR_NOT_IMPLEMENTED: the URL does not match with the supported
 *             file
 ************************************************************************
*/

M4OSA_ERR M4OSA_fileExtraChangeCurrentDir(const M4OSA_Char* pUrl)
{
    M4OSA_ERR    err;
    M4OSA_Char* pFileName = M4OSA_NULL;
    M4OSA_Int32 iValue = 0;

    M4OSA_TRACE1_1("M4OSA_fileExtraChangeCurrentDir\t\tM4OSA_Char* %s", pUrl);
    M4OSA_DEBUG_IF2(M4OSA_NULL == pUrl, M4ERR_PARAMETER,
                         "M4OSA_fileExtraChangeCurrentDir: pUrl is M4OSA_NULL");

    err = M4OSA_fileCommonGetFilename((M4OSA_Char*)pUrl, &pFileName);
    if(M4NO_ERROR != err)
    {
        M4OSA_DEBUG(err,
                "M4OSA_fileExtraChangeCurrentDir: M4OSA_fileCommonGetFilename");
        return err;
    }

    iValue = chdir((char*)pFileName);

    if (iValue != 0)
    {
    /*
    What error code shall be returned ? From MSDN:
    Each of these functions returns a value of 0 if successful. A return
    value of -1 indicates that the specified path could not be found, in
    which case errno is set to ENOENT.*/
        M4OSA_DEBUG(M4ERR_PARAMETER,
                               "M4OSA_fileExtraChangeCurrentDir: chdir failed");
        return(M4ERR_PARAMETER);
    }

    free(pFileName);

    return M4NO_ERROR;
}

/**
 ************************************************************************
 * @brief      This function creates a new directory to the specified 'url'.
 * @note
 * @param      pUrl: (IN) Path to create new directory with name
 * @return     M4NO_ERROR: there is no error
 * @return     M4ERR_ALLOC: there is no more memory available
 * @return     M4ERR_PARAMETER: at least one parameter is NULL
 * @return     M4ERR_NOT_IMPLEMENTED: the URL does not match with the supported
 *             file
 ************************************************************************
*/
M4OSA_ERR M4OSA_fileExtraCreateDir(const M4OSA_Char* pUrl)
{
    M4OSA_Int32        err;

    M4OSA_TRACE2_1("M4OSA_fileExtraCreateDir %s", pUrl);
    M4OSA_DEBUG_IF2(M4OSA_NULL == pUrl, M4ERR_PARAMETER,
                                "M4OSA_fileExtraCreateDir: pUrl is M4OSA_NULL");

    err = mkdir((char*)pUrl, S_IRWXU | S_IRWXG | S_IRWXO);

    if( err < 0 )
    {
           return M4OSA_ERR_CREATE(M4_ERR, M4OSA_FILE_EXTRA, errno);
    }

    return M4NO_ERROR;
}


/**
 ************************************************************************
 * @brief      This function removes the current directory.
 * @note
 * @param      pUrl: (IN) Path of directory with name
 * @return     M4NO_ERROR: there is no error
 * @return     M4ERR_ALLOC: there is no more memory available
 * @return     M4ERR_PARAMETER: at least one parameter is NULL
 * @return     M4ERR_NOT_IMPLEMENTED: the URL does not match with the supported
 *             file
 ************************************************************************
*/
M4OSA_ERR M4OSA_fileExtraRemoveDir(const M4OSA_Char* pUrl)
{
    M4OSA_Int32    err;

    M4OSA_TRACE2_1("M4OSA_fileExtraRemoveDir %s", pUrl);
    M4OSA_DEBUG_IF2(M4OSA_NULL == pUrl, M4ERR_PARAMETER,
                                "M4OSA_fileExtraRemoveDir: pUrl is M4OSA_NULL");

    err = rmdir((char*)pUrl);
    if(err < 0 )
    {
        M4OSA_DEBUG(M4ERR_PARAMETER, "M4OSA_fileExtraRemoveDir failed");
        return M4OSA_ERR_CREATE(M4_ERR, M4OSA_FILE_EXTRA, errno);
    }

    return M4NO_ERROR;
}

/**
 ************************************************************************
 * @brief      retrieves the free space.
 * @note
 * @param      pUrl: (IN) root directory
 * @return     M4NO_ERROR: there is no error
 * @return     M4ERR_ALLOC: there is no more memory available
 * @return     M4ERR_PARAMETER: at least one parameter is NULL
 * @return     M4ERR_NOT_IMPLEMENTED: the URL does not match with the supported
 *             file
 ************************************************************************
*/
M4OSA_UInt32 M4OSA_fileExtraGetFreeSpace(const M4OSA_Char* pUrl)
{
    M4OSA_UInt32 size = 0;
    struct statfs stat;

    if ( M4OSA_NULL != pUrl )
    {
        if (0 == statfs( (const char *)pUrl, &stat ))
        {
            if ((stat.f_bfree * stat.f_bsize) > M4OSA_UINT32_MAX)
            {
                size = M4OSA_UINT32_MAX;
            }
            else
            {
                size = (M4OSA_UInt32)(stat.f_bfree * stat.f_bsize);
            }
        }
    }

    return (size);
}

/**
 ************************************************************************
 * @brief      This function gets the total space
 * @note
 * @param      pUrl: (IN) Path of directory with name
 * @return     M4NO_ERROR: there is no error
 * @return     M4ERR_ALLOC: there is no more memory available
 * @return     M4ERR_PARAMETER: at least one parameter is NULL
 * @return     M4ERR_NOT_IMPLEMENTED: the URL does not match with the supported
 *             file
 ************************************************************************
*/
M4OSA_UInt32 M4OSA_fileExtraGetTotalSpace(const M4OSA_Char* pUrl)
{
    M4OSA_UInt32 size = 0;
    struct statfs stat;

    if ( M4OSA_NULL != pUrl )
    {
        if (0 == statfs( (const char *)pUrl, &stat ))
        {
             if ((stat.f_blocks * stat.f_bsize) > M4OSA_UINT32_MAX)
            {
                size = M4OSA_UINT32_MAX;
            }
            else
            {
                size = (M4OSA_UInt32)(stat.f_blocks * stat.f_bsize);
            }
        }
    }

    return (size);
}

/**
 ************************************************************************
 * @brief      This function retrieve the file type (Directory or file).
 * @note
 * @param      pUrl: (IN) Path of directory with name
 * @return     M4NO_ERROR: there is no error
 * @return     M4ERR_ALLOC: there is no more memory available
 * @return     M4ERR_PARAMETER: at least one parameter is NULL
 * @return     M4ERR_NOT_IMPLEMENTED: the URL does not match with the supported
 *             file
 ************************************************************************
*/
M4OSA_EntryType M4OSA_fileExtraGetType(const M4OSA_Char* pUrl)
{
    M4OSA_EntryType type = M4OSA_TypeInvalid;
    struct stat fileStat;

    if ( M4OSA_NULL != pUrl )
    {
        if (0 == stat( (const char *)pUrl, &fileStat))
        {
            if ( S_ISDIR( fileStat.st_mode ) )
            {
                type = M4OSA_TypeDir;
            }
            else
            {
                type = M4OSA_TypeFile;
            }
        }
    }

    return (type);
}


/**
 ************************************************************************
 * @brief      This function truncate a file.
 *               the file must be previously opened in write mode
 * @note       the position pointer in the file is set to the beginning
 *               of the file after the truncate
 * @param      context: (IN) media context
 * @param      length: (IN) length of the file after truncation
 * @return     M4NO_ERROR: there is no error
 * @return     M4ERR_ALLOC: there is no more memory available
 * @return     M4ERR_PARAMETER: at least one parameter is NULL
 ************************************************************************
*/
M4OSA_ERR M4OSA_fileExtrafTruncate(M4OSA_Context context, M4OSA_FilePosition length)
{
    M4OSA_ERR err = M4NO_ERROR;
    M4OSA_UInt16 result = M4OSA_FALSE;
    M4OSA_FileContext* pFileContext = context;

    M4OSA_DEBUG_IF2(M4OSA_NULL == context, M4ERR_PARAMETER,
                             "M4OSA_fileExtrafTruncate: context is M4OSA_NULL");
    M4OSA_DEBUG_IF2(M4OSA_NULL == length, M4ERR_PARAMETER,
                              "M4OSA_fileExtrafTruncate: length is M4OSA_NULL");

    result = ftruncate(pFileContext->file_desc->_file, length);

    if(result != 0)
    {
        err = errno;
        M4OSA_TRACE1_1("SetEndOfFile returns err: 0x%x\n", err);
        return M4OSA_ERR_CREATE(M4_ERR, M4OSA_FILE_EXTRA, err);
    }
    return M4NO_ERROR;
}