1 /*
2  * This file is part of libaacs
3  * Copyright (C) 2009-2010  Obliter0n
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library. If not, see
17  * <http://www.gnu.org/licenses/>.
18  */
19 
20 #if HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include "file.h"
25 #include "util/macro.h"
26 #include "util/logging.h"
27 
28 #include <errno.h>
29 #include <inttypes.h>
30 #include <stdio.h> // remove()
31 #include <stdlib.h>
32 #include <string.h>
33 
34 #include <unistd.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 
39 #ifdef __ANDROID__
40 # undef  lseek
41 # define lseek lseek64
42 # undef  off_t
43 # define off_t off64_t
44 #endif
45 
_file_close(AACS_FILE_H * file)46 static void _file_close(AACS_FILE_H *file)
47 {
48     if (file) {
49         if (close((int)(intptr_t)file->internal)) {
50             BD_DEBUG(DBG_CRIT | DBG_FILE, "Error closing POSIX file (%p)\n", (void*)file);
51         }
52 
53         BD_DEBUG(DBG_FILE, "Closed POSIX file (%p)\n", (void*)file);
54 
55         X_FREE(file);
56     }
57 }
58 
_file_seek(AACS_FILE_H * file,int64_t offset,int32_t origin)59 static int64_t _file_seek(AACS_FILE_H *file, int64_t offset, int32_t origin)
60 {
61     off_t result = lseek((int)(intptr_t)file->internal, offset, origin);
62     if (result == (off_t)-1) {
63         BD_DEBUG(DBG_FILE, "lseek() failed (%p)\n", (void*)file);
64         return -1;
65     }
66     return (int64_t)result;
67 }
68 
_file_tell(AACS_FILE_H * file)69 static int64_t _file_tell(AACS_FILE_H *file)
70 {
71     return _file_seek(file, 0, SEEK_CUR);
72 }
73 
_file_read(AACS_FILE_H * file,uint8_t * buf,int64_t size)74 static int64_t _file_read(AACS_FILE_H *file, uint8_t *buf, int64_t size)
75 {
76     ssize_t got, result;
77 
78     if (size <= 0 || size >= BD_MAX_SSIZE) {
79         BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid read of size %" PRId64 " (%p)\n", size, (void*)file);
80         return 0;
81     }
82 
83     for (got = 0; got < (ssize_t)size; got += result) {
84         result = read((int)(intptr_t)file->internal, buf + got, size - got);
85         if (result < 0) {
86             if (errno != EINTR) {
87                 BD_DEBUG(DBG_FILE, "read() failed (%p)\n", (void*)file);
88                 break;
89             }
90             result = 0;
91         } else if (result == 0) {
92             // hit EOF.
93             break;
94         }
95     }
96     return (int64_t)got;
97 }
98 
_file_write(AACS_FILE_H * file,const uint8_t * buf,int64_t size)99 static int64_t _file_write(AACS_FILE_H *file, const uint8_t *buf, int64_t size)
100 {
101     ssize_t written, result;
102 
103     if (size <= 0 || size >= BD_MAX_SSIZE) {
104         if (size == 0) {
105             if (fsync((int)(intptr_t)file->internal)) {
106                 BD_DEBUG(DBG_FILE, "fsync() failed (%p)\n", (void*)file);
107                 return -1;
108             }
109             return 0;
110         }
111         BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid write of size %" PRId64 " (%p)\n", size, (void*)file);
112         return 0;
113     }
114 
115     for (written = 0; written < (ssize_t)size; written += result) {
116         result = write((int)(intptr_t)file->internal, buf + written, size - written);
117         if (result < 0) {
118             if (errno != EINTR) {
119                 BD_DEBUG(DBG_FILE, "write() failed (%p)\n", (void*)file);
120                 break;
121             }
122             result = 0;
123         }
124     }
125     return (int64_t)written;
126 }
127 
_file_open(const char * filename,const char * cmode)128 static AACS_FILE_H *_file_open(const char* filename, const char *cmode)
129 {
130     AACS_FILE_H *file;
131     int fd    = -1;
132     int flags = 0;
133     int mode  = 0;
134 
135     if (strchr(cmode, 'w')) {
136         flags = O_WRONLY | O_CREAT | O_TRUNC;
137         mode  = S_IRUSR | S_IWUSR;
138     } else {
139         flags = O_RDONLY;
140     }
141 
142 #ifdef O_CLOEXEC
143     flags |= O_CLOEXEC;
144 #endif
145 #ifdef O_BINARY
146     flags |= O_BINARY;
147 #endif
148 
149     if ((fd = open(filename, flags, mode)) < 0) {
150         BD_DEBUG(DBG_FILE, "Error opening file %s\n", filename);
151         return NULL;
152     }
153 
154     file = calloc(1, sizeof(AACS_FILE_H));
155     if (!file) {
156         close(fd);
157         BD_DEBUG(DBG_FILE, "Error opening file %s (out of memory)\n", filename);
158         return NULL;
159     }
160 
161     file->close = _file_close;
162     file->seek  = _file_seek;
163     file->read  = _file_read;
164     file->write = _file_write;
165     file->tell  = _file_tell;
166 
167     file->internal = (void*)(intptr_t)fd;
168 
169     BD_DEBUG(DBG_FILE, "Opened POSIX file %s (%p)\n", filename, (void*)file);
170 
171     return file;
172 }
173 
174 AACS_FILE_H* (*file_open)(const char* filename, const char *mode) = _file_open;
175 
file_unlink(const char * file)176 int file_unlink(const char *file)
177 {
178     return remove(file);
179 }
180 
file_path_exists(const char * path)181 int file_path_exists(const char *path)
182 {
183     struct stat s;
184     return stat(path, &s);
185 }
186 
file_mkdir(const char * dir)187 int file_mkdir(const char *dir)
188 {
189     return mkdir(dir, S_IRWXU);
190 }
191