2007-12-02 21:04:26 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Vreixo Formoso
|
|
|
|
*
|
|
|
|
* This file is part of the libisofs project; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation. See COPYING file for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libisofs.h"
|
|
|
|
#include "stream.h"
|
|
|
|
#include "fsource.h"
|
|
|
|
#include "error.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2007-12-29 00:58:42 +00:00
|
|
|
#include <string.h>
|
2007-12-02 21:04:26 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
IsoFileSource *src;
|
2007-12-18 19:46:28 +00:00
|
|
|
|
|
|
|
/* key for file identification inside filesystem */
|
|
|
|
dev_t dev_id;
|
|
|
|
ino_t ino_id;
|
|
|
|
off_t size; /**< size of this file */
|
|
|
|
} FSrcStreamData;
|
2007-12-02 21:04:26 +00:00
|
|
|
|
|
|
|
static
|
|
|
|
int fsrc_open(IsoStream *stream)
|
|
|
|
{
|
|
|
|
IsoFileSource *src;
|
|
|
|
if (stream == NULL) {
|
|
|
|
return ISO_NULL_POINTER;
|
|
|
|
}
|
2007-12-18 19:46:28 +00:00
|
|
|
src = ((FSrcStreamData*)stream->data)->src;
|
2007-12-20 19:47:39 +00:00
|
|
|
return iso_file_source_open(src);
|
2007-12-02 21:04:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int fsrc_close(IsoStream *stream)
|
|
|
|
{
|
|
|
|
IsoFileSource *src;
|
|
|
|
if (stream == NULL) {
|
|
|
|
return ISO_NULL_POINTER;
|
|
|
|
}
|
2007-12-18 19:46:28 +00:00
|
|
|
src = ((FSrcStreamData*)stream->data)->src;
|
2007-12-20 19:47:39 +00:00
|
|
|
return iso_file_source_close(src);
|
2007-12-02 21:04:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
off_t fsrc_get_size(IsoStream *stream)
|
|
|
|
{
|
2007-12-18 19:46:28 +00:00
|
|
|
FSrcStreamData *data;
|
|
|
|
data = (FSrcStreamData*)stream->data;
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-18 19:46:28 +00:00
|
|
|
return data->size;
|
2007-12-02 21:04:26 +00:00
|
|
|
}
|
|
|
|
|
2007-12-28 21:10:17 +00:00
|
|
|
static
|
2007-12-02 21:04:26 +00:00
|
|
|
int fsrc_read(IsoStream *stream, void *buf, size_t count)
|
|
|
|
{
|
|
|
|
IsoFileSource *src;
|
|
|
|
if (stream == NULL) {
|
|
|
|
return ISO_NULL_POINTER;
|
|
|
|
}
|
2007-12-18 19:46:28 +00:00
|
|
|
src = ((FSrcStreamData*)stream->data)->src;
|
2007-12-20 19:47:39 +00:00
|
|
|
return iso_file_source_read(src, buf, count);
|
2007-12-02 21:04:26 +00:00
|
|
|
}
|
|
|
|
|
2007-12-28 21:10:17 +00:00
|
|
|
static
|
2007-12-02 21:04:26 +00:00
|
|
|
int fsrc_is_repeatable(IsoStream *stream)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct stat info;
|
2007-12-18 19:46:28 +00:00
|
|
|
FSrcStreamData *data;
|
2007-12-02 21:04:26 +00:00
|
|
|
if (stream == NULL) {
|
|
|
|
return ISO_NULL_POINTER;
|
|
|
|
}
|
2007-12-18 19:46:28 +00:00
|
|
|
data = (FSrcStreamData*)stream->data;
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-18 19:46:28 +00:00
|
|
|
/* mode is not cached, this function is only useful for filters */
|
2007-12-20 19:47:39 +00:00
|
|
|
ret = iso_file_source_stat(data->src, &info);
|
2007-12-02 21:04:26 +00:00
|
|
|
if (ret < 0) {
|
2007-12-28 21:10:17 +00:00
|
|
|
return ret;
|
2007-12-02 21:04:26 +00:00
|
|
|
}
|
2007-12-18 19:46:28 +00:00
|
|
|
if (S_ISREG(info.st_mode) || S_ISBLK(info.st_mode)) {
|
2007-12-02 21:04:26 +00:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-15 11:51:48 +00:00
|
|
|
static
|
2007-12-28 21:10:17 +00:00
|
|
|
int fsrc_get_id(IsoStream *stream, unsigned int *fs_id, dev_t *dev_id,
|
2007-12-15 11:51:48 +00:00
|
|
|
ino_t *ino_id)
|
|
|
|
{
|
2007-12-18 19:46:28 +00:00
|
|
|
FSrcStreamData *data;
|
2007-12-15 11:51:48 +00:00
|
|
|
IsoFilesystem *fs;
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-15 11:51:48 +00:00
|
|
|
if (stream == NULL || fs_id == NULL || dev_id == NULL || ino_id == NULL) {
|
|
|
|
return ISO_NULL_POINTER;
|
|
|
|
}
|
2007-12-18 19:46:28 +00:00
|
|
|
data = (FSrcStreamData*)stream->data;
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-20 19:47:39 +00:00
|
|
|
fs = iso_file_source_get_filesystem(data->src);
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-15 12:13:49 +00:00
|
|
|
*fs_id = fs->get_id(fs);
|
|
|
|
if (fs_id == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-18 19:46:28 +00:00
|
|
|
*dev_id = data->dev_id;
|
|
|
|
*ino_id = data->ino_id;
|
2007-12-15 11:51:48 +00:00
|
|
|
return ISO_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2007-12-29 00:58:42 +00:00
|
|
|
static
|
|
|
|
char *fsrc_get_name(IsoStream *stream)
|
|
|
|
{
|
|
|
|
FSrcStreamData *data;
|
|
|
|
data = (FSrcStreamData*)stream->data;
|
|
|
|
return strdup(iso_file_source_get_path(data->src));
|
|
|
|
}
|
|
|
|
|
2007-12-02 21:04:26 +00:00
|
|
|
static
|
|
|
|
void fsrc_free(IsoStream *stream)
|
|
|
|
{
|
2007-12-18 19:46:28 +00:00
|
|
|
FSrcStreamData *data;
|
|
|
|
data = (FSrcStreamData*)stream->data;
|
|
|
|
iso_file_source_unref(data->src);
|
|
|
|
free(data);
|
2007-12-02 21:04:26 +00:00
|
|
|
}
|
|
|
|
|
2007-12-20 19:58:03 +00:00
|
|
|
IsoStreamIface fsrc_stream_class = {
|
|
|
|
fsrc_open,
|
|
|
|
fsrc_close,
|
|
|
|
fsrc_get_size,
|
|
|
|
fsrc_read,
|
|
|
|
fsrc_is_repeatable,
|
|
|
|
fsrc_get_id,
|
2007-12-29 00:58:42 +00:00
|
|
|
fsrc_get_name,
|
2007-12-20 19:58:03 +00:00
|
|
|
fsrc_free
|
|
|
|
};
|
|
|
|
|
2007-12-02 21:04:26 +00:00
|
|
|
int iso_file_source_stream_new(IsoFileSource *src, IsoStream **stream)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
struct stat info;
|
|
|
|
IsoStream *str;
|
2007-12-18 19:46:28 +00:00
|
|
|
FSrcStreamData *data;
|
|
|
|
|
2007-12-02 21:04:26 +00:00
|
|
|
if (src == NULL || stream == NULL) {
|
|
|
|
return ISO_NULL_POINTER;
|
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-20 19:47:39 +00:00
|
|
|
r = iso_file_source_stat(src, &info);
|
2007-12-02 21:04:26 +00:00
|
|
|
if (r < 0) {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
if (S_ISDIR(info.st_mode)) {
|
|
|
|
return ISO_FILE_IS_DIR;
|
|
|
|
}
|
2007-12-29 15:34:17 +00:00
|
|
|
|
|
|
|
/* check for read access to contents */
|
|
|
|
r = iso_file_source_access(src);
|
|
|
|
if (r < 0) {
|
|
|
|
return r;
|
|
|
|
}
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-02 21:04:26 +00:00
|
|
|
str = malloc(sizeof(IsoStream));
|
|
|
|
if (str == NULL) {
|
|
|
|
return ISO_MEM_ERROR;
|
|
|
|
}
|
2007-12-18 19:46:28 +00:00
|
|
|
data = malloc(sizeof(FSrcStreamData));
|
|
|
|
if (str == NULL) {
|
|
|
|
free(str);
|
|
|
|
return ISO_MEM_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* take the ref to IsoFileSource */
|
|
|
|
data->src = src;
|
|
|
|
data->dev_id = info.st_dev;
|
|
|
|
data->ino_id = info.st_ino;
|
|
|
|
data->size = info.st_size;
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-02 21:04:26 +00:00
|
|
|
str->refcount = 1;
|
2007-12-18 19:46:28 +00:00
|
|
|
str->data = data;
|
2007-12-20 19:58:03 +00:00
|
|
|
str->class = &fsrc_stream_class;
|
2007-12-28 21:10:17 +00:00
|
|
|
|
2007-12-02 21:04:26 +00:00
|
|
|
*stream = str;
|
|
|
|
return ISO_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
void iso_stream_ref(IsoStream *stream)
|
|
|
|
{
|
|
|
|
++stream->refcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void iso_stream_unref(IsoStream *stream)
|
|
|
|
{
|
|
|
|
if (--stream->refcount == 0) {
|
2007-12-20 19:58:03 +00:00
|
|
|
stream->class->free(stream);
|
2007-12-02 21:04:26 +00:00
|
|
|
free(stream);
|
|
|
|
}
|
|
|
|
}
|