support webm format
[platform/core/multimedia/libmm-fileinfo.git] / utils / mm_file_util_io_file.c
1 /*
2  * libmm-fileinfo
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Haejeong Kim <backto.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #include "mm_file_debug.h"
31 #include "mm_file_utils.h"
32
33 typedef struct mmfileiodata {
34         int fd;
35         long long offset;
36 } tMMFORMAT_FILEIO_DATA;
37
38
39 static int file_open(MMFileIOHandle *handle, const char *filename, int flags)
40 {
41         tMMFORMAT_FILEIO_DATA *privateData = NULL;
42         int access = 0;
43         int fd = 0;
44
45         if (!handle || !filename) {
46                 debug_error(DEBUG, "invalid param\n");
47                 return MMFILE_IO_FAILED;
48         }
49
50         filename += strlen(handle->iofunc->handleName) + 3; /* :// */
51
52         if (flags & MMFILE_RDWR) {
53                 access = O_CREAT | O_TRUNC | O_RDWR;
54         } else if (flags & MMFILE_WRONLY) {
55                 access = O_CREAT | O_TRUNC | O_WRONLY;
56         } else {
57                 access = O_RDONLY;
58         }
59
60 #ifdef O_BINARY
61         access |= O_BINARY;
62 #endif
63
64         fd = open(filename, access, 0666);
65         if (fd < 0) {
66                 debug_error(DEBUG, "open error\n");
67                 return MMFILE_IO_FAILED;
68         }
69
70         privateData = mmfile_malloc(sizeof(tMMFORMAT_FILEIO_DATA));
71         if (!privateData) {
72                 close(fd);
73                 debug_error(DEBUG, "calloc privateData\n");
74                 return MMFILE_IO_FAILED;
75         }
76
77         privateData->fd = fd;
78         privateData->offset = 0;
79
80         handle->privateData = (void *)privateData;
81         return MMFILE_IO_SUCCESS;
82 }
83
84 static int file_read(MMFileIOHandle *handle, unsigned char *buf, int size)
85 {
86         tMMFORMAT_FILEIO_DATA *privateData = handle->privateData;
87         int readSize = 0;
88
89         readSize = read(privateData->fd, buf, size);
90         if (readSize < 0) {
91                 debug_error(RELEASE, "read\n");
92                 return MMFILE_IO_FAILED;
93         }
94
95         privateData->offset += readSize;
96
97         return readSize;
98 }
99
100 static int file_write(MMFileIOHandle *handle, unsigned char *buf, int size)
101 {
102         tMMFORMAT_FILEIO_DATA *privateData = handle->privateData;
103         int writtenSize = 0;
104
105         writtenSize = write(privateData->fd, buf, size);
106         if (writtenSize < 0) {
107                 debug_error(RELEASE, "write\n");
108                 return MMFILE_IO_FAILED;
109         }
110
111         privateData->offset += writtenSize;
112
113         return writtenSize;
114 }
115
116 static int64_t file_seek(MMFileIOHandle *handle, int64_t pos, int whence)
117 {
118         tMMFORMAT_FILEIO_DATA *privateData = handle->privateData;
119         privateData->offset = lseek(privateData->fd, pos, whence);
120         return privateData->offset;
121 }
122
123 static long long file_tell(MMFileIOHandle *handle)
124 {
125         tMMFORMAT_FILEIO_DATA *privateData = handle->privateData;
126
127         return privateData->offset;
128 }
129
130
131 static int file_close(MMFileIOHandle *handle)
132 {
133         tMMFORMAT_FILEIO_DATA *privateData = handle->privateData;
134         /*int ret = 0;*/
135
136         if (privateData) {
137                 /*ret = */close(privateData->fd);
138                 mmfile_free(privateData);
139                 handle->privateData = NULL;
140                 return MMFILE_IO_SUCCESS;
141         }
142
143         return MMFILE_IO_FAILED;
144 }
145
146
147 MMFileIOFunc mmfile_file_io_handler = {
148         "file",
149         file_open,
150         file_read,
151         file_write,
152         file_seek,
153         file_tell,
154         file_close,
155         NULL
156 };
157