4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Haejeong Kim <backto.kim@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <sys/types.h>
30 #include "mm_file_debug.h"
31 #include "mm_file_utils.h"
33 typedef struct mmfileiodata {
36 } tMMFORMAT_FILEIO_DATA;
39 static int file_open(MMFileIOHandle *handle, const char *filename, int flags)
41 tMMFORMAT_FILEIO_DATA *privateData = NULL;
45 if (!handle || !filename) {
46 debug_error(DEBUG, "invalid param\n");
47 return MMFILE_IO_FAILED;
50 filename += strlen(handle->iofunc->handleName) + 3; /* :// */
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;
64 fd = open(filename, access, 0666);
66 debug_error(DEBUG, "open error\n");
67 return MMFILE_IO_FAILED;
70 privateData = mmfile_malloc(sizeof(tMMFORMAT_FILEIO_DATA));
73 debug_error(DEBUG, "calloc privateData\n");
74 return MMFILE_IO_FAILED;
78 privateData->offset = 0;
80 handle->privateData = (void *)privateData;
81 return MMFILE_IO_SUCCESS;
84 static int file_read(MMFileIOHandle *handle, unsigned char *buf, int size)
86 tMMFORMAT_FILEIO_DATA *privateData = handle->privateData;
89 readSize = read(privateData->fd, buf, size);
91 debug_error(RELEASE, "read\n");
92 return MMFILE_IO_FAILED;
95 privateData->offset += readSize;
100 static int file_write(MMFileIOHandle *handle, unsigned char *buf, int size)
102 tMMFORMAT_FILEIO_DATA *privateData = handle->privateData;
105 writtenSize = write(privateData->fd, buf, size);
106 if (writtenSize < 0) {
107 debug_error(RELEASE, "write\n");
108 return MMFILE_IO_FAILED;
111 privateData->offset += writtenSize;
116 static int64_t file_seek(MMFileIOHandle *handle, int64_t pos, int whence)
118 tMMFORMAT_FILEIO_DATA *privateData = handle->privateData;
119 privateData->offset = lseek(privateData->fd, pos, whence);
120 return privateData->offset;
123 static long long file_tell(MMFileIOHandle *handle)
125 tMMFORMAT_FILEIO_DATA *privateData = handle->privateData;
127 return privateData->offset;
131 static int file_close(MMFileIOHandle *handle)
133 tMMFORMAT_FILEIO_DATA *privateData = handle->privateData;
137 /*ret = */close(privateData->fd);
138 mmfile_free(privateData);
139 handle->privateData = NULL;
140 return MMFILE_IO_SUCCESS;
143 return MMFILE_IO_FAILED;
147 MMFileIOFunc mmfile_file_io_handler = {