filesystemext.cpp revision 54d2d2606d7c83a456819cd038a73e0f9a600ca4
/* $Id$ */
/** @file
* IPRT Filesystem API (FileSys) - ext2/3 format.
*/
/*
* Copyright (C) 2012 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DEFAULT
#include <iprt/filesystem.h>
#include "internal/filesystem.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/*
* The filesystem structures are from http://wiki.osdev.org/Ext2 and
*/
/**
* Ext superblock.
*
* Everything is stored little endian on the disk.
*/
#pragma pack(1)
typedef struct ExtSuperBlock
{
/** Total number of inodes in the filesystem. */
/** Total number of blocks in the filesystem. */
/** Number of blocks reserved for the super user. */
/** Total number of unallocated blocks. */
/** Total number of unallocated inodes. */
/** Block number of block containing the superblock. */
/** Number of bits to shift 1024 to the left to get the block size */
/** Number of bits to shift 1024 to the left to get the fragment size */
/** Number of blocks in each block group. */
/** Number of fragments in each block group. */
/** Number of inodes in each block group. */
/** Last mount time. */
/** Last written time. */
/** Number of times the volume was mounted since the last check. */
/** Number of mounts allowed before a consistency check. */
/** Signature to identify a ext2 volume. */
/** What to do on an error. */
/** Minor version field. */
/** Time of last check. */
/** Interval between consistency checks. */
/** Operating system ID of the filesystem creator. */
/** Major version field. */
/** User ID that is allowed to use reserved blocks. */
/** Group ID that is allowed to use reserved blocks. */
/** Reserved fields. */
#pragma pack()
/** Pointer to an ext super block. */
typedef ExtSuperBlock *PExtSuperBlock;
/** Ext2 signature. */
#define RTFILESYSTEM_EXT2_SIGNATURE (0xef53)
/** Clean filesystem state. */
#define RTFILESYSTEM_EXT2_STATE_CLEAN (0x0001)
/** Error filesystem state. */
#define RTFILESYSTEM_EXT2_STATE_ERRORS (0x0002)
/**
* Block group descriptor.
*/
#pragma pack(1)
typedef struct BlockGroupDesc
{
/** Block address of the block bitmap. */
/** Block address of the inode bitmap. */
/** Start block address of the inode table. */
/** Number of unallocated blocks in group. */
/** Number of unallocated inodes in group. */
/** Number of directories in the group. */
/** Padding. */
/** Reserved. */
#pragma pack()
/** Pointer to an ext block group descriptor. */
typedef BlockGroupDesc *PBlockGroupDesc;
/**
* Cached block group descriptor data.
*/
typedef struct RTFILESYSTEMEXTBLKGRP
{
/** Start offset (in bytes and from the start of the disk). */
/** Last offset in the block group (inclusive). */
/** Block bitmap - variable in size (depends on the block size
* and number of blocks per group). */
/** Pointer to block group descriptor data. */
typedef RTFILESYSTEMEXTBLKGRP *PRTFILESYSTEMEXTBLKGRP;
/**
* Ext2/3 filesystem data.
*/
typedef struct RTFILESYSTEMEXT
{
/** VFS file handle. */
/** Block number of the superblock. */
/** Size of one block. */
/** Number of blocks in one group. */
unsigned cBlocksPerGroup;
/** Number of blocks groups in the volume. */
unsigned cBlockGroups;
/** Cached block group descriptor data. */
/** Pointer to the ext filesystem data. */
typedef RTFILESYSTEMEXT *PRTFILESYSTEMEXT;
/*******************************************************************************
* Methods *
*******************************************************************************/
/**
* Loads the block descriptor of the given block group from the medium.
*
* @returns IPRT status code.
* @param pThis EXT filesystem instance data.
* @param iBlkGrp Block group number to load.
*/
{
int rc = VINF_SUCCESS;
if (!pBlkGrpDesc)
{
if (!pBlkGrpDesc)
return VERR_NO_MEMORY;
}
if (RT_SUCCESS(rc))
{
pBlkGrpDesc->offStart = pThis->iSbBlock + (uint64_t)iBlkGrp * pThis->cBlocksPerGroup * pThis->cbBlock;
}
return rc;
}
{
bool fUsed = false;
while (cBlocks)
{
{
fUsed = true;
break;
}
cBlocks--;
}
return fUsed;
}
{
int rc = VINF_SUCCESS;
if (RT_SUCCESS(rc))
{
{
if (RT_SUCCESS(rc))
{
#if defined(RT_BIGENDIAN)
/** @todo: Convert to host endianess. */
#endif
}
}
}
return rc;
}
{
int rc = VINF_SUCCESS;
if (RT_SUCCESS(rc))
{
#if defined(RT_BIGENDIAN)
/** @todo: Convert to host endianess. */
#endif
else
{
/* Load first block group descriptor. */
}
}
return rc;
}
{
if (pThis->pBlkGrpDesc)
}
{
return VERR_NOT_IMPLEMENTED;
}
bool *pfUsed)
{
int rc = VINF_SUCCESS;
*pfUsed = false;
while (cb > 0)
{
bool fUsed;
{
/* Load new block descriptor. */
if (RT_FAILURE(rc))
break;
}
if (fUsed)
{
*pfUsed = true;
break;
}
}
return rc;
}
{
/** cbFs */
sizeof(RTFILESYSTEMEXT),
/** VfsOps */
{
/** uVersion. */
/** fFeatures */
0,
/** pszName */
"ExtVfsOps",
/** pfnDestroy */
/** pfnOpenRoot */
/** pfnIsRangeInUse */
/** uEndMarker */
},
/** pfnProbe */
/** pfnInit */
};