You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
2698 lines
79 KiB
2698 lines
79 KiB
15 years ago
|
/*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
|
/*
|
||
|
* Filesystem/FileSource implementation to access an ISO image, using an
|
||
|
* IsoDataSource to read image data.
|
||
|
*/
|
||
|
|
||
|
#include "libisofs.h"
|
||
|
#include "ecma119.h"
|
||
|
#include "messages.h"
|
||
|
#include "rockridge.h"
|
||
|
#include "image.h"
|
||
|
#include "tree.h"
|
||
|
#include "eltorito.h"
|
||
|
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
#include <locale.h>
|
||
|
#include <langinfo.h>
|
||
|
#include <limits.h>
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Options for image reading.
|
||
|
* There are four kind of options:
|
||
|
* - Related to multisession support.
|
||
|
* In most cases, an image begins at LBA 0 of the data source. However,
|
||
|
* in multisession discs, the later image begins in the last session on
|
||
|
* disc. The block option can be used to specify the start of that last
|
||
|
* session.
|
||
|
* - Related to the tree that will be read.
|
||
|
* As default, when Rock Ridge extensions are present in the image, that
|
||
|
* will be used to get the tree. If RR extensions are not present, libisofs
|
||
|
* will use the Joliet extensions if available. Finally, the plain ISO-9660
|
||
|
* tree is used if neither RR nor Joliet extensions are available. With
|
||
|
* norock, nojoliet, and preferjoliet options, you can change this
|
||
|
* default behavior.
|
||
|
* - Related to default POSIX attributes.
|
||
|
* When Rock Ridege extensions are not used, libisofs can't figure out what
|
||
|
* are the the permissions, uid or gid for the files. You should supply
|
||
|
* default values for that.
|
||
|
*/
|
||
|
struct iso_read_opts
|
||
|
{
|
||
|
/**
|
||
|
* Block where the image begins, usually 0, can be different on a
|
||
|
* multisession disc.
|
||
|
*/
|
||
|
uint32_t block;
|
||
|
|
||
|
unsigned int norock : 1; /*< Do not read Rock Ridge extensions */
|
||
|
unsigned int nojoliet : 1; /*< Do not read Joliet extensions */
|
||
|
unsigned int noiso1999 : 1; /*< Do not read ISO 9660:1999 enhanced tree */
|
||
|
|
||
|
/**
|
||
|
* When both Joliet and RR extensions are present, the RR tree is used.
|
||
|
* If you prefer using Joliet, set this to 1.
|
||
|
*/
|
||
|
unsigned int preferjoliet : 1;
|
||
|
|
||
|
uid_t uid; /**< Default uid when no RR */
|
||
|
gid_t gid; /**< Default uid when no RR */
|
||
|
mode_t dir_mode; /**< Default mode when no RR (only permissions) */
|
||
|
mode_t file_mode;
|
||
|
/* TODO #00024 : option to convert names to lower case for iso reading */
|
||
|
|
||
|
/**
|
||
|
* Input charset for RR file names. NULL to use default locale charset.
|
||
|
*/
|
||
|
char *input_charset;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Return information for image.
|
||
|
* Both size, hasRR and hasJoliet will be filled by libisofs with suitable
|
||
|
* values.
|
||
|
*/
|
||
|
struct iso_read_image_features
|
||
|
{
|
||
|
/**
|
||
|
* Will be filled with the size (in 2048 byte block) of the image, as
|
||
|
* reported in the PVM.
|
||
|
*/
|
||
|
uint32_t size;
|
||
|
|
||
|
/** It will be set to 1 if RR extensions are present, to 0 if not. */
|
||
|
unsigned int hasRR :1;
|
||
|
|
||
|
/** It will be set to 1 if Joliet extensions are present, to 0 if not. */
|
||
|
unsigned int hasJoliet :1;
|
||
|
|
||
|
/**
|
||
|
* It will be set to 1 if the image is an ISO 9660:1999, i.e. it has
|
||
|
* a version 2 Enhanced Volume Descriptor.
|
||
|
*/
|
||
|
unsigned int hasIso1999 :1;
|
||
|
|
||
|
/** It will be set to 1 if El-Torito boot record is present, to 0 if not.*/
|
||
|
unsigned int hasElTorito :1;
|
||
|
};
|
||
|
|
||
|
static int ifs_fs_open(IsoImageFilesystem *fs);
|
||
|
static int ifs_fs_close(IsoImageFilesystem *fs);
|
||
|
static int iso_file_source_new_ifs(IsoImageFilesystem *fs,
|
||
|
IsoFileSource *parent, struct ecma119_dir_record *record,
|
||
|
IsoFileSource **src);
|
||
|
|
||
|
/** unique identifier for each image */
|
||
|
unsigned int fs_dev_id = 0;
|
||
|
|
||
|
/**
|
||
|
* Should the RR extensions be read?
|
||
|
*/
|
||
|
enum read_rr_ext {
|
||
|
RR_EXT_NO = 0, /*< Do not use RR extensions */
|
||
|
RR_EXT_110 = 1, /*< RR extensions conforming version 1.10 */
|
||
|
RR_EXT_112 = 2 /*< RR extensions conforming version 1.12 */
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Private data for the image IsoFilesystem
|
||
|
*/
|
||
|
typedef struct
|
||
|
{
|
||
|
/** DataSource from where data will be read */
|
||
|
IsoDataSource *src;
|
||
|
|
||
|
/** unique id for the each image (filesystem instance) */
|
||
|
unsigned int id;
|
||
|
|
||
|
/**
|
||
|
* Counter of the times the filesystem has been openned still pending of
|
||
|
* close. It is used to keep track of when we need to actually open or
|
||
|
* close the IsoDataSource.
|
||
|
*/
|
||
|
unsigned int open_count;
|
||
|
|
||
|
uid_t uid; /**< Default uid when no RR */
|
||
|
gid_t gid; /**< Default uid when no RR */
|
||
|
mode_t dir_mode; /**< Default mode when no RR (only permissions) */
|
||
|
mode_t file_mode;
|
||
|
|
||
|
int msgid;
|
||
|
|
||
|
char *input_charset; /**< Input charset for RR names */
|
||
|
char *local_charset; /**< For RR names, will be set to the locale one */
|
||
|
|
||
|
/**
|
||
|
* Will be filled with the block lba of the extend for the root directory
|
||
|
* of the hierarchy that will be read, either from the PVD (ISO, RR) or
|
||
|
* from the SVD (Joliet)
|
||
|
*/
|
||
|
uint32_t iso_root_block;
|
||
|
|
||
|
/**
|
||
|
* Will be filled with the block lba of the extend for the root directory,
|
||
|
* as read from the PVM
|
||
|
*/
|
||
|
uint32_t pvd_root_block;
|
||
|
|
||
|
/**
|
||
|
* Will be filled with the block lba of the extend for the root directory,
|
||
|
* as read from the SVD
|
||
|
*/
|
||
|
uint32_t svd_root_block;
|
||
|
|
||
|
/**
|
||
|
* Will be filled with the block lba of the extend for the root directory,
|
||
|
* as read from the enhanced volume descriptor (ISO 9660:1999)
|
||
|
*/
|
||
|
uint32_t evd_root_block;
|
||
|
|
||
|
/**
|
||
|
* If we need to read RR extensions. i.e., if the image contains RR
|
||
|
* extensions, and the user wants to read them.
|
||
|
*/
|
||
|
enum read_rr_ext rr;
|
||
|
|
||
|
/**
|
||
|
* Bytes skipped within the System Use field of a directory record, before
|
||
|
* the beginning of the SUSP system user entries. See IEEE 1281, SUSP. 5.3.
|
||
|
*/
|
||
|
uint8_t len_skp;
|
||
|
|
||
|
/* Volume attributes */
|
||
|
char *volset_id;
|
||
|
char *volume_id; /**< Volume identifier. */
|
||
|
char *publisher_id; /**< Volume publisher. */
|
||
|
char *data_preparer_id; /**< Volume data preparer. */
|
||
|
char *system_id; /**< Volume system identifier. */
|
||
|
char *application_id; /**< Volume application id */
|
||
|
char *copyright_file_id;
|
||
|
char *abstract_file_id;
|
||
|
char *biblio_file_id;
|
||
|
|
||
|
/* extension information */
|
||
|
|
||
|
/**
|
||
|
* RR version being used in image.
|
||
|
* 0 no RR extension, 1 RRIP 1.10, 2 RRIP 1.12
|
||
|
*/
|
||
|
enum read_rr_ext rr_version;
|
||
|
|
||
|
/** If Joliet extensions are available on image */
|
||
|
unsigned int joliet : 1;
|
||
|
|
||
|
/** If ISO 9660:1999 is available on image */
|
||
|
unsigned int iso1999 : 1;
|
||
|
|
||
|
/**
|
||
|
* Number of blocks of the volume, as reported in the PVM.
|
||
|
*/
|
||
|
uint32_t nblocks;
|
||
|
|
||
|
/* el-torito information */
|
||
|
unsigned int eltorito : 1; /* is el-torito available */
|
||
|
unsigned int bootable:1; /**< If the entry is bootable. */
|
||
|
unsigned char type; /**< The type of image */
|
||
|
unsigned char partition_type; /**< type of partition for HD-emul images */
|
||
|
short load_seg; /**< Load segment for the initial boot image. */
|
||
|
short load_size; /**< Number of sectors to load. */
|
||
|
uint32_t imgblock; /**< Block for El-Torito boot image */
|
||
|
uint32_t catblock; /**< Block for El-Torito catalog */
|
||
|
|
||
|
} _ImageFsData;
|
||
|
|
||
|
typedef struct image_fs_data ImageFileSourceData;
|
||
|
|
||
|
struct image_fs_data
|
||
|
{
|
||
|
IsoImageFilesystem *fs; /**< reference to the image it belongs to */
|
||
|
IsoFileSource *parent; /**< reference to the parent (NULL if root) */
|
||
|
|
||
|
struct stat info; /**< filled struct stat */
|
||
|
char *name; /**< name of this file */
|
||
|
|
||
|
uint32_t block; /**< block of the extend */
|
||
|
unsigned int opened : 2; /**< 0 not opened, 1 opened file, 2 opened dir */
|
||
|
|
||
|
/* info for content reading */
|
||
|
struct
|
||
|
{
|
||
|
/**
|
||
|
* - For regular files, once opened it points to a temporary data
|
||
|
* buffer of 2048 bytes.
|
||
|
* - For dirs, once opened it points to a IsoFileSource* array with
|
||
|
* its children
|
||
|
* - For symlinks, it points to link destination
|
||
|
*/
|
||
|
void *content;
|
||
|
|
||
|
/**
|
||
|
* - For regular files, number of bytes already read.
|
||
|
*/
|
||
|
off_t offset;
|
||
|
} data;
|
||
|
};
|
||
|
|
||
|
struct child_list
|
||
|
{
|
||
|
IsoFileSource *file;
|
||
|
struct child_list *next;
|
||
|
};
|
||
|
|
||
|
void child_list_free(struct child_list *list)
|
||
|
{
|
||
|
struct child_list *temp;
|
||
|
struct child_list *next = list;
|
||
|
while (next != NULL) {
|
||
|
temp = next->next;
|
||
|
iso_file_source_unref(next->file);
|
||
|
free(next);
|
||
|
next = temp;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static
|
||
|
char* ifs_get_path(IsoFileSource *src)
|
||
|
{
|
||
|
ImageFileSourceData *data;
|
||
|
data = src->data;
|
||
|
|
||
|
if (data->parent == NULL) {
|
||
|
return strdup("");
|
||
|
} else {
|
||
|
char *path = ifs_get_path(data->parent);
|
||
|
int pathlen = strlen(path);
|
||
|
path = realloc(path, pathlen + strlen(data->name) + 2);
|
||
|
path[pathlen] = '/';
|
||
|
path[pathlen + 1] = '\0';
|
||
|
return strcat(path, data->name);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static
|
||
|
char* ifs_get_name(IsoFileSource *src)
|
||
|
{
|
||
|
ImageFileSourceData *data;
|
||
|
data = src->data;
|
||
|
return data->name == NULL ? NULL : strdup(data->name);
|
||
|
}
|
||
|
|
||
|
static
|
||
|
int ifs_lstat(IsoFileSource *src, struct stat *info)
|
||
|
{
|
||
|
ImageFileSourceData *data;
|
||
|
|
||
|
if (src == NULL || info == NULL) {
|
||
|
return ISO_NULL_POINTER;
|
||
|
}
|
||
|
|
||
|
data = src->data;
|
||
|
*info = data->info;
|
||
|
return ISO_SUCCESS;
|
||
|
}
|
||
|
|
||
|
static
|
||
|
int ifs_stat(IsoFileSource *src, struct stat *info)
|
||
|
{
|
||
|
ImageFileSourceData *data;
|
||
|
|
||
|
if (src == NULL || info == NULL || src->data == NULL) {
|
||
|
return ISO_NULL_POINTER;
|
||
|
}
|
||
|
|
||
|
data = (ImageFileSourceData*)src->data;
|
||
|
|
||
|
if (S_ISLNK(data->info.st_mode)) {
|
||
|
/* TODO #00012 : support follow symlinks on image filesystem */
|
||
|
return ISO_FILE_BAD_PATH;
|
||
|
}
|
||
|
*info = data->info;
|
||
|
return ISO_SUCCESS;
|
||
|
}
|
||
|
|
||
|
static
|
||
|
int ifs_access(IsoFileSource *src)
|
||
|
{
|
||
|
/* we always have access, it is controlled by DataSource */
|
||
|
return ISO_SUCCESS;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Read all directory records in a directory, and creates an IsoFileSource for
|
||
|
* each of them, storing them in the data field of the IsoFileSource for the
|
||
|
* given dir.
|
||
|
*/
|
||
|
static
|
||
|
int read_dir(ImageFileSourceData *data)
|
||
|
{
|
||
|
int ret;
|
||
|
uint32_t size;
|
||
|
uint32_t block;
|
||
|
IsoImageFilesystem *fs;
|
||
|
_ImageFsData *fsdata;
|
||
|
struct ecma119_dir_record *record;
|
||
|
uint8_t buffer[BLOCK_SIZE];
|
||
|
IsoFileSource *child = NULL;
|
||
|
uint32_t pos = 0;
|
||
|
uint32_t tlen = 0;
|
||
|
|
||
|
if (data == NULL) {
|
||
|
return ISO_NULL_POINTER;
|
||
|
}
|
||
|
|
||
|
fs = data->fs;
|
||
|
fsdata = fs->data;
|
||
|
|
||
|
block = data->block;
|
||
|
ret = fsdata->src->read_block(fsdata->src, block, buffer);
|
||
|
if (ret < 0) {
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
/* "." entry, get size of the dir and skip */
|
||
|
record = (struct ecma119_dir_record *)(buffer + pos);
|
||
|
size = iso_read_bb(record->length, 4, NULL);
|
||
|
tlen += record->len_dr[0];
|
||
|
pos += record->len_dr[0];
|
||
|
|
||
|
/* skip ".." */
|
||
|
record = (struct ecma119_dir_record *)(buffer + pos);
|
||
|
tlen += record->len_dr[0];
|
||
|
pos += record->len_dr[0];
|
||
|
|
||
|
while (tlen < size) {
|
||
|
|
||
|
record = (struct ecma119_dir_record *)(buffer + pos);
|
||
|
if (pos == 2048 || record->len_dr[0] == 0) {
|
||
|
/*
|
||
|
* The directory entries are splitted in several blocks
|
||
|
* read next block
|
||
|
*/
|
||
|
ret = fsdata->src->read_block(fsdata->src, ++block, buffer);
|
||
|
if (ret < 0) {
|
||
|
return ret;
|
||
|
}
|
||
|
tlen += 2048 - pos;
|
||
|
pos = 0;
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* What about ignoring files with existence flag?
|
||
|
* if (record->flags[0] & 0x01)
|
||
|
* continue;
|
||
|
*/
|
||
|
|
||
|
/*
|
||
|
* For a extrange reason, mkisofs relocates directories under
|
||
|
* a RR_MOVED dir. It seems that it is only used for that purposes,
|
||
|
* and thus it should be removed from the iso tree before
|
||
|
* generating a new image with libisofs, that don't uses it.
|
||
|
*/
|
||
|
if (data->parent == NULL && record->len_fi[0] == 8
|
||
|
&& !strncmp((char*)record->file_id, "RR_MOVED", 8)) {
|
||
|
|
||
|
iso_msg_debug(fsdata->msgid, "Skipping RR_MOVE entry.");
|
||
|
|
||
|
tlen += record->len_dr[0];
|
||
|
pos += record->len_dr[0];
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* We pass a NULL parent instead of dir, to prevent the circular
|
||
|
* reference from child to parent.
|
||
|
*/
|
||
|
ret = iso_file_source_new_ifs(fs, NULL, record, &child);
|
||
|
if (ret < 0) {
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
/* add to the child list */
|
||
|
if (ret != 0) {
|
||
|
struct child_list *node;
|
||
|
node = malloc(sizeof(struct child_list));
|
||
|
if (node == NULL) {
|
||
|
iso_file_source_unref(child);
|
||
|
return ISO_OUT_OF_MEM;
|
||
|
}
|
||
|
/*
|
||
|
* Note that we insert in reverse order. This leads to faster
|
||
|
* addition here, but also when adding to the tree, as insertion
|
||
|
* will be done, sorted, in the first position of the list.
|
||
|
*/
|
||
|
node->next = data->data.content;
|
||
|
node->file = child;
|
||
|
data->data.content = node;
|
||
|
}
|
||
|
|
||
|
tlen += record->len_dr[0];
|
||
|
pos += record->len_dr[0];
|
||
|
}
|
||
|
|
||
|
return ISO_SUCCESS;
|
||
|
}
|
||
|
|
||
|
static
|
||
|
int ifs_open(IsoFileSource *src)
|
||
|
{
|
||
|
int ret;
|
||
|
ImageFileSourceData *data;
|
||
|
|
||
|
if (src == NULL || src->data == NULL) {
|
||
|
return ISO_NULL_POINTER;
|
||
|
}
|
||
|
data = (ImageFileSourceData*)src->data;
|
||
|
|
||
|
if (data->opened) {
|
||
15 years ago
|
return ISO_FILE_ALREADY_OPENED;
|
||
15 years ago
|
}
|
||
|
|
||
|
if (S_ISDIR(data->info.st_mode)) {
|
||
|
/* ensure fs is openned */
|
||
|
ret = data->fs->open(data->fs);
|
||
|
if (ret < 0) {
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* Cache all directory entries.
|
||
|
* This can waste more memory, but improves as disc is read in much more
|
||
|
* sequencially way, thus reducing jump between tracks on disc
|
||
|
*/
|
||
|
ret = read_dir(data);
|
||
|
data->fs->close(data->fs);
|
||
|
|
||
|
if (ret < 0) {
|
||
|
/* free probably allocated children */
|
||
|
child_list_free((struct child_list*)data->data.content);
|
||
|
} else {
|
||
|
data->opened = 2;
|
||
|
}
|
||
|
|
||
|
return ret;
|
||
|
} else if (S_ISREG(data->info.st_mode)) {
|
||
|
/* ensure fs is openned */
|
||
|
ret = data->fs->open(data->fs);
|
||
|
if (ret < 0) {
|
||
|
return ret;
|
||
|
}
|
||
|
data->data.content = malloc(BLOCK_SIZE);
|
||
|
if (data->data.content == NULL) {
|
||
|
return ISO_OUT_OF_MEM;
|
||
|
}
|
||
|
data->data.offset = 0;
|
||
|
data->opened = 1;
|
||
|
} else {
|
||
|
/* symlinks and special files inside image can't be openned */
|
||
|
return ISO_FILE_ERROR;
|
||
|
}
|
||
|
return ISO_SUCCESS;
|
||
|
}
|
||
|
|
||
|
static
|
||
|
int ifs_close(IsoFileSource *src)
|
||
|
{
|
||
|
ImageFileSourceData *data;
|
||
|
|
||
|
if (src == NULL || src->data == NULL) {
|
||
|
return ISO_NULL_POINTER;
|
||
|
}
|
||
|
data = (ImageFileSourceData*)src->data;
|
||
|
|
||
|
if (!data->opened) {
|
||
15 years ago
|
return ISO_FILE_NOT_OPENED;
|
||
15 years ago
|
}
|
||
|
|
||
|
if (data->opened == 2) {
|
||
|
/*
|
||
|
* close a dir, free all pending pre-allocated children.
|
||
|
* not that we don't need to close the filesystem, it was already
|
||
|
* closed
|
||
|
*/
|
||
|
child_list_free((struct child_list*) data->data.content);
|
||
|
data->data.content = NULL;
|
||
|
data->opened = 0;
|
||
|
} else if (data->opened == 1) {
|
||
|
/* close regular file */
|
||
|
free(data->data.content);
|
||
|
data->fs->close(data->fs);
|
||
|
data->data.content = NULL;
|
||
|
data->opened = 0;
|
||
|
} else {
|
||
|
/* TODO only dirs and files supported for now */
|
||
|
return ISO_ERROR;
|
||
|
}
|
||
|
|
||
|
return ISO_SUCCESS;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Attempts to read up to count bytes from the given source into
|
||
|
* the buffer starting at buf.
|
||
|
*
|
||
|
* The file src must be open() before calling this, and close() when no
|
||
|
* more needed. Not valid for dirs. On symlinks it reads the destination
|
||
|
* file.
|
||
|
*
|
||
|
* @return
|
||
|
* number of bytes read, 0 if EOF, < 0 on error
|
||
|
* Error codes:
|
||
|
* ISO_FILE_ERROR
|
||
|
* ISO_NULL_POINTER
|
||
15 years ago
|
* ISO_FILE_NOT_OPENED
|
||
15 years ago
|
* ISO_FILE_IS_DIR
|
||
|
* ISO_OUT_OF_MEM
|
||
|
* ISO_INTERRUPTED
|
||
|
*/
|
||
|
static
|
||
|
int ifs_read(IsoFileSource *src, void *buf, size_t count)
|
||
|
{
|
||
|
int ret;
|
||
|
ImageFileSourceData *data;
|
||
|
uint32_t read = 0;
|
||
|
|
||
|
if (src == NULL || src->data == NULL || buf == NULL) {
|
||
|
return ISO_NULL_POINTER;
|
||
|
}
|
||
|
if (count == 0) {
|
||
|
return ISO_WRONG_ARG_VALUE;
|
||
|
}
|
||
|
data = (ImageFileSourceData*)src->data;
|
||
|
|
||
|
if (!data->opened) {
|
||
15 years ago
|
return ISO_FILE_NOT_OPENED;
|
||
15 years ago
|
} else if (data->opened != 1) {
|
||
|
return ISO_FILE_IS_DIR;
|
||
|
}
|
||
|
|
||
|
while (read < count && data->data.offset < data->info.st_size) {
|
||
|
size_t bytes;
|
||
|
uint8_t *orig;
|
||
|
|
||
|
if (data->data.offset % BLOCK_SIZE == 0) {
|
||
|
/* we need to buffer next block */
|
||
|
uint32_t block;
|
||
|
_ImageFsData *fsdata;
|
||
|
|
||
|
if (data->data.offset >= data->info.st_size) {
|
||
|
/* EOF */
|
||
|
break;
|
||
|
}
|
||
|
fsdata = data->fs->data;
|
||
|
block = data->block + (data->data.offset / BLOCK_SIZE);
|
||
|
ret = fsdata->src->read_block(fsdata->src, block,
|
||
|
data->data.content);
|
||
|
if (ret < 0) {
|
||
|
return ret;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* how much can I read */
|
||
|
bytes = MIN(BLOCK_SIZE - (data->data.offset % BLOCK_SIZE),
|
||
|
count - read);
|
||
|
if (data->data.offset + (off_t)bytes > data->info.st_size) {
|
||
|
bytes = data->info.st_size - data->data.offset;
|
||
|
}
|
||
|
orig = data->data.content;
|
||
|
orig += data->data.offset % BLOCK_SIZE;
|
||
|
memcpy((uint8_t*)buf + read, orig, bytes);
|
||
|
read += bytes;
|
||
|
data->data.offset += (off_t)bytes;
|
||
|
}
|
||
|
return read;
|
||
|
}
|
||
|
|
||
15 years ago
|
static
|
||
|
off_t ifs_lseek(IsoFileSource *src, off_t offset, int flag)
|
||
|
{
|
||
|
ImageFileSourceData *data;
|
||
|
|
||
|
if (src == NULL) {
|
||
|
return (off_t)ISO_NULL_POINTER;
|
||
|
}
|
||
|
if (offset < (off_t)0) {
|
||
|
return (off_t)ISO_WRONG_ARG_VALUE;
|
||
|
}
|
||
|
|
||
|
data = src->data;
|
||
|
|
||
|
if (!data->opened) {
|
||
|
return (off_t)ISO_FILE_NOT_OPENED;
|
||
|
} else if (data->opened != 1) {
|
||
|
return (off_t)ISO_FILE_IS_DIR;
|
||
|
}
|
||
|
|
||
|
switch (flag) {
|
||
|
case 0: /* SEEK_SET */
|
||
|
data->data.offset = offset;
|
||
|
break;
|
||
|
case 1: /* SEEK_CUR */
|
||
|
data->data.offset += offset;
|
||
|
break;
|
||
|
case 2: /* SEEK_END */
|
||
|
/* do this make sense? */
|
||
|
data->data.offset = data->info.st_size + offset;
|
||
|
break;
|
||
|
default:
|
||
|
return (off_t)ISO_WRONG_ARG_VALUE;
|
||
|
}
|
||
|
|
||
|
if (data->data.offset % BLOCK_SIZE != 0) {
|
||
|
/* we need to buffer the block */
|
||
|
uint32_t block;
|
||
|
_ImageFsData *fsdata;
|
||
|
|
||
|
if (data->data.offset < data->info.st_size) {
|
||
|
int ret;
|
||
|
fsdata = data->fs->data;
|
||
|
block = data->block + (data->data.offset / BLOCK_SIZE);
|
||
|
ret = fsdata->src->read_block(fsdata->src, block,
|
||
|
data->data.content);
|
||
|
if (ret < 0) {
|
||
|
return (off_t)ret;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return data->data.offset;
|
||
|
}
|
||
|
|
||
15 years ago
|
static
|
||
|
int ifs_readdir(IsoFileSource *src, IsoFileSource **child)
|
||
|
{
|
||
|
ImageFileSourceData *data, *cdata;
|
||
|
struct child_list *children;
|
||
|
|
||
|
if (src == NULL || src->data == NULL || child == NULL) {
|
||
|
return ISO_NULL_POINTER;
|
||
|
}
|
||
|
data = (ImageFileSourceData*)src->data;
|
||
|
|
||
|
if (!data->opened) {
|
||
15 years ago
|
return ISO_FILE_NOT_OPENED;
|
||
15 years ago
|
} else if (data->opened != 2) {
|
||
|
return ISO_FILE_IS_NOT_DIR;
|
||
|
}
|
||
|
|
||
|
/* return the first child and free it */
|
||
|
if (data->data.content == NULL) {
|
||
|
return 0; /* EOF */
|
||
|
}
|
||
|
|
||
|
children = (struct child_list*)data->data.content;
|
||
|
*child = children->file;
|
||
|
cdata = (ImageFileSourceData*)(*child)->data;
|
||
|
|
||
|
/* set the ref to the parent */
|
||
|
cdata->parent = src;
|
||
|
iso_file_source_ref(src);
|
||
|
|
||
|
/* free the first element of the list */
|
||
|
data->data.content = children->next;
|
||
|
free(children);
|
||
|
|
||
|
return ISO_SUCCESS;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Read the destination of a symlink. You don't need to open the file
|
||
|
* to call this.
|
||
|
*
|
||
|
* @param buf
|
||
|
* allocated buffer of at least bufsiz bytes.
|
||
|
* The dest. will be copied there, and it will be NULL-terminated
|
||
|
* @param bufsiz
|
||
|
* characters to be copied. Destination link will be truncated if
|
||
|
* it is larger than given size. This include the \0 character.
|
||
|
* @return
|
||
|
* 1 on success, < 0 on error
|
||
|
* Error codes:
|
||
|
* ISO_FILE_ERROR
|
||
|
* ISO_NULL_POINTER
|
||
|
* ISO_WRONG_ARG_VALUE -> if bufsiz <= 0
|
||
|
* ISO_FILE_IS_NOT_SYMLINK
|
||
|
* ISO_OUT_OF_MEM
|
||
|
* ISO_FILE_BAD_PATH
|
||
|
* ISO_FILE_DOESNT_EXIST
|
||
|
*
|
||
|
*/
|
||
|
static
|
||
|
int ifs_readlink(IsoFileSource *src, char *buf, size_t bufsiz)
|
||
|
{
|
||
|
char *dest;
|
||
|
size_t len;
|
||
|
ImageFileSourceData *data;
|
||
|
|
||
|
if (src == NULL || buf == NULL || src->data == NULL) {
|
||
|
return ISO_NULL_POINTER;
|
||
|
}
|
||
|
|
||
|
if (bufsiz <= 0) {
|
||
|
return ISO_WRONG_ARG_VALUE;
|
||
|
}
|
||
|
|
||
|
data = (ImageFileSourceData*)src->data;
|
||
|
|
||
|
if (!S_ISLNK(data->info.st_mode)) {
|
||
|
return ISO_FILE_IS_NOT_SYMLINK;
|
||
|
}
|
||
|
|
||
|
dest = (char*)data->data.content;
|
||
|
len = strlen(dest);
|
||
|
if (bufsiz <= len) {
|
||
|
len = bufsiz - 1;
|
||
|
}
|
||
|
|
||
|
strncpy(buf, dest, len);
|
||
|
buf[len] = '\0';
|
||
|
|
||
|
return ISO_SUCCESS;
|
||
|
}
|
||
|
|
||
|
static
|
||
|
IsoFilesystem* ifs_get_filesystem(IsoFileSource *src)
|
||
|
{
|
||
|
ImageFileSourceData *data;
|
||
|
|
||
|
if (src == NULL) {
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
data = src->data;
|
||
|
return data->fs;
|
||
|
}
|
||
|
|
||
|
static
|
||
|
void ifs_free(IsoFileSource *src)
|
||
|
{
|
||
|
ImageFileSourceData *data;
|
||
|
|
||
|
data = src->data;
|
||
|
|
||
|
/* close the file if it is already openned */
|
||
|
if (data->opened) {
|
||
|
src->class->close(src);
|
||
|
}
|
||
|
|
||
|
/* free destination if it is a link */
|
||
|
if (S_ISLNK(data->info.st_mode)) {
|
||
|
free(data->data.content);
|
||
|
}
|
||
|
iso_filesystem_unref(data->fs);
|
||
|
if (data->parent != NULL) {
|
||
|
iso_file_source_unref(data->parent);
|
||
|
}
|
||
|
free(data->name);
|
||
|
free(data);
|
||
|
}
|
||
|
|
||
|
IsoFileSourceIface ifs_class = {
|
||
|
0, /* version */
|
||
|
ifs_get_path,
|
||
|
ifs_get_name,
|
||
|
ifs_lstat,
|
||
|
ifs_stat,
|
||
|
ifs_access,
|
||
|
ifs_open,
|
||
|
ifs_close,
|
||
|
ifs_read,
|
||
|
ifs_readdir,
|
||
|
ifs_readlink,
|
||
|
ifs_get_filesystem,
|
||
15 years ago
|
ifs_free,
|
||
|
ifs_lseek
|
||
15 years ago
|
};
|
||
|
|
||
|
/**
|
||
|
* Read a file name from a directory record, doing the needed charset
|
||
|
* conversion
|
||
|
*/
|
||
|
static
|
||
|
char *get_name(_ImageFsData *fsdata, const char *str, size_t len)
|
||
|
{
|
||
|
int ret;
|
||
|
char *name = NULL;
|
||
|
if (strcmp(fsdata->local_charset, fsdata->input_charset)) {
|
||
|
/* charset conversion needed */
|
||
|
ret = strnconv(str, fsdata->input_charset, fsdata->local_charset, len,
|
||
|
&name);
|
||
|
if (ret == 1) {
|
||
|
return name;
|
||
|
} else {
|
||
|
ret = iso_msg_submit(fsdata->msgid, ISO_FILENAME_WRONG_CHARSET, ret,
|
||
|
"Charset conversion error. Can't convert %s from %s to %s",
|
||
|
str, fsdata->input_charset, fsdata->local_charset);
|
||
|
if (ret < 0) {
|
||
|
return NULL; /* aborted */
|
||
|
}
|
||
|
/* fallback */
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* we reach here when the charset conversion is not needed or has failed */
|
||
|
|
||
|
name = malloc(len + 1);
|
||
|
if (name == NULL) {
|
||
|
return NULL;
|
||
|
}
|
||
|
memcpy(name, str, len);
|
||
|
name[len] = '\0';
|
||
|
return name;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* @return
|
||
|
* 1 success, 0 record ignored (not an error, can be a relocated dir),
|
||
|
* < 0 error
|
||
|
*/
|
||
|
static
|
||
|
int iso_file_source_new_ifs(IsoImageFilesystem *fs, IsoFileSource *parent,
|
||
|
struct ecma119_dir_record *record,
|
||
|
IsoFileSource **src)
|
||
|
{
|
||
|
int ret;
|
||
|
struct stat atts;
|
||
|
time_t recorded;
|
||
|
_ImageFsData *fsdata;
|
||
|
IsoFileSource *ifsrc = NULL;
|
||
|
ImageFileSourceData *ifsdata = NULL;
|
||
|
|
||
|
int namecont = 0; /* 1 if found a NM with CONTINUE flag */
|
||
|
char *name = NULL;
|
||
|
|
||
|
/* 1 if found a SL with CONTINUE flag,
|
||
|
* 2 if found a component with continue flag */
|
||
|
int linkdestcont = 0;
|
||
|
char *linkdest = NULL;
|
||
|
|
||
|
uint32_t relocated_dir = 0;
|
||
|
|
||
|
if (fs == NULL || fs->data == NULL || record == NULL || src == NULL) {
|
||
|
return ISO_NULL_POINTER;
|
||
|
}
|
||
|
|
||
|
fsdata = (_ImageFsData*)fs->data;
|
||
|
|
||
|
memset(&atts, 0, sizeof(struct stat));
|
||
|
|
||
|
/*
|
||
|
* First of all, check for unsupported ECMA-119 features
|
||
|
*/
|
||
|
|
||
|
/* check for unsupported multiextend */
|
||
|
if (record->flags[0] & 0x80) {
|
||
|
iso_msg_submit(fsdata->msgid, ISO_UNSUPPORTED_ECMA119, 0,
|
||
|
"Unsupported image. This image makes use of Multi-Extend"
|
||
|
" features, that are not supported at this time. If you "
|
||
|
"need support for that, please request us this feature.");
|
||
|
return ISO_UNSUPPORTED_ECMA119;
|
||
|
}
|
||
|
|
||
|
/* check for unsupported interleaved mode */
|
||
|
if (record->file_unit_size[0] || record->interleave_gap_size[0]) {
|
||
|
iso_msg_submit(fsdata->msgid, ISO_UNSUPPORTED_ECMA119, 0,
|
||
|
"Unsupported image. This image has at least one file recorded "
|
||
|
"in interleaved mode. We don't support this mode, as we think "
|
||
|
"it's not used. If you're reading this, then we're wrong :) "
|
||
|
"Please contact libisofs developers, so we can fix this.");
|
||
|
return ISO_UNSUPPORTED_ECMA119;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* Check for extended attributes, that are not supported. Note that even
|
||
|
* if we don't support them, it is easy to ignore them.
|
||
|
*/
|
||
|
if (record->len_xa[0]) {
|
||
|
iso_msg_submit(fsdata->msgid, ISO_UNSUPPORTED_ECMA119, 0,
|
||
|
"Unsupported image. This image has at least one file with "
|
||
|
"Extended Attributes, that are not supported");
|
||
|
return ISO_UNSUPPORTED_ECMA119;
|
||
|
}
|
||
|
|
||
|
/* TODO #00013 : check for unsupported flags when reading a dir record */
|
||
|
|
||
|
/*
|
||
|
* The idea is to read all the RR entries (if we want to do that and RR
|
||
|
* extensions exist on image), storing the info we want from that.
|
||
|
* Then, we need some sanity checks.
|
||
|
* Finally, we select what kind of node it is, and set values properly.
|
||
|
*/
|
||
|
|
||
|
if (fsdata->rr) {
|
||
|
struct susp_sys_user_entry *sue;
|
||
|
SuspIterator *iter;
|
||
|
|
||
|
|
||
|
iter = susp_iter_new(fsdata->src, record, fsdata->len_skp,
|
||
|
fsdata->msgid);
|
||
|
if (iter == NULL) {
|
||
|
return ISO_OUT_OF_MEM;
|
||
|
}
|
||
|
|
||
|
while ((ret = susp_iter_next(iter, &sue)) > 0) {
|
||
|
|
||
|
/* ignore entries from different version */
|
||
|
if (sue->version[0] != 1)
|
||
|
continue;
|
||
|
|
||
|
if (SUSP_SIG(sue, 'P', 'X')) {
|
||
|
ret = read_rr_PX(sue, &atts);
|
||
|
if (ret < 0) {
|
||
|
/* notify and continue */
|
||
|
ret = iso_msg_submit(fsdata->msgid, ISO_WRONG_RR_WARN, ret,
|
||
|
"Invalid PX entry");
|
||
|
}
|
||
|
} else if (SUSP_SIG(sue, 'T', 'F')) {
|
||
|
ret = read_rr_TF(sue, &atts);
|
||
|
if (ret < 0) {
|
||
|
/* notify and continue */
|
||
|
ret = iso_msg_submit(fsdata->msgid, ISO_WRONG_RR_WARN, ret,
|
||
|
"Invalid TF entry");
|
||
|
}
|
||
|
} else if (SUSP_SIG(sue, 'N', 'M')) {
|
||
|
if (name != NULL && namecont == 0) {
|
||
|
/* ups, RR standard violation */
|
||
|
ret = iso_msg_submit(fsdata->msgid, ISO_WRONG_RR_WARN, 0,
|
||
|
"New NM entry found without previous"
|
||
|
"CONTINUE flag. Ignored");
|
||
|
continue;
|
||
|
}
|
||
|
ret = read_rr_NM(sue, &name, &namecont);
|
||
|
if (ret < 0) {
|
||
|
/* notify and continue */
|
||
|
ret = iso_msg_submit(fsdata->msgid, ISO_WRONG_RR_WARN, ret,
|
||
|
"Invalid NM entry");
|
||
|
}
|
||
|
} else if (SUSP_SIG(sue, 'S', 'L')) {
|
||
|
if (linkdest != NULL && linkdestcont == 0) {
|
||
|
/* ups, RR standard violation */
|
||
|
ret = iso_msg_submit(fsdata->msgid, ISO_WRONG_RR_WARN, 0,
|
||
|
"New SL entry found without previous"
|
||
|
"CONTINUE flag. Ignored");
|
||
|
continue;
|
||
|
}
|
||
|
ret = read_rr_SL(sue, &linkdest, &linkdestcont);
|
||
|
if (ret < 0) {
|
||
|
/* notify and continue */
|
||
|
ret = iso_msg_submit(fsdata->msgid, ISO_WRONG_RR_WARN, ret,
|
||
|
"Invalid SL entry");
|
||
|
}
|
||
|
} else if (SUSP_SIG(sue, 'R', 'E')) {
|
||
|
/*
|
||
|
* this directory entry refers to a relocated directory.
|
||
|
* We simply ignore it, as it will be correctly handled
|
||
|
* when found the CL
|
||
|
*/
|
||
|
susp_iter_free(iter);
|
||
|
free(name);
|
||
|
return 0; /* it's not an error */
|
||
|
} else if (SUSP_SIG(sue, 'C', 'L')) {
|
||
|
/*
|
||
|
* This entry is a placeholder for a relocated dir.
|
||
|
* We need to ignore other entries, with the exception of NM.
|
||
|
* Then we create a directory node that represents the
|
||
|
* relocated dir, and iterate over its children.
|
||
|
*/
|
||
|
relocated_dir = iso_read_bb(sue->data.CL.child_loc, 4, NULL);
|
||
|
if (relocated_dir == 0) {
|
||
|
ret = iso_msg_submit(fsdata->msgid, ISO_WRONG_RR, 0,
|
||
|
"Invalid SL entry, no child location");
|
||
|
break;
|
||
|
}
|
||
|
} else if (SUSP_SIG(sue, 'P', 'N')) {
|
||
|
ret = read_rr_PN(sue, &atts);
|
||
|
if (ret < 0) {
|
||
|
/* notify and continue */
|
||
|
ret = iso_msg_submit(fsdata->msgid, ISO_WRONG_RR_WARN, ret,
|
||
|
"Invalid PN entry");
|
||
|
}
|
||
|
} else if (SUSP_SIG(sue, 'S', 'F')) {
|
||
|
ret = iso_msg_submit(fsdata->msgid, ISO_UNSUPPORTED_RR, 0,
|
||
|
"Sparse files not supported.");
|
||
|
break;
|
||
|
} else if (SUSP_SIG(sue, 'R', 'R')) {
|
||
|
/* TODO I've seen this RR on mkisofs images. what's this? */
|
||
|
continue;
|
||
|
} else if (SUSP_SIG(sue, 'S', 'P')) {
|
||
|
/*
|
||
|
* Ignore this, to prevent the hint message, if we are dealing
|
||
|
* with root node (SP is only valid in "." of root node)
|
||
|
*/
|
||
|
if (parent != NULL) {
|
||
|
/* notify and continue */
|
||
|
ret = iso_msg_submit(fsdata->msgid, ISO_WRONG_RR, 0,
|
||
|
"SP entry found in a directory entry other "
|
||
|
"than '.' entry of root node");
|
||
|
}
|
||
|
continue;
|
||
|
} else if (SUSP_SIG(sue, 'E', 'R')) {
|
||
|
/*
|
||
|
* Ignore this, to prevent the hint message, if we are dealing
|
||
|
* with root node (ER is only valid in "." of root node)
|
||
|
*/
|
||
|
if (parent != NULL) {
|
||
|
/* notify and continue */
|
||
|
ret = iso_msg_submit(fsdata->msgid, ISO_WRONG_RR, 0,
|
||
|
"ER entry found in a directory entry other "
|
||
|
"than '.' entry of root node");
|
||
|
}
|
||
|
continue;
|
||
|
} else {
|
||
|
ret = iso_msg_submit(fsdata->msgid, ISO_SUSP_UNHANDLED, 0,
|
||
|
"Unhandled SUSP entry %c%c.", sue->sig[0], sue->sig[1]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
susp_iter_free(iter);
|
||
|
|
||
|
/* check for RR problems */
|
||
|
|
||
|
if (ret < 0) {
|
||
|
/* error was already submitted above */
|
||
|
iso_msg_debug(fsdata->msgid, "Error parsing RR entries");
|
||
|
} else if (!relocated_dir && atts.st_mode == (mode_t) 0 ) {
|
||
|
ret = iso_msg_submit(fsdata->msgid, ISO_WRONG_RR, 0, "Mandatory "
|
||
|
"Rock Ridge PX entry is not present or it "
|
||
|
"contains invalid values.");
|
||
|
} else {
|
||
|
/* ensure both name and link dest are finished */
|
||
|
if (namecont != 0) {
|
||
|
ret = iso_msg_submit(fsdata->msgid, ISO_WRONG_RR, 0,
|
||
|
"Incomplete RR name, last NM entry continues");
|
||
|
}
|
||
|
if (linkdestcont != 0) {
|
||
|
ret = iso_msg_submit(fsdata->msgid, ISO_WRONG_RR, 0,
|
||
|
"Incomplete link destination, last SL entry continues");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (ret < 0) {
|
||
|
free(name);
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
/* convert name to needed charset */
|
||
|
if (strcmp(fsdata->input_charset, fsdata->local_charset) && name) {
|
||
|
/* we need to convert name charset */
|
||
|
char *newname = NULL;
|
||
|
ret = strconv(name, fsdata->input_charset, fsdata->local_charset,
|
||
|
&newname);
|
||
|
if (ret < 0) {
|
||
|
/* its just a hint message */
|
||
|
ret = iso_msg_submit(fsdata->msgid, ISO_FILENAME_WRONG_CHARSET,
|
||
|
ret, "Charset conversion error. Can't "
|
||
|
"convert %s from %s to %s", name,
|
||
|
fsdata->input_charset, fsdata->local_charset);
|
||
|
free(newname);
|
||
|
if (ret < 0) {
|
||
|
free(name);
|
||
|
return ret;
|
||
|
}
|
||
|
} else {
|
||
|
free(name);
|
||
|
name = newname;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* convert link destination to needed charset */
|
||
|
if (strcmp(fsdata->input_charset, fsdata->local_charset) && linkdest) {
|
||
|
/* we need to convert name charset */
|
||
|
char *newlinkdest = NULL;
|
||
|
ret = strconv(linkdest, fsdata->input_charset,
|
||
|
fsdata->local_charset, &newlinkdest);
|
||
|
if (ret < 0) {
|
||
|
ret = iso_msg_submit(fsdata->msgid, ISO_FILENAME_WRONG_CHARSET,
|
||
|
ret, "Charset conversion error. Can't "
|
||
|
"convert %s from %s to %s", name,
|
||
|
fsdata->input_charset, fsdata->local_charset);
|
||
|
free(newlinkdest);
|
||
|
if (ret < 0) {
|
||
|
free(name);
|
||
|
return ret;
|
||
|
}
|
||
|
} else {
|
||
|
free(linkdest);
|
||
|
linkdest = newlinkdest;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
} else {
|
||
|
/* RR extensions are not read / used */
|
||
|
atts.st_gid = fsdata->gid;
|
||
|
atts.st_uid = fsdata->uid;
|
||
|
if (record->flags[0] & 0x02) {
|
||
|
atts.st_mode = S_IFDIR | fsdata->dir_mode;
|
||
|
} else {
|
||
|
atts.st_mode = S_IFREG | fsdata->file_mode;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* if we haven't RR extensions, or no NM entry is present,
|
||
|