genode/repos/os/src/server/iso9660/iso9660.cc

476 lines
10 KiB
C++
Raw Normal View History

2011-12-22 16:19:25 +01:00
/*
* \brief ISO 9660 file system support
* \author Sebastian Sumpf <sebastian.sumpf@genode-labs.com>
* \date 2010-07-26
*/
/*
* Copyright (C) 2010-2017 Genode Labs GmbH
2011-12-22 16:19:25 +01:00
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
2011-12-22 16:19:25 +01:00
*/
/* Genode includes */
#include <base/allocator_avl.h>
#include <base/exception.h>
#include <base/log.h>
2011-12-22 16:19:25 +01:00
#include <base/stdint.h>
#include <util/misc_math.h>
#include <util/token.h>
#include "iso9660.h"
using namespace Genode;
namespace Iso {
class Sector;
class Rock_ridge;
class Iso_base;
}
2011-12-22 16:19:25 +01:00
/*
* Sector reads one or more packets from the block interface
*/
class Iso::Sector {
public:
2011-12-22 16:19:25 +01:00
enum {
MAX_SECTORS = 32, /* max. number sectors that can be read in one
transaction */
BLOCK_SIZE = 2048,
};
2011-12-22 16:19:25 +01:00
private:
2011-12-22 16:19:25 +01:00
Block::Session::Tx::Source &_source;
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
Block::Packet_descriptor _p { };
2011-12-22 16:19:25 +01:00
public:
2011-12-22 16:19:25 +01:00
Sector(Block::Connection<> &block,
unsigned long blk_nr, unsigned long count)
: _source(*block.tx())
{
try {
_p = Block::Packet_descriptor(
block.alloc_packet(blk_size() * count),
Block::Packet_descriptor::READ,
blk_nr * ((float)blk_size() / BLOCK_SIZE),
count * ((float)blk_size() / BLOCK_SIZE));
2011-12-22 16:19:25 +01:00
_source.submit_packet(_p);
_p = _source.get_acked_packet();
2011-12-22 16:19:25 +01:00
if (!_p.succeeded()) {
Genode::error("Could not read block ", blk_nr);
2011-12-22 16:19:25 +01:00
throw Io_error();
}
} catch (Block::Session::Tx::Source::Packet_alloc_failed) {
Genode::error("packet overrun!");
_p = _source.get_acked_packet();
throw Io_error();
2011-12-22 16:19:25 +01:00
}
}
2011-12-22 16:19:25 +01:00
~Sector() {
_source.release_packet(_p);
}
2011-12-22 16:19:25 +01:00
/**
* Return address of packet content
*/
template <typename T>
T addr() { return reinterpret_cast<T>(_source.packet_content(_p)); }
2011-12-22 16:19:25 +01:00
static size_t blk_size() { return BLOCK_SIZE; }
2011-12-22 16:19:25 +01:00
static unsigned long to_blk(unsigned long bytes) {
return ((bytes + blk_size() - 1) & ~(blk_size() - 1)) / blk_size(); }
};
2011-12-22 16:19:25 +01:00
/**
* Rock ridge extension (see IEEE P1282)
*/
class Iso::Rock_ridge
{
public:
2011-12-22 16:19:25 +01:00
enum {
NM = 0x4d4e, /* POSIX name system use entry (little endian) */
};
2011-12-22 16:19:25 +01:00
private:
2011-12-22 16:19:25 +01:00
uint16_t _signature;
uint8_t _length;
uint8_t _version;
uint8_t _flags;
char _name;
2011-12-22 16:19:25 +01:00
public:
2011-12-22 16:19:25 +01:00
char *name() { return &_name; }
uint8_t length() { return _length - 5; }
2011-12-22 16:19:25 +01:00
static Rock_ridge* scan_name(uint8_t *ptr, uint8_t size)
{
Rock_ridge *rr = (Rock_ridge *)ptr;
2011-12-22 16:19:25 +01:00
while (rr->_length && ((uint8_t *)rr < ptr + size - 4)) {
if (rr->_signature == NM)
return rr;
rr = rr->next();
}
2011-12-22 16:19:25 +01:00
return 0;
}
2011-12-22 16:19:25 +01:00
Rock_ridge *next() { return (Rock_ridge *)((uint8_t *)this + _length); }
};
2011-12-22 16:19:25 +01:00
/*********************
** ISO9660 classes **
*********************/
2011-12-22 16:19:25 +01:00
/**
* Access memory using offsets
*/
class Iso::Iso_base
{
protected:
2011-12-22 16:19:25 +01:00
template <typename T>
T value(int offset) { return *((T *)(this + offset)); }
2011-12-22 16:19:25 +01:00
template <typename T>
T ptr(int offset) { return (T)(this + offset); }
};
2011-12-22 16:19:25 +01:00
/**
* Class representing a directory descriptions (see ECMA 119)
*/
class Directory_record : public Iso::Iso_base
{
enum {
TABLE_LENGTH = 33, /* fixed length of directory record */
2011-12-22 16:19:25 +01:00
ROOT_DIR = 0x0, /* special names of root and parent directories */
PARENT_DIR = 0x1,
2011-12-22 16:19:25 +01:00
DIR_FLAG = 0x2, /* directory flag in attributes */
};
2011-12-22 16:19:25 +01:00
public:
2011-12-22 16:19:25 +01:00
/* total length of this record */
uint8_t record_length() { return value<uint8_t>(0); }
2011-12-22 16:19:25 +01:00
/* starting block of extent */
uint32_t blk_nr() { return value<uint32_t>(2); }
2011-12-22 16:19:25 +01:00
/* length in bytes of extent */
uint32_t data_length() { return value<uint32_t>(10); }
2011-12-22 16:19:25 +01:00
/* attributes */
uint8_t file_flags() { return value<uint8_t>(25); }
2011-12-22 16:19:25 +01:00
/* length of file name */
uint8_t file_name_length() { return value<uint8_t>(32); }
2011-12-22 16:19:25 +01:00
/* retrieve the file name */
void file_name(char *buf)
{
buf[0] = 0;
2011-12-22 16:19:25 +01:00
/* try Rock Ridge name */
Iso::Rock_ridge *rr = Iso::Rock_ridge::scan_name(system_use(),
system_use_size());
2011-12-22 16:19:25 +01:00
if (rr) {
memcpy(buf, rr->name(), rr->length());
buf[rr->length()] = 0;
return;
}
2011-12-22 16:19:25 +01:00
/* retrieve iso name */
char *name = ptr<char*>(33);
2011-12-22 16:19:25 +01:00
/*
* Check for root and parent directory names and modify them
* to '.' and '..' respectively.
*/
if (file_name_length() == 1)
2011-12-22 16:19:25 +01:00
switch (name[0]) {
2011-12-22 16:19:25 +01:00
case PARENT_DIR:
2011-12-22 16:19:25 +01:00
buf[2] = 0;
buf[1] = '.';
buf[0] = '.';
return;
2011-12-22 16:19:25 +01:00
case ROOT_DIR:
2011-12-22 16:19:25 +01:00
buf[1] = 0;
buf[0] = '.';
return;
}
2011-12-22 16:19:25 +01:00
memcpy(buf, name, file_name_length());
buf[file_name_length()] = 0;
}
2011-12-22 16:19:25 +01:00
/* pad byte after file name (if file name length is even, only) */
uint8_t pad_byte() { return !(file_name_length() % 2) ? 1 : 0; }
2011-12-22 16:19:25 +01:00
/* system use area */
uint8_t *system_use()
{
return (uint8_t*)this + TABLE_LENGTH
+ file_name_length() + pad_byte();
}
2011-12-22 16:19:25 +01:00
/* length in bytes of system use area */
uint8_t system_use_size()
{
return record_length() - file_name_length()
- TABLE_LENGTH - pad_byte();
}
2011-12-22 16:19:25 +01:00
/* retrieve next record */
Directory_record *next()
{
Directory_record *_next = this + record_length();
2011-12-22 16:19:25 +01:00
if (_next->record_length())
return _next;
2011-12-22 16:19:25 +01:00
return 0;
}
2011-12-22 16:19:25 +01:00
/* find a directory record with file name matching 'level' */
Directory_record *locate(char const *level)
{
Directory_record *dir = this;
2011-12-22 16:19:25 +01:00
while (dir) {
2011-12-22 16:19:25 +01:00
char name[Iso::LEVEL_LENGTH];
dir->file_name(name);
2011-12-22 16:19:25 +01:00
if (!strcmp(name, level))
return dir;
2011-12-22 16:19:25 +01:00
dir = dir->next();
}
2011-12-22 16:19:25 +01:00
return 0;
}
2011-12-22 16:19:25 +01:00
/* describes this record a directory */
bool directory() { return file_flags() & DIR_FLAG; }
};
2011-12-22 16:19:25 +01:00
/**
* Volume descriptor (see ECMA 119)
*/
class Volume_descriptor : public Iso::Iso_base
{
enum {
/* volume types */
PRIMARY = 0x01, /* type of primary volume descriptor */
TERMINATOR = 0xff, /* type of terminating descriptor */
/* constants */
ROOT_SIZE = 34, /* the root directory record has a fixed length */
};
2011-12-22 16:19:25 +01:00
public:
2011-12-22 16:19:25 +01:00
/* descriptor type */
uint8_t type() { return value<uint8_t>(0); }
2011-12-22 16:19:25 +01:00
/* root directory record */
Directory_record * root_record() { return ptr<Directory_record *>(156); }
2011-12-22 16:19:25 +01:00
/* check for primary descriptor */
bool primary() { return type() == PRIMARY; }
2011-12-22 16:19:25 +01:00
/* check for terminating descriptor */
bool terminator() { return type() == TERMINATOR; }
2011-12-22 16:19:25 +01:00
/* copy the root record */
Directory_record *copy_root_record(Genode::Allocator &alloc)
{
Directory_record *buf;
2011-12-22 16:19:25 +01:00
if (!(alloc.alloc(ROOT_SIZE, &buf)))
throw Insufficient_ram_quota();
2011-12-22 16:19:25 +01:00
memcpy(buf, root_record(), ROOT_SIZE);
2011-12-22 16:19:25 +01:00
return buf;
2011-12-22 16:19:25 +01:00
}
};
2011-12-22 16:19:25 +01:00
/**
* Locate the root-directory record in the primary volume descriptor
*/
static Directory_record *locate_root(Genode::Allocator &alloc,
Block::Connection<> &block)
{
/* volume descriptors in ISO9660 start at block 16 */
for (unsigned long blk_nr = 16;; blk_nr++) {
Iso::Sector sec(block, blk_nr, 1);
Volume_descriptor *vol = sec.addr<Volume_descriptor *>();
if (vol->primary())
return vol->copy_root_record(alloc);
if (vol->terminator())
return nullptr;
2011-12-22 16:19:25 +01:00
}
}
2011-12-22 16:19:25 +01:00
/**
* Return root directory record
*/
static Directory_record *root_dir(Genode::Allocator &alloc,
Block::Connection<> &block)
{
Directory_record *root = locate_root(alloc, block);
2011-12-22 16:19:25 +01:00
if (!root) { throw Iso::Non_data_disc(); }
2011-12-22 16:19:25 +01:00
return root;
}
2011-12-22 16:19:25 +01:00
/*******************
** Iso interface **
*******************/
2011-12-22 16:19:25 +01:00
static Directory_record *_root_dir;
2011-12-22 16:19:25 +01:00
Iso::File_info *Iso::file_info(Genode::Allocator &alloc,
Block::Connection<> &block, char const *path)
{
char level[PATH_LENGTH];
2011-12-22 16:19:25 +01:00
struct Scanner_policy_file
{
static bool identifier_char(char c, unsigned /* i */)
{
return c != '/' && c != 0;
}
};
typedef ::Genode::Token<Scanner_policy_file> Token;
Token t(path);
2011-12-22 16:19:25 +01:00
if (!_root_dir) {
_root_dir = root_dir(alloc, block);
}
2011-12-22 16:19:25 +01:00
Directory_record *dir = _root_dir;
uint32_t blk_nr = 0, data_length = 0;
2011-12-22 16:19:25 +01:00
/* determine block nr and file length on disk, parse directory records */
while (t) {
2011-12-22 16:19:25 +01:00
if (t.type() != Token::IDENT) {
t = t.next();
continue;
}
2011-12-22 16:19:25 +01:00
t.string(level, PATH_LENGTH);
/*
* Save current block number in a variable because successive
* iterations might override the memory location where dir points
* to when a directory entry spans several sectors.
*/
uint32_t current_blk_nr = dir->blk_nr();
2011-12-22 16:19:25 +01:00
/* load extent of directory record and search for level */
for (unsigned long i = 0; i < Sector::to_blk(dir->data_length()); i++) {
Sector sec(block, current_blk_nr + i, 1);
Directory_record *tmp = sec.addr<Directory_record *>()->locate(level);
2011-12-22 16:19:25 +01:00
if (!tmp && i == Sector::to_blk(dir->data_length()) - 1) {
Genode::error("file not found: ", Genode::Cstring(path));
throw File_not_found();
}
2011-12-22 16:19:25 +01:00
if (!tmp) continue;
2011-12-22 16:19:25 +01:00
dir = tmp;
current_blk_nr = dir->blk_nr();
2011-12-22 16:19:25 +01:00
if (!dir->directory()) {
blk_nr = current_blk_nr;
data_length = dir->data_length();
2011-12-22 16:19:25 +01:00
}
break;
2011-12-22 16:19:25 +01:00
}
t = t.next();
}
2011-12-22 16:19:25 +01:00
/* Warning: Don't access 'dir' after this point, since the sector is gone */
2011-12-22 16:19:25 +01:00
if (!blk_nr && !data_length) {
Genode::error("file not found: ", Genode::Cstring(path));
throw File_not_found();
2011-12-22 16:19:25 +01:00
}
return new (alloc) File_info(blk_nr, data_length);
}
2011-12-22 16:19:25 +01:00
unsigned long Iso::read_file(Block::Connection<> &block, File_info *info,
off_t file_offset, uint32_t length, void *buf_ptr)
{
uint8_t *buf = (uint8_t *)buf_ptr;
if (info->size() <= (size_t)(length + file_offset))
length = info->size() - file_offset - 1;
2011-12-22 16:19:25 +01:00
unsigned long total_blk_count = ((length + (Sector::blk_size() - 1)) &
~((Sector::blk_size()) - 1)) / Sector::blk_size();
unsigned long ret = total_blk_count;
unsigned long blk_count;
unsigned long blk_nr = info->blk_nr() + (file_offset / Sector::blk_size());
while ((blk_count = min<unsigned long>(Sector::MAX_SECTORS, total_blk_count))) {
Sector sec(block, blk_nr, blk_count);
2011-12-22 16:19:25 +01:00
total_blk_count -= blk_count;
blk_nr += blk_count;
2011-12-22 16:19:25 +01:00
unsigned long copy_length = blk_count * Sector::blk_size();
memcpy(buf, sec.addr<void *>(), copy_length);
length -= copy_length;
buf += copy_length;
/* zero out rest of page */
if (!total_blk_count && (blk_count % 2))
memset(buf, 0, Sector::blk_size());
2011-12-22 16:19:25 +01:00
}
return ret * Sector::blk_size();
}