libisofs/src/stream.c

189 lines
3.8 KiB
C
Raw Normal View History

/*
* 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>
typedef struct
{
IsoFileSource *src;
/* key for file identification inside filesystem */
dev_t dev_id;
ino_t ino_id;
off_t size; /**< size of this file */
} FSrcStreamData;
static
int fsrc_open(IsoStream *stream)
{
IsoFileSource *src;
if (stream == NULL) {
return ISO_NULL_POINTER;
}
src = ((FSrcStreamData*)stream->data)->src;
return iso_file_source_open(src);
}
static
int fsrc_close(IsoStream *stream)
{
IsoFileSource *src;
if (stream == NULL) {
return ISO_NULL_POINTER;
}
src = ((FSrcStreamData*)stream->data)->src;
return iso_file_source_close(src);
}
static
off_t fsrc_get_size(IsoStream *stream)
{
FSrcStreamData *data;
data = (FSrcStreamData*)stream->data;
return data->size;
}
static
int fsrc_read(IsoStream *stream, void *buf, size_t count)
{
IsoFileSource *src;
if (stream == NULL) {
return ISO_NULL_POINTER;
}
src = ((FSrcStreamData*)stream->data)->src;
return iso_file_source_read(src, buf, count);
}
static
int fsrc_is_repeatable(IsoStream *stream)
{
int ret;
struct stat info;
FSrcStreamData *data;
if (stream == NULL) {
return ISO_NULL_POINTER;
}
data = (FSrcStreamData*)stream->data;
/* mode is not cached, this function is only useful for filters */
ret = iso_file_source_stat(data->src, &info);
if (ret < 0) {
return ret;
}
if (S_ISREG(info.st_mode) || S_ISBLK(info.st_mode)) {
return 1;
} else {
return 0;
}
}
static
int fsrc_get_id(IsoStream *stream, unsigned int *fs_id, dev_t *dev_id,
ino_t *ino_id)
{
FSrcStreamData *data;
IsoFilesystem *fs;
if (stream == NULL || fs_id == NULL || dev_id == NULL || ino_id == NULL) {
return ISO_NULL_POINTER;
}
data = (FSrcStreamData*)stream->data;
fs = iso_file_source_get_filesystem(data->src);
*fs_id = fs->get_id(fs);
if (fs_id == 0) {
return 0;
}
*dev_id = data->dev_id;
*ino_id = data->ino_id;
return ISO_SUCCESS;
}
static
void fsrc_free(IsoStream *stream)
{
FSrcStreamData *data;
data = (FSrcStreamData*)stream->data;
iso_file_source_unref(data->src);
free(data);
}
IsoStreamIface fsrc_stream_class = {
fsrc_open,
fsrc_close,
fsrc_get_size,
fsrc_read,
fsrc_is_repeatable,
fsrc_get_id,
fsrc_free
};
int iso_file_source_stream_new(IsoFileSource *src, IsoStream **stream)
{
int r;
struct stat info;
IsoStream *str;
FSrcStreamData *data;
if (src == NULL || stream == NULL) {
return ISO_NULL_POINTER;
}
r = iso_file_source_stat(src, &info);
if (r < 0) {
return r;
}
if (S_ISDIR(info.st_mode)) {
return ISO_FILE_IS_DIR;
}
str = malloc(sizeof(IsoStream));
if (str == NULL) {
return ISO_MEM_ERROR;
}
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;
str->refcount = 1;
str->data = data;
str->class = &fsrc_stream_class;
*stream = str;
return ISO_SUCCESS;
}
void iso_stream_ref(IsoStream *stream)
{
++stream->refcount;
}
void iso_stream_unref(IsoStream *stream)
{
if (--stream->refcount == 0) {
stream->class->free(stream);
free(stream);
}
}