Commit 21da4112 authored by hpi1's avatar hpi1

file: merge changes from libbluray

- add space between string constant and macro literal
- add error checks
- android support
- C++ support
parent 51e2c635
......@@ -38,15 +38,28 @@
* file access
*/
#define file_close(X) X->close(X)
#define file_seek(X,Y,Z) X->seek(X,Y,Z)
#define file_tell(X) X->tell(X)
static inline void file_close(AACS_FILE_H *fp)
{
fp->close(fp);
}
static inline int64_t file_tell(AACS_FILE_H *fp)
{
return fp->tell(fp);
}
static inline BD_USED int64_t file_seek(AACS_FILE_H *fp, int64_t offset, int32_t origin)
{
return fp->seek(fp, offset, origin);
}
static inline int64_t file_read(AACS_FILE_H *fp, void *buf, int64_t size) {
static inline int64_t file_read(AACS_FILE_H *fp, void *buf, int64_t size)
{
return fp->read(fp, buf, size);
}
static inline int64_t file_write(AACS_FILE_H *fp, const void *buf, int64_t size) {
static inline int64_t file_write(AACS_FILE_H *fp, const void *buf, int64_t size)
{
return fp->write ? fp->write(fp, buf, size) : 0;
}
......
......@@ -31,15 +31,24 @@
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef __ANDROID__
# undef lseek
# define lseek lseek64
# undef off_t
# define off_t off64_t
#endif
static void _file_close(AACS_FILE_H *file)
{
if (file) {
close((int)(intptr_t)file->internal);
if (close((int)(intptr_t)file->internal)) {
BD_DEBUG(DBG_CRIT | DBG_FILE, "Error closing POSIX file (%p)\n", (void*)file);
}
BD_DEBUG(DBG_FILE, "Closed POSIX file (%p)\n", (void*)file);
......@@ -67,7 +76,7 @@ static int64_t _file_read(AACS_FILE_H *file, uint8_t *buf, int64_t size)
ssize_t got, result;
if (size <= 0 || size >= BD_MAX_SSIZE) {
BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid read of size %"PRId64" (%p)\n", size, (void*)file);
BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid read of size %" PRId64 " (%p)\n", size, (void*)file);
return 0;
}
......@@ -92,7 +101,14 @@ static int64_t _file_write(AACS_FILE_H *file, const uint8_t *buf, int64_t size)
ssize_t written, result;
if (size <= 0 || size >= BD_MAX_SSIZE) {
BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid write of size %"PRId64" (%p)\n", size, (void*)file);
if (size == 0) {
if (fsync((int)(intptr_t)file->internal)) {
BD_DEBUG(DBG_FILE, "fsync() failed (%p)\n", (void*)file);
return -1;
}
return 0;
}
BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid write of size %" PRId64 " (%p)\n", size, (void*)file);
return 0;
}
......
......@@ -39,7 +39,9 @@
static void _file_close(AACS_FILE_H *file)
{
if (file) {
fclose((FILE *)file->internal);
if (fclose((FILE *)file->internal)) {
BD_DEBUG(DBG_FILE | DBG_CRIT, "Error closing WIN32 file (%p)\n", (void*)file);
}
BD_DEBUG(DBG_FILE, "Closed WIN32 file (%p)\n", (void*)file);
......@@ -71,7 +73,7 @@ static int64_t _file_read(AACS_FILE_H *file, uint8_t *buf, int64_t size)
return (int64_t)fread(buf, 1, (size_t)size, (FILE *)file->internal);
}
BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid read of size %"PRId64" (%p)\n", size, (void*)file);
BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid read of size %" PRId64 " (%p)\n", size, (void*)file);
return 0;
}
......@@ -81,7 +83,15 @@ static int64_t _file_write(AACS_FILE_H *file, const uint8_t *buf, int64_t size)
return (int64_t)fwrite(buf, 1, (size_t)size, (FILE *)file->internal);
}
BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid write of size %"PRId64" (%p)\n", size, (void*)file);
if (size == 0) {
if (fflush((FILE *)file->internal)) {
BD_DEBUG(DBG_FILE, "fflush() failed (%p)\n", (void*)file);
return -1;
}
return 0;
}
BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid write of size %" PRId64 " (%p)\n", size, (void*)file);
return 0;
}
......@@ -128,7 +138,10 @@ int file_unlink(const char *file)
{
wchar_t wfile[MAX_PATH];
MultiByteToWideChar(CP_UTF8, 0, file, -1, wfile, MAX_PATH);
if (!MultiByteToWideChar(CP_UTF8, 0, file, -1, wfile, MAX_PATH)) {
return -1;
}
return _wremove(wfile);
}
......@@ -136,7 +149,9 @@ int file_path_exists(const char *path)
{
wchar_t wpath[MAX_PATH];
MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, MAX_PATH);
if (!MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, MAX_PATH)) {
return -1;
}
DWORD dwAttrib = GetFileAttributesW(wpath);
if (dwAttrib != INVALID_FILE_ATTRIBUTES) {
......@@ -149,7 +164,9 @@ int file_mkdir(const char *dir)
{
wchar_t wdir[MAX_PATH];
MultiByteToWideChar(CP_UTF8, 0, dir, -1, wdir, MAX_PATH);
if (!MultiByteToWideChar(CP_UTF8, 0, dir, -1, wdir, MAX_PATH)) {
return -1;
}
if (!CreateDirectoryW(wdir, NULL))
return -1;
return 0;
......
......@@ -20,8 +20,16 @@
#ifndef AACS_FILESYSTEM_H_
#define AACS_FILESYSTEM_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
/*
* file access
*/
typedef struct aacs_file_s AACS_FILE_H;
struct aacs_file_s
{
......@@ -68,5 +76,8 @@ typedef AACS_FILE_H* (*AACS_FILE_OPEN2)(void *handle, const char* filename);
struct aacs;
AACS_PUBLIC void aacs_set_fopen(struct aacs *aacs, void *handle, AACS_FILE_OPEN2 p);
#ifdef __cplusplus
}
#endif
#endif /* AACS_FILESYSTEM_H_ */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment