mirror of
https://github.com/wiidev/usbloadergx.git
synced 2024-11-25 12:46:53 +01:00
*Mem2 fix
*updated libntfs (write fix) *updated libfat *lots of changes in the startup code, removed almost everything. This might cause problems for some drives at loading the gamelist and needs to be adjusted later but better this time. more cleanup is needed in main.cpp and will come. *using libogc sd/usb for config loading and reload to cIOS afterwards *added missing boothomebrew stuff pune forgot NOTE: From now on we will be doing a lot of revs which we won't be compiling and releasing. This revs are officially not available for public so don't making issues regarding those revs. Those will be closed right away. We need first to cleanup a lot of crap and update loader to new standards before releasing stuff again.
This commit is contained in:
parent
ac26935769
commit
64f8406b07
@ -2,8 +2,8 @@
|
||||
<app version="1">
|
||||
<name> USB Loader GX</name>
|
||||
<coder>USB Loader GX Team</coder>
|
||||
<version>1.0 r943</version>
|
||||
<release_date>201009161155</release_date>
|
||||
<version>1.0 r944</version>
|
||||
<release_date>201009161846</release_date>
|
||||
<short_description>Loads games from USB-devices</short_description>
|
||||
<long_description>USB Loader GX is a libwiigui based USB iso loader with a wii-like GUI. You can install games to your HDDs and boot them with shorter loading times.
|
||||
The interactive GUI is completely controllable with WiiMote, Classic Controller or GC Controller.
|
||||
|
BIN
data/app_booter.dol
Normal file
BIN
data/app_booter.dol
Normal file
Binary file not shown.
After Width: | Height: | Size: 98 KiB |
File diff suppressed because one or more lines are too long
@ -1,151 +0,0 @@
|
||||
#include <gccore.h>
|
||||
#include <ogcsys.h>
|
||||
#include <malloc.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <ogc/machine/processor.h>
|
||||
#include <wiiuse/wpad.h>
|
||||
#include "../lstub.h"
|
||||
|
||||
#include "fatmounter.h"
|
||||
#include "dolloader.h"
|
||||
#include "elfloader.h"
|
||||
|
||||
void *innetbuffer = NULL;
|
||||
|
||||
int AllocHomebrewMemory(u32 filesize) {
|
||||
|
||||
innetbuffer = malloc(filesize);
|
||||
|
||||
if (!innetbuffer)
|
||||
return -1;
|
||||
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
void FreeHomebrewBuffer() {
|
||||
free(innetbuffer);
|
||||
innetbuffer = NULL;
|
||||
}
|
||||
|
||||
void CopyHomebrewMemory(u32 read, u8 *temp, u32 len) {
|
||||
|
||||
memcpy(((u8 *) innetbuffer)+read, temp, len);
|
||||
|
||||
}
|
||||
|
||||
int BootHomebrew(char * path) {
|
||||
loadStub();
|
||||
if (Set_Stub_Split(0x00010001,"UNEO")<0)
|
||||
Set_Stub_Split(0x00010001,"ULNR");
|
||||
void *buffer = NULL;
|
||||
u32 filesize = 0;
|
||||
entrypoint entry;
|
||||
u32 cpu_isr;
|
||||
|
||||
FILE * file = fopen(path, "rb");
|
||||
if (!file) SYS_ResetSystem(SYS_RETURNTOMENU, 0, 0);
|
||||
|
||||
fseek (file, 0, SEEK_END);
|
||||
filesize = ftell(file);
|
||||
rewind(file);
|
||||
|
||||
buffer = malloc(filesize);
|
||||
|
||||
if (fread (buffer, 1, filesize, file) != filesize) {
|
||||
fclose (file);
|
||||
free(buffer);
|
||||
SDCard_deInit();
|
||||
USBDevice_deInit();
|
||||
SYS_ResetSystem(SYS_RETURNTOMENU, 0, 0);
|
||||
}
|
||||
fclose (file);
|
||||
|
||||
struct __argv args;
|
||||
bzero(&args, sizeof(args));
|
||||
args.argvMagic = ARGV_MAGIC;
|
||||
args.length = strlen(path) + 2;
|
||||
args.commandLine = (char*)malloc(args.length);
|
||||
if (!args.commandLine) SYS_ResetSystem(SYS_RETURNTOMENU, 0, 0);
|
||||
strcpy(args.commandLine, path);
|
||||
args.commandLine[args.length - 1] = '\0';
|
||||
args.argc = 1;
|
||||
args.argv = &args.commandLine;
|
||||
args.endARGV = args.argv + 1;
|
||||
|
||||
int ret = valid_elf_image(buffer);
|
||||
if (ret == 1)
|
||||
entry = (entrypoint) load_elf_image(buffer);
|
||||
else
|
||||
entry = (entrypoint) load_dol(buffer, &args);
|
||||
|
||||
free(buffer);
|
||||
|
||||
if (!entry) {
|
||||
SDCard_deInit();
|
||||
USBDevice_deInit();
|
||||
SYS_ResetSystem(SYS_RETURNTOMENU, 0, 0);
|
||||
}
|
||||
|
||||
SDCard_deInit();
|
||||
USBDevice_deInit();
|
||||
|
||||
WPAD_Flush(0);
|
||||
WPAD_Disconnect(0);
|
||||
WPAD_Shutdown();
|
||||
|
||||
SYS_ResetSystem(SYS_SHUTDOWN, 0, 0);
|
||||
_CPU_ISR_Disable (cpu_isr);
|
||||
__exception_closeall();
|
||||
entry();
|
||||
_CPU_ISR_Restore (cpu_isr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int BootHomebrewFromMem() {
|
||||
loadStub();
|
||||
if (Set_Stub_Split(0x00010001,"UNEO")<0)
|
||||
Set_Stub_Split(0x00010001,"ULNR");
|
||||
entrypoint entry;
|
||||
u32 cpu_isr;
|
||||
|
||||
if (!innetbuffer) {
|
||||
SDCard_deInit();
|
||||
USBDevice_deInit();
|
||||
SYS_ResetSystem(SYS_RETURNTOMENU, 0, 0);
|
||||
}
|
||||
|
||||
struct __argv args;
|
||||
|
||||
int ret = valid_elf_image(innetbuffer);
|
||||
if (ret == 1)
|
||||
entry = (entrypoint) load_elf_image(innetbuffer);
|
||||
else
|
||||
entry = (entrypoint) load_dol(innetbuffer, &args);
|
||||
|
||||
free(innetbuffer);
|
||||
|
||||
if (!entry) {
|
||||
SDCard_deInit();
|
||||
USBDevice_deInit();
|
||||
SYS_ResetSystem(SYS_RETURNTOMENU, 0, 0);
|
||||
}
|
||||
|
||||
SDCard_deInit();
|
||||
USBDevice_deInit();
|
||||
|
||||
WPAD_Flush(0);
|
||||
WPAD_Disconnect(0);
|
||||
WPAD_Shutdown();
|
||||
|
||||
SYS_ResetSystem(SYS_SHUTDOWN, 0, 0);
|
||||
_CPU_ISR_Disable (cpu_isr);
|
||||
__exception_closeall();
|
||||
entry();
|
||||
_CPU_ISR_Restore (cpu_isr);
|
||||
|
||||
return 0;
|
||||
}
|
131
source/homebrewboot/BootHomebrew.cpp
Normal file
131
source/homebrewboot/BootHomebrew.cpp
Normal file
@ -0,0 +1,131 @@
|
||||
#include <gccore.h>
|
||||
#include <ogcsys.h>
|
||||
#include <malloc.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ogc/machine/processor.h>
|
||||
#include <wiiuse/wpad.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include "usbloader/usbstorage2.h"
|
||||
#include "dolloader.h"
|
||||
#include "fatmounter.h"
|
||||
#include "sys.h"
|
||||
|
||||
extern const u8 app_booter_dol[];
|
||||
extern const u32 app_booter_dol_size;
|
||||
|
||||
static u8 *homebrewbuffer = (u8 *) 0x92000000;
|
||||
static int homebrewsize = 0;
|
||||
static std::vector<std::string> Arguments;
|
||||
|
||||
void AddBootArgument(const char * argv)
|
||||
{
|
||||
std::string arg(argv);
|
||||
Arguments.push_back(arg);
|
||||
}
|
||||
|
||||
int CopyHomebrewMemory(u8 *temp, u32 pos, u32 len)
|
||||
{
|
||||
homebrewsize += len;
|
||||
memcpy((homebrewbuffer)+pos, temp, len);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void FreeHomebrewBuffer()
|
||||
{
|
||||
homebrewbuffer = (u8 *)0x92000000;
|
||||
homebrewsize = 0;
|
||||
}
|
||||
|
||||
static int SetupARGV(struct __argv * args)
|
||||
{
|
||||
if(!args)
|
||||
return -1;
|
||||
|
||||
bzero(args, sizeof(struct __argv));
|
||||
args->argvMagic = ARGV_MAGIC;
|
||||
|
||||
u32 stringlength = 1;
|
||||
|
||||
/** Append Arguments **/
|
||||
for(u32 i = 0; i < Arguments.size(); i++)
|
||||
{
|
||||
stringlength += Arguments[i].size()+1;
|
||||
}
|
||||
|
||||
args->length = stringlength;
|
||||
args->commandLine = (char*) malloc(args->length);
|
||||
if (!args->commandLine)
|
||||
return -1;
|
||||
|
||||
u32 argc = 0;
|
||||
u32 position = 0;
|
||||
|
||||
/** Append Arguments **/
|
||||
for(u32 i = 0; i < Arguments.size(); i++)
|
||||
{
|
||||
strcpy(&args->commandLine[position], Arguments[i].c_str());
|
||||
position += Arguments[i].size()+1;
|
||||
argc++;
|
||||
}
|
||||
|
||||
args->argc = argc;
|
||||
|
||||
args->commandLine[args->length - 1] = '\0';
|
||||
args->argv = &args->commandLine;
|
||||
args->endARGV = args->argv + 1;
|
||||
|
||||
Arguments.clear();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int BootHomebrew()
|
||||
{
|
||||
if(homebrewsize <= 0)
|
||||
Sys_BackToLoader();
|
||||
|
||||
SDCard_deInit();
|
||||
USBDevice_deInit();
|
||||
USBStorage2_Deinit();
|
||||
|
||||
struct __argv args;
|
||||
SetupARGV(&args);
|
||||
|
||||
u32 cpu_isr;
|
||||
|
||||
entrypoint entry = (entrypoint) load_dol(app_booter_dol, &args);
|
||||
|
||||
if (!entry)
|
||||
Sys_BackToLoader();
|
||||
|
||||
SYS_ResetSystem(SYS_SHUTDOWN, 0, 0);
|
||||
_CPU_ISR_Disable (cpu_isr);
|
||||
__exception_closeall();
|
||||
entry();
|
||||
_CPU_ISR_Restore (cpu_isr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int BootHomebrew(const char * filepath)
|
||||
{
|
||||
FILE * file = fopen(filepath, "rb");
|
||||
if(!file)
|
||||
Sys_BackToLoader();
|
||||
|
||||
fseek(file, 0, SEEK_END);
|
||||
|
||||
int size = ftell(file);
|
||||
rewind(file);
|
||||
|
||||
homebrewsize = fread(homebrewbuffer, 1, size, file);
|
||||
fclose(file);
|
||||
|
||||
AddBootArgument(filepath);
|
||||
|
||||
return BootHomebrew();
|
||||
}
|
@ -1,13 +1,11 @@
|
||||
#ifndef _BOOTHOMEBREW_H_
|
||||
#define _BOOTHOMEBREW_H_
|
||||
|
||||
|
||||
int BootHomebrew(char * path);
|
||||
int BootHomebrewFromMem();
|
||||
void CopyHomebrewMemory(u32 read, u8 *temp, u32 len);
|
||||
int AllocHomebrewMemory(u32 filesize);
|
||||
int BootHomebrew();
|
||||
int BootHomebrew(const char * filepath);
|
||||
int CopyHomebrewMemory(u8 *temp, u32 pos, u32 len);
|
||||
void AddBootArgument(const char * arg);
|
||||
void FreeHomebrewBuffer();
|
||||
void AddBootArgument(const char * argv);
|
||||
extern u32 homebrewsize;
|
||||
int LoadHomebrew(const char * filepath);
|
||||
|
||||
#endif
|
||||
|
@ -41,7 +41,6 @@ extern char wiiloadVersion[2];
|
||||
extern u8 shutdown;
|
||||
extern u8 reset;
|
||||
extern struct SSettings Settings;
|
||||
extern void *innetbuffer;
|
||||
|
||||
/*** Variables used elsewhere ***/
|
||||
u8 boothomebrew = 0;
|
||||
@ -794,14 +793,8 @@ int MenuHomebrewBrowse() {
|
||||
|
||||
int choice = WindowPrompt(filesizetxt, temp, tr("OK"), tr("Cancel"));
|
||||
|
||||
if (choice == 1) {
|
||||
|
||||
int res = AllocHomebrewMemory(infilesize);
|
||||
|
||||
if (res < 0) {
|
||||
CloseConnection();
|
||||
WindowPrompt(tr("Not enough free memory."), 0, tr("OK"));
|
||||
} else {
|
||||
if (choice == 1)
|
||||
{
|
||||
u32 read = 0;
|
||||
u8 *temp = NULL;
|
||||
int len = NETWORKBLOCKSIZE;
|
||||
@ -879,7 +872,7 @@ int MenuHomebrewBrowse() {
|
||||
uLongf f = uncfilesize;
|
||||
error = uncompress(unc, &f, temp, infilesize) != Z_OK;
|
||||
uncfilesize = f;
|
||||
homebrewsize = uncfilesize;
|
||||
infilesize = f;
|
||||
|
||||
free(temp);
|
||||
temp = unc;
|
||||
@ -887,8 +880,8 @@ int MenuHomebrewBrowse() {
|
||||
}
|
||||
|
||||
if (!error && strstr(filename,".zip") == NULL) {
|
||||
innetbuffer = temp;
|
||||
|
||||
CopyHomebrewMemory(temp, 0, infilesize);
|
||||
free(temp);
|
||||
}
|
||||
}
|
||||
|
||||
@ -899,7 +892,8 @@ int MenuHomebrewBrowse() {
|
||||
FreeHomebrewBuffer();
|
||||
} else {
|
||||
if (strstr(filename,".dol") || strstr(filename,".DOL")
|
||||
|| strstr(filename,".elf") || strstr(filename,".ELF")) {
|
||||
|| strstr(filename,".elf") || strstr(filename,".ELF"))
|
||||
{
|
||||
boothomebrew = 2;
|
||||
AddBootArgument( filename );
|
||||
menu = MENU_EXIT;
|
||||
@ -914,7 +908,6 @@ int MenuHomebrewBrowse() {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
CloseConnection();
|
||||
ResumeNetworkWait();
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ typedef struct _dolheader {
|
||||
u32 entry_point;
|
||||
} dolheader;
|
||||
|
||||
u32 load_dol(void *dolstart, struct __argv *argv) {
|
||||
u32 load_dol(const void *dolstart, struct __argv *argv) {
|
||||
u32 i;
|
||||
dolheader *dolfile;
|
||||
|
||||
@ -28,15 +28,13 @@ u32 load_dol(void *dolstart, struct __argv *argv) {
|
||||
dolfile = (dolheader *) dolstart;
|
||||
for (i = 0; i < 7; i++) {
|
||||
if ((!dolfile->text_size[i]) || (dolfile->text_start[i] < 0x100)) continue;
|
||||
VIDEO_WaitVSync();
|
||||
ICInvalidateRange ((void *) dolfile->text_start[i],dolfile->text_size[i]);
|
||||
memmove ((void *) dolfile->text_start[i],dolstart+dolfile->text_pos[i],dolfile->text_size[i]);
|
||||
memcpy((void *) dolfile->text_start[i],dolstart+dolfile->text_pos[i],dolfile->text_size[i]);
|
||||
}
|
||||
|
||||
for (i = 0; i < 11; i++) {
|
||||
if ((!dolfile->data_size[i]) || (dolfile->data_start[i] < 0x100)) continue;
|
||||
VIDEO_WaitVSync();
|
||||
memmove ((void *) dolfile->data_start[i],dolstart+dolfile->data_pos[i],dolfile->data_size[i]);
|
||||
memcpy((void *) dolfile->data_start[i],dolstart+dolfile->data_pos[i],dolfile->data_size[i]);
|
||||
DCFlushRangeNoSync ((void *) dolfile->data_start[i],dolfile->data_size[i]);
|
||||
}
|
||||
|
||||
@ -45,7 +43,7 @@ u32 load_dol(void *dolstart, struct __argv *argv) {
|
||||
|
||||
if (argv && argv->argvMagic == ARGV_MAGIC) {
|
||||
void *new_argv = (void *)(dolfile->entry_point + 8);
|
||||
memmove(new_argv, argv, sizeof(*argv));
|
||||
memcpy(new_argv, argv, sizeof(*argv));
|
||||
DCFlushRange(new_argv, sizeof(*argv));
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,7 @@ extern "C" {
|
||||
extern void __exception_closeall();
|
||||
typedef void (*entrypoint) (void);
|
||||
|
||||
u32 load_dol(void *dolstart, struct __argv *argv);
|
||||
u32 load_dol(const void *dolstart, struct __argv *argv);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -1,590 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 1995, 1996, 2001, 2002
|
||||
* Erik Theisen. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This is the ELF ABI header file
|
||||
* formerly known as "elf_abi.h".
|
||||
*/
|
||||
|
||||
#ifndef _ELF_ABI_H
|
||||
#define _ELF_ABI_H
|
||||
|
||||
#include <gctypes.h>
|
||||
|
||||
/*
|
||||
* This version doesn't work for 64-bit ABIs - Erik.
|
||||
*/
|
||||
|
||||
/*
|
||||
* These typedefs need to be handled better.
|
||||
*/
|
||||
typedef u32 Elf32_Addr; /* Unsigned program address */
|
||||
typedef u32 Elf32_Off; /* Unsigned file offset */
|
||||
typedef s32 Elf32_Sword; /* Signed large integer */
|
||||
typedef u32 Elf32_Word; /* Unsigned large integer */
|
||||
typedef u16 Elf32_Half; /* Unsigned medium integer */
|
||||
|
||||
/* e_ident[] identification indexes */
|
||||
#define EI_MAG0 0 /* file ID */
|
||||
#define EI_MAG1 1 /* file ID */
|
||||
#define EI_MAG2 2 /* file ID */
|
||||
#define EI_MAG3 3 /* file ID */
|
||||
#define EI_CLASS 4 /* file class */
|
||||
#define EI_DATA 5 /* data encoding */
|
||||
#define EI_VERSION 6 /* ELF header version */
|
||||
#define EI_OSABI 7 /* OS/ABI specific ELF extensions */
|
||||
#define EI_ABIVERSION 8 /* ABI target version */
|
||||
#define EI_PAD 9 /* start of pad bytes */
|
||||
#define EI_NIDENT 16 /* Size of e_ident[] */
|
||||
|
||||
/* e_ident[] magic number */
|
||||
#define ELFMAG0 0x7f /* e_ident[EI_MAG0] */
|
||||
#define ELFMAG1 'E' /* e_ident[EI_MAG1] */
|
||||
#define ELFMAG2 'L' /* e_ident[EI_MAG2] */
|
||||
#define ELFMAG3 'F' /* e_ident[EI_MAG3] */
|
||||
#define ELFMAG "\177ELF" /* magic */
|
||||
#define SELFMAG 4 /* size of magic */
|
||||
|
||||
/* e_ident[] file class */
|
||||
#define ELFCLASSNONE 0 /* invalid */
|
||||
#define ELFCLASS32 1 /* 32-bit objs */
|
||||
#define ELFCLASS64 2 /* 64-bit objs */
|
||||
#define ELFCLASSNUM 3 /* number of classes */
|
||||
|
||||
/* e_ident[] data encoding */
|
||||
#define ELFDATANONE 0 /* invalid */
|
||||
#define ELFDATA2LSB 1 /* Little-Endian */
|
||||
#define ELFDATA2MSB 2 /* Big-Endian */
|
||||
#define ELFDATANUM 3 /* number of data encode defines */
|
||||
|
||||
/* e_ident[] OS/ABI specific ELF extensions */
|
||||
#define ELFOSABI_NONE 0 /* No extension specified */
|
||||
#define ELFOSABI_HPUX 1 /* Hewlett-Packard HP-UX */
|
||||
#define ELFOSABI_NETBSD 2 /* NetBSD */
|
||||
#define ELFOSABI_LINUX 3 /* Linux */
|
||||
#define ELFOSABI_SOLARIS 6 /* Sun Solaris */
|
||||
#define ELFOSABI_AIX 7 /* AIX */
|
||||
#define ELFOSABI_IRIX 8 /* IRIX */
|
||||
#define ELFOSABI_FREEBSD 9 /* FreeBSD */
|
||||
#define ELFOSABI_TRU64 10 /* Compaq TRU64 UNIX */
|
||||
#define ELFOSABI_MODESTO 11 /* Novell Modesto */
|
||||
#define ELFOSABI_OPENBSD 12 /* OpenBSD */
|
||||
/* 64-255 Architecture-specific value range */
|
||||
|
||||
/* e_ident[] ABI Version */
|
||||
#define ELFABIVERSION 0
|
||||
|
||||
/* e_ident */
|
||||
#define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \
|
||||
(ehdr).e_ident[EI_MAG1] == ELFMAG1 && \
|
||||
(ehdr).e_ident[EI_MAG2] == ELFMAG2 && \
|
||||
(ehdr).e_ident[EI_MAG3] == ELFMAG3)
|
||||
|
||||
/* ELF Header */
|
||||
typedef struct elfhdr {
|
||||
unsigned char e_ident[EI_NIDENT]; /* ELF Identification */
|
||||
Elf32_Half e_type; /* object file type */
|
||||
Elf32_Half e_machine; /* machine */
|
||||
Elf32_Word e_version; /* object file version */
|
||||
Elf32_Addr e_entry; /* virtual entry point */
|
||||
Elf32_Off e_phoff; /* program header table offset */
|
||||
Elf32_Off e_shoff; /* section header table offset */
|
||||
Elf32_Word e_flags; /* processor-specific flags */
|
||||
Elf32_Half e_ehsize; /* ELF header size */
|
||||
Elf32_Half e_phentsize; /* program header entry size */
|
||||
Elf32_Half e_phnum; /* number of program header entries */
|
||||
Elf32_Half e_shentsize; /* section header entry size */
|
||||
Elf32_Half e_shnum; /* number of section header entries */
|
||||
Elf32_Half e_shstrndx; /* section header table's "section
|
||||
header string table" entry offset */
|
||||
} Elf32_Ehdr;
|
||||
|
||||
/* e_type */
|
||||
#define ET_NONE 0 /* No file type */
|
||||
#define ET_REL 1 /* relocatable file */
|
||||
#define ET_EXEC 2 /* executable file */
|
||||
#define ET_DYN 3 /* shared object file */
|
||||
#define ET_CORE 4 /* core file */
|
||||
#define ET_NUM 5 /* number of types */
|
||||
#define ET_LOOS 0xfe00 /* reserved range for operating */
|
||||
#define ET_HIOS 0xfeff /* system specific e_type */
|
||||
#define ET_LOPROC 0xff00 /* reserved range for processor */
|
||||
#define ET_HIPROC 0xffff /* specific e_type */
|
||||
|
||||
/* e_machine */
|
||||
#define EM_NONE 0 /* No Machine */
|
||||
#define EM_M32 1 /* AT&T WE 32100 */
|
||||
#define EM_SPARC 2 /* SPARC */
|
||||
#define EM_386 3 /* Intel 80386 */
|
||||
#define EM_68K 4 /* Motorola 68000 */
|
||||
#define EM_88K 5 /* Motorola 88000 */
|
||||
#if 0
|
||||
#define EM_486 6 /* RESERVED - was Intel 80486 */
|
||||
#endif
|
||||
#define EM_860 7 /* Intel 80860 */
|
||||
#define EM_MIPS 8 /* MIPS R3000 Big-Endian only */
|
||||
#define EM_S370 9 /* IBM System/370 Processor */
|
||||
#define EM_MIPS_RS4_BE 10 /* MIPS R4000 Big-Endian */
|
||||
#if 0
|
||||
#define EM_SPARC64 11 /* RESERVED - was SPARC v9
|
||||
64-bit unoffical */
|
||||
#endif
|
||||
/* RESERVED 11-14 for future use */
|
||||
#define EM_PARISC 15 /* HPPA */
|
||||
/* RESERVED 16 for future use */
|
||||
#define EM_VPP500 17 /* Fujitsu VPP500 */
|
||||
#define EM_SPARC32PLUS 18 /* Enhanced instruction set SPARC */
|
||||
#define EM_960 19 /* Intel 80960 */
|
||||
#define EM_PPC 20 /* PowerPC */
|
||||
#define EM_PPC64 21 /* 64-bit PowerPC */
|
||||
#define EM_S390 22 /* IBM System/390 Processor */
|
||||
/* RESERVED 23-35 for future use */
|
||||
#define EM_V800 36 /* NEC V800 */
|
||||
#define EM_FR20 37 /* Fujitsu FR20 */
|
||||
#define EM_RH32 38 /* TRW RH-32 */
|
||||
#define EM_RCE 39 /* Motorola RCE */
|
||||
#define EM_ARM 40 /* Advanced Risc Machines ARM */
|
||||
#define EM_ALPHA 41 /* Digital Alpha */
|
||||
#define EM_SH 42 /* Hitachi SH */
|
||||
#define EM_SPARCV9 43 /* SPARC Version 9 */
|
||||
#define EM_TRICORE 44 /* Siemens TriCore embedded processor */
|
||||
#define EM_ARC 45 /* Argonaut RISC Core */
|
||||
#define EM_H8_300 46 /* Hitachi H8/300 */
|
||||
#define EM_H8_300H 47 /* Hitachi H8/300H */
|
||||
#define EM_H8S 48 /* Hitachi H8S */
|
||||
#define EM_H8_500 49 /* Hitachi H8/500 */
|
||||
#define EM_IA_64 50 /* Intel Merced */
|
||||
#define EM_MIPS_X 51 /* Stanford MIPS-X */
|
||||
#define EM_COLDFIRE 52 /* Motorola Coldfire */
|
||||
#define EM_68HC12 53 /* Motorola M68HC12 */
|
||||
#define EM_MMA 54 /* Fujitsu MMA Multimedia Accelerator*/
|
||||
#define EM_PCP 55 /* Siemens PCP */
|
||||
#define EM_NCPU 56 /* Sony nCPU embeeded RISC */
|
||||
#define EM_NDR1 57 /* Denso NDR1 microprocessor */
|
||||
#define EM_STARCORE 58 /* Motorola Start*Core processor */
|
||||
#define EM_ME16 59 /* Toyota ME16 processor */
|
||||
#define EM_ST100 60 /* STMicroelectronic ST100 processor */
|
||||
#define EM_TINYJ 61 /* Advanced Logic Corp. Tinyj emb.fam*/
|
||||
#define EM_X86_64 62 /* AMD x86-64 */
|
||||
#define EM_PDSP 63 /* Sony DSP Processor */
|
||||
/* RESERVED 64,65 for future use */
|
||||
#define EM_FX66 66 /* Siemens FX66 microcontroller */
|
||||
#define EM_ST9PLUS 67 /* STMicroelectronics ST9+ 8/16 mc */
|
||||
#define EM_ST7 68 /* STmicroelectronics ST7 8 bit mc */
|
||||
#define EM_68HC16 69 /* Motorola MC68HC16 microcontroller */
|
||||
#define EM_68HC11 70 /* Motorola MC68HC11 microcontroller */
|
||||
#define EM_68HC08 71 /* Motorola MC68HC08 microcontroller */
|
||||
#define EM_68HC05 72 /* Motorola MC68HC05 microcontroller */
|
||||
#define EM_SVX 73 /* Silicon Graphics SVx */
|
||||
#define EM_ST19 74 /* STMicroelectronics ST19 8 bit mc */
|
||||
#define EM_VAX 75 /* Digital VAX */
|
||||
#define EM_CHRIS 76 /* Axis Communications embedded proc. */
|
||||
#define EM_JAVELIN 77 /* Infineon Technologies emb. proc. */
|
||||
#define EM_FIREPATH 78 /* Element 14 64-bit DSP Processor */
|
||||
#define EM_ZSP 79 /* LSI Logic 16-bit DSP Processor */
|
||||
#define EM_MMIX 80 /* Donald Knuth's edu 64-bit proc. */
|
||||
#define EM_HUANY 81 /* Harvard University mach-indep objs */
|
||||
#define EM_PRISM 82 /* SiTera Prism */
|
||||
#define EM_AVR 83 /* Atmel AVR 8-bit microcontroller */
|
||||
#define EM_FR30 84 /* Fujitsu FR30 */
|
||||
#define EM_D10V 85 /* Mitsubishi DV10V */
|
||||
#define EM_D30V 86 /* Mitsubishi DV30V */
|
||||
#define EM_V850 87 /* NEC v850 */
|
||||
#define EM_M32R 88 /* Mitsubishi M32R */
|
||||
#define EM_MN10300 89 /* Matsushita MN10200 */
|
||||
#define EM_MN10200 90 /* Matsushita MN10200 */
|
||||
#define EM_PJ 91 /* picoJava */
|
||||
#define EM_NUM 92 /* number of machine types */
|
||||
|
||||
/* Version */
|
||||
#define EV_NONE 0 /* Invalid */
|
||||
#define EV_CURRENT 1 /* Current */
|
||||
#define EV_NUM 2 /* number of versions */
|
||||
|
||||
/* Section Header */
|
||||
typedef struct {
|
||||
Elf32_Word sh_name; /* name - index into section header
|
||||
string table section */
|
||||
Elf32_Word sh_type; /* type */
|
||||
Elf32_Word sh_flags; /* flags */
|
||||
Elf32_Addr sh_addr; /* address */
|
||||
Elf32_Off sh_offset; /* file offset */
|
||||
Elf32_Word sh_size; /* section size */
|
||||
Elf32_Word sh_link; /* section header table index link */
|
||||
Elf32_Word sh_info; /* extra information */
|
||||
Elf32_Word sh_addralign; /* address alignment */
|
||||
Elf32_Word sh_entsize; /* section entry size */
|
||||
} Elf32_Shdr;
|
||||
|
||||
/* Special Section Indexes */
|
||||
#define SHN_UNDEF 0 /* undefined */
|
||||
#define SHN_LORESERVE 0xff00 /* lower bounds of reserved indexes */
|
||||
#define SHN_LOPROC 0xff00 /* reserved range for processor */
|
||||
#define SHN_HIPROC 0xff1f /* specific section indexes */
|
||||
#define SHN_LOOS 0xff20 /* reserved range for operating */
|
||||
#define SHN_HIOS 0xff3f /* specific semantics */
|
||||
#define SHN_ABS 0xfff1 /* absolute value */
|
||||
#define SHN_COMMON 0xfff2 /* common symbol */
|
||||
#define SHN_XINDEX 0xffff /* Index is an extra table */
|
||||
#define SHN_HIRESERVE 0xffff /* upper bounds of reserved indexes */
|
||||
|
||||
/* sh_type */
|
||||
#define SHT_NULL 0 /* inactive */
|
||||
#define SHT_PROGBITS 1 /* program defined information */
|
||||
#define SHT_SYMTAB 2 /* symbol table section */
|
||||
#define SHT_STRTAB 3 /* string table section */
|
||||
#define SHT_RELA 4 /* relocation section with addends*/
|
||||
#define SHT_HASH 5 /* symbol hash table section */
|
||||
#define SHT_DYNAMIC 6 /* dynamic section */
|
||||
#define SHT_NOTE 7 /* note section */
|
||||
#define SHT_NOBITS 8 /* no space section */
|
||||
#define SHT_REL 9 /* relation section without addends */
|
||||
#define SHT_SHLIB 10 /* reserved - purpose unknown */
|
||||
#define SHT_DYNSYM 11 /* dynamic symbol table section */
|
||||
#define SHT_INIT_ARRAY 14 /* Array of constructors */
|
||||
#define SHT_FINI_ARRAY 15 /* Array of destructors */
|
||||
#define SHT_PREINIT_ARRAY 16 /* Array of pre-constructors */
|
||||
#define SHT_GROUP 17 /* Section group */
|
||||
#define SHT_SYMTAB_SHNDX 18 /* Extended section indeces */
|
||||
#define SHT_NUM 19 /* number of section types */
|
||||
#define SHT_LOOS 0x60000000 /* Start OS-specific */
|
||||
#define SHT_HIOS 0x6fffffff /* End OS-specific */
|
||||
#define SHT_LOPROC 0x70000000 /* reserved range for processor */
|
||||
#define SHT_HIPROC 0x7fffffff /* specific section header types */
|
||||
#define SHT_LOUSER 0x80000000 /* reserved range for application */
|
||||
#define SHT_HIUSER 0xffffffff /* specific indexes */
|
||||
|
||||
/* Section names */
|
||||
#define ELF_BSS ".bss" /* uninitialized data */
|
||||
#define ELF_COMMENT ".comment" /* version control information */
|
||||
#define ELF_DATA ".data" /* initialized data */
|
||||
#define ELF_DATA1 ".data1" /* initialized data */
|
||||
#define ELF_DEBUG ".debug" /* debug */
|
||||
#define ELF_DYNAMIC ".dynamic" /* dynamic linking information */
|
||||
#define ELF_DYNSTR ".dynstr" /* dynamic string table */
|
||||
#define ELF_DYNSYM ".dynsym" /* dynamic symbol table */
|
||||
#define ELF_FINI ".fini" /* termination code */
|
||||
#define ELF_FINI_ARRAY ".fini_array" /* Array of destructors */
|
||||
#define ELF_GOT ".got" /* global offset table */
|
||||
#define ELF_HASH ".hash" /* symbol hash table */
|
||||
#define ELF_INIT ".init" /* initialization code */
|
||||
#define ELF_INIT_ARRAY ".init_array" /* Array of constuctors */
|
||||
#define ELF_INTERP ".interp" /* Pathname of program interpreter */
|
||||
#define ELF_LINE ".line" /* Symbolic line numnber information */
|
||||
#define ELF_NOTE ".note" /* Contains note section */
|
||||
#define ELF_PLT ".plt" /* Procedure linkage table */
|
||||
#define ELF_PREINIT_ARRAY ".preinit_array" /* Array of pre-constructors */
|
||||
#define ELF_REL_DATA ".rel.data" /* relocation data */
|
||||
#define ELF_REL_FINI ".rel.fini" /* relocation termination code */
|
||||
#define ELF_REL_INIT ".rel.init" /* relocation initialization code */
|
||||
#define ELF_REL_DYN ".rel.dyn" /* relocaltion dynamic link info */
|
||||
#define ELF_REL_RODATA ".rel.rodata" /* relocation read-only data */
|
||||
#define ELF_REL_TEXT ".rel.text" /* relocation code */
|
||||
#define ELF_RODATA ".rodata" /* read-only data */
|
||||
#define ELF_RODATA1 ".rodata1" /* read-only data */
|
||||
#define ELF_SHSTRTAB ".shstrtab" /* section header string table */
|
||||
#define ELF_STRTAB ".strtab" /* string table */
|
||||
#define ELF_SYMTAB ".symtab" /* symbol table */
|
||||
#define ELF_SYMTAB_SHNDX ".symtab_shndx"/* symbol table section index */
|
||||
#define ELF_TBSS ".tbss" /* thread local uninit data */
|
||||
#define ELF_TDATA ".tdata" /* thread local init data */
|
||||
#define ELF_TDATA1 ".tdata1" /* thread local init data */
|
||||
#define ELF_TEXT ".text" /* code */
|
||||
|
||||
/* Section Attribute Flags - sh_flags */
|
||||
#define SHF_WRITE 0x1 /* Writable */
|
||||
#define SHF_ALLOC 0x2 /* occupies memory */
|
||||
#define SHF_EXECINSTR 0x4 /* executable */
|
||||
#define SHF_MERGE 0x10 /* Might be merged */
|
||||
#define SHF_STRINGS 0x20 /* Contains NULL terminated strings */
|
||||
#define SHF_INFO_LINK 0x40 /* sh_info contains SHT index */
|
||||
#define SHF_LINK_ORDER 0x80 /* Preserve order after combining*/
|
||||
#define SHF_OS_NONCONFORMING 0x100 /* Non-standard OS specific handling */
|
||||
#define SHF_GROUP 0x200 /* Member of section group */
|
||||
#define SHF_TLS 0x400 /* Thread local storage */
|
||||
#define SHF_MASKOS 0x0ff00000 /* OS specific */
|
||||
#define SHF_MASKPROC 0xf0000000 /* reserved bits for processor */
|
||||
/* specific section attributes */
|
||||
|
||||
/* Section Group Flags */
|
||||
#define GRP_COMDAT 0x1 /* COMDAT group */
|
||||
#define GRP_MASKOS 0x0ff00000 /* Mask OS specific flags */
|
||||
#define GRP_MASKPROC 0xf0000000 /* Mask processor specific flags */
|
||||
|
||||
/* Symbol Table Entry */
|
||||
typedef struct elf32_sym {
|
||||
Elf32_Word st_name; /* name - index into string table */
|
||||
Elf32_Addr st_value; /* symbol value */
|
||||
Elf32_Word st_size; /* symbol size */
|
||||
unsigned char st_info; /* type and binding */
|
||||
unsigned char st_other; /* 0 - no defined meaning */
|
||||
Elf32_Half st_shndx; /* section header index */
|
||||
} Elf32_Sym;
|
||||
|
||||
/* Symbol table index */
|
||||
#define STN_UNDEF 0 /* undefined */
|
||||
|
||||
/* Extract symbol info - st_info */
|
||||
#define ELF32_ST_BIND(x) ((x) >> 4)
|
||||
#define ELF32_ST_TYPE(x) (((unsigned int) x) & 0xf)
|
||||
#define ELF32_ST_INFO(b,t) (((b) << 4) + ((t) & 0xf))
|
||||
#define ELF32_ST_VISIBILITY(x) ((x) & 0x3)
|
||||
|
||||
/* Symbol Binding - ELF32_ST_BIND - st_info */
|
||||
#define STB_LOCAL 0 /* Local symbol */
|
||||
#define STB_GLOBAL 1 /* Global symbol */
|
||||
#define STB_WEAK 2 /* like global - lower precedence */
|
||||
#define STB_NUM 3 /* number of symbol bindings */
|
||||
#define STB_LOOS 10 /* reserved range for operating */
|
||||
#define STB_HIOS 12 /* system specific symbol bindings */
|
||||
#define STB_LOPROC 13 /* reserved range for processor */
|
||||
#define STB_HIPROC 15 /* specific symbol bindings */
|
||||
|
||||
/* Symbol type - ELF32_ST_TYPE - st_info */
|
||||
#define STT_NOTYPE 0 /* not specified */
|
||||
#define STT_OBJECT 1 /* data object */
|
||||
#define STT_FUNC 2 /* function */
|
||||
#define STT_SECTION 3 /* section */
|
||||
#define STT_FILE 4 /* file */
|
||||
#define STT_NUM 5 /* number of symbol types */
|
||||
#define STT_TLS 6 /* Thread local storage symbol */
|
||||
#define STT_LOOS 10 /* reserved range for operating */
|
||||
#define STT_HIOS 12 /* system specific symbol types */
|
||||
#define STT_LOPROC 13 /* reserved range for processor */
|
||||
#define STT_HIPROC 15 /* specific symbol types */
|
||||
|
||||
/* Symbol visibility - ELF32_ST_VISIBILITY - st_other */
|
||||
#define STV_DEFAULT 0 /* Normal visibility rules */
|
||||
#define STV_INTERNAL 1 /* Processor specific hidden class */
|
||||
#define STV_HIDDEN 2 /* Symbol unavailable in other mods */
|
||||
#define STV_PROTECTED 3 /* Not preemptible, not exported */
|
||||
|
||||
|
||||
/* Relocation entry with implicit addend */
|
||||
typedef struct {
|
||||
Elf32_Addr r_offset; /* offset of relocation */
|
||||
Elf32_Word r_info; /* symbol table index and type */
|
||||
} Elf32_Rel;
|
||||
|
||||
/* Relocation entry with explicit addend */
|
||||
typedef struct {
|
||||
Elf32_Addr r_offset; /* offset of relocation */
|
||||
Elf32_Word r_info; /* symbol table index and type */
|
||||
Elf32_Sword r_addend;
|
||||
} Elf32_Rela;
|
||||
|
||||
/* Extract relocation info - r_info */
|
||||
#define ELF32_R_SYM(i) ((i) >> 8)
|
||||
#define ELF32_R_TYPE(i) ((unsigned char) (i))
|
||||
#define ELF32_R_INFO(s,t) (((s) << 8) + (unsigned char)(t))
|
||||
|
||||
/* Program Header */
|
||||
typedef struct {
|
||||
Elf32_Word p_type; /* segment type */
|
||||
Elf32_Off p_offset; /* segment offset */
|
||||
Elf32_Addr p_vaddr; /* virtual address of segment */
|
||||
Elf32_Addr p_paddr; /* physical address - ignored? */
|
||||
Elf32_Word p_filesz; /* number of bytes in file for seg. */
|
||||
Elf32_Word p_memsz; /* number of bytes in mem. for seg. */
|
||||
Elf32_Word p_flags; /* flags */
|
||||
Elf32_Word p_align; /* memory alignment */
|
||||
} Elf32_Phdr;
|
||||
|
||||
/* Segment types - p_type */
|
||||
#define PT_NULL 0 /* unused */
|
||||
#define PT_LOAD 1 /* loadable segment */
|
||||
#define PT_DYNAMIC 2 /* dynamic linking section */
|
||||
#define PT_INTERP 3 /* the RTLD */
|
||||
#define PT_NOTE 4 /* auxiliary information */
|
||||
#define PT_SHLIB 5 /* reserved - purpose undefined */
|
||||
#define PT_PHDR 6 /* program header */
|
||||
#define PT_TLS 7 /* Thread local storage template */
|
||||
#define PT_NUM 8 /* Number of segment types */
|
||||
#define PT_LOOS 0x60000000 /* reserved range for operating */
|
||||
#define PT_HIOS 0x6fffffff /* system specific segment types */
|
||||
#define PT_LOPROC 0x70000000 /* reserved range for processor */
|
||||
#define PT_HIPROC 0x7fffffff /* specific segment types */
|
||||
|
||||
/* Segment flags - p_flags */
|
||||
#define PF_X 0x1 /* Executable */
|
||||
#define PF_W 0x2 /* Writable */
|
||||
#define PF_R 0x4 /* Readable */
|
||||
#define PF_MASKOS 0x0ff00000 /* OS specific segment flags */
|
||||
#define PF_MASKPROC 0xf0000000 /* reserved bits for processor */
|
||||
/* specific segment flags */
|
||||
/* Dynamic structure */
|
||||
typedef struct {
|
||||
Elf32_Sword d_tag; /* controls meaning of d_val */
|
||||
union {
|
||||
Elf32_Word d_val; /* Multiple meanings - see d_tag */
|
||||
Elf32_Addr d_ptr; /* program virtual address */
|
||||
} d_un;
|
||||
} Elf32_Dyn;
|
||||
|
||||
extern Elf32_Dyn _DYNAMIC[];
|
||||
|
||||
/* Dynamic Array Tags - d_tag */
|
||||
#define DT_NULL 0 /* marks end of _DYNAMIC array */
|
||||
#define DT_NEEDED 1 /* string table offset of needed lib */
|
||||
#define DT_PLTRELSZ 2 /* size of relocation entries in PLT */
|
||||
#define DT_PLTGOT 3 /* address PLT/GOT */
|
||||
#define DT_HASH 4 /* address of symbol hash table */
|
||||
#define DT_STRTAB 5 /* address of string table */
|
||||
#define DT_SYMTAB 6 /* address of symbol table */
|
||||
#define DT_RELA 7 /* address of relocation table */
|
||||
#define DT_RELASZ 8 /* size of relocation table */
|
||||
#define DT_RELAENT 9 /* size of relocation entry */
|
||||
#define DT_STRSZ 10 /* size of string table */
|
||||
#define DT_SYMENT 11 /* size of symbol table entry */
|
||||
#define DT_INIT 12 /* address of initialization func. */
|
||||
#define DT_FINI 13 /* address of termination function */
|
||||
#define DT_SONAME 14 /* string table offset of shared obj */
|
||||
#define DT_RPATH 15 /* string table offset of library
|
||||
search path */
|
||||
#define DT_SYMBOLIC 16 /* start sym search in shared obj. */
|
||||
#define DT_REL 17 /* address of rel. tbl. w addends */
|
||||
#define DT_RELSZ 18 /* size of DT_REL relocation table */
|
||||
#define DT_RELENT 19 /* size of DT_REL relocation entry */
|
||||
#define DT_PLTREL 20 /* PLT referenced relocation entry */
|
||||
#define DT_DEBUG 21 /* bugger */
|
||||
#define DT_TEXTREL 22 /* Allow rel. mod. to unwritable seg */
|
||||
#define DT_JMPREL 23 /* add. of PLT's relocation entries */
|
||||
#define DT_BIND_NOW 24 /* Process relocations of object */
|
||||
#define DT_INIT_ARRAY 25 /* Array with addresses of init fct */
|
||||
#define DT_FINI_ARRAY 26 /* Array with addresses of fini fct */
|
||||
#define DT_INIT_ARRAYSZ 27 /* Size in bytes of DT_INIT_ARRAY */
|
||||
#define DT_FINI_ARRAYSZ 28 /* Size in bytes of DT_FINI_ARRAY */
|
||||
#define DT_RUNPATH 29 /* Library search path */
|
||||
#define DT_FLAGS 30 /* Flags for the object being loaded */
|
||||
#define DT_ENCODING 32 /* Start of encoded range */
|
||||
#define DT_PREINIT_ARRAY 32 /* Array with addresses of preinit fct*/
|
||||
#define DT_PREINIT_ARRAYSZ 33 /* size in bytes of DT_PREINIT_ARRAY */
|
||||
#define DT_NUM 34 /* Number used. */
|
||||
#define DT_LOOS 0x60000000 /* reserved range for OS */
|
||||
#define DT_HIOS 0x6fffffff /* specific dynamic array tags */
|
||||
#define DT_LOPROC 0x70000000 /* reserved range for processor */
|
||||
#define DT_HIPROC 0x7fffffff /* specific dynamic array tags */
|
||||
|
||||
/* Dynamic Tag Flags - d_un.d_val */
|
||||
#define DF_ORIGIN 0x01 /* Object may use DF_ORIGIN */
|
||||
#define DF_SYMBOLIC 0x02 /* Symbol resolutions starts here */
|
||||
#define DF_TEXTREL 0x04 /* Object contains text relocations */
|
||||
#define DF_BIND_NOW 0x08 /* No lazy binding for this object */
|
||||
#define DF_STATIC_TLS 0x10 /* Static thread local storage */
|
||||
|
||||
/* Standard ELF hashing function */
|
||||
unsigned long elf_hash(const unsigned char *name);
|
||||
|
||||
#define ELF_TARG_VER 1 /* The ver for which this code is intended */
|
||||
|
||||
/*
|
||||
* XXX - PowerPC defines really don't belong in here,
|
||||
* but we'll put them in for simplicity.
|
||||
*/
|
||||
|
||||
/* Values for Elf32/64_Ehdr.e_flags. */
|
||||
#define EF_PPC_EMB 0x80000000 /* PowerPC embedded flag */
|
||||
|
||||
/* Cygnus local bits below */
|
||||
#define EF_PPC_RELOCATABLE 0x00010000 /* PowerPC -mrelocatable flag*/
|
||||
#define EF_PPC_RELOCATABLE_LIB 0x00008000 /* PowerPC -mrelocatable-lib
|
||||
flag */
|
||||
|
||||
/* PowerPC relocations defined by the ABIs */
|
||||
#define R_PPC_NONE 0
|
||||
#define R_PPC_ADDR32 1 /* 32bit absolute address */
|
||||
#define R_PPC_ADDR24 2 /* 26bit address, 2 bits ignored. */
|
||||
#define R_PPC_ADDR16 3 /* 16bit absolute address */
|
||||
#define R_PPC_ADDR16_LO 4 /* lower 16bit of absolute address */
|
||||
#define R_PPC_ADDR16_HI 5 /* high 16bit of absolute address */
|
||||
#define R_PPC_ADDR16_HA 6 /* adjusted high 16bit */
|
||||
#define R_PPC_ADDR14 7 /* 16bit address, 2 bits ignored */
|
||||
#define R_PPC_ADDR14_BRTAKEN 8
|
||||
#define R_PPC_ADDR14_BRNTAKEN 9
|
||||
#define R_PPC_REL24 10 /* PC relative 26 bit */
|
||||
#define R_PPC_REL14 11 /* PC relative 16 bit */
|
||||
#define R_PPC_REL14_BRTAKEN 12
|
||||
#define R_PPC_REL14_BRNTAKEN 13
|
||||
#define R_PPC_GOT16 14
|
||||
#define R_PPC_GOT16_LO 15
|
||||
#define R_PPC_GOT16_HI 16
|
||||
#define R_PPC_GOT16_HA 17
|
||||
#define R_PPC_PLTREL24 18
|
||||
#define R_PPC_COPY 19
|
||||
#define R_PPC_GLOB_DAT 20
|
||||
#define R_PPC_JMP_SLOT 21
|
||||
#define R_PPC_RELATIVE 22
|
||||
#define R_PPC_LOCAL24PC 23
|
||||
#define R_PPC_UADDR32 24
|
||||
#define R_PPC_UADDR16 25
|
||||
#define R_PPC_REL32 26
|
||||
#define R_PPC_PLT32 27
|
||||
#define R_PPC_PLTREL32 28
|
||||
#define R_PPC_PLT16_LO 29
|
||||
#define R_PPC_PLT16_HI 30
|
||||
#define R_PPC_PLT16_HA 31
|
||||
#define R_PPC_SDAREL16 32
|
||||
#define R_PPC_SECTOFF 33
|
||||
#define R_PPC_SECTOFF_LO 34
|
||||
#define R_PPC_SECTOFF_HI 35
|
||||
#define R_PPC_SECTOFF_HA 36
|
||||
/* Keep this the last entry. */
|
||||
#define R_PPC_NUM 37
|
||||
|
||||
/* The remaining relocs are from the Embedded ELF ABI, and are not
|
||||
in the SVR4 ELF ABI. */
|
||||
#define R_PPC_EMB_NADDR32 101
|
||||
#define R_PPC_EMB_NADDR16 102
|
||||
#define R_PPC_EMB_NADDR16_LO 103
|
||||
#define R_PPC_EMB_NADDR16_HI 104
|
||||
#define R_PPC_EMB_NADDR16_HA 105
|
||||
#define R_PPC_EMB_SDAI16 106
|
||||
#define R_PPC_EMB_SDA2I16 107
|
||||
#define R_PPC_EMB_SDA2REL 108
|
||||
#define R_PPC_EMB_SDA21 109 /* 16 bit offset in SDA */
|
||||
#define R_PPC_EMB_MRKREF 110
|
||||
#define R_PPC_EMB_RELSEC16 111
|
||||
#define R_PPC_EMB_RELST_LO 112
|
||||
#define R_PPC_EMB_RELST_HI 113
|
||||
#define R_PPC_EMB_RELST_HA 114
|
||||
#define R_PPC_EMB_BIT_FLD 115
|
||||
#define R_PPC_EMB_RELSDA 116 /* 16 bit relative offset in SDA */
|
||||
|
||||
/* Diab tool relocations. */
|
||||
#define R_PPC_DIAB_SDA21_LO 180 /* like EMB_SDA21, but lower 16 bit */
|
||||
#define R_PPC_DIAB_SDA21_HI 181 /* like EMB_SDA21, but high 16 bit */
|
||||
#define R_PPC_DIAB_SDA21_HA 182 /* like EMB_SDA21, adjusted high 16 */
|
||||
#define R_PPC_DIAB_RELSDA_LO 183 /* like EMB_RELSDA, but lower 16 bit */
|
||||
#define R_PPC_DIAB_RELSDA_HI 184 /* like EMB_RELSDA, but high 16 bit */
|
||||
#define R_PPC_DIAB_RELSDA_HA 185 /* like EMB_RELSDA, adjusted high 16 */
|
||||
|
||||
/* This is a phony reloc to handle any old fashioned TOC16 references
|
||||
that may still be in object files. */
|
||||
#define R_PPC_TOC16 255
|
||||
|
||||
#endif /* _ELF_H */
|
||||
|
@ -1,101 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2001 William L. Pitts
|
||||
* Modifications (c) 2004 Felix Domke
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms are freely
|
||||
* permitted provided that the above copyright notice and this
|
||||
* paragraph and the following disclaimer are duplicated in all
|
||||
* such forms.
|
||||
*
|
||||
* This software is provided "AS IS" and without any express or
|
||||
* implied warranties, including, without limitation, the implied
|
||||
* warranties of merchantability and fitness for a particular
|
||||
* purpose.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <gccore.h>
|
||||
|
||||
#include "elf_abi.h"
|
||||
|
||||
/* ======================================================================
|
||||
* Determine if a valid ELF image exists at the given memory location.
|
||||
* First looks at the ELF header magic field, the makes sure that it is
|
||||
* executable and makes sure that it is for a PowerPC.
|
||||
* ====================================================================== */
|
||||
s32 valid_elf_image (void *addr) {
|
||||
Elf32_Ehdr *ehdr; /* Elf header structure pointer */
|
||||
|
||||
ehdr = (Elf32_Ehdr *) addr;
|
||||
|
||||
if (!IS_ELF (*ehdr))
|
||||
return 0;
|
||||
|
||||
if (ehdr->e_type != ET_EXEC)
|
||||
return -1;
|
||||
|
||||
if (ehdr->e_machine != EM_PPC)
|
||||
return -1;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/* ======================================================================
|
||||
* A very simple elf loader, assumes the image is valid, returns the
|
||||
* entry point address.
|
||||
* ====================================================================== */
|
||||
u32 load_elf_image (void *addr) {
|
||||
Elf32_Ehdr *ehdr;
|
||||
Elf32_Shdr *shdr;
|
||||
u8 *strtab = 0;
|
||||
u8 *image;
|
||||
int i;
|
||||
|
||||
ehdr = (Elf32_Ehdr *) addr;
|
||||
/* Find the section header string table for output info */
|
||||
shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
|
||||
(ehdr->e_shstrndx * sizeof (Elf32_Shdr)));
|
||||
|
||||
if (shdr->sh_type == SHT_STRTAB)
|
||||
strtab = (u8 *) (addr + shdr->sh_offset);
|
||||
|
||||
/* Load each appropriate section */
|
||||
for (i = 0; i < ehdr->e_shnum; ++i) {
|
||||
shdr = (Elf32_Shdr *) (addr + ehdr->e_shoff +
|
||||
(i * sizeof (Elf32_Shdr)));
|
||||
|
||||
if (!(shdr->sh_flags & SHF_ALLOC)
|
||||
|| shdr->sh_addr == 0 || shdr->sh_size == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
shdr->sh_addr &= 0x3FFFFFFF;
|
||||
shdr->sh_addr |= 0x80000000;
|
||||
|
||||
if (strtab) {
|
||||
/*printf ("%sing section %s @ 0x%08x (0x%08x bytes)\n",
|
||||
(shdr->sh_type == SHT_NOBITS) ?
|
||||
"clear" : "load",
|
||||
&strtab[shdr->sh_name],
|
||||
(u32) shdr->sh_addr,
|
||||
(u32) shdr->sh_size);*/
|
||||
}
|
||||
|
||||
if (shdr->sh_type == SHT_NOBITS) {
|
||||
memset ((void *) shdr->sh_addr, 0, shdr->sh_size);
|
||||
} else {
|
||||
image = (u8 *) addr + shdr->sh_offset;
|
||||
memcpy ((void *) shdr->sh_addr,
|
||||
(const void *) image,
|
||||
shdr->sh_size);
|
||||
}
|
||||
DCFlushRangeNoSync ((void *) shdr->sh_addr, shdr->sh_size);
|
||||
}
|
||||
|
||||
return (ehdr->e_entry & 0x3FFFFFFF) | 0x80000000;
|
||||
}
|
||||
|
@ -1,17 +0,0 @@
|
||||
#ifndef _ELFLOADER_H_
|
||||
#define _ELFLOADER_H_
|
||||
|
||||
#include <wiiuse/wpad.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
s32 valid_elf_image (void *addr);
|
||||
u32 load_elf_image (void *addr);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -26,8 +26,8 @@
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef __BIT_OPS_H
|
||||
#define __BIT_OPS_H
|
||||
#ifndef _BIT_OPS_H
|
||||
#define _BIT_OPS_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
130
source/libfat/cache.h
Normal file
130
source/libfat/cache.h
Normal file
@ -0,0 +1,130 @@
|
||||
/*
|
||||
cache.h
|
||||
The cache is not visible to the user. It should be flushed
|
||||
when any file is closed or changes are made to the filesystem.
|
||||
|
||||
This cache implements a least-used-page replacement policy. This will
|
||||
distribute sectors evenly over the pages, so if less than the maximum
|
||||
pages are used at once, they should all eventually remain in the cache.
|
||||
This also has the benefit of throwing out old sectors, so as not to keep
|
||||
too many stale pages around.
|
||||
|
||||
Copyright (c) 2006 Michael "Chishm" Chisholm
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation and/or
|
||||
other materials provided with the distribution.
|
||||
3. The name of the author may not be used to endorse or promote products derived
|
||||
from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef _CACHE_H
|
||||
#define _CACHE_H
|
||||
|
||||
#include "common.h"
|
||||
#include "disc.h"
|
||||
|
||||
#define PAGE_SECTORS 64
|
||||
#define CACHE_PAGE_SIZE (BYTES_PER_READ * PAGE_SECTORS)
|
||||
|
||||
typedef struct {
|
||||
sec_t sector;
|
||||
unsigned int count;
|
||||
unsigned int last_access;
|
||||
bool dirty;
|
||||
uint8_t* cache;
|
||||
} CACHE_ENTRY;
|
||||
|
||||
typedef struct {
|
||||
const DISC_INTERFACE* disc;
|
||||
sec_t endOfPartition;
|
||||
unsigned int numberOfPages;
|
||||
unsigned int sectorsPerPage;
|
||||
CACHE_ENTRY* cacheEntries;
|
||||
} CACHE;
|
||||
|
||||
/*
|
||||
Read data from a sector in the cache
|
||||
If the sector is not in the cache, it will be swapped in
|
||||
offset is the position to start reading from
|
||||
size is the amount of data to read
|
||||
Precondition: offset + size <= BYTES_PER_READ
|
||||
*/
|
||||
bool _FAT_cache_readPartialSector (CACHE* cache, void* buffer, sec_t sector, unsigned int offset, size_t size);
|
||||
|
||||
bool _FAT_cache_readLittleEndianValue (CACHE* cache, uint32_t *value, sec_t sector, unsigned int offset, int num_bytes);
|
||||
|
||||
/*
|
||||
Write data to a sector in the cache
|
||||
If the sector is not in the cache, it will be swapped in.
|
||||
When the sector is swapped out, the data will be written to the disc
|
||||
offset is the position to start writing to
|
||||
size is the amount of data to write
|
||||
Precondition: offset + size <= BYTES_PER_READ
|
||||
*/
|
||||
bool _FAT_cache_writePartialSector (CACHE* cache, const void* buffer, sec_t sector, unsigned int offset, size_t size);
|
||||
|
||||
bool _FAT_cache_writeLittleEndianValue (CACHE* cache, const uint32_t value, sec_t sector, unsigned int offset, int num_bytes);
|
||||
|
||||
/*
|
||||
Write data to a sector in the cache, zeroing the sector first
|
||||
If the sector is not in the cache, it will be swapped in.
|
||||
When the sector is swapped out, the data will be written to the disc
|
||||
offset is the position to start writing to
|
||||
size is the amount of data to write
|
||||
Precondition: offset + size <= BYTES_PER_READ
|
||||
*/
|
||||
bool _FAT_cache_eraseWritePartialSector (CACHE* cache, const void* buffer, sec_t sector, unsigned int offset, size_t size);
|
||||
|
||||
/*
|
||||
Read several sectors from the cache
|
||||
*/
|
||||
bool _FAT_cache_readSectors (CACHE* cache, sec_t sector, sec_t numSectors, void* buffer);
|
||||
|
||||
/*
|
||||
Read a full sector from the cache
|
||||
*/
|
||||
static inline bool _FAT_cache_readSector (CACHE* cache, void* buffer, sec_t sector) {
|
||||
return _FAT_cache_readPartialSector (cache, buffer, sector, 0, BYTES_PER_READ);
|
||||
}
|
||||
|
||||
/*
|
||||
Write a full sector to the cache
|
||||
*/
|
||||
static inline bool _FAT_cache_writeSector (CACHE* cache, const void* buffer, sec_t sector) {
|
||||
return _FAT_cache_writePartialSector (cache, buffer, sector, 0, BYTES_PER_READ);
|
||||
}
|
||||
|
||||
bool _FAT_cache_writeSectors (CACHE* cache, sec_t sector, sec_t numSectors, const void* buffer);
|
||||
|
||||
/*
|
||||
Write any dirty sectors back to disc and clear out the contents of the cache
|
||||
*/
|
||||
bool _FAT_cache_flush (CACHE* cache);
|
||||
|
||||
/*
|
||||
Clear out the contents of the cache without writing any dirty sectors first
|
||||
*/
|
||||
void _FAT_cache_invalidate (CACHE* cache);
|
||||
|
||||
CACHE* _FAT_cache_constructor (unsigned int numberOfPages, unsigned int sectorsPerPage, const DISC_INTERFACE* discInterface, sec_t endOfPartition);
|
||||
|
||||
void _FAT_cache_destructor (CACHE* cache);
|
||||
|
||||
#endif // _CACHE_H
|
||||
|
@ -26,23 +26,54 @@
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef __COMMON_H
|
||||
#define __COMMON_H
|
||||
#ifndef _COMMON_H
|
||||
#define _COMMON_H
|
||||
|
||||
#define BYTES_PER_READ 512
|
||||
#include "fat.h"
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include "libfat/fat.h"
|
||||
|
||||
// When compiling for NDS, make sure NDS is defined
|
||||
#ifndef NDS
|
||||
#if defined ARM9 || defined ARM7
|
||||
#define NDS
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Platform specific includes
|
||||
#if defined(__gamecube__) || defined (__wii__)
|
||||
#include <gctypes.h>
|
||||
#include <ogc/disc_io.h>
|
||||
#include <gccore.h>
|
||||
#elif defined(NDS)
|
||||
#include <nds/ndstypes.h>
|
||||
#include <nds/system.h>
|
||||
#include <nds/disc_io.h>
|
||||
#elif defined(GBA)
|
||||
#include <gba_types.h>
|
||||
#include <disc_io.h>
|
||||
#endif
|
||||
|
||||
// Platform specific options
|
||||
#if defined (__wii__)
|
||||
#define DEFAULT_CACHE_PAGES 4
|
||||
#define DEFAULT_SECTORS_PAGE 64
|
||||
#define USE_LWP_LOCK
|
||||
#define USE_RTC_TIME
|
||||
#elif defined (__gamecube__)
|
||||
#define DEFAULT_CACHE_PAGES 4
|
||||
#define DEFAULT_SECTORS_PAGE 64
|
||||
#define USE_LWP_LOCK
|
||||
#define USE_RTC_TIME
|
||||
#elif defined (NDS)
|
||||
#define DEFAULT_CACHE_PAGES 4
|
||||
#define DEFAULT_SECTORS_PAGE 8
|
||||
#define USE_RTC_TIME
|
||||
#elif defined (GBA)
|
||||
#define DEFAULT_CACHE_PAGES 2
|
||||
#define DEFAULT_SECTORS_PAGE 8
|
||||
#define LIMIT_SECTORS 128
|
||||
#endif
|
||||
|
||||
#endif // _COMMON_H
|
||||
|
@ -329,7 +329,8 @@ bool _FAT_directory_getNextEntry (PARTITION* partition, DIR_ENTRY* entry) {
|
||||
}
|
||||
lfn[lfnPos] = '\0'; // Set end of lfn to null character
|
||||
lfnChkSum = entryData[LFN_offset_checkSum];
|
||||
} if (lfnChkSum != entryData[LFN_offset_checkSum]) {
|
||||
}
|
||||
if (lfnChkSum != entryData[LFN_offset_checkSum]) {
|
||||
lfnExists = false;
|
||||
}
|
||||
if (lfnExists) {
|
||||
@ -417,6 +418,49 @@ bool _FAT_directory_getRootEntry (PARTITION* partition, DIR_ENTRY* entry) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool _FAT_directory_getVolumeLabel (PARTITION* partition, char *label) {
|
||||
DIR_ENTRY entry;
|
||||
DIR_ENTRY_POSITION entryEnd;
|
||||
uint8_t entryData[DIR_ENTRY_DATA_SIZE];
|
||||
int i;
|
||||
bool end;
|
||||
|
||||
_FAT_directory_getRootEntry(partition, &entry);
|
||||
|
||||
entryEnd = entry.dataEnd;
|
||||
|
||||
// Make sure we are using the correct root directory, in case of FAT32
|
||||
if (entryEnd.cluster == FAT16_ROOT_DIR_CLUSTER) {
|
||||
entryEnd.cluster = partition->rootDirCluster;
|
||||
}
|
||||
|
||||
label[0]='\0';
|
||||
label[11]='\0';
|
||||
end = false;
|
||||
//this entry should be among the first 3 entries in the root directory table, if not, then system can have trouble displaying the right volume label
|
||||
while(!end) {
|
||||
if (_FAT_directory_incrementDirEntryPosition (partition, &entryEnd, false) == false) {
|
||||
end = true;
|
||||
}
|
||||
|
||||
if(!_FAT_cache_readPartialSector (partition->cache, entryData,
|
||||
_FAT_fat_clusterToSector(partition, entryEnd.cluster) + entryEnd.sector,
|
||||
entryEnd.offset * DIR_ENTRY_DATA_SIZE, DIR_ENTRY_DATA_SIZE))
|
||||
{ //error reading
|
||||
return false;
|
||||
}
|
||||
if (entryData[DIR_ENTRY_attributes] == ATTRIB_VOL && entryData[0] != DIR_ENTRY_FREE) {
|
||||
for (i = 0; i < 11; i++) {
|
||||
label[i] = entryData[DIR_ENTRY_name + i];
|
||||
}
|
||||
return true;
|
||||
} else if (entryData[0] == DIR_ENTRY_LAST) {
|
||||
end = true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool _FAT_directory_entryFromPosition (PARTITION* partition, DIR_ENTRY* entry) {
|
||||
DIR_ENTRY_POSITION entryStart = entry->dataStart;
|
||||
DIR_ENTRY_POSITION entryEnd = entry->dataEnd;
|
||||
|
@ -27,8 +27,8 @@
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef __DIRECTORY_H
|
||||
#define __DIRECTORY_H
|
||||
#ifndef _DIRECTORY_H
|
||||
#define _DIRECTORY_H
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
@ -170,4 +170,9 @@ Fill in a stat struct based on a file entry
|
||||
*/
|
||||
void _FAT_directory_entryStat (PARTITION* partition, DIR_ENTRY* entry, struct stat *st);
|
||||
|
||||
/*
|
||||
Get volume label
|
||||
*/
|
||||
bool _FAT_directory_getVolumeLabel (PARTITION* partition, char *label);
|
||||
|
||||
#endif // _DIRECTORY_H
|
||||
|
110
source/libfat/disc.h
Normal file
110
source/libfat/disc.h
Normal file
@ -0,0 +1,110 @@
|
||||
/*
|
||||
disc.h
|
||||
Interface to the low level disc functions. Used by the higher level
|
||||
file system code.
|
||||
|
||||
Copyright (c) 2006 Michael "Chishm" Chisholm
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation and/or
|
||||
other materials provided with the distribution.
|
||||
3. The name of the author may not be used to endorse or promote products derived
|
||||
from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#ifndef _DISC_H
|
||||
#define _DISC_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
/*
|
||||
A list of all default devices to try at startup,
|
||||
terminated by a {NULL,NULL} entry.
|
||||
*/
|
||||
typedef struct {
|
||||
const char* name;
|
||||
const DISC_INTERFACE* (*getInterface)(void);
|
||||
} INTERFACE_ID;
|
||||
extern const INTERFACE_ID _FAT_disc_interfaces[];
|
||||
|
||||
/*
|
||||
Check if a disc is inserted
|
||||
Return true if a disc is inserted and ready, false otherwise
|
||||
*/
|
||||
static inline bool _FAT_disc_isInserted (const DISC_INTERFACE* disc) {
|
||||
return disc->isInserted();
|
||||
}
|
||||
|
||||
/*
|
||||
Read numSectors sectors from a disc, starting at sector.
|
||||
numSectors is between 1 and LIMIT_SECTORS if LIMIT_SECTORS is defined,
|
||||
else it is at least 1
|
||||
sector is 0 or greater
|
||||
buffer is a pointer to the memory to fill
|
||||
*/
|
||||
static inline bool _FAT_disc_readSectors (const DISC_INTERFACE* disc, sec_t sector, sec_t numSectors, void* buffer) {
|
||||
return disc->readSectors (sector, numSectors, buffer);
|
||||
}
|
||||
|
||||
/*
|
||||
Write numSectors sectors to a disc, starting at sector.
|
||||
numSectors is between 1 and LIMIT_SECTORS if LIMIT_SECTORS is defined,
|
||||
else it is at least 1
|
||||
sector is 0 or greater
|
||||
buffer is a pointer to the memory to read from
|
||||
*/
|
||||
static inline bool _FAT_disc_writeSectors (const DISC_INTERFACE* disc, sec_t sector, sec_t numSectors, const void* buffer) {
|
||||
return disc->writeSectors (sector, numSectors, buffer);
|
||||
}
|
||||
|
||||
/*
|
||||
Reset the card back to a ready state
|
||||
*/
|
||||
static inline bool _FAT_disc_clearStatus (const DISC_INTERFACE* disc) {
|
||||
return disc->clearStatus();
|
||||
}
|
||||
|
||||
/*
|
||||
Initialise the disc to a state ready for data reading or writing
|
||||
*/
|
||||
static inline bool _FAT_disc_startup (const DISC_INTERFACE* disc) {
|
||||
return disc->startup();
|
||||
}
|
||||
|
||||
/*
|
||||
Put the disc in a state ready for power down.
|
||||
Complete any pending writes and disable the disc if necessary
|
||||
*/
|
||||
static inline bool _FAT_disc_shutdown (const DISC_INTERFACE* disc) {
|
||||
return disc->shutdown();
|
||||
}
|
||||
|
||||
/*
|
||||
Return a 32 bit value unique to each type of interface
|
||||
*/
|
||||
static inline uint32_t _FAT_disc_hostType (const DISC_INTERFACE* disc) {
|
||||
return disc->ioType;
|
||||
}
|
||||
|
||||
/*
|
||||
Return a 32 bit value that specifies the capabilities of the disc
|
||||
*/
|
||||
static inline uint32_t _FAT_disc_features (const DISC_INTERFACE* disc) {
|
||||
return disc->features;
|
||||
}
|
||||
|
||||
#endif // _DISC_H
|
@ -2,7 +2,9 @@
|
||||
fat.h
|
||||
Simple functionality for startup, mounting and unmounting of FAT-based devices.
|
||||
|
||||
Copyright (c) 2006 Michael "Chishm" Chisholm
|
||||
Copyright (c) 2006 - 2009
|
||||
Michael "Chishm" Chisholm
|
||||
Dave "WinterMute" Murphy
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
@ -34,8 +36,24 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// When compiling for NDS, make sure NDS is defined
|
||||
#ifndef NDS
|
||||
#if defined ARM9 || defined ARM7
|
||||
#define NDS
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#if defined(__gamecube__) || defined (__wii__)
|
||||
# include <ogc/disc_io.h>
|
||||
#else
|
||||
# ifdef NDS
|
||||
# include "nds/disc_io.h"
|
||||
# else
|
||||
# include "disc_io.h"
|
||||
# endif
|
||||
#endif
|
||||
|
||||
/*
|
||||
Initialise any inserted block-devices.
|
||||
@ -67,12 +85,18 @@ cacheSize specifies the number of pages to allocate for the cache.
|
||||
This will not startup the disc, so you need to call interface->startup(); first.
|
||||
*/
|
||||
extern bool fatMount (const char* name, const DISC_INTERFACE* interface, sec_t startSector, uint32_t cacheSize, uint32_t SectorsPerPage);
|
||||
|
||||
/*
|
||||
Unmount the partition specified by name.
|
||||
If there are open files, it will attempt to synchronise them to disc.
|
||||
*/
|
||||
extern void fatUnmount (const char* name);
|
||||
|
||||
/*
|
||||
Get Volume Label
|
||||
*/
|
||||
extern void fatGetVolumeLabel (const char* name, char *label);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -37,8 +37,8 @@
|
||||
#include <limits.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "fat_cache.h"
|
||||
#include "disc_fat.h"
|
||||
#include "cache.h"
|
||||
#include "disc.h"
|
||||
|
||||
#include "mem_allocate.h"
|
||||
#include "bit_ops.h"
|
||||
|
@ -27,7 +27,7 @@
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "disc_fat.h"
|
||||
#include "disc.h"
|
||||
|
||||
/*
|
||||
The list of interfaces consists of a series of name/interface pairs.
|
||||
@ -39,15 +39,16 @@ The list is terminated by a NULL/NULL entry.
|
||||
*/
|
||||
|
||||
/* ====================== Wii ====================== */
|
||||
#if defined (__wii__)
|
||||
#include <sdcard/wiisd_io.h>
|
||||
#include "usbloader/usbstorage2.h"
|
||||
#include <ogc/usbstorage.h>
|
||||
#include <sdcard/gcsd.h>
|
||||
|
||||
static const DISC_INTERFACE* get_io_wiisd (void) {
|
||||
return &__io_wiisd;
|
||||
}
|
||||
static const DISC_INTERFACE* get_io_usbstorage (void) {
|
||||
return &__io_usbstorage2;
|
||||
return &__io_usbstorage;
|
||||
}
|
||||
|
||||
static const DISC_INTERFACE* get_io_gcsda (void) {
|
||||
@ -65,3 +66,40 @@ const INTERFACE_ID _FAT_disc_interfaces[] = {
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* ==================== Gamecube ==================== */
|
||||
#elif defined (__gamecube__)
|
||||
#include <sdcard/gcsd.h>
|
||||
|
||||
static const DISC_INTERFACE* get_io_gcsda (void) {
|
||||
return &__io_gcsda;
|
||||
}
|
||||
static const DISC_INTERFACE* get_io_gcsdb (void) {
|
||||
return &__io_gcsdb;
|
||||
}
|
||||
|
||||
const INTERFACE_ID _FAT_disc_interfaces[] = {
|
||||
{"carda", get_io_gcsda},
|
||||
{"cardb", get_io_gcsdb},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* ====================== NDS ====================== */
|
||||
#elif defined (NDS)
|
||||
#include <nds/arm9/dldi.h>
|
||||
|
||||
const INTERFACE_ID _FAT_disc_interfaces[] = {
|
||||
{"fat", dldiGetInternal},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* ====================== GBA ====================== */
|
||||
#elif defined (GBA)
|
||||
#include <disc.h>
|
||||
|
||||
const INTERFACE_ID _FAT_disc_interfaces[] = {
|
||||
{"fat", discGetInterface},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -36,7 +36,7 @@
|
||||
|
||||
#include "fatdir.h"
|
||||
|
||||
#include "fat_cache.h"
|
||||
#include "cache.h"
|
||||
#include "file_allocation_table.h"
|
||||
#include "partition.h"
|
||||
#include "directory.h"
|
||||
|
@ -29,8 +29,8 @@
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __FATDIR_H
|
||||
#define __FATDIR_H
|
||||
#ifndef _FATDIR_H
|
||||
#define _FATDIR_H
|
||||
|
||||
#include <sys/reent.h>
|
||||
#include <sys/stat.h>
|
||||
|
@ -39,7 +39,7 @@
|
||||
#include <ctype.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "fat_cache.h"
|
||||
#include "cache.h"
|
||||
#include "file_allocation_table.h"
|
||||
#include "bit_ops.h"
|
||||
#include "filetime.h"
|
||||
|
@ -27,8 +27,8 @@
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef __FAT_H
|
||||
#define __FAT_H
|
||||
#ifndef _FAT_H
|
||||
#define _FAT_H
|
||||
|
||||
#include "common.h"
|
||||
#include "partition.h"
|
||||
|
@ -26,8 +26,8 @@
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef __FILETIME_H
|
||||
#define __FILETIME_H
|
||||
#ifndef _FILETIME_H
|
||||
#define _FILETIME_H
|
||||
|
||||
#include "common.h"
|
||||
#include <sys/types.h>
|
||||
|
@ -36,7 +36,7 @@
|
||||
#include "fatdir.h"
|
||||
#include "lock.h"
|
||||
#include "mem_allocate.h"
|
||||
#include "disc_fat.h"
|
||||
#include "disc.h"
|
||||
|
||||
static const devoptab_t dotab_fat = {
|
||||
"fat",
|
||||
@ -69,17 +69,17 @@ bool fatMount (const char* name, const DISC_INTERFACE* interface, sec_t startSec
|
||||
devoptab_t* devops;
|
||||
char* nameCopy;
|
||||
|
||||
if(!name || !interface)
|
||||
return false;
|
||||
|
||||
if(!interface->startup())
|
||||
return false;
|
||||
|
||||
if(!interface->isInserted()) {
|
||||
interface->shutdown();
|
||||
if(!interface->isInserted())
|
||||
return false;
|
||||
}
|
||||
|
||||
devops = _FAT_mem_allocate (sizeof(devoptab_t) + strlen(name) + 1);
|
||||
if (!devops) {
|
||||
interface->shutdown();
|
||||
return false;
|
||||
}
|
||||
// Use the space allocated at the end of the devoptab struct for storing the name
|
||||
@ -89,7 +89,6 @@ bool fatMount (const char* name, const DISC_INTERFACE* interface, sec_t startSec
|
||||
partition = _FAT_partition_constructor (interface, cacheSize, SectorsPerPage, startSector);
|
||||
if (!partition) {
|
||||
_FAT_mem_free (devops);
|
||||
interface->shutdown();
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -111,7 +110,9 @@ bool fatMountSimple (const char* name, const DISC_INTERFACE* interface) {
|
||||
void fatUnmount (const char* name) {
|
||||
devoptab_t *devops;
|
||||
PARTITION* partition;
|
||||
const DISC_INTERFACE *disc;
|
||||
|
||||
if(!name)
|
||||
return;
|
||||
|
||||
devops = (devoptab_t*)GetDeviceOpTab (name);
|
||||
if (!devops) {
|
||||
@ -128,10 +129,8 @@ void fatUnmount (const char* name) {
|
||||
}
|
||||
|
||||
partition = (PARTITION*)devops->deviceData;
|
||||
disc = partition->disc;
|
||||
_FAT_partition_destructor (partition);
|
||||
_FAT_mem_free (devops);
|
||||
disc->shutdown();
|
||||
}
|
||||
|
||||
bool fatInit (uint32_t cacheSize, bool setAsDefaultDevice) {
|
||||
@ -194,4 +193,49 @@ bool fatInitDefault (void) {
|
||||
return fatInit (DEFAULT_CACHE_PAGES, true);
|
||||
}
|
||||
|
||||
void fatGetVolumeLabel (const char* name, char *label) {
|
||||
devoptab_t *devops;
|
||||
PARTITION* partition;
|
||||
char *buf;
|
||||
int namelen,i;
|
||||
|
||||
if(!name || !label)
|
||||
return;
|
||||
|
||||
namelen = strlen(name);
|
||||
buf=(char*)_FAT_mem_allocate(sizeof(char)*namelen+2);
|
||||
strcpy(buf,name);
|
||||
|
||||
if (name[namelen-1] == '/') {
|
||||
buf[namelen-1]='\0';
|
||||
namelen--;
|
||||
}
|
||||
|
||||
if (name[namelen-1] != ':') {
|
||||
buf[namelen]=':';
|
||||
buf[namelen+1]='\0';
|
||||
}
|
||||
|
||||
devops = (devoptab_t*)GetDeviceOpTab(buf);
|
||||
|
||||
for(i=0;buf[i]!='\0' && buf[i]!=':';i++);
|
||||
if (!devops || strncasecmp(buf,devops->name,i)) {
|
||||
free(buf);
|
||||
return;
|
||||
}
|
||||
|
||||
free(buf);
|
||||
|
||||
// Perform a quick check to make sure we're dealing with a libfat controlled device
|
||||
if (devops->open_r != dotab_fat.open_r) {
|
||||
return;
|
||||
}
|
||||
|
||||
partition = (PARTITION*)devops->deviceData;
|
||||
|
||||
if(!_FAT_directory_getVolumeLabel(partition, label)) {
|
||||
strncpy(label,partition->label,11);
|
||||
label[11]='\0';
|
||||
}
|
||||
if(!strncmp(label, "NO NAME", 7)) label[0]='\0';
|
||||
}
|
||||
|
10
source/libfat/libfatversion.h
Normal file
10
source/libfat/libfatversion.h
Normal file
@ -0,0 +1,10 @@
|
||||
#ifndef __LIBFATVERSION_H__
|
||||
#define __LIBFATVERSION_H__
|
||||
|
||||
#define _LIBFAT_MAJOR_ 1
|
||||
#define _LIBFAT_MINOR_ 0
|
||||
#define _LIBFAT_PATCH_ 7
|
||||
|
||||
#define _LIBFAT_STRING "libFAT Release 1.0.7"
|
||||
|
||||
#endif // __LIBFATVERSION_H__
|
@ -26,8 +26,8 @@
|
||||
|
||||
*/
|
||||
|
||||
#ifndef __LOCK_H
|
||||
#define __LOCK_H
|
||||
#ifndef _LOCK_H
|
||||
#define _LOCK_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
|
@ -28,8 +28,8 @@
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef __MEM_ALLOCATE_H_
|
||||
#define __MEM_ALLOCATE_H_
|
||||
#ifndef _MEM_ALLOCATE_H
|
||||
#define _MEM_ALLOCATE_H
|
||||
|
||||
#include <malloc.h>
|
||||
|
||||
@ -38,7 +38,6 @@ static inline void* _FAT_mem_allocate (size_t size) {
|
||||
}
|
||||
|
||||
static inline void* _FAT_mem_align (size_t size) {
|
||||
|
||||
return memalign (32, size);
|
||||
}
|
||||
|
||||
|
@ -205,11 +205,17 @@ PARTITION* _FAT_partition_constructor (const DISC_INTERFACE* disc, uint32_t cach
|
||||
return NULL;
|
||||
}
|
||||
|
||||
_FAT_startSector = startSector;
|
||||
|
||||
// Init the partition lock
|
||||
_FAT_lock_init(&partition->lock);
|
||||
|
||||
_FAT_startSector = startSector;
|
||||
|
||||
if (!memcmp(sectorBuffer + BPB_FAT16_fileSysType, FAT_SIG, sizeof(FAT_SIG)))
|
||||
strncpy(partition->label, (char*)(sectorBuffer + BPB_FAT16_volumeLabel), 11);
|
||||
else
|
||||
strncpy(partition->label, (char*)(sectorBuffer + BPB_FAT32_volumeLabel), 11);
|
||||
partition->label[11] = '\0';
|
||||
|
||||
// Set partition's disc interface
|
||||
partition->disc = disc;
|
||||
|
||||
|
@ -27,11 +27,11 @@
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef __PARTITION_H
|
||||
#define __PARTITION_H
|
||||
#ifndef _PARTITION_H
|
||||
#define _PARTITION_H
|
||||
|
||||
#include "common.h"
|
||||
#include "fat_cache.h"
|
||||
#include "cache.h"
|
||||
#include "lock.h"
|
||||
|
||||
// Device name
|
||||
@ -67,6 +67,7 @@ typedef struct {
|
||||
struct _FILE_STRUCT* firstOpenFile; // The start of a linked list of files
|
||||
mutex_t lock; // A lock for partition operations
|
||||
bool readOnly; // If this is set, then do not try writing to the disc
|
||||
char label[12]; // Volume label
|
||||
} PARTITION;
|
||||
|
||||
/*
|
||||
|
@ -563,7 +563,8 @@ static BOOL valid_acl(const ACL *pacl, unsigned int end)
|
||||
&((const char*)pacl)[offace];
|
||||
acesz = le16_to_cpu(pace->size);
|
||||
if (((offace + acesz) > end)
|
||||
|| !ntfs_valid_sid(&pace->sid))
|
||||
|| !ntfs_valid_sid(&pace->sid)
|
||||
|| ((ntfs_sid_size(&pace->sid) + 8) != (int)acesz))
|
||||
ok = FALSE;
|
||||
offace += acesz;
|
||||
}
|
||||
@ -614,7 +615,6 @@ BOOL ntfs_valid_descr(const char *securattr, unsigned int attrsz)
|
||||
* old revision and no DACL though SE_DACL_PRESENT is set
|
||||
*/
|
||||
if ((attrsz >= sizeof(SECURITY_DESCRIPTOR_RELATIVE))
|
||||
&& (ntfs_attr_size(securattr) <= attrsz)
|
||||
&& (phead->revision == SECURITY_DESCRIPTOR_REVISION)
|
||||
&& (offowner >= sizeof(SECURITY_DESCRIPTOR_RELATIVE))
|
||||
&& ((offowner + 2) < attrsz)
|
||||
@ -622,14 +622,15 @@ BOOL ntfs_valid_descr(const char *securattr, unsigned int attrsz)
|
||||
&& ((offgroup + 2) < attrsz)
|
||||
&& (!offdacl
|
||||
|| ((offdacl >= sizeof(SECURITY_DESCRIPTOR_RELATIVE))
|
||||
&& (offdacl < attrsz)))
|
||||
&& (offdacl+sizeof(ACL) < attrsz)))
|
||||
&& (!offsacl
|
||||
|| ((offsacl >= sizeof(SECURITY_DESCRIPTOR_RELATIVE))
|
||||
&& (offsacl < attrsz)))
|
||||
&& (offsacl+sizeof(ACL) < attrsz)))
|
||||
&& !(phead->owner & const_cpu_to_le32(3))
|
||||
&& !(phead->group & const_cpu_to_le32(3))
|
||||
&& !(phead->dacl & const_cpu_to_le32(3))
|
||||
&& !(phead->sacl & const_cpu_to_le32(3))
|
||||
&& (ntfs_attr_size(securattr) <= attrsz)
|
||||
&& ntfs_valid_sid((const SID*)&securattr[offowner])
|
||||
&& ntfs_valid_sid((const SID*)&securattr[offgroup])
|
||||
/*
|
||||
@ -707,10 +708,12 @@ int ntfs_inherit_acl(const ACL *oldacl, ACL *newacl,
|
||||
if (ntfs_same_sid(&pnewace->sid, ownersid)) {
|
||||
memcpy(&pnewace->sid, usid, usidsz);
|
||||
acesz = usidsz + 8;
|
||||
pnewace->size = cpu_to_le16(acesz);
|
||||
}
|
||||
if (ntfs_same_sid(&pnewace->sid, groupsid)) {
|
||||
memcpy(&pnewace->sid, gsid, gsidsz);
|
||||
acesz = gsidsz + 8;
|
||||
pnewace->size = cpu_to_le16(acesz);
|
||||
}
|
||||
if (pnewace->mask & GENERIC_ALL) {
|
||||
pnewace->mask &= ~GENERIC_ALL;
|
||||
@ -2985,8 +2988,10 @@ static int build_std_permissions(const char *securattr,
|
||||
if (offdacl) {
|
||||
acecnt = le16_to_cpu(pacl->ace_count);
|
||||
offace = offdacl + sizeof(ACL);
|
||||
} else
|
||||
} else {
|
||||
acecnt = 0;
|
||||
offace = 0;
|
||||
}
|
||||
for (nace = 0; nace < acecnt; nace++) {
|
||||
pace = (const ACCESS_ALLOWED_ACE*)&securattr[offace];
|
||||
if (!(pace->flags & INHERIT_ONLY_ACE)) {
|
||||
@ -3255,8 +3260,10 @@ static int build_ownadmin_permissions(const char *securattr,
|
||||
if (offdacl) {
|
||||
acecnt = le16_to_cpu(pacl->ace_count);
|
||||
offace = offdacl + sizeof(ACL);
|
||||
} else
|
||||
} else {
|
||||
acecnt = 0;
|
||||
offace = 0;
|
||||
}
|
||||
firstapply = TRUE;
|
||||
isforeign = 3;
|
||||
for (nace = 0; nace < acecnt; nace++) {
|
||||
|
@ -29,8 +29,6 @@
|
||||
* should be moved to some config file
|
||||
*/
|
||||
|
||||
#define FORCE_FORMAT_v1x 0 /* Insert security data as in NTFS v1.x */
|
||||
#define OWNERFROMACL 1 /* Get the owner from ACL (not Windows owner) */
|
||||
#define BUFSZ 1024 /* buffer size to read mapping file */
|
||||
#define MAPPINGFILE ".NTFS-3G/UserMapping" /* default mapping file */
|
||||
#define LINESZ 120 /* maximum useful size of a mapping line */
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -189,6 +189,7 @@ struct _ntfs_attr {
|
||||
u32 compression_block_size;
|
||||
u8 compression_block_size_bits;
|
||||
u8 compression_block_clusters;
|
||||
s8 unused_runs; /* pre-reserved entries available */
|
||||
};
|
||||
|
||||
/**
|
||||
@ -198,6 +199,9 @@ struct _ntfs_attr {
|
||||
typedef enum {
|
||||
NA_Initialized, /* 1: structure is initialized. */
|
||||
NA_NonResident, /* 1: Attribute is not resident. */
|
||||
NA_BeingNonResident, /* 1: Attribute is being made not resident. */
|
||||
NA_FullyMapped, /* 1: Attribute has been fully mapped */
|
||||
NA_ComprClosing, /* 1: Compressed attribute is being closed */
|
||||
} ntfs_attr_state_bits;
|
||||
|
||||
#define test_nattr_flag(na, flag) test_bit(NA_##flag, (na)->state)
|
||||
@ -212,6 +216,18 @@ typedef enum {
|
||||
#define NAttrSetNonResident(na) set_nattr_flag(na, NonResident)
|
||||
#define NAttrClearNonResident(na) clear_nattr_flag(na, NonResident)
|
||||
|
||||
#define NAttrBeingNonResident(na) test_nattr_flag(na, BeingNonResident)
|
||||
#define NAttrSetBeingNonResident(na) set_nattr_flag(na, BeingNonResident)
|
||||
#define NAttrClearBeingNonResident(na) clear_nattr_flag(na, BeingNonResident)
|
||||
|
||||
#define NAttrFullyMapped(na) test_nattr_flag(na, FullyMapped)
|
||||
#define NAttrSetFullyMapped(na) set_nattr_flag(na, FullyMapped)
|
||||
#define NAttrClearFullyMapped(na) clear_nattr_flag(na, FullyMapped)
|
||||
|
||||
#define NAttrComprClosing(na) test_nattr_flag(na, ComprClosing)
|
||||
#define NAttrSetComprClosing(na) set_nattr_flag(na, ComprClosing)
|
||||
#define NAttrClearComprClosing(na) clear_nattr_flag(na, ComprClosing)
|
||||
|
||||
#define GenNAttrIno(func_name, flag) \
|
||||
extern int NAttr##func_name(ntfs_attr *na); \
|
||||
extern void NAttrSet##func_name(ntfs_attr *na); \
|
||||
@ -288,6 +304,7 @@ extern int ntfs_attr_can_be_resident(const ntfs_volume *vol,
|
||||
const ATTR_TYPES type);
|
||||
int ntfs_attr_make_non_resident(ntfs_attr *na,
|
||||
ntfs_attr_search_ctx *ctx);
|
||||
int ntfs_attr_force_non_resident(ntfs_attr *na);
|
||||
extern int ntfs_make_room_for_attr(MFT_RECORD *m, u8 *pos, u32 size);
|
||||
|
||||
extern int ntfs_resident_attr_record_add(ntfs_inode *ni, ATTR_TYPES type,
|
||||
|
@ -1,378 +1,609 @@
|
||||
/*
|
||||
cache.c
|
||||
The cache is not visible to the user. It should be flushed
|
||||
when any file is closed or changes are made to the filesystem.
|
||||
|
||||
This cache implements a least-used-page replacement policy. This will
|
||||
distribute sectors evenly over the pages, so if less than the maximum
|
||||
pages are used at once, they should all eventually remain in the cache.
|
||||
This also has the benefit of throwing out old sectors, so as not to keep
|
||||
too many stale pages around.
|
||||
|
||||
Copyright (c) 2006 Michael "Chishm" Chisholm
|
||||
Copyright (c) 2009 shareese, rodries
|
||||
Copyright (c) 2010 Dimok
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation and/or
|
||||
other materials provided with the distribution.
|
||||
3. The name of the author may not be used to endorse or promote products derived
|
||||
from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
/**
|
||||
* cache.c : deal with LRU caches
|
||||
*
|
||||
* Copyright (c) 2008-2009 Jean-Pierre Andre
|
||||
*
|
||||
* This program/include file is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as published
|
||||
* by the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program/include file is distributed in the hope that it will be
|
||||
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
||||
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program (in the main directory of the NTFS-3G
|
||||
* distribution in the file COPYING); if not, write to the Free Software
|
||||
* Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_STDLIB_H
|
||||
#include <stdlib.h>
|
||||
#endif
|
||||
#ifdef HAVE_STRING_H
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
#endif
|
||||
|
||||
//#include "common.h"
|
||||
#include "types.h"
|
||||
#include "security.h"
|
||||
#include "cache.h"
|
||||
#include "bit_ops.h"
|
||||
//#include "disc.h"
|
||||
#include "misc.h"
|
||||
#include "logging.h"
|
||||
|
||||
#include "mem_allocate.h"
|
||||
#include <ogc/lwp_watchdog.h>
|
||||
//#include "bit_ops.h"
|
||||
//#include "file_allocation_table.h"
|
||||
/*
|
||||
* General functions to deal with LRU caches
|
||||
*
|
||||
* The cached data have to be organized in a structure in which
|
||||
* the first fields must follow a mandatory pattern and further
|
||||
* fields may contain any fixed size data. They are stored in an
|
||||
* LRU list.
|
||||
*
|
||||
* A compare function must be provided for finding a wanted entry
|
||||
* in the cache. Another function may be provided for invalidating
|
||||
* an entry to facilitate multiple invalidation.
|
||||
*
|
||||
* These functions never return error codes. When there is a
|
||||
* shortage of memory, data is simply not cached.
|
||||
* When there is a hashing bug, hashing is dropped, and sequential
|
||||
* searches are used.
|
||||
*/
|
||||
|
||||
#define CACHE_FREE UINT_MAX
|
||||
/*
|
||||
* Enter a new hash index, after a new record has been inserted
|
||||
*
|
||||
* Do not call when a record has been modified (with no key change)
|
||||
*/
|
||||
|
||||
NTFS_CACHE* _NTFS_cache_constructor (unsigned int numberOfPages, unsigned int sectorsPerPage, const DISC_INTERFACE* discInterface, sec_t endOfPartition) {
|
||||
NTFS_CACHE* cache;
|
||||
unsigned int i;
|
||||
NTFS_CACHE_ENTRY* cacheEntries;
|
||||
|
||||
if(numberOfPages==0 || sectorsPerPage==0) return NULL;
|
||||
|
||||
if (numberOfPages < 4) {
|
||||
numberOfPages = 4;
|
||||
}
|
||||
|
||||
if (sectorsPerPage < 32) {
|
||||
sectorsPerPage = 32;
|
||||
}
|
||||
|
||||
cache = (NTFS_CACHE*) ntfs_alloc (sizeof(NTFS_CACHE));
|
||||
if (cache == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cache->disc = discInterface;
|
||||
cache->endOfPartition = endOfPartition;
|
||||
cache->numberOfPages = numberOfPages;
|
||||
cache->sectorsPerPage = sectorsPerPage;
|
||||
|
||||
|
||||
cacheEntries = (NTFS_CACHE_ENTRY*) ntfs_alloc ( sizeof(NTFS_CACHE_ENTRY) * numberOfPages);
|
||||
if (cacheEntries == NULL) {
|
||||
ntfs_free (cache);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (i = 0; i < numberOfPages; i++) {
|
||||
cacheEntries[i].sector = CACHE_FREE;
|
||||
cacheEntries[i].count = 0;
|
||||
cacheEntries[i].last_access = 0;
|
||||
cacheEntries[i].dirty = false;
|
||||
cacheEntries[i].cache = (uint8_t*) ntfs_align ( sectorsPerPage * BYTES_PER_READ );
|
||||
}
|
||||
|
||||
cache->cacheEntries = cacheEntries;
|
||||
|
||||
return cache;
|
||||
}
|
||||
|
||||
void _NTFS_cache_destructor (NTFS_CACHE* cache) {
|
||||
unsigned int i;
|
||||
|
||||
if(cache==NULL) return;
|
||||
|
||||
// Clear out cache before destroying it
|
||||
_NTFS_cache_flush(cache);
|
||||
|
||||
// Free memory in reverse allocation order
|
||||
for (i = 0; i < cache->numberOfPages; i++) {
|
||||
ntfs_free (cache->cacheEntries[i].cache);
|
||||
}
|
||||
ntfs_free (cache->cacheEntries);
|
||||
ntfs_free (cache);
|
||||
}
|
||||
|
||||
static u32 accessCounter = 0;
|
||||
|
||||
static u32 accessTime(){
|
||||
accessCounter++;
|
||||
return accessCounter;
|
||||
}
|
||||
|
||||
static NTFS_CACHE_ENTRY* _NTFS_cache_getPage(NTFS_CACHE *cache,sec_t sector)
|
||||
static void inserthashindex(struct CACHE_HEADER *cache,
|
||||
struct CACHED_GENERIC *current)
|
||||
{
|
||||
unsigned int i;
|
||||
NTFS_CACHE_ENTRY* cacheEntries = cache->cacheEntries;
|
||||
unsigned int numberOfPages = cache->numberOfPages;
|
||||
unsigned int sectorsPerPage = cache->sectorsPerPage;
|
||||
int h;
|
||||
struct HASH_ENTRY *link;
|
||||
struct HASH_ENTRY *first;
|
||||
|
||||
bool foundFree = false;
|
||||
unsigned int oldUsed = 0;
|
||||
unsigned int oldAccess = UINT_MAX;
|
||||
|
||||
for(i=0;i<numberOfPages;i++) {
|
||||
if(sector>=cacheEntries[i].sector && sector<(cacheEntries[i].sector + cacheEntries[i].count)) {
|
||||
cacheEntries[i].last_access = accessTime();
|
||||
return &(cacheEntries[i]);
|
||||
}
|
||||
|
||||
if(foundFree==false && (cacheEntries[i].sector==CACHE_FREE || cacheEntries[i].last_access<oldAccess)) {
|
||||
if(cacheEntries[i].sector==CACHE_FREE) foundFree = true;
|
||||
oldUsed = i;
|
||||
oldAccess = cacheEntries[i].last_access;
|
||||
}
|
||||
}
|
||||
|
||||
if(foundFree==false && cacheEntries[oldUsed].dirty==true) {
|
||||
if(!cache->disc->writeSectors(cacheEntries[oldUsed].sector,cacheEntries[oldUsed].count,cacheEntries[oldUsed].cache)) return NULL;
|
||||
cacheEntries[oldUsed].dirty = false;
|
||||
}
|
||||
sector = (sector/sectorsPerPage)*sectorsPerPage; // align base sector to page size
|
||||
sec_t next_page = sector + sectorsPerPage;
|
||||
if(next_page > cache->endOfPartition) next_page = cache->endOfPartition;
|
||||
|
||||
if(!cache->disc->readSectors(sector,next_page-sector,cacheEntries[oldUsed].cache)) return NULL;
|
||||
|
||||
cacheEntries[oldUsed].sector = sector;
|
||||
cacheEntries[oldUsed].count = next_page-sector;
|
||||
cacheEntries[oldUsed].last_access = accessTime();
|
||||
|
||||
return &(cacheEntries[oldUsed]);
|
||||
}
|
||||
|
||||
static NTFS_CACHE_ENTRY* _NTFS_cache_findPage(NTFS_CACHE *cache, sec_t sector, sec_t count) {
|
||||
|
||||
unsigned int i;
|
||||
NTFS_CACHE_ENTRY* cacheEntries = cache->cacheEntries;
|
||||
unsigned int numberOfPages = cache->numberOfPages;
|
||||
NTFS_CACHE_ENTRY *entry = NULL;
|
||||
sec_t lowest = UINT_MAX;
|
||||
|
||||
for(i=0;i<numberOfPages;i++) {
|
||||
if (cacheEntries[i].sector != CACHE_FREE) {
|
||||
bool intersect;
|
||||
if (sector > cacheEntries[i].sector) {
|
||||
intersect = sector - cacheEntries[i].sector < cacheEntries[i].count;
|
||||
if (cache->dohash) {
|
||||
h = cache->dohash(current);
|
||||
if ((h >= 0) && (h < cache->max_hash)) {
|
||||
/* get a free link and insert at top of hash list */
|
||||
link = cache->free_hash;
|
||||
if (link) {
|
||||
cache->free_hash = link->next;
|
||||
first = cache->first_hash[h];
|
||||
if (first)
|
||||
link->next = first;
|
||||
else
|
||||
link->next = NULL;
|
||||
link->entry = current;
|
||||
cache->first_hash[h] = link;
|
||||
} else {
|
||||
intersect = cacheEntries[i].sector - sector < count;
|
||||
ntfs_log_error("No more hash entries,"
|
||||
" cache %s hashing dropped\n",
|
||||
cache->name);
|
||||
cache->dohash = (cache_hash)NULL;
|
||||
}
|
||||
|
||||
if ( intersect && (cacheEntries[i].sector < lowest)) {
|
||||
lowest = cacheEntries[i].sector;
|
||||
entry = &cacheEntries[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
bool _NTFS_cache_readSectors(NTFS_CACHE *cache,sec_t sector,sec_t numSectors,void *buffer)
|
||||
{
|
||||
sec_t sec;
|
||||
sec_t secs_to_read;
|
||||
NTFS_CACHE_ENTRY *entry;
|
||||
uint8_t *dest = buffer;
|
||||
|
||||
while(numSectors>0) {
|
||||
entry = _NTFS_cache_getPage(cache,sector);
|
||||
if(entry==NULL) return false;
|
||||
|
||||
sec = sector - entry->sector;
|
||||
secs_to_read = entry->count - sec;
|
||||
if(secs_to_read>numSectors) secs_to_read = numSectors;
|
||||
|
||||
memcpy(dest,entry->cache + (sec*BYTES_PER_READ),(secs_to_read*BYTES_PER_READ));
|
||||
|
||||
dest += (secs_to_read*BYTES_PER_READ);
|
||||
sector += secs_to_read;
|
||||
numSectors -= secs_to_read;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
Reads some data from a cache page, determined by the sector number
|
||||
*/
|
||||
|
||||
bool _NTFS_cache_readPartialSector (NTFS_CACHE* cache, void* buffer, sec_t sector, unsigned int offset, size_t size)
|
||||
{
|
||||
sec_t sec;
|
||||
NTFS_CACHE_ENTRY *entry;
|
||||
|
||||
if (offset + size > BYTES_PER_READ) return false;
|
||||
|
||||
entry = _NTFS_cache_getPage(cache,sector);
|
||||
if(entry==NULL) return false;
|
||||
|
||||
sec = sector - entry->sector;
|
||||
memcpy(buffer,entry->cache + ((sec*BYTES_PER_READ) + offset),size);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool _NTFS_cache_readLittleEndianValue (NTFS_CACHE* cache, uint32_t *value, sec_t sector, unsigned int offset, int num_bytes) {
|
||||
uint8_t buf[4];
|
||||
if (!_NTFS_cache_readPartialSector(cache, buf, sector, offset, num_bytes)) return false;
|
||||
|
||||
switch(num_bytes) {
|
||||
case 1: *value = buf[0]; break;
|
||||
case 2: *value = u8array_to_u16(buf,0); break;
|
||||
case 4: *value = u8array_to_u32(buf,0); break;
|
||||
default: return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
Writes some data to a cache page, making sure it is loaded into memory first.
|
||||
*/
|
||||
|
||||
bool _NTFS_cache_writePartialSector (NTFS_CACHE* cache, const void* buffer, sec_t sector, unsigned int offset, size_t size)
|
||||
{
|
||||
sec_t sec;
|
||||
NTFS_CACHE_ENTRY *entry;
|
||||
|
||||
if (offset + size > BYTES_PER_READ) return false;
|
||||
|
||||
entry = _NTFS_cache_getPage(cache,sector);
|
||||
if(entry==NULL) return false;
|
||||
|
||||
sec = sector - entry->sector;
|
||||
memcpy(entry->cache + ((sec*BYTES_PER_READ) + offset),buffer,size);
|
||||
|
||||
entry->dirty = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool _NTFS_cache_writeLittleEndianValue (NTFS_CACHE* cache, const uint32_t value, sec_t sector, unsigned int offset, int size) {
|
||||
uint8_t buf[4] = {0, 0, 0, 0};
|
||||
|
||||
switch(size) {
|
||||
case 1: buf[0] = value; break;
|
||||
case 2: u16_to_u8array(buf, 0, value); break;
|
||||
case 4: u32_to_u8array(buf, 0, value); break;
|
||||
default: return false;
|
||||
}
|
||||
|
||||
return _NTFS_cache_writePartialSector(cache, buf, sector, offset, size);
|
||||
}
|
||||
|
||||
/*
|
||||
Writes some data to a cache page, zeroing out the page first
|
||||
*/
|
||||
|
||||
bool _NTFS_cache_eraseWritePartialSector (NTFS_CACHE* cache, const void* buffer, sec_t sector, unsigned int offset, size_t size)
|
||||
{
|
||||
sec_t sec;
|
||||
NTFS_CACHE_ENTRY *entry;
|
||||
|
||||
if (offset + size > BYTES_PER_READ) return false;
|
||||
|
||||
entry = _NTFS_cache_getPage(cache,sector);
|
||||
if(entry==NULL) return false;
|
||||
|
||||
sec = sector - entry->sector;
|
||||
memset(entry->cache + (sec*BYTES_PER_READ),0,BYTES_PER_READ);
|
||||
memcpy(entry->cache + ((sec*BYTES_PER_READ) + offset),buffer,size);
|
||||
|
||||
entry->dirty = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool _NTFS_cache_writeSectors (NTFS_CACHE* cache, sec_t sector, sec_t numSectors, const void* buffer)
|
||||
{
|
||||
sec_t sec;
|
||||
sec_t secs_to_write;
|
||||
NTFS_CACHE_ENTRY* entry;
|
||||
const uint8_t *src = buffer;
|
||||
|
||||
while(numSectors>0)
|
||||
{
|
||||
entry = _NTFS_cache_findPage(cache,sector,numSectors);
|
||||
|
||||
if(entry!=NULL) {
|
||||
|
||||
if ( entry->sector > sector) {
|
||||
|
||||
secs_to_write = entry->sector - sector;
|
||||
|
||||
cache->disc->writeSectors(sector,secs_to_write,src);
|
||||
src += (secs_to_write*BYTES_PER_READ);
|
||||
sector += secs_to_write;
|
||||
numSectors -= secs_to_write;
|
||||
}
|
||||
|
||||
sec = sector - entry->sector;
|
||||
secs_to_write = entry->count - sec;
|
||||
|
||||
if(secs_to_write>numSectors) secs_to_write = numSectors;
|
||||
|
||||
memcpy(entry->cache + (sec*BYTES_PER_READ),src,(secs_to_write*BYTES_PER_READ));
|
||||
|
||||
src += (secs_to_write*BYTES_PER_READ);
|
||||
sector += secs_to_write;
|
||||
numSectors -= secs_to_write;
|
||||
|
||||
entry->dirty = true;
|
||||
|
||||
} else {
|
||||
cache->disc->writeSectors(sector,numSectors,src);
|
||||
numSectors=0;
|
||||
ntfs_log_error("Illegal hash value,"
|
||||
" cache %s hashing dropped\n",
|
||||
cache->name);
|
||||
cache->dohash = (cache_hash)NULL;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
Flushes all dirty pages to disc, clearing the dirty flag.
|
||||
* Drop a hash index when a record is about to be deleted
|
||||
*/
|
||||
bool _NTFS_cache_flush (NTFS_CACHE* cache) {
|
||||
unsigned int i;
|
||||
if(cache==NULL) return true;
|
||||
|
||||
for (i = 0; i < cache->numberOfPages; i++) {
|
||||
if (cache->cacheEntries[i].dirty) {
|
||||
if (!cache->disc->writeSectors (cache->cacheEntries[i].sector, cache->cacheEntries[i].count, cache->cacheEntries[i].cache)) {
|
||||
return false;
|
||||
static void drophashindex(struct CACHE_HEADER *cache,
|
||||
const struct CACHED_GENERIC *current, int hash)
|
||||
{
|
||||
struct HASH_ENTRY *link;
|
||||
struct HASH_ENTRY *previous;
|
||||
|
||||
if (cache->dohash) {
|
||||
if ((hash >= 0) && (hash < cache->max_hash)) {
|
||||
/* find the link and unlink */
|
||||
link = cache->first_hash[hash];
|
||||
previous = (struct HASH_ENTRY*)NULL;
|
||||
while (link && (link->entry != current)) {
|
||||
previous = link;
|
||||
link = link->next;
|
||||
}
|
||||
if (link) {
|
||||
if (previous)
|
||||
previous->next = link->next;
|
||||
else
|
||||
cache->first_hash[hash] = link->next;
|
||||
link->next = cache->free_hash;
|
||||
cache->free_hash = link;
|
||||
} else {
|
||||
ntfs_log_error("Bad hash list,"
|
||||
" cache %s hashing dropped\n",
|
||||
cache->name);
|
||||
cache->dohash = (cache_hash)NULL;
|
||||
}
|
||||
} else {
|
||||
ntfs_log_error("Illegal hash value,"
|
||||
" cache %s hashing dropped\n",
|
||||
cache->name);
|
||||
cache->dohash = (cache_hash)NULL;
|
||||
}
|
||||
}
|
||||
cache->cacheEntries[i].dirty = false;
|
||||
}
|
||||
|
||||
return true;
|
||||
/*
|
||||
* Fetch an entry from cache
|
||||
*
|
||||
* returns the cache entry, or NULL if not available
|
||||
* The returned entry may be modified, but not freed
|
||||
*/
|
||||
|
||||
struct CACHED_GENERIC *ntfs_fetch_cache(struct CACHE_HEADER *cache,
|
||||
const struct CACHED_GENERIC *wanted, cache_compare compare)
|
||||
{
|
||||
struct CACHED_GENERIC *current;
|
||||
struct CACHED_GENERIC *previous;
|
||||
struct HASH_ENTRY *link;
|
||||
int h;
|
||||
|
||||
current = (struct CACHED_GENERIC*)NULL;
|
||||
if (cache) {
|
||||
if (cache->dohash) {
|
||||
/*
|
||||
* When possible, use the hash table to
|
||||
* locate the entry if present
|
||||
*/
|
||||
h = cache->dohash(wanted);
|
||||
link = cache->first_hash[h];
|
||||
while (link && compare(link->entry, wanted))
|
||||
link = link->next;
|
||||
if (link)
|
||||
current = link->entry;
|
||||
}
|
||||
if (!cache->dohash) {
|
||||
/*
|
||||
* Search sequentially in LRU list if no hash table
|
||||
* or if hashing has just failed
|
||||
*/
|
||||
current = cache->most_recent_entry;
|
||||
while (current
|
||||
&& compare(current, wanted)) {
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
if (current) {
|
||||
previous = current->previous;
|
||||
cache->hits++;
|
||||
if (previous) {
|
||||
/*
|
||||
* found and not at head of list, unlink from current
|
||||
* position and relink as head of list
|
||||
*/
|
||||
previous->next = current->next;
|
||||
if (current->next)
|
||||
current->next->previous
|
||||
= current->previous;
|
||||
else
|
||||
cache->oldest_entry
|
||||
= current->previous;
|
||||
current->next = cache->most_recent_entry;
|
||||
current->previous
|
||||
= (struct CACHED_GENERIC*)NULL;
|
||||
cache->most_recent_entry->previous = current;
|
||||
cache->most_recent_entry = current;
|
||||
}
|
||||
}
|
||||
cache->reads++;
|
||||
}
|
||||
return (current);
|
||||
}
|
||||
|
||||
void _NTFS_cache_invalidate (NTFS_CACHE* cache) {
|
||||
unsigned int i;
|
||||
if(cache==NULL)
|
||||
return;
|
||||
/*
|
||||
* Enter an inode number into cache
|
||||
* returns the cache entry or NULL if not possible
|
||||
*/
|
||||
|
||||
_NTFS_cache_flush(cache);
|
||||
for (i = 0; i < cache->numberOfPages; i++) {
|
||||
cache->cacheEntries[i].sector = CACHE_FREE;
|
||||
cache->cacheEntries[i].last_access = 0;
|
||||
cache->cacheEntries[i].count = 0;
|
||||
cache->cacheEntries[i].dirty = false;
|
||||
struct CACHED_GENERIC *ntfs_enter_cache(struct CACHE_HEADER *cache,
|
||||
const struct CACHED_GENERIC *item,
|
||||
cache_compare compare)
|
||||
{
|
||||
struct CACHED_GENERIC *current;
|
||||
struct CACHED_GENERIC *before;
|
||||
struct HASH_ENTRY *link;
|
||||
int h;
|
||||
|
||||
current = (struct CACHED_GENERIC*)NULL;
|
||||
if (cache) {
|
||||
if (cache->dohash) {
|
||||
/*
|
||||
* When possible, use the hash table to
|
||||
* find out whether the entry if present
|
||||
*/
|
||||
h = cache->dohash(item);
|
||||
link = cache->first_hash[h];
|
||||
while (link && compare(link->entry, item))
|
||||
link = link->next;
|
||||
if (link) {
|
||||
current = link->entry;
|
||||
}
|
||||
}
|
||||
if (!cache->dohash) {
|
||||
/*
|
||||
* Search sequentially in LRU list to locate the end,
|
||||
* and find out whether the entry is already in list
|
||||
* As we normally go to the end, no statistics is
|
||||
* kept.
|
||||
*/
|
||||
current = cache->most_recent_entry;
|
||||
while (current
|
||||
&& compare(current, item)) {
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
|
||||
if (!current) {
|
||||
/*
|
||||
* Not in list, get a free entry or reuse the
|
||||
* last entry, and relink as head of list
|
||||
* Note : we assume at least three entries, so
|
||||
* before, previous and first are different when
|
||||
* an entry is reused.
|
||||
*/
|
||||
|
||||
if (cache->free_entry) {
|
||||
current = cache->free_entry;
|
||||
cache->free_entry = cache->free_entry->next;
|
||||
if (item->varsize) {
|
||||
current->variable = ntfs_malloc(
|
||||
item->varsize);
|
||||
} else
|
||||
current->variable = (void*)NULL;
|
||||
current->varsize = item->varsize;
|
||||
if (!cache->oldest_entry)
|
||||
cache->oldest_entry = current;
|
||||
} else {
|
||||
/* reusing the oldest entry */
|
||||
current = cache->oldest_entry;
|
||||
before = current->previous;
|
||||
before->next = (struct CACHED_GENERIC*)NULL;
|
||||
if (cache->dohash)
|
||||
drophashindex(cache,current,
|
||||
cache->dohash(current));
|
||||
if (cache->dofree)
|
||||
cache->dofree(current);
|
||||
cache->oldest_entry = current->previous;
|
||||
if (item->varsize) {
|
||||
if (current->varsize)
|
||||
current->variable = realloc(
|
||||
current->variable,
|
||||
item->varsize);
|
||||
else
|
||||
current->variable = ntfs_malloc(
|
||||
item->varsize);
|
||||
} else {
|
||||
if (current->varsize)
|
||||
free(current->variable);
|
||||
current->variable = (void*)NULL;
|
||||
}
|
||||
current->varsize = item->varsize;
|
||||
}
|
||||
current->next = cache->most_recent_entry;
|
||||
current->previous = (struct CACHED_GENERIC*)NULL;
|
||||
if (cache->most_recent_entry)
|
||||
cache->most_recent_entry->previous = current;
|
||||
cache->most_recent_entry = current;
|
||||
memcpy(current->fixed, item->fixed, cache->fixed_size);
|
||||
if (item->varsize) {
|
||||
if (current->variable) {
|
||||
memcpy(current->variable,
|
||||
item->variable, item->varsize);
|
||||
} else {
|
||||
/*
|
||||
* no more memory for variable part
|
||||
* recycle entry in free list
|
||||
* not an error, just uncacheable
|
||||
*/
|
||||
cache->most_recent_entry = current->next;
|
||||
current->next = cache->free_entry;
|
||||
cache->free_entry = current;
|
||||
current = (struct CACHED_GENERIC*)NULL;
|
||||
}
|
||||
} else {
|
||||
current->variable = (void*)NULL;
|
||||
current->varsize = 0;
|
||||
}
|
||||
if (cache->dohash && current)
|
||||
inserthashindex(cache,current);
|
||||
}
|
||||
cache->writes++;
|
||||
}
|
||||
return (current);
|
||||
}
|
||||
|
||||
/*
|
||||
* Invalidate a cache entry
|
||||
* The entry is moved to the free entry list
|
||||
* A specific function may be called for entry deletion
|
||||
*/
|
||||
|
||||
static void do_invalidate(struct CACHE_HEADER *cache,
|
||||
struct CACHED_GENERIC *current, int flags)
|
||||
{
|
||||
struct CACHED_GENERIC *previous;
|
||||
|
||||
previous = current->previous;
|
||||
if ((flags & CACHE_FREE) && cache->dofree)
|
||||
cache->dofree(current);
|
||||
/*
|
||||
* Relink into free list
|
||||
*/
|
||||
if (current->next)
|
||||
current->next->previous = current->previous;
|
||||
else
|
||||
cache->oldest_entry = current->previous;
|
||||
if (previous)
|
||||
previous->next = current->next;
|
||||
else
|
||||
cache->most_recent_entry = current->next;
|
||||
current->next = cache->free_entry;
|
||||
cache->free_entry = current;
|
||||
if (current->variable)
|
||||
free(current->variable);
|
||||
current->varsize = 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Invalidate entries in cache
|
||||
*
|
||||
* Several entries may have to be invalidated (at least for inodes
|
||||
* associated to directories which have been renamed), a different
|
||||
* compare function may be provided to select entries to invalidate
|
||||
*
|
||||
* Returns the number of deleted entries, this can be used by
|
||||
* the caller to signal a cache corruption if the entry was
|
||||
* supposed to be found.
|
||||
*/
|
||||
|
||||
int ntfs_invalidate_cache(struct CACHE_HEADER *cache,
|
||||
const struct CACHED_GENERIC *item, cache_compare compare,
|
||||
int flags)
|
||||
{
|
||||
struct CACHED_GENERIC *current;
|
||||
struct CACHED_GENERIC *previous;
|
||||
struct CACHED_GENERIC *next;
|
||||
struct HASH_ENTRY *link;
|
||||
int count;
|
||||
int h;
|
||||
|
||||
current = (struct CACHED_GENERIC*)NULL;
|
||||
count = 0;
|
||||
if (cache) {
|
||||
if (!(flags & CACHE_NOHASH) && cache->dohash) {
|
||||
/*
|
||||
* When possible, use the hash table to
|
||||
* find out whether the entry if present
|
||||
*/
|
||||
h = cache->dohash(item);
|
||||
link = cache->first_hash[h];
|
||||
while (link) {
|
||||
if (compare(link->entry, item))
|
||||
link = link->next;
|
||||
else {
|
||||
current = link->entry;
|
||||
link = link->next;
|
||||
if (current) {
|
||||
drophashindex(cache,current,h);
|
||||
do_invalidate(cache,
|
||||
current,flags);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if ((flags & CACHE_NOHASH) || !cache->dohash) {
|
||||
/*
|
||||
* Search sequentially in LRU list
|
||||
*/
|
||||
current = cache->most_recent_entry;
|
||||
previous = (struct CACHED_GENERIC*)NULL;
|
||||
while (current) {
|
||||
if (!compare(current, item)) {
|
||||
next = current->next;
|
||||
if (cache->dohash)
|
||||
drophashindex(cache,current,
|
||||
cache->dohash(current));
|
||||
do_invalidate(cache,current,flags);
|
||||
current = next;
|
||||
count++;
|
||||
} else {
|
||||
previous = current;
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return (count);
|
||||
}
|
||||
|
||||
int ntfs_remove_cache(struct CACHE_HEADER *cache,
|
||||
struct CACHED_GENERIC *item, int flags)
|
||||
{
|
||||
int count;
|
||||
|
||||
count = 0;
|
||||
if (cache) {
|
||||
if (cache->dohash)
|
||||
drophashindex(cache,item,cache->dohash(item));
|
||||
do_invalidate(cache,item,flags);
|
||||
count++;
|
||||
}
|
||||
return (count);
|
||||
}
|
||||
|
||||
/*
|
||||
* Free memory allocated to a cache
|
||||
*/
|
||||
|
||||
static void ntfs_free_cache(struct CACHE_HEADER *cache)
|
||||
{
|
||||
struct CACHED_GENERIC *entry;
|
||||
|
||||
if (cache) {
|
||||
for (entry=cache->most_recent_entry; entry; entry=entry->next) {
|
||||
if (cache->dofree)
|
||||
cache->dofree(entry);
|
||||
if (entry->variable)
|
||||
free(entry->variable);
|
||||
}
|
||||
free(cache);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Create a cache
|
||||
*
|
||||
* Returns the cache header, or NULL if the cache could not be created
|
||||
*/
|
||||
|
||||
static struct CACHE_HEADER *ntfs_create_cache(const char *name,
|
||||
cache_free dofree, cache_hash dohash,
|
||||
int full_item_size,
|
||||
int item_count, int max_hash)
|
||||
{
|
||||
struct CACHE_HEADER *cache;
|
||||
struct CACHED_GENERIC *pc;
|
||||
struct CACHED_GENERIC *qc;
|
||||
struct HASH_ENTRY *ph;
|
||||
struct HASH_ENTRY *qh;
|
||||
struct HASH_ENTRY **px;
|
||||
size_t size;
|
||||
int i;
|
||||
|
||||
size = sizeof(struct CACHE_HEADER) + item_count*full_item_size;
|
||||
if (max_hash)
|
||||
size += item_count*sizeof(struct HASH_ENTRY)
|
||||
+ max_hash*sizeof(struct HASH_ENTRY*);
|
||||
cache = (struct CACHE_HEADER*)ntfs_malloc(size);
|
||||
if (cache) {
|
||||
/* header */
|
||||
cache->name = name;
|
||||
cache->dofree = dofree;
|
||||
if (dohash && max_hash) {
|
||||
cache->dohash = dohash;
|
||||
cache->max_hash = max_hash;
|
||||
} else {
|
||||
cache->dohash = (cache_hash)NULL;
|
||||
cache->max_hash = 0;
|
||||
}
|
||||
cache->fixed_size = full_item_size - sizeof(struct CACHED_GENERIC);
|
||||
cache->reads = 0;
|
||||
cache->writes = 0;
|
||||
cache->hits = 0;
|
||||
/* chain the data entries, and mark an invalid entry */
|
||||
cache->most_recent_entry = (struct CACHED_GENERIC*)NULL;
|
||||
cache->oldest_entry = (struct CACHED_GENERIC*)NULL;
|
||||
cache->free_entry = &cache->entry[0];
|
||||
pc = &cache->entry[0];
|
||||
for (i=0; i<(item_count - 1); i++) {
|
||||
qc = (struct CACHED_GENERIC*)((char*)pc
|
||||
+ full_item_size);
|
||||
pc->next = qc;
|
||||
pc->variable = (void*)NULL;
|
||||
pc->varsize = 0;
|
||||
pc = qc;
|
||||
}
|
||||
/* special for the last entry */
|
||||
pc->next = (struct CACHED_GENERIC*)NULL;
|
||||
pc->variable = (void*)NULL;
|
||||
pc->varsize = 0;
|
||||
|
||||
if (max_hash) {
|
||||
/* chain the hash entries */
|
||||
ph = (struct HASH_ENTRY*)(((char*)pc) + full_item_size);
|
||||
cache->free_hash = ph;
|
||||
for (i=0; i<(item_count - 1); i++) {
|
||||
qh = &ph[1];
|
||||
ph->next = qh;
|
||||
ph = qh;
|
||||
}
|
||||
/* special for the last entry */
|
||||
if (item_count) {
|
||||
ph->next = (struct HASH_ENTRY*)NULL;
|
||||
}
|
||||
/* create and initialize the hash indexes */
|
||||
px = (struct HASH_ENTRY**)&ph[1];
|
||||
cache->first_hash = px;
|
||||
for (i=0; i<max_hash; i++)
|
||||
px[i] = (struct HASH_ENTRY*)NULL;
|
||||
} else {
|
||||
cache->free_hash = (struct HASH_ENTRY*)NULL;
|
||||
cache->first_hash = (struct HASH_ENTRY**)NULL;
|
||||
}
|
||||
}
|
||||
return (cache);
|
||||
}
|
||||
|
||||
/*
|
||||
* Create all LRU caches
|
||||
*
|
||||
* No error return, if creation is not possible, cacheing will
|
||||
* just be not available
|
||||
*/
|
||||
|
||||
void ntfs_create_lru_caches(ntfs_volume *vol)
|
||||
{
|
||||
#if CACHE_INODE_SIZE
|
||||
/* inode cache */
|
||||
vol->xinode_cache = ntfs_create_cache("inode",(cache_free)NULL,
|
||||
ntfs_dir_inode_hash, sizeof(struct CACHED_INODE),
|
||||
CACHE_INODE_SIZE, 2*CACHE_INODE_SIZE);
|
||||
#endif
|
||||
#if CACHE_NIDATA_SIZE
|
||||
/* idata cache */
|
||||
vol->nidata_cache = ntfs_create_cache("nidata",
|
||||
ntfs_inode_nidata_free, ntfs_inode_nidata_hash,
|
||||
sizeof(struct CACHED_NIDATA),
|
||||
CACHE_NIDATA_SIZE, 2*CACHE_NIDATA_SIZE);
|
||||
#endif
|
||||
#if CACHE_LOOKUP_SIZE
|
||||
/* lookup cache */
|
||||
vol->lookup_cache = ntfs_create_cache("lookup",
|
||||
(cache_free)NULL, ntfs_dir_lookup_hash,
|
||||
sizeof(struct CACHED_LOOKUP),
|
||||
CACHE_LOOKUP_SIZE, 2*CACHE_LOOKUP_SIZE);
|
||||
#endif
|
||||
vol->securid_cache = ntfs_create_cache("securid",(cache_free)NULL,
|
||||
(cache_hash)NULL,sizeof(struct CACHED_SECURID), CACHE_SECURID_SIZE, 0);
|
||||
#if CACHE_LEGACY_SIZE
|
||||
vol->legacy_cache = ntfs_create_cache("legacy",(cache_free)NULL,
|
||||
(cache_hash)NULL, sizeof(struct CACHED_PERMISSIONS_LEGACY), CACHE_LEGACY_SIZE, 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
* Free all LRU caches
|
||||
*/
|
||||
|
||||
void ntfs_free_lru_caches(ntfs_volume *vol)
|
||||
{
|
||||
#if CACHE_INODE_SIZE
|
||||
ntfs_free_cache(vol->xinode_cache);
|
||||
#endif
|
||||
#if CACHE_NIDATA_SIZE
|
||||
ntfs_free_cache(vol->nidata_cache);
|
||||
#endif
|
||||
#if CACHE_LOOKUP_SIZE
|
||||
ntfs_free_cache(vol->lookup_cache);
|
||||
#endif
|
||||
ntfs_free_cache(vol->securid_cache);
|
||||
#if CACHE_LEGACY_SIZE
|
||||
ntfs_free_cache(vol->legacy_cache);
|
||||
#endif
|
||||
}
|
||||
|
@ -1,136 +1,119 @@
|
||||
/*
|
||||
NTFS_CACHE.h
|
||||
The NTFS_CACHE is not visible to the user. It should be flushed
|
||||
when any file is closed or changes are made to the filesystem.
|
||||
|
||||
This NTFS_CACHE implements a least-used-page replacement policy. This will
|
||||
distribute sectors evenly over the pages, so if less than the maximum
|
||||
pages are used at once, they should all eventually remain in the NTFS_CACHE.
|
||||
This also has the benefit of throwing out old sectors, so as not to keep
|
||||
too many stale pages around.
|
||||
|
||||
Copyright (c) 2006 Michael "Chishm" Chisholm
|
||||
Copyright (c) 2009 shareese, rodries
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation and/or
|
||||
other materials provided with the distribution.
|
||||
3. The name of the author may not be used to endorse or promote products derived
|
||||
from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
* cache.h : deal with indexed LRU caches
|
||||
*
|
||||
* Copyright (c) 2008-2009 Jean-Pierre Andre
|
||||
*
|
||||
* This program/include file is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as published
|
||||
* by the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program/include file is distributed in the hope that it will be
|
||||
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
||||
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program (in the main directory of the NTFS-3G
|
||||
* distribution in the file COPYING); if not, write to the Free Software
|
||||
* Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#ifndef _CACHE_H
|
||||
#define _CACHE_H
|
||||
#ifndef _NTFS_CACHE_H_
|
||||
#define _NTFS_CACHE_H_
|
||||
|
||||
//#include "common.h"
|
||||
//#include "disc.h"
|
||||
#include "volume.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <gctypes.h>
|
||||
#include <ogc/disc_io.h>
|
||||
#include <gccore.h>
|
||||
struct CACHED_GENERIC {
|
||||
struct CACHED_GENERIC *next;
|
||||
struct CACHED_GENERIC *previous;
|
||||
void *variable;
|
||||
size_t varsize;
|
||||
union {
|
||||
/* force alignment for pointers and u64 */
|
||||
u64 u64align;
|
||||
void *ptralign;
|
||||
} fixed[0];
|
||||
} ;
|
||||
|
||||
#define BYTES_PER_READ 512
|
||||
struct CACHED_INODE {
|
||||
struct CACHED_INODE *next;
|
||||
struct CACHED_INODE *previous;
|
||||
const char *pathname;
|
||||
size_t varsize;
|
||||
/* above fields must match "struct CACHED_GENERIC" */
|
||||
u64 inum;
|
||||
} ;
|
||||
|
||||
typedef struct {
|
||||
sec_t sector;
|
||||
unsigned int count;
|
||||
u64 last_access;
|
||||
bool dirty;
|
||||
u8* cache;
|
||||
} NTFS_CACHE_ENTRY;
|
||||
struct CACHED_NIDATA {
|
||||
struct CACHED_NIDATA *next;
|
||||
struct CACHED_NIDATA *previous;
|
||||
const char *pathname; /* not used */
|
||||
size_t varsize; /* not used */
|
||||
/* above fields must match "struct CACHED_GENERIC" */
|
||||
u64 inum;
|
||||
ntfs_inode *ni;
|
||||
} ;
|
||||
|
||||
typedef struct {
|
||||
const DISC_INTERFACE* disc;
|
||||
sec_t endOfPartition;
|
||||
unsigned int numberOfPages;
|
||||
unsigned int sectorsPerPage;
|
||||
NTFS_CACHE_ENTRY* cacheEntries;
|
||||
} NTFS_CACHE;
|
||||
struct CACHED_LOOKUP {
|
||||
struct CACHED_LOOKUP *next;
|
||||
struct CACHED_LOOKUP *previous;
|
||||
const char *name;
|
||||
size_t namesize;
|
||||
/* above fields must match "struct CACHED_GENERIC" */
|
||||
u64 parent;
|
||||
u64 inum;
|
||||
} ;
|
||||
|
||||
/*
|
||||
Read data from a sector in the NTFS_CACHE
|
||||
If the sector is not in the NTFS_CACHE, it will be swapped in
|
||||
offset is the position to start reading from
|
||||
size is the amount of data to read
|
||||
Precondition: offset + size <= BYTES_PER_READ
|
||||
*/
|
||||
//bool _NTFS_cache_readPartialSector (NTFS_CACHE* NTFS_CACHE, void* buffer, sec_t sector, unsigned int offset, size_t size);
|
||||
enum {
|
||||
CACHE_FREE = 1,
|
||||
CACHE_NOHASH = 2
|
||||
} ;
|
||||
|
||||
//bool _NTFS_cache_readLittleEndianValue (NTFS_CACHE* NTFS_CACHE, uint32_t *value, sec_t sector, unsigned int offset, int num_bytes);
|
||||
typedef int (*cache_compare)(const struct CACHED_GENERIC *cached,
|
||||
const struct CACHED_GENERIC *item);
|
||||
typedef void (*cache_free)(const struct CACHED_GENERIC *cached);
|
||||
typedef int (*cache_hash)(const struct CACHED_GENERIC *cached);
|
||||
|
||||
/*
|
||||
Write data to a sector in the NTFS_CACHE
|
||||
If the sector is not in the NTFS_CACHE, it will be swapped in.
|
||||
When the sector is swapped out, the data will be written to the disc
|
||||
offset is the position to start writing to
|
||||
size is the amount of data to write
|
||||
Precondition: offset + size <= BYTES_PER_READ
|
||||
*/
|
||||
//bool _NTFS_cache_writePartialSector (NTFS_CACHE* NTFS_CACHE, const void* buffer, sec_t sector, unsigned int offset, size_t size);
|
||||
struct HASH_ENTRY {
|
||||
struct HASH_ENTRY *next;
|
||||
struct CACHED_GENERIC *entry;
|
||||
} ;
|
||||
|
||||
//bool _NTFS_cache_writeLittleEndianValue (NTFS_CACHE* NTFS_CACHE, const uint32_t value, sec_t sector, unsigned int offset, int num_bytes);
|
||||
struct CACHE_HEADER {
|
||||
const char *name;
|
||||
struct CACHED_GENERIC *most_recent_entry;
|
||||
struct CACHED_GENERIC *oldest_entry;
|
||||
struct CACHED_GENERIC *free_entry;
|
||||
struct HASH_ENTRY *free_hash;
|
||||
struct HASH_ENTRY **first_hash;
|
||||
cache_free dofree;
|
||||
cache_hash dohash;
|
||||
unsigned long reads;
|
||||
unsigned long writes;
|
||||
unsigned long hits;
|
||||
int fixed_size;
|
||||
int max_hash;
|
||||
struct CACHED_GENERIC entry[0];
|
||||
} ;
|
||||
|
||||
/*
|
||||
Write data to a sector in the NTFS_CACHE, zeroing the sector first
|
||||
If the sector is not in the NTFS_CACHE, it will be swapped in.
|
||||
When the sector is swapped out, the data will be written to the disc
|
||||
offset is the position to start writing to
|
||||
size is the amount of data to write
|
||||
Precondition: offset + size <= BYTES_PER_READ
|
||||
*/
|
||||
//bool _NTFS_cache_eraseWritePartialSector (NTFS_CACHE* NTFS_CACHE, const void* buffer, sec_t sector, unsigned int offset, size_t size);
|
||||
/* cast to generic, avoiding gcc warnings */
|
||||
#define GENERIC(pstr) ((const struct CACHED_GENERIC*)(const void*)(pstr))
|
||||
|
||||
/*
|
||||
Read several sectors from the NTFS_CACHE
|
||||
*/
|
||||
bool _NTFS_cache_readSectors (NTFS_CACHE* NTFS_CACHE, sec_t sector, sec_t numSectors, void* buffer);
|
||||
struct CACHED_GENERIC *ntfs_fetch_cache(struct CACHE_HEADER *cache,
|
||||
const struct CACHED_GENERIC *wanted,
|
||||
cache_compare compare);
|
||||
struct CACHED_GENERIC *ntfs_enter_cache(struct CACHE_HEADER *cache,
|
||||
const struct CACHED_GENERIC *item,
|
||||
cache_compare compare);
|
||||
int ntfs_invalidate_cache(struct CACHE_HEADER *cache,
|
||||
const struct CACHED_GENERIC *item,
|
||||
cache_compare compare, int flags);
|
||||
int ntfs_remove_cache(struct CACHE_HEADER *cache,
|
||||
struct CACHED_GENERIC *item, int flags);
|
||||
|
||||
/*
|
||||
Read a full sector from the NTFS_CACHE
|
||||
*/
|
||||
//static inline bool _NTFS_cache_readSector (NTFS_CACHE* NTFS_CACHE, void* buffer, sec_t sector) {
|
||||
// return _NTFS_cache_readPartialSector (NTFS_CACHE, buffer, sector, 0, BYTES_PER_READ);
|
||||
//}
|
||||
void ntfs_create_lru_caches(ntfs_volume *vol);
|
||||
void ntfs_free_lru_caches(ntfs_volume *vol);
|
||||
|
||||
/*
|
||||
Write a full sector to the NTFS_CACHE
|
||||
*/
|
||||
//static inline bool _NTFS_cache_writeSector (NTFS_CACHE* NTFS_CACHE, const void* buffer, sec_t sector) {
|
||||
// return _NTFS_cache_writePartialSector (NTFS_CACHE, buffer, sector, 0, BYTES_PER_READ);
|
||||
//}
|
||||
|
||||
bool _NTFS_cache_writeSectors (NTFS_CACHE* NTFS_CACHE, sec_t sector, sec_t numSectors, const void* buffer);
|
||||
|
||||
/*
|
||||
Write any dirty sectors back to disc and clear out the contents of the NTFS_CACHE
|
||||
*/
|
||||
bool _NTFS_cache_flush (NTFS_CACHE* NTFS_CACHE);
|
||||
|
||||
/*
|
||||
Clear out the contents of the NTFS_CACHE without writing any dirty sectors first
|
||||
*/
|
||||
void _NTFS_cache_invalidate (NTFS_CACHE* NTFS_CACHE);
|
||||
|
||||
NTFS_CACHE* _NTFS_cache_constructor (unsigned int numberOfPages, unsigned int sectorsPerPage, const DISC_INTERFACE* discInterface, sec_t endOfPartition);
|
||||
|
||||
void _NTFS_cache_destructor (NTFS_CACHE* NTFS_CACHE);
|
||||
|
||||
#endif // _CACHE_H
|
||||
#endif /* _NTFS_CACHE_H_ */
|
||||
|
||||
|
374
source/libntfs/cache2.c
Normal file
374
source/libntfs/cache2.c
Normal file
@ -0,0 +1,374 @@
|
||||
/*
|
||||
cache.c
|
||||
The cache is not visible to the user. It should be flushed
|
||||
when any file is closed or changes are made to the filesystem.
|
||||
|
||||
This cache implements a least-used-page replacement policy. This will
|
||||
distribute sectors evenly over the pages, so if less than the maximum
|
||||
pages are used at once, they should all eventually remain in the cache.
|
||||
This also has the benefit of throwing out old sectors, so as not to keep
|
||||
too many stale pages around.
|
||||
|
||||
Copyright (c) 2006 Michael "Chishm" Chisholm
|
||||
Copyright (c) 2009 shareese, rodries
|
||||
Copyright (c) 2010 Dimok
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation and/or
|
||||
other materials provided with the distribution.
|
||||
3. The name of the author may not be used to endorse or promote products derived
|
||||
from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <ogc/lwp_watchdog.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "cache2.h"
|
||||
#include "bit_ops.h"
|
||||
#include "mem_allocate.h"
|
||||
|
||||
#define CACHE_FREE UINT_MAX
|
||||
|
||||
NTFS_CACHE* _NTFS_cache_constructor (unsigned int numberOfPages, unsigned int sectorsPerPage, const DISC_INTERFACE* discInterface, sec_t endOfPartition, sec_t sectorSize) {
|
||||
NTFS_CACHE* cache;
|
||||
unsigned int i;
|
||||
NTFS_CACHE_ENTRY* cacheEntries;
|
||||
|
||||
if(numberOfPages==0 || sectorsPerPage==0) return NULL;
|
||||
|
||||
if (numberOfPages < 4) {
|
||||
numberOfPages = 4;
|
||||
}
|
||||
|
||||
if (sectorsPerPage < 32) {
|
||||
sectorsPerPage = 32;
|
||||
}
|
||||
|
||||
cache = (NTFS_CACHE*) ntfs_alloc (sizeof(NTFS_CACHE));
|
||||
if (cache == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cache->disc = discInterface;
|
||||
cache->endOfPartition = endOfPartition;
|
||||
cache->numberOfPages = numberOfPages;
|
||||
cache->sectorsPerPage = sectorsPerPage;
|
||||
cache->sectorSize = sectorSize;
|
||||
|
||||
|
||||
cacheEntries = (NTFS_CACHE_ENTRY*) ntfs_alloc ( sizeof(NTFS_CACHE_ENTRY) * numberOfPages);
|
||||
if (cacheEntries == NULL) {
|
||||
ntfs_free (cache);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (i = 0; i < numberOfPages; i++) {
|
||||
cacheEntries[i].sector = CACHE_FREE;
|
||||
cacheEntries[i].count = 0;
|
||||
cacheEntries[i].last_access = 0;
|
||||
cacheEntries[i].dirty = false;
|
||||
cacheEntries[i].cache = (uint8_t*) ntfs_align ( sectorsPerPage * cache->sectorSize );
|
||||
}
|
||||
|
||||
cache->cacheEntries = cacheEntries;
|
||||
|
||||
return cache;
|
||||
}
|
||||
|
||||
void _NTFS_cache_destructor (NTFS_CACHE* cache) {
|
||||
unsigned int i;
|
||||
|
||||
if(cache==NULL) return;
|
||||
|
||||
// Clear out cache before destroying it
|
||||
_NTFS_cache_flush(cache);
|
||||
|
||||
// Free memory in reverse allocation order
|
||||
for (i = 0; i < cache->numberOfPages; i++) {
|
||||
ntfs_free (cache->cacheEntries[i].cache);
|
||||
}
|
||||
ntfs_free (cache->cacheEntries);
|
||||
ntfs_free (cache);
|
||||
}
|
||||
|
||||
static u32 accessCounter = 0;
|
||||
|
||||
static u32 accessTime(){
|
||||
accessCounter++;
|
||||
return accessCounter;
|
||||
}
|
||||
|
||||
static NTFS_CACHE_ENTRY* _NTFS_cache_getPage(NTFS_CACHE *cache,sec_t sector)
|
||||
{
|
||||
unsigned int i;
|
||||
NTFS_CACHE_ENTRY* cacheEntries = cache->cacheEntries;
|
||||
unsigned int numberOfPages = cache->numberOfPages;
|
||||
unsigned int sectorsPerPage = cache->sectorsPerPage;
|
||||
|
||||
bool foundFree = false;
|
||||
unsigned int oldUsed = 0;
|
||||
unsigned int oldAccess = UINT_MAX;
|
||||
|
||||
for(i=0;i<numberOfPages;i++) {
|
||||
if(sector>=cacheEntries[i].sector && sector<(cacheEntries[i].sector + cacheEntries[i].count)) {
|
||||
cacheEntries[i].last_access = accessTime();
|
||||
return &(cacheEntries[i]);
|
||||
}
|
||||
|
||||
if(foundFree==false && (cacheEntries[i].sector==CACHE_FREE || cacheEntries[i].last_access<oldAccess)) {
|
||||
if(cacheEntries[i].sector==CACHE_FREE) foundFree = true;
|
||||
oldUsed = i;
|
||||
oldAccess = cacheEntries[i].last_access;
|
||||
}
|
||||
}
|
||||
|
||||
if(foundFree==false && cacheEntries[oldUsed].dirty==true) {
|
||||
if(!cache->disc->writeSectors(cacheEntries[oldUsed].sector,cacheEntries[oldUsed].count,cacheEntries[oldUsed].cache)) return NULL;
|
||||
cacheEntries[oldUsed].dirty = false;
|
||||
}
|
||||
sector = (sector/sectorsPerPage)*sectorsPerPage; // align base sector to page size
|
||||
sec_t next_page = sector + sectorsPerPage;
|
||||
if(next_page > cache->endOfPartition) next_page = cache->endOfPartition;
|
||||
|
||||
if(!cache->disc->readSectors(sector,next_page-sector,cacheEntries[oldUsed].cache)) return NULL;
|
||||
|
||||
cacheEntries[oldUsed].sector = sector;
|
||||
cacheEntries[oldUsed].count = next_page-sector;
|
||||
cacheEntries[oldUsed].last_access = accessTime();
|
||||
|
||||
return &(cacheEntries[oldUsed]);
|
||||
}
|
||||
|
||||
static NTFS_CACHE_ENTRY* _NTFS_cache_findPage(NTFS_CACHE *cache, sec_t sector, sec_t count) {
|
||||
|
||||
unsigned int i;
|
||||
NTFS_CACHE_ENTRY* cacheEntries = cache->cacheEntries;
|
||||
unsigned int numberOfPages = cache->numberOfPages;
|
||||
NTFS_CACHE_ENTRY *entry = NULL;
|
||||
sec_t lowest = UINT_MAX;
|
||||
|
||||
for(i=0;i<numberOfPages;i++) {
|
||||
if (cacheEntries[i].sector != CACHE_FREE) {
|
||||
bool intersect;
|
||||
if (sector > cacheEntries[i].sector) {
|
||||
intersect = sector - cacheEntries[i].sector < cacheEntries[i].count;
|
||||
} else {
|
||||
intersect = cacheEntries[i].sector - sector < count;
|
||||
}
|
||||
|
||||
if ( intersect && (cacheEntries[i].sector < lowest)) {
|
||||
lowest = cacheEntries[i].sector;
|
||||
entry = &cacheEntries[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
bool _NTFS_cache_readSectors(NTFS_CACHE *cache,sec_t sector,sec_t numSectors,void *buffer)
|
||||
{
|
||||
sec_t sec;
|
||||
sec_t secs_to_read;
|
||||
NTFS_CACHE_ENTRY *entry;
|
||||
uint8_t *dest = buffer;
|
||||
|
||||
while(numSectors>0) {
|
||||
entry = _NTFS_cache_getPage(cache,sector);
|
||||
if(entry==NULL) return false;
|
||||
|
||||
sec = sector - entry->sector;
|
||||
secs_to_read = entry->count - sec;
|
||||
if(secs_to_read>numSectors) secs_to_read = numSectors;
|
||||
|
||||
memcpy(dest,entry->cache + (sec*cache->sectorSize),(secs_to_read*cache->sectorSize));
|
||||
|
||||
dest += (secs_to_read*cache->sectorSize);
|
||||
sector += secs_to_read;
|
||||
numSectors -= secs_to_read;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
Reads some data from a cache page, determined by the sector number
|
||||
*/
|
||||
|
||||
bool _NTFS_cache_readPartialSector (NTFS_CACHE* cache, void* buffer, sec_t sector, unsigned int offset, size_t size)
|
||||
{
|
||||
sec_t sec;
|
||||
NTFS_CACHE_ENTRY *entry;
|
||||
|
||||
if (offset + size > cache->sectorSize) return false;
|
||||
|
||||
entry = _NTFS_cache_getPage(cache,sector);
|
||||
if(entry==NULL) return false;
|
||||
|
||||
sec = sector - entry->sector;
|
||||
memcpy(buffer,entry->cache + ((sec*cache->sectorSize) + offset),size);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool _NTFS_cache_readLittleEndianValue (NTFS_CACHE* cache, uint32_t *value, sec_t sector, unsigned int offset, int num_bytes) {
|
||||
uint8_t buf[4];
|
||||
if (!_NTFS_cache_readPartialSector(cache, buf, sector, offset, num_bytes)) return false;
|
||||
|
||||
switch(num_bytes) {
|
||||
case 1: *value = buf[0]; break;
|
||||
case 2: *value = u8array_to_u16(buf,0); break;
|
||||
case 4: *value = u8array_to_u32(buf,0); break;
|
||||
default: return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
Writes some data to a cache page, making sure it is loaded into memory first.
|
||||
*/
|
||||
|
||||
bool _NTFS_cache_writePartialSector (NTFS_CACHE* cache, const void* buffer, sec_t sector, unsigned int offset, size_t size)
|
||||
{
|
||||
sec_t sec;
|
||||
NTFS_CACHE_ENTRY *entry;
|
||||
|
||||
if (offset + size > cache->sectorSize) return false;
|
||||
|
||||
entry = _NTFS_cache_getPage(cache,sector);
|
||||
if(entry==NULL) return false;
|
||||
|
||||
sec = sector - entry->sector;
|
||||
memcpy(entry->cache + ((sec*cache->sectorSize) + offset),buffer,size);
|
||||
|
||||
entry->dirty = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool _NTFS_cache_writeLittleEndianValue (NTFS_CACHE* cache, const uint32_t value, sec_t sector, unsigned int offset, int size) {
|
||||
uint8_t buf[4] = {0, 0, 0, 0};
|
||||
|
||||
switch(size) {
|
||||
case 1: buf[0] = value; break;
|
||||
case 2: u16_to_u8array(buf, 0, value); break;
|
||||
case 4: u32_to_u8array(buf, 0, value); break;
|
||||
default: return false;
|
||||
}
|
||||
|
||||
return _NTFS_cache_writePartialSector(cache, buf, sector, offset, size);
|
||||
}
|
||||
|
||||
/*
|
||||
Writes some data to a cache page, zeroing out the page first
|
||||
*/
|
||||
|
||||
bool _NTFS_cache_eraseWritePartialSector (NTFS_CACHE* cache, const void* buffer, sec_t sector, unsigned int offset, size_t size)
|
||||
{
|
||||
sec_t sec;
|
||||
NTFS_CACHE_ENTRY *entry;
|
||||
|
||||
if (offset + size > cache->sectorSize) return false;
|
||||
|
||||
entry = _NTFS_cache_getPage(cache,sector);
|
||||
if(entry==NULL) return false;
|
||||
|
||||
sec = sector - entry->sector;
|
||||
memset(entry->cache + (sec*cache->sectorSize),0,cache->sectorSize);
|
||||
memcpy(entry->cache + ((sec*cache->sectorSize) + offset),buffer,size);
|
||||
|
||||
entry->dirty = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool _NTFS_cache_writeSectors (NTFS_CACHE* cache, sec_t sector, sec_t numSectors, const void* buffer)
|
||||
{
|
||||
sec_t sec;
|
||||
sec_t secs_to_write;
|
||||
NTFS_CACHE_ENTRY* entry;
|
||||
const uint8_t *src = buffer;
|
||||
|
||||
while(numSectors>0)
|
||||
{
|
||||
entry = _NTFS_cache_findPage(cache,sector,numSectors);
|
||||
|
||||
if(entry!=NULL) {
|
||||
|
||||
if ( entry->sector > sector) {
|
||||
|
||||
secs_to_write = entry->sector - sector;
|
||||
|
||||
cache->disc->writeSectors(sector,secs_to_write,src);
|
||||
src += (secs_to_write*cache->sectorSize);
|
||||
sector += secs_to_write;
|
||||
numSectors -= secs_to_write;
|
||||
}
|
||||
|
||||
sec = sector - entry->sector;
|
||||
secs_to_write = entry->count - sec;
|
||||
|
||||
if(secs_to_write>numSectors) secs_to_write = numSectors;
|
||||
|
||||
memcpy(entry->cache + (sec*cache->sectorSize),src,(secs_to_write*cache->sectorSize));
|
||||
|
||||
src += (secs_to_write*cache->sectorSize);
|
||||
sector += secs_to_write;
|
||||
numSectors -= secs_to_write;
|
||||
|
||||
entry->dirty = true;
|
||||
|
||||
} else {
|
||||
cache->disc->writeSectors(sector,numSectors,src);
|
||||
numSectors=0;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
Flushes all dirty pages to disc, clearing the dirty flag.
|
||||
*/
|
||||
bool _NTFS_cache_flush (NTFS_CACHE* cache) {
|
||||
unsigned int i;
|
||||
if(cache==NULL) return true;
|
||||
|
||||
for (i = 0; i < cache->numberOfPages; i++) {
|
||||
if (cache->cacheEntries[i].dirty) {
|
||||
if (!cache->disc->writeSectors (cache->cacheEntries[i].sector, cache->cacheEntries[i].count, cache->cacheEntries[i].cache)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
cache->cacheEntries[i].dirty = false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void _NTFS_cache_invalidate (NTFS_CACHE* cache) {
|
||||
unsigned int i;
|
||||
if(cache==NULL)
|
||||
return;
|
||||
|
||||
_NTFS_cache_flush(cache);
|
||||
for (i = 0; i < cache->numberOfPages; i++) {
|
||||
cache->cacheEntries[i].sector = CACHE_FREE;
|
||||
cache->cacheEntries[i].last_access = 0;
|
||||
cache->cacheEntries[i].count = 0;
|
||||
cache->cacheEntries[i].dirty = false;
|
||||
}
|
||||
}
|
135
source/libntfs/cache2.h
Normal file
135
source/libntfs/cache2.h
Normal file
@ -0,0 +1,135 @@
|
||||
/*
|
||||
NTFS_CACHE.h
|
||||
The NTFS_CACHE is not visible to the user. It should be flushed
|
||||
when any file is closed or changes are made to the filesystem.
|
||||
|
||||
This NTFS_CACHE implements a least-used-page replacement policy. This will
|
||||
distribute sectors evenly over the pages, so if less than the maximum
|
||||
pages are used at once, they should all eventually remain in the NTFS_CACHE.
|
||||
This also has the benefit of throwing out old sectors, so as not to keep
|
||||
too many stale pages around.
|
||||
|
||||
Copyright (c) 2006 Michael "Chishm" Chisholm
|
||||
Copyright (c) 2009 shareese, rodries
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation and/or
|
||||
other materials provided with the distribution.
|
||||
3. The name of the author may not be used to endorse or promote products derived
|
||||
from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef _CACHE2_H
|
||||
#define _CACHE2_H
|
||||
|
||||
//#include "common.h"
|
||||
//#include "disc.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <gctypes.h>
|
||||
#include <ogc/disc_io.h>
|
||||
#include <gccore.h>
|
||||
|
||||
typedef struct {
|
||||
sec_t sector;
|
||||
unsigned int count;
|
||||
u64 last_access;
|
||||
bool dirty;
|
||||
u8* cache;
|
||||
} NTFS_CACHE_ENTRY;
|
||||
|
||||
typedef struct {
|
||||
const DISC_INTERFACE* disc;
|
||||
sec_t endOfPartition;
|
||||
unsigned int numberOfPages;
|
||||
unsigned int sectorsPerPage;
|
||||
sec_t sectorSize;
|
||||
NTFS_CACHE_ENTRY* cacheEntries;
|
||||
} NTFS_CACHE;
|
||||
|
||||
/*
|
||||
Read data from a sector in the NTFS_CACHE
|
||||
If the sector is not in the NTFS_CACHE, it will be swapped in
|
||||
offset is the position to start reading from
|
||||
size is the amount of data to read
|
||||
Precondition: offset + size <= BYTES_PER_READ
|
||||
*/
|
||||
//bool _NTFS_cache_readPartialSector (NTFS_CACHE* NTFS_CACHE, void* buffer, sec_t sector, unsigned int offset, size_t size);
|
||||
|
||||
//bool _NTFS_cache_readLittleEndianValue (NTFS_CACHE* NTFS_CACHE, uint32_t *value, sec_t sector, unsigned int offset, int num_bytes);
|
||||
|
||||
/*
|
||||
Write data to a sector in the NTFS_CACHE
|
||||
If the sector is not in the NTFS_CACHE, it will be swapped in.
|
||||
When the sector is swapped out, the data will be written to the disc
|
||||
offset is the position to start writing to
|
||||
size is the amount of data to write
|
||||
Precondition: offset + size <= BYTES_PER_READ
|
||||
*/
|
||||
//bool _NTFS_cache_writePartialSector (NTFS_CACHE* NTFS_CACHE, const void* buffer, sec_t sector, unsigned int offset, size_t size);
|
||||
|
||||
//bool _NTFS_cache_writeLittleEndianValue (NTFS_CACHE* NTFS_CACHE, const uint32_t value, sec_t sector, unsigned int offset, int num_bytes);
|
||||
|
||||
/*
|
||||
Write data to a sector in the NTFS_CACHE, zeroing the sector first
|
||||
If the sector is not in the NTFS_CACHE, it will be swapped in.
|
||||
When the sector is swapped out, the data will be written to the disc
|
||||
offset is the position to start writing to
|
||||
size is the amount of data to write
|
||||
Precondition: offset + size <= BYTES_PER_READ
|
||||
*/
|
||||
//bool _NTFS_cache_eraseWritePartialSector (NTFS_CACHE* NTFS_CACHE, const void* buffer, sec_t sector, unsigned int offset, size_t size);
|
||||
|
||||
/*
|
||||
Read several sectors from the NTFS_CACHE
|
||||
*/
|
||||
bool _NTFS_cache_readSectors (NTFS_CACHE* NTFS_CACHE, sec_t sector, sec_t numSectors, void* buffer);
|
||||
|
||||
/*
|
||||
Read a full sector from the NTFS_CACHE
|
||||
*/
|
||||
//static inline bool _NTFS_cache_readSector (NTFS_CACHE* NTFS_CACHE, void* buffer, sec_t sector) {
|
||||
// return _NTFS_cache_readPartialSector (NTFS_CACHE, buffer, sector, 0, BYTES_PER_READ);
|
||||
//}
|
||||
|
||||
/*
|
||||
Write a full sector to the NTFS_CACHE
|
||||
*/
|
||||
//static inline bool _NTFS_cache_writeSector (NTFS_CACHE* NTFS_CACHE, const void* buffer, sec_t sector) {
|
||||
// return _NTFS_cache_writePartialSector (NTFS_CACHE, buffer, sector, 0, BYTES_PER_READ);
|
||||
//}
|
||||
|
||||
bool _NTFS_cache_writeSectors (NTFS_CACHE* NTFS_CACHE, sec_t sector, sec_t numSectors, const void* buffer);
|
||||
|
||||
/*
|
||||
Write any dirty sectors back to disc and clear out the contents of the NTFS_CACHE
|
||||
*/
|
||||
bool _NTFS_cache_flush (NTFS_CACHE* NTFS_CACHE);
|
||||
|
||||
/*
|
||||
Clear out the contents of the NTFS_CACHE without writing any dirty sectors first
|
||||
*/
|
||||
void _NTFS_cache_invalidate (NTFS_CACHE* NTFS_CACHE);
|
||||
|
||||
NTFS_CACHE* _NTFS_cache_constructor (unsigned int numberOfPages, unsigned int sectorsPerPage, const DISC_INTERFACE* discInterface, sec_t endOfPartition, sec_t sectorSize);
|
||||
|
||||
void _NTFS_cache_destructor (NTFS_CACHE* NTFS_CACHE);
|
||||
|
||||
#endif // _CACHE_H
|
||||
|
@ -23,32 +23,23 @@
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_STDLIB_H
|
||||
#include <stdlib.h>
|
||||
#endif
|
||||
#ifdef HAVE_STRING_H
|
||||
#include <string.h>
|
||||
#endif
|
||||
#ifdef HAVE_ERRNO_H
|
||||
#include <errno.h>
|
||||
#endif
|
||||
|
||||
#include "attrib.h"
|
||||
#include "index.h"
|
||||
#include "collate.h"
|
||||
#include "debug.h"
|
||||
#include "unistr.h"
|
||||
#include "logging.h"
|
||||
|
||||
BOOL ntfs_is_collation_rule_supported(COLLATION_RULES cr)
|
||||
{
|
||||
/*
|
||||
* FIXME: At the moment we only support COLLATION_BINARY,
|
||||
* COLLATION_NTOFS_ULONG and COLLATION_FILE_NAME so we return false
|
||||
* for everything else.
|
||||
* JPA added COLLATION_NTOFS_SECURITY_HASH
|
||||
*/
|
||||
if (cr != COLLATION_BINARY && cr != COLLATION_NTOFS_ULONG
|
||||
&& cr != COLLATION_FILE_NAME
|
||||
&& cr != COLLATION_NTOFS_SECURITY_HASH
|
||||
&& cr != COLLATION_NTOFS_ULONGS)
|
||||
return FALSE;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* ntfs_collate_binary - Which of two binary objects should be listed first
|
||||
* @vol: unused
|
||||
@ -177,15 +168,15 @@ static int ntfs_collate_ntofs_security_hash(ntfs_volume *vol __attribute__((unus
|
||||
{
|
||||
int rc;
|
||||
u32 d1, d2;
|
||||
const u32 *p1, *p2;
|
||||
const le32 *p1, *p2;
|
||||
|
||||
ntfs_log_trace("Entering.\n");
|
||||
if (data1_len != data2_len || data1_len != 8) {
|
||||
ntfs_log_error("data1_len or/and data2_len not equal to 8.\n");
|
||||
return NTFS_COLLATION_ERROR;
|
||||
}
|
||||
p1 = (const u32*)data1;
|
||||
p2 = (const u32*)data2;
|
||||
p1 = (const le32*)data1;
|
||||
p2 = (const le32*)data2;
|
||||
d1 = le32_to_cpup(p1);
|
||||
d2 = le32_to_cpup(p2);
|
||||
if (d1 < d2)
|
||||
@ -228,89 +219,53 @@ static int ntfs_collate_file_name(ntfs_volume *vol,
|
||||
const void *data1, const int data1_len __attribute__((unused)),
|
||||
const void *data2, const int data2_len __attribute__((unused)))
|
||||
{
|
||||
const FILE_NAME_ATTR *file_name_attr1;
|
||||
const FILE_NAME_ATTR *file_name_attr2;
|
||||
int rc;
|
||||
|
||||
ntfs_log_trace("Entering.\n");
|
||||
rc = ntfs_file_values_compare(data1, data2, NTFS_COLLATION_ERROR,
|
||||
IGNORE_CASE, vol->upcase, vol->upcase_len);
|
||||
if (!rc)
|
||||
rc = ntfs_file_values_compare(data1, data2,
|
||||
NTFS_COLLATION_ERROR, CASE_SENSITIVE,
|
||||
vol->upcase, vol->upcase_len);
|
||||
file_name_attr1 = (const FILE_NAME_ATTR*)data1;
|
||||
file_name_attr2 = (const FILE_NAME_ATTR*)data2;
|
||||
rc = ntfs_names_full_collate(
|
||||
(ntfschar*)&file_name_attr1->file_name,
|
||||
file_name_attr1->file_name_length,
|
||||
(ntfschar*)&file_name_attr2->file_name,
|
||||
file_name_attr2->file_name_length,
|
||||
CASE_SENSITIVE, vol->upcase, vol->upcase_len);
|
||||
ntfs_log_trace("Done, returning %i.\n", rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
typedef int (*ntfs_collate_func_t)(ntfs_volume *, const void *, const int,
|
||||
const void *, const int);
|
||||
|
||||
static ntfs_collate_func_t ntfs_do_collate0x0[3] = {
|
||||
ntfs_collate_binary,
|
||||
ntfs_collate_file_name,
|
||||
NULL/*ntfs_collate_unicode_string*/,
|
||||
};
|
||||
|
||||
static ntfs_collate_func_t ntfs_do_collate0x1[4] = {
|
||||
ntfs_collate_ntofs_ulong,
|
||||
NULL/*ntfs_collate_ntofs_sid*/,
|
||||
ntfs_collate_ntofs_security_hash,
|
||||
ntfs_collate_ntofs_ulongs
|
||||
};
|
||||
|
||||
/**
|
||||
* ntfs_collate - collate two data items using a specified collation rule
|
||||
* @vol: ntfs volume to which the data items belong
|
||||
* @cr: collation rule to use when comparing the items
|
||||
* @data1: first data item to collate
|
||||
* @data1_len: length in bytes of @data1
|
||||
* @data2: second data item to collate
|
||||
* @data2_len: length in bytes of @data2
|
||||
*
|
||||
* Collate the two data items @data1 and @data2 using the collation rule @cr
|
||||
* and return -1, 0, or 1 if @data1 is found, respectively, to collate before,
|
||||
* to match, or to collate after @data2.
|
||||
*
|
||||
* For speed we use the collation rule @cr as an index into two tables of
|
||||
* function pointers to call the appropriate collation function.
|
||||
*
|
||||
* Return NTFS_COLLATION_ERROR if error occurred.
|
||||
*/
|
||||
int ntfs_collate(ntfs_volume *vol, COLLATION_RULES cr,
|
||||
const void *data1, const int data1_len,
|
||||
const void *data2, const int data2_len)
|
||||
{
|
||||
int i;
|
||||
|
||||
ntfs_log_trace("Entering.\n");
|
||||
if (!vol || !data1 || !data2 || data1_len < 0 || data2_len < 0) {
|
||||
ntfs_log_error("Invalid arguments passed.\n");
|
||||
return NTFS_COLLATION_ERROR;
|
||||
}
|
||||
/*
|
||||
* FIXME: At the moment we only support COLLATION_BINARY,
|
||||
* COLLATION_NTOFS_ULONG and COLLATION_FILE_NAME so we return error
|
||||
* for everything else.
|
||||
* JPA added COLLATION_NTOFS_SECURITY_HASH
|
||||
* JPA added COLLATION_NTOFS_ULONGS
|
||||
* Get a pointer to appropriate collation function.
|
||||
*
|
||||
* Returns NULL if the needed function is not implemented
|
||||
*/
|
||||
if (cr != COLLATION_BINARY && cr != COLLATION_NTOFS_ULONG
|
||||
&& cr != COLLATION_FILE_NAME
|
||||
&& cr != COLLATION_NTOFS_SECURITY_HASH
|
||||
&& cr != COLLATION_NTOFS_ULONGS)
|
||||
goto err;
|
||||
i = le32_to_cpu(cr);
|
||||
if (i < 0)
|
||||
goto err;
|
||||
if (i <= 0x02)
|
||||
return ntfs_do_collate0x0[i](vol, data1, data1_len,
|
||||
data2, data2_len);
|
||||
if (i < 0x10)
|
||||
goto err;
|
||||
i -= 0x10;
|
||||
if (i <= 3)
|
||||
return ntfs_do_collate0x1[i](vol, data1, data1_len,
|
||||
data2, data2_len);
|
||||
err:
|
||||
ntfs_log_debug("Unknown collation rule.\n");
|
||||
return NTFS_COLLATION_ERROR;
|
||||
|
||||
COLLATE ntfs_get_collate_function(COLLATION_RULES cr)
|
||||
{
|
||||
COLLATE collate;
|
||||
|
||||
switch (cr) {
|
||||
case COLLATION_BINARY :
|
||||
collate = ntfs_collate_binary;
|
||||
break;
|
||||
case COLLATION_FILE_NAME :
|
||||
collate = ntfs_collate_file_name;
|
||||
break;
|
||||
case COLLATION_NTOFS_SECURITY_HASH :
|
||||
collate = ntfs_collate_ntofs_security_hash;
|
||||
break;
|
||||
case COLLATION_NTOFS_ULONG :
|
||||
collate = ntfs_collate_ntofs_ulong;
|
||||
break;
|
||||
case COLLATION_NTOFS_ULONGS :
|
||||
collate = ntfs_collate_ntofs_ulongs;
|
||||
break;
|
||||
default :
|
||||
errno = EOPNOTSUPP;
|
||||
collate = (COLLATE)NULL;
|
||||
break;
|
||||
}
|
||||
return (collate);
|
||||
}
|
||||
|
@ -29,9 +29,6 @@
|
||||
|
||||
#define NTFS_COLLATION_ERROR -2
|
||||
|
||||
extern BOOL ntfs_is_collation_rule_supported(COLLATION_RULES cr);
|
||||
extern int ntfs_collate(ntfs_volume *vol, COLLATION_RULES cr,
|
||||
const void *data1, const int data1_len,
|
||||
const void *data2, const int data2_len);
|
||||
extern COLLATE ntfs_get_collate_function(COLLATION_RULES);
|
||||
|
||||
#endif /* _NTFS_COLLATE_H */
|
||||
|
@ -77,7 +77,7 @@ int ffs(int x)
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static const char sccsid[] = "@(#)daemon.c 8.1 (Berkeley) 6/4/93";
|
||||
static const char rcsid[] = "$Id: compat.c,v 1.1.1.1.2.7 2009/03/27 09:09:59 jpandre Exp $";
|
||||
static const char rcsid[] = "$Id: compat.c,v 1.1.1.1.2.1 2008-08-16 15:17:44 jpandre Exp $";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
/*
|
||||
@ -164,7 +164,7 @@ int daemon(int nochdir, int noclose) {
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static const char sccsid[] = "strsep.c 8.1 (Berkeley) 6/4/93";
|
||||
static const char rcsid[] = "$Id: compat.c,v 1.1.1.1.2.7 2009/03/27 09:09:59 jpandre Exp $";
|
||||
static const char rcsid[] = "$Id: compat.c,v 1.1.1.1.2.1 2008-08-16 15:17:44 jpandre Exp $";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
/*
|
||||
|
@ -5,7 +5,7 @@
|
||||
* Copyright (c) 2004-2005 Anton Altaparmakov
|
||||
* Copyright (c) 2004-2006 Szabolcs Szakacsits
|
||||
* Copyright (c) 2005 Yura Pakhuchiy
|
||||
* Copyright (c) 2009 Jean-Pierre Andre
|
||||
* Copyright (c) 2009-2010 Jean-Pierre Andre
|
||||
*
|
||||
* This program/include file is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as published
|
||||
@ -118,7 +118,7 @@ static void ntfs_new_node (struct COMPRESS_CONTEXT *pctx,
|
||||
BOOL done;
|
||||
const unsigned char *key;
|
||||
int c;
|
||||
unsigned int mxi;
|
||||
unsigned long mxi;
|
||||
unsigned int mxl;
|
||||
|
||||
mxl = (1 << (16 - pctx->nbt)) + 2;
|
||||
@ -147,16 +147,21 @@ static void ntfs_new_node (struct COMPRESS_CONTEXT *pctx,
|
||||
}
|
||||
}
|
||||
if (!done) {
|
||||
register unsigned int i;
|
||||
register unsigned long i;
|
||||
register const unsigned char *p1,*p2;
|
||||
|
||||
i = 1;
|
||||
mxi = NTFS_SB_SIZE - r;
|
||||
if (mxi < 2)
|
||||
less = FALSE;
|
||||
else {
|
||||
p1 = key;
|
||||
p2 = &pctx->inbuf[pp];
|
||||
mxi = NTFS_SB_SIZE - r;
|
||||
/* this loop has a significant impact on performances */
|
||||
do {
|
||||
} while ((p1[i] == p2[i]) && (++i < mxi));
|
||||
less = (i < mxi) && (p1[i] < p2[i]);
|
||||
}
|
||||
if (i >= THRESHOLD) {
|
||||
if (i > pctx->match_length) {
|
||||
pctx->match_position =
|
||||
@ -174,7 +179,8 @@ static void ntfs_new_node (struct COMPRESS_CONTEXT *pctx,
|
||||
pctx->rson[i] = r;
|
||||
else
|
||||
pctx->lson[i] = r;
|
||||
pctx->dad[pp] = NIL; /* remove pp */
|
||||
/* remove pp */
|
||||
pctx->dad[pp] = NIL;
|
||||
done = TRUE;
|
||||
pctx->match_length = mxl;
|
||||
}
|
||||
@ -196,7 +202,8 @@ static void ntfs_new_node (struct COMPRESS_CONTEXT *pctx,
|
||||
* or zero if there was a bug
|
||||
*/
|
||||
|
||||
static unsigned int ntfs_nextmatch(struct COMPRESS_CONTEXT *pctx, unsigned int rr, int dd)
|
||||
static unsigned int ntfs_nextmatch(struct COMPRESS_CONTEXT *pctx,
|
||||
unsigned int rr, int dd)
|
||||
{
|
||||
unsigned int bestlen = 0;
|
||||
|
||||
@ -214,7 +221,8 @@ static unsigned int ntfs_nextmatch(struct COMPRESS_CONTEXT *pctx, unsigned int r
|
||||
goto bug;
|
||||
}
|
||||
if (((rr + bestlen) < NTFS_SB_SIZE)) {
|
||||
while ((unsigned int)(1 << pctx->nbt) <= (rr - 1))
|
||||
while ((unsigned int)(1 << pctx->nbt)
|
||||
<= (rr - 1))
|
||||
pctx->nbt++;
|
||||
ntfs_new_node(pctx,rr);
|
||||
if (pctx->match_length > bestlen)
|
||||
@ -247,7 +255,8 @@ bug :
|
||||
* or zero if there was an error
|
||||
*/
|
||||
|
||||
static unsigned int ntfs_compress_block(const char *inbuf, unsigned int size, char *outbuf)
|
||||
static unsigned int ntfs_compress_block(const char *inbuf,
|
||||
unsigned int size, char *outbuf)
|
||||
{
|
||||
struct COMPRESS_CONTEXT *pctx;
|
||||
char *ptag;
|
||||
@ -284,7 +293,8 @@ static unsigned int ntfs_compress_block(const char *inbuf, unsigned int size, ch
|
||||
outbuf[xout++] = inbuf[rr];
|
||||
ntag++;
|
||||
} else {
|
||||
while ((unsigned int)(1 << pctx->nbt) <= (rr - 1))
|
||||
while ((unsigned int)(1 << pctx->nbt)
|
||||
<= (rr - 1))
|
||||
pctx->nbt++;
|
||||
q = (pctx->match_position << (16 - pctx->nbt))
|
||||
+ pctx->match_length - THRESHOLD;
|
||||
@ -732,7 +742,15 @@ do_next_cb:
|
||||
na->data_size = na->initialized_size = na->allocated_size;
|
||||
do {
|
||||
br = ntfs_attr_pread(na, ofs, to_read, b);
|
||||
if (br < 0) {
|
||||
if (br <= 0) {
|
||||
if (!br) {
|
||||
ntfs_log_error("Failed to read an"
|
||||
" uncompressed cluster,"
|
||||
" inode %lld offs 0x%llx\n",
|
||||
(long long)na->ni->mft_no,
|
||||
(long long)ofs);
|
||||
errno = EIO;
|
||||
}
|
||||
err = errno;
|
||||
na->data_size = tdata_size;
|
||||
na->initialized_size = tinitialized_size;
|
||||
@ -783,7 +801,15 @@ do_next_cb:
|
||||
br = ntfs_attr_pread(na,
|
||||
(vcn << vol->cluster_size_bits) +
|
||||
(cb_pos - cb), to_read, cb_pos);
|
||||
if (br < 0) {
|
||||
if (br <= 0) {
|
||||
if (!br) {
|
||||
ntfs_log_error("Failed to read a"
|
||||
" compressed cluster, "
|
||||
" inode %lld offs 0x%llx\n",
|
||||
(long long)na->ni->mft_no,
|
||||
(long long)(vcn << vol->cluster_size_bits));
|
||||
errno = EIO;
|
||||
}
|
||||
err = errno;
|
||||
na->data_size = tdata_size;
|
||||
na->initialized_size = tinitialized_size;
|
||||
@ -881,11 +907,11 @@ static u32 read_clusters(ntfs_volume *vol, const runlist_element *rl,
|
||||
* Returns the amount of data written
|
||||
*/
|
||||
|
||||
static int write_clusters(ntfs_volume *vol, const runlist_element *rl,
|
||||
s64 offs, int to_write, const char *outbuf)
|
||||
static s32 write_clusters(ntfs_volume *vol, const runlist_element *rl,
|
||||
s64 offs, s32 to_write, const char *outbuf)
|
||||
{
|
||||
int count;
|
||||
int put, xput;
|
||||
s32 count;
|
||||
s32 put, xput;
|
||||
s64 xpos;
|
||||
BOOL first;
|
||||
const char *xoutbuf;
|
||||
@ -926,17 +952,17 @@ static int write_clusters(ntfs_volume *vol, const runlist_element *rl,
|
||||
* or -2 if there were an irrecoverable error (errno set)
|
||||
*/
|
||||
|
||||
static int ntfs_comp_set(ntfs_attr *na, runlist_element *rl,
|
||||
s64 offs, unsigned int insz, const char *inbuf)
|
||||
static s32 ntfs_comp_set(ntfs_attr *na, runlist_element *rl,
|
||||
s64 offs, u32 insz, const char *inbuf)
|
||||
{
|
||||
ntfs_volume *vol;
|
||||
char *outbuf;
|
||||
char *pbuf;
|
||||
unsigned int compsz;
|
||||
int written;
|
||||
int rounded;
|
||||
u32 compsz;
|
||||
s32 written;
|
||||
s32 rounded;
|
||||
unsigned int clsz;
|
||||
unsigned int p;
|
||||
u32 p;
|
||||
unsigned int sz;
|
||||
unsigned int bsz;
|
||||
BOOL fail;
|
||||
@ -1002,7 +1028,10 @@ static int ntfs_comp_set(ntfs_attr *na, runlist_element *rl,
|
||||
rounded = ((compsz - 1) | (clsz - 1)) + 1;
|
||||
written = write_clusters(vol, rl, offs, rounded, outbuf);
|
||||
if (written != rounded) {
|
||||
// previously written text has been spoilt, should return a specific error
|
||||
/*
|
||||
* TODO : previously written text has been
|
||||
* spoilt, should return a specific error
|
||||
*/
|
||||
ntfs_log_error("error writing compressed data\n");
|
||||
errno = EIO;
|
||||
written = -2;
|
||||
@ -1016,25 +1045,297 @@ static int ntfs_comp_set(ntfs_attr *na, runlist_element *rl,
|
||||
}
|
||||
|
||||
/*
|
||||
* Free unneeded clusters after compression
|
||||
* Check the validity of a compressed runlist
|
||||
* The check starts at the beginning of current run and ends
|
||||
* at the end of runlist
|
||||
* errno is set if the runlist is not valid
|
||||
*/
|
||||
|
||||
static BOOL valid_compressed_run(ntfs_attr *na, runlist_element *rl,
|
||||
BOOL fullcheck, const char *text)
|
||||
{
|
||||
runlist_element *xrl;
|
||||
const char *err;
|
||||
BOOL ok = TRUE;
|
||||
|
||||
xrl = rl;
|
||||
while (xrl->vcn & (na->compression_block_clusters - 1))
|
||||
xrl--;
|
||||
err = (const char*)NULL;
|
||||
while (xrl->length) {
|
||||
if ((xrl->vcn + xrl->length) != xrl[1].vcn)
|
||||
err = "Runs not adjacent";
|
||||
if (xrl->lcn == LCN_HOLE) {
|
||||
if ((xrl->vcn + xrl->length)
|
||||
& (na->compression_block_clusters - 1)) {
|
||||
err = "Invalid hole";
|
||||
}
|
||||
if (fullcheck && (xrl[1].lcn == LCN_HOLE)) {
|
||||
err = "Adjacent holes";
|
||||
}
|
||||
}
|
||||
if (err) {
|
||||
ntfs_log_error("%s at %s index %ld inode %lld\n",
|
||||
err, text, (long)(xrl - na->rl),
|
||||
(long long)na->ni->mft_no);
|
||||
errno = EIO;
|
||||
ok = FALSE;
|
||||
err = (const char*)NULL;
|
||||
}
|
||||
xrl++;
|
||||
}
|
||||
return (ok);
|
||||
}
|
||||
|
||||
/*
|
||||
* Free unneeded clusters after overwriting compressed data
|
||||
*
|
||||
* This generally requires an empty slot at the end of runlist,
|
||||
* This generally requires one or two empty slots at the end of runlist,
|
||||
* but we do not want to reallocate the runlist here because
|
||||
* there are many pointers to it.
|
||||
* So the empty slot has to be reserved beforehand
|
||||
* So the empty slots have to be reserved beforehand
|
||||
*
|
||||
* Returns zero unless some error occurred (described by errno)
|
||||
*
|
||||
* +======= start of block =====+
|
||||
* 0 |A chunk may overflow | <-- rl usedcnt : A + B
|
||||
* |A on previous block | then B
|
||||
* |A |
|
||||
* +-- end of allocated chunk --+ freelength : C
|
||||
* |B | (incl overflow)
|
||||
* +== end of compressed data ==+
|
||||
* |C | <-- freerl freecnt : C + D
|
||||
* |C chunk may overflow |
|
||||
* |C on next block |
|
||||
* +-- end of allocated chunk --+
|
||||
* |D |
|
||||
* |D chunk may overflow |
|
||||
* 15 |D on next block |
|
||||
* +======== end of block ======+
|
||||
*
|
||||
*/
|
||||
|
||||
static int ntfs_compress_overwr_free(ntfs_attr *na, runlist_element *rl,
|
||||
s32 usedcnt, s32 freecnt, VCN *update_from)
|
||||
{
|
||||
BOOL beginhole;
|
||||
BOOL mergeholes;
|
||||
s32 oldlength;
|
||||
s32 freelength;
|
||||
s64 freelcn;
|
||||
s64 freevcn;
|
||||
runlist_element *freerl;
|
||||
ntfs_volume *vol;
|
||||
s32 carry;
|
||||
int res;
|
||||
|
||||
vol = na->ni->vol;
|
||||
res = 0;
|
||||
freelcn = rl->lcn + usedcnt;
|
||||
freevcn = rl->vcn + usedcnt;
|
||||
freelength = rl->length - usedcnt;
|
||||
beginhole = !usedcnt && !rl->vcn;
|
||||
/* can merge with hole before ? */
|
||||
mergeholes = !usedcnt
|
||||
&& rl[0].vcn
|
||||
&& (rl[-1].lcn == LCN_HOLE);
|
||||
/* truncate current run, carry to subsequent hole */
|
||||
carry = freelength;
|
||||
oldlength = rl->length;
|
||||
if (mergeholes) {
|
||||
/* merging with a hole before */
|
||||
freerl = rl;
|
||||
} else {
|
||||
rl->length -= freelength; /* warning : can be zero */
|
||||
freerl = ++rl;
|
||||
}
|
||||
if (!mergeholes && (usedcnt || beginhole)) {
|
||||
s32 freed;
|
||||
runlist_element *frl;
|
||||
runlist_element *erl;
|
||||
int holes = 0;
|
||||
BOOL threeparts;
|
||||
|
||||
/* free the unneeded clusters from initial run, then freerl */
|
||||
threeparts = (freelength > freecnt);
|
||||
freed = 0;
|
||||
frl = freerl;
|
||||
if (freelength) {
|
||||
res = ntfs_cluster_free_basic(vol,freelcn,
|
||||
(threeparts ? freecnt : freelength));
|
||||
if (!res)
|
||||
freed += (threeparts ? freecnt : freelength);
|
||||
if (!usedcnt) {
|
||||
holes++;
|
||||
freerl--;
|
||||
freerl->length += (threeparts
|
||||
? freecnt : freelength);
|
||||
if (freerl->vcn < *update_from)
|
||||
*update_from = freerl->vcn;
|
||||
}
|
||||
}
|
||||
while (!res && frl->length && (freed < freecnt)) {
|
||||
if (frl->length <= (freecnt - freed)) {
|
||||
res = ntfs_cluster_free_basic(vol, frl->lcn,
|
||||
frl->length);
|
||||
if (!res) {
|
||||
freed += frl->length;
|
||||
frl->lcn = LCN_HOLE;
|
||||
frl->length += carry;
|
||||
carry = 0;
|
||||
holes++;
|
||||
}
|
||||
} else {
|
||||
res = ntfs_cluster_free_basic(vol, frl->lcn,
|
||||
freecnt - freed);
|
||||
if (!res) {
|
||||
frl->lcn += freecnt - freed;
|
||||
frl->vcn += freecnt - freed;
|
||||
frl->length -= freecnt - freed;
|
||||
freed = freecnt;
|
||||
}
|
||||
}
|
||||
frl++;
|
||||
}
|
||||
na->compressed_size -= freed << vol->cluster_size_bits;
|
||||
switch (holes) {
|
||||
case 0 :
|
||||
/* there are no hole, must insert one */
|
||||
/* space for hole has been prereserved */
|
||||
if (freerl->lcn == LCN_HOLE) {
|
||||
if (threeparts) {
|
||||
erl = freerl;
|
||||
while (erl->length)
|
||||
erl++;
|
||||
do {
|
||||
erl[2] = *erl;
|
||||
} while (erl-- != freerl);
|
||||
|
||||
freerl[1].length = freelength - freecnt;
|
||||
freerl->length = freecnt;
|
||||
freerl[1].lcn = freelcn + freecnt;
|
||||
freerl[1].vcn = freevcn + freecnt;
|
||||
freerl[2].lcn = LCN_HOLE;
|
||||
freerl[2].vcn = freerl[1].vcn
|
||||
+ freerl[1].length;
|
||||
freerl->vcn = freevcn;
|
||||
} else {
|
||||
freerl->vcn = freevcn;
|
||||
freerl->length += freelength;
|
||||
}
|
||||
} else {
|
||||
erl = freerl;
|
||||
while (erl->length)
|
||||
erl++;
|
||||
if (threeparts) {
|
||||
do {
|
||||
erl[2] = *erl;
|
||||
} while (erl-- != freerl);
|
||||
freerl[1].lcn = freelcn + freecnt;
|
||||
freerl[1].vcn = freevcn + freecnt;
|
||||
freerl[1].length = oldlength - usedcnt - freecnt;
|
||||
} else {
|
||||
do {
|
||||
erl[1] = *erl;
|
||||
} while (erl-- != freerl);
|
||||
}
|
||||
freerl->lcn = LCN_HOLE;
|
||||
freerl->vcn = freevcn;
|
||||
freerl->length = freecnt;
|
||||
}
|
||||
break;
|
||||
case 1 :
|
||||
/* there is a single hole, may have to merge */
|
||||
freerl->vcn = freevcn;
|
||||
if (freerl[1].lcn == LCN_HOLE) {
|
||||
freerl->length += freerl[1].length;
|
||||
erl = freerl;
|
||||
do {
|
||||
erl++;
|
||||
*erl = erl[1];
|
||||
} while (erl->length);
|
||||
}
|
||||
break;
|
||||
default :
|
||||
/* there were several holes, must merge them */
|
||||
freerl->lcn = LCN_HOLE;
|
||||
freerl->vcn = freevcn;
|
||||
freerl->length = freecnt;
|
||||
if (freerl[holes].lcn == LCN_HOLE) {
|
||||
freerl->length += freerl[holes].length;
|
||||
holes++;
|
||||
}
|
||||
erl = freerl;
|
||||
do {
|
||||
erl++;
|
||||
*erl = erl[holes - 1];
|
||||
} while (erl->length);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
s32 freed;
|
||||
runlist_element *frl;
|
||||
runlist_element *xrl;
|
||||
|
||||
freed = 0;
|
||||
frl = freerl--;
|
||||
if (freerl->vcn < *update_from)
|
||||
*update_from = freerl->vcn;
|
||||
while (!res && frl->length && (freed < freecnt)) {
|
||||
if (frl->length <= (freecnt - freed)) {
|
||||
freerl->length += frl->length;
|
||||
freed += frl->length;
|
||||
res = ntfs_cluster_free_basic(vol, frl->lcn,
|
||||
frl->length);
|
||||
frl++;
|
||||
} else {
|
||||
freerl->length += freecnt - freed;
|
||||
res = ntfs_cluster_free_basic(vol, frl->lcn,
|
||||
freecnt - freed);
|
||||
frl->lcn += freecnt - freed;
|
||||
frl->vcn += freecnt - freed;
|
||||
frl->length -= freecnt - freed;
|
||||
freed = freecnt;
|
||||
}
|
||||
}
|
||||
/* remove unneded runlist entries */
|
||||
xrl = freerl;
|
||||
/* group with next run if also a hole */
|
||||
if (frl->length && (frl->lcn == LCN_HOLE)) {
|
||||
xrl->length += frl->length;
|
||||
frl++;
|
||||
}
|
||||
while (frl->length) {
|
||||
*++xrl = *frl++;
|
||||
}
|
||||
*++xrl = *frl; /* terminator */
|
||||
na->compressed_size -= freed << vol->cluster_size_bits;
|
||||
}
|
||||
return (res);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Free unneeded clusters after compression
|
||||
*
|
||||
* This generally requires one or two empty slots at the end of runlist,
|
||||
* but we do not want to reallocate the runlist here because
|
||||
* there are many pointers to it.
|
||||
* So the empty slots have to be reserved beforehand
|
||||
*
|
||||
* Returns zero unless some error occurred (described by errno)
|
||||
*/
|
||||
|
||||
static int ntfs_compress_free(ntfs_attr *na, runlist_element *rl,
|
||||
s64 used, s64 reserved)
|
||||
s64 used, s64 reserved, BOOL appending,
|
||||
VCN *update_from)
|
||||
{
|
||||
int freecnt;
|
||||
int usedcnt;
|
||||
s32 freecnt;
|
||||
s32 usedcnt;
|
||||
int res;
|
||||
s64 freelcn;
|
||||
s64 freevcn;
|
||||
int freelength;
|
||||
s32 freelength;
|
||||
BOOL mergeholes;
|
||||
BOOL beginhole;
|
||||
ntfs_volume *vol;
|
||||
@ -1044,9 +1345,11 @@ static int ntfs_compress_free(ntfs_attr *na, runlist_element *rl,
|
||||
vol = na->ni->vol;
|
||||
freecnt = (reserved - used) >> vol->cluster_size_bits;
|
||||
usedcnt = (reserved >> vol->cluster_size_bits) - freecnt;
|
||||
if (rl->vcn < *update_from)
|
||||
*update_from = rl->vcn;
|
||||
/* skip entries fully used, if any */
|
||||
while (rl->length && (rl->length < usedcnt)) {
|
||||
usedcnt -= rl->length;
|
||||
usedcnt -= rl->length; /* must be > 0 */
|
||||
rl++;
|
||||
}
|
||||
if (rl->length) {
|
||||
@ -1056,22 +1359,37 @@ static int ntfs_compress_free(ntfs_attr *na, runlist_element *rl,
|
||||
* The required entry has been prereserved when
|
||||
* mapping the runlist.
|
||||
*/
|
||||
/* get the free part in initial run */
|
||||
freelcn = rl->lcn + usedcnt;
|
||||
freevcn = rl->vcn + usedcnt;
|
||||
freelength = rl->length - usedcnt;
|
||||
/* new count of allocated clusters */
|
||||
rl->length = usedcnt; /* warning : can be zero */
|
||||
if (!((freevcn + freecnt)
|
||||
& (na->compression_block_clusters - 1))) {
|
||||
if (!appending)
|
||||
res = ntfs_compress_overwr_free(na,rl,
|
||||
usedcnt,freecnt,update_from);
|
||||
else {
|
||||
freelength = rl->length - usedcnt;
|
||||
beginhole = !usedcnt && !rl->vcn;
|
||||
mergeholes = !usedcnt
|
||||
&& rl[0].vcn
|
||||
&& (rl[-1].lcn == LCN_HOLE);
|
||||
if (mergeholes) {
|
||||
s32 carry;
|
||||
|
||||
/* shorten the runs which have free space */
|
||||
carry = freecnt;
|
||||
freerl = rl;
|
||||
freerl->length = freecnt;
|
||||
} else
|
||||
while (freerl->length < carry) {
|
||||
carry -= freerl->length;
|
||||
freerl++;
|
||||
}
|
||||
freerl->length = carry;
|
||||
freerl = rl;
|
||||
} else {
|
||||
rl->length = usedcnt; /* can be zero ? */
|
||||
freerl = ++rl;
|
||||
}
|
||||
if ((freelength > 0)
|
||||
&& !mergeholes
|
||||
&& (usedcnt || beginhole)) {
|
||||
@ -1092,10 +1410,23 @@ static int ntfs_compress_free(ntfs_attr *na, runlist_element *rl,
|
||||
rl->vcn = freevcn;
|
||||
rl->lcn = freelcn;
|
||||
rl->length = freelength;
|
||||
} else {
|
||||
/* why is this different from the begin hole case ? */
|
||||
if ((freelength > 0)
|
||||
&& !mergeholes
|
||||
&& !usedcnt) {
|
||||
freerl--;
|
||||
freerl->length = freelength;
|
||||
if (freerl->vcn < *update_from)
|
||||
*update_from
|
||||
= freerl->vcn;
|
||||
}
|
||||
}
|
||||
/* free the hole */
|
||||
res = ntfs_cluster_free_from_rl(vol,freerl);
|
||||
if (!res) {
|
||||
na->compressed_size -= freecnt
|
||||
<< vol->cluster_size_bits;
|
||||
if (mergeholes) {
|
||||
/* merge with adjacent hole */
|
||||
freerl--;
|
||||
@ -1108,11 +1439,14 @@ static int ntfs_compress_free(ntfs_attr *na, runlist_element *rl,
|
||||
freerl->vcn = freevcn;
|
||||
freerl->length = freecnt;
|
||||
}
|
||||
if (freerl->vcn < *update_from)
|
||||
*update_from = freerl->vcn;
|
||||
/* and set up the new end */
|
||||
freerl[1].lcn = LCN_ENOENT;
|
||||
freerl[1].vcn = freevcn + freecnt;
|
||||
freerl[1].length = 0;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ntfs_log_error("Bad end of a compression block set\n");
|
||||
errno = EIO;
|
||||
@ -1130,7 +1464,7 @@ static int ntfs_compress_free(ntfs_attr *na, runlist_element *rl,
|
||||
*/
|
||||
|
||||
static int ntfs_read_append(ntfs_attr *na, const runlist_element *rl,
|
||||
s64 offs, u32 compsz, int pos,
|
||||
s64 offs, u32 compsz, s32 pos, BOOL appending,
|
||||
char *outbuf, s64 to_write, const void *b)
|
||||
{
|
||||
int fail = 1;
|
||||
@ -1147,7 +1481,10 @@ static int ntfs_read_append(ntfs_attr *na, const runlist_element *rl,
|
||||
compbuf = (char*)ntfs_malloc(compsz);
|
||||
if (compbuf) {
|
||||
/* must align to full block for decompression */
|
||||
if (appending)
|
||||
decompsz = ((pos - 1) | (NTFS_SB_SIZE - 1)) + 1;
|
||||
else
|
||||
decompsz = na->compression_block_size;
|
||||
got = read_clusters(na->ni->vol, rl, offs,
|
||||
compsz, compbuf);
|
||||
if ((got == compsz)
|
||||
@ -1171,7 +1508,8 @@ static int ntfs_read_append(ntfs_attr *na, const runlist_element *rl,
|
||||
*/
|
||||
|
||||
static int ntfs_flush(ntfs_attr *na, runlist_element *rl, s64 offs,
|
||||
const char *outbuf, int count, BOOL compress)
|
||||
const char *outbuf, s32 count, BOOL compress,
|
||||
BOOL appending, VCN *update_from)
|
||||
{
|
||||
int rounded;
|
||||
int written;
|
||||
@ -1183,9 +1521,11 @@ static int ntfs_flush(ntfs_attr *na, runlist_element *rl, s64 offs,
|
||||
compress = FALSE;
|
||||
if ((written >= 0)
|
||||
&& ntfs_compress_free(na,rl,offs + written,
|
||||
offs + na->compression_block_size))
|
||||
offs + na->compression_block_size, appending,
|
||||
update_from))
|
||||
written = -1;
|
||||
}
|
||||
} else
|
||||
written = 0;
|
||||
if (!compress) {
|
||||
clsz = 1 << na->ni->vol->cluster_size_bits;
|
||||
rounded = ((count - 1) | (clsz - 1)) + 1;
|
||||
@ -1204,32 +1544,55 @@ static int ntfs_flush(ntfs_attr *na, runlist_element *rl, s64 offs,
|
||||
* it has to be reserved beforehand.
|
||||
*
|
||||
* Returns the size of uncompressed data written,
|
||||
* or zero if an error occurred.
|
||||
* or negative if an error occurred.
|
||||
* When the returned size is less than requested, new clusters have
|
||||
* to be allocated before the function is called again.
|
||||
*/
|
||||
|
||||
s64 ntfs_compressed_pwrite(ntfs_attr *na, runlist_element *wrl, s64 wpos,
|
||||
s64 offs, s64 to_write, s64 rounded,
|
||||
const void *b, int compressed_part)
|
||||
const void *b, int compressed_part,
|
||||
VCN *update_from)
|
||||
{
|
||||
ntfs_volume *vol;
|
||||
runlist_element *brl; /* entry containing the beginning of block */
|
||||
int compression_length;
|
||||
s64 written;
|
||||
s64 to_read;
|
||||
s64 to_flush;
|
||||
s64 roffs;
|
||||
s64 got;
|
||||
s64 start_vcn;
|
||||
s64 nextblock;
|
||||
s64 endwrite;
|
||||
u32 compsz;
|
||||
char *inbuf;
|
||||
char *outbuf;
|
||||
BOOL fail;
|
||||
BOOL done;
|
||||
BOOL compress;
|
||||
BOOL appending;
|
||||
|
||||
written = 0; /* default return */
|
||||
if (!valid_compressed_run(na,wrl,FALSE,"begin compressed write")) {
|
||||
return (-1);
|
||||
}
|
||||
if ((*update_from < 0)
|
||||
|| (compressed_part < 0)
|
||||
|| (compressed_part > (int)na->compression_block_clusters)) {
|
||||
ntfs_log_error("Bad update vcn or compressed_part %d for compressed write\n",
|
||||
compressed_part);
|
||||
errno = EIO;
|
||||
return (-1);
|
||||
}
|
||||
/* make sure there are two unused entries in runlist */
|
||||
if (na->unused_runs < 2) {
|
||||
ntfs_log_error("No unused runs for compressed write\n");
|
||||
errno = EIO;
|
||||
return (-1);
|
||||
}
|
||||
if (wrl->vcn < *update_from)
|
||||
*update_from = wrl->vcn;
|
||||
written = -1; /* default return */
|
||||
vol = na->ni->vol;
|
||||
compression_length = na->compression_block_clusters;
|
||||
compress = FALSE;
|
||||
@ -1243,8 +1606,10 @@ s64 ntfs_compressed_pwrite(ntfs_attr *na, runlist_element *wrl, s64 wpos,
|
||||
*/
|
||||
nextblock = ((offs + (wrl->vcn << vol->cluster_size_bits))
|
||||
| (na->compression_block_size - 1)) + 1;
|
||||
if ((offs + to_write + (wrl->vcn << vol->cluster_size_bits))
|
||||
>= nextblock) {
|
||||
/* determine whether we are appending to file */
|
||||
endwrite = offs + to_write + (wrl->vcn << vol->cluster_size_bits);
|
||||
appending = endwrite >= na->initialized_size;
|
||||
if (endwrite >= nextblock) {
|
||||
/* it is time to compress */
|
||||
compress = TRUE;
|
||||
/* only process what we can */
|
||||
@ -1265,6 +1630,8 @@ s64 ntfs_compressed_pwrite(ntfs_attr *na, runlist_element *wrl, s64 wpos,
|
||||
/* find the beginning of block */
|
||||
start_vcn = (wrl->vcn + (offs >> vol->cluster_size_bits))
|
||||
& -compression_length;
|
||||
if (start_vcn < *update_from)
|
||||
*update_from = start_vcn;
|
||||
while (brl->vcn && (brl->vcn > start_vcn)) {
|
||||
/* jumping back a hole means big trouble */
|
||||
if (brl->lcn == (LCN)LCN_HOLE) {
|
||||
@ -1284,14 +1651,24 @@ s64 ntfs_compressed_pwrite(ntfs_attr *na, runlist_element *wrl, s64 wpos,
|
||||
* Decompress the data and append
|
||||
*/
|
||||
compsz = compressed_part << vol->cluster_size_bits;
|
||||
// improve the needed size
|
||||
outbuf = (char*)ntfs_malloc(na->compression_block_size);
|
||||
if (outbuf) {
|
||||
if (appending) {
|
||||
to_read = offs - roffs;
|
||||
to_flush = to_read + to_write;
|
||||
} else {
|
||||
to_read = na->data_size
|
||||
- (brl->vcn << vol->cluster_size_bits);
|
||||
if (to_read > na->compression_block_size)
|
||||
to_read = na->compression_block_size;
|
||||
to_flush = to_read;
|
||||
}
|
||||
if (!ntfs_read_append(na, brl, roffs, compsz,
|
||||
to_read, outbuf, to_write, b)) {
|
||||
(s32)(offs - roffs), appending,
|
||||
outbuf, to_write, b)) {
|
||||
written = ntfs_flush(na, brl, roffs,
|
||||
outbuf, to_read + to_write, compress);
|
||||
outbuf, to_flush, compress, appending,
|
||||
update_from);
|
||||
if (written >= 0) {
|
||||
written = to_write;
|
||||
done = TRUE;
|
||||
@ -1302,8 +1679,8 @@ s64 ntfs_compressed_pwrite(ntfs_attr *na, runlist_element *wrl, s64 wpos,
|
||||
} else {
|
||||
if (compress && !fail) {
|
||||
/*
|
||||
* we are filling up a block, read the full set of blocks
|
||||
* and compress it
|
||||
* we are filling up a block, read the full set
|
||||
* of blocks and compress it
|
||||
*/
|
||||
inbuf = (char*)ntfs_malloc(na->compression_block_size);
|
||||
if (inbuf) {
|
||||
@ -1326,7 +1703,8 @@ s64 ntfs_compressed_pwrite(ntfs_attr *na, runlist_element *wrl, s64 wpos,
|
||||
&& !ntfs_compress_free(na,brl,
|
||||
written + roffs,
|
||||
na->compression_block_size
|
||||
+ roffs)) {
|
||||
+ roffs,
|
||||
appending, update_from)) {
|
||||
done = TRUE;
|
||||
written = to_write;
|
||||
}
|
||||
@ -1354,18 +1732,22 @@ s64 ntfs_compressed_pwrite(ntfs_attr *na, runlist_element *wrl, s64 wpos,
|
||||
}
|
||||
}
|
||||
}
|
||||
if ((written >= 0)
|
||||
&& !valid_compressed_run(na,wrl,TRUE,"end compressed write"))
|
||||
written = -1;
|
||||
return (written);
|
||||
}
|
||||
|
||||
/*
|
||||
* Close a file written compressed.
|
||||
* This compresses the last partial compression block of the file.
|
||||
* An empty runlist slot has to be reserved beforehand.
|
||||
* Two empty runlist slots have to be reserved beforehand.
|
||||
*
|
||||
* Returns zero if closing is successful.
|
||||
*/
|
||||
|
||||
int ntfs_compressed_close(ntfs_attr *na, runlist_element *wrl, s64 offs)
|
||||
int ntfs_compressed_close(ntfs_attr *na, runlist_element *wrl, s64 offs,
|
||||
VCN *update_from)
|
||||
{
|
||||
ntfs_volume *vol;
|
||||
runlist_element *brl; /* entry containing the beginning of block */
|
||||
@ -1379,6 +1761,18 @@ int ntfs_compressed_close(ntfs_attr *na, runlist_element *wrl, s64 offs)
|
||||
BOOL fail;
|
||||
BOOL done;
|
||||
|
||||
if (na->unused_runs < 2) {
|
||||
ntfs_log_error("No unused runs for compressed close\n");
|
||||
errno = EIO;
|
||||
return (-1);
|
||||
}
|
||||
if (*update_from < 0) {
|
||||
ntfs_log_error("Bad update vcn for compressed close\n");
|
||||
errno = EIO;
|
||||
return (-1);
|
||||
}
|
||||
if (wrl->vcn < *update_from)
|
||||
*update_from = wrl->vcn;
|
||||
vol = na->ni->vol;
|
||||
compression_length = na->compression_block_clusters;
|
||||
done = FALSE;
|
||||
@ -1390,7 +1784,10 @@ int ntfs_compressed_close(ntfs_attr *na, runlist_element *wrl, s64 offs)
|
||||
if (inbuf) {
|
||||
start_vcn = (wrl->vcn + (offs >> vol->cluster_size_bits))
|
||||
& -compression_length;
|
||||
to_read = offs + ((wrl->vcn - start_vcn) << vol->cluster_size_bits);
|
||||
if (start_vcn < *update_from)
|
||||
*update_from = start_vcn;
|
||||
to_read = offs + ((wrl->vcn - start_vcn)
|
||||
<< vol->cluster_size_bits);
|
||||
brl = wrl;
|
||||
fail = FALSE;
|
||||
while (brl->vcn && (brl->vcn > start_vcn)) {
|
||||
@ -1403,7 +1800,8 @@ int ntfs_compressed_close(ntfs_attr *na, runlist_element *wrl, s64 offs)
|
||||
}
|
||||
if (!fail) {
|
||||
/* roffs can be an offset from another uncomp block */
|
||||
roffs = (start_vcn - brl->vcn) << vol->cluster_size_bits;
|
||||
roffs = (start_vcn - brl->vcn)
|
||||
<< vol->cluster_size_bits;
|
||||
if (to_read) {
|
||||
got = read_clusters(vol, brl, roffs, to_read,
|
||||
inbuf);
|
||||
@ -1414,7 +1812,8 @@ int ntfs_compressed_close(ntfs_attr *na, runlist_element *wrl, s64 offs)
|
||||
/* free the unused clusters */
|
||||
&& !ntfs_compress_free(na,brl,
|
||||
written + roffs,
|
||||
na->compression_block_size + roffs)) {
|
||||
na->compression_block_size + roffs,
|
||||
TRUE, update_from)) {
|
||||
done = TRUE;
|
||||
} else
|
||||
/* if compression failed, leave uncompressed */
|
||||
@ -1426,5 +1825,7 @@ int ntfs_compressed_close(ntfs_attr *na, runlist_element *wrl, s64 offs)
|
||||
free(inbuf);
|
||||
}
|
||||
}
|
||||
if (done && !valid_compressed_run(na,wrl,TRUE,"end compressed close"))
|
||||
done = FALSE;
|
||||
return (!done);
|
||||
}
|
||||
|
@ -31,9 +31,11 @@ extern s64 ntfs_compressed_attr_pread(ntfs_attr *na, s64 pos, s64 count,
|
||||
|
||||
extern s64 ntfs_compressed_pwrite(ntfs_attr *na, runlist_element *brl, s64 wpos,
|
||||
s64 offs, s64 to_write, s64 rounded,
|
||||
const void *b, int compressed_part);
|
||||
const void *b, int compressed_part,
|
||||
VCN *update_from);
|
||||
|
||||
extern int ntfs_compressed_close(ntfs_attr *na, runlist_element *brl, s64 offs);
|
||||
extern int ntfs_compressed_close(ntfs_attr *na, runlist_element *brl,
|
||||
s64 offs, VCN *update_from);
|
||||
|
||||
#endif /* defined _NTFS_COMPRESS_H */
|
||||
|
||||
|
@ -1,45 +1,43 @@
|
||||
/* config.h. Generated from config.h.in by configure. */
|
||||
/* config.h.in. Generated from configure.ac by autoheader. */
|
||||
|
||||
/* Define if building universal (internal helper macro) */
|
||||
/* #undef AC_APPLE_UNIVERSAL_BUILD */
|
||||
|
||||
//#define DEBUG
|
||||
/* Define to 1 if debug should be enabled */
|
||||
#ifdef DEBUG
|
||||
#define ENABLE_DEBUG
|
||||
#define NTFS_ENABLE_LOG
|
||||
#endif
|
||||
#undef ENABLE_DEBUG
|
||||
|
||||
/* Define to 1 if the nfconv patch should be enabled */
|
||||
#undef ENABLE_NFCONV
|
||||
|
||||
/* Define to 1 if using internal fuse */
|
||||
/* #undef FUSE_INTERNAL */
|
||||
#undef FUSE_INTERNAL
|
||||
|
||||
/* Define to 1 if you have the `atexit' function. */
|
||||
#define HAVE_ATEXIT 1
|
||||
|
||||
/* Define to 1 if you have the `basename' function. */
|
||||
/* #undef HAVE_BASENAME */
|
||||
#undef HAVE_BASENAME
|
||||
|
||||
/* Define to 1 if you have the <byteswap.h> header file. */
|
||||
/* #undef HAVE_BYTESWAP_H */
|
||||
#undef HAVE_BYTESWAP_H
|
||||
|
||||
/* Define to 1 if you have the `clock_gettime' function. */
|
||||
#undef HAVE_CLOCK_GETTIME
|
||||
|
||||
/* Define to 1 if you have the <ctype.h> header file. */
|
||||
#define HAVE_CTYPE_H 1
|
||||
|
||||
/* Define to 1 if you have the `daemon' function. */
|
||||
/* #undef HAVE_DAEMON */
|
||||
#undef HAVE_DAEMON
|
||||
|
||||
/* Define to 1 if you have the <dlfcn.h> header file. */
|
||||
/* #undef HAVE_DLFCN_H */
|
||||
#undef HAVE_DLFCN_H
|
||||
|
||||
/* Define to 1 if you don't have `vprintf' but do have `_doprnt.' */
|
||||
/* #undef HAVE_DOPRNT */
|
||||
#undef HAVE_DOPRNT
|
||||
|
||||
/* Define to 1 if you have the `dup2' function. */
|
||||
/* #undef HAVE_DUP2 */
|
||||
#undef HAVE_DUP2
|
||||
|
||||
/* Define to 1 if you have the <endian.h> header file. */
|
||||
/* #undef HAVE_ENDIAN_H */
|
||||
#undef HAVE_ENDIAN_H
|
||||
|
||||
/* Define to 1 if you have the <errno.h> header file. */
|
||||
#define HAVE_ERRNO_H 1
|
||||
@ -48,10 +46,10 @@
|
||||
#define HAVE_FCNTL_H 1
|
||||
|
||||
/* Define to 1 if you have the `fdatasync' function. */
|
||||
/* #undef HAVE_FDATASYNC */
|
||||
#undef HAVE_FDATASYNC
|
||||
|
||||
/* Define to 1 if you have the <features.h> header file. */
|
||||
/* #undef HAVE_FEATURES_H */
|
||||
#undef HAVE_FEATURES_H
|
||||
|
||||
/* Define to 1 if you have the `ffs' function. */
|
||||
#define HAVE_FFS 1
|
||||
@ -60,7 +58,7 @@
|
||||
#define HAVE_FORK 1
|
||||
|
||||
/* Define to 1 if you have the `getmntent' function. */
|
||||
/* #undef HAVE_GETMNTENT */
|
||||
#undef HAVE_GETMNTENT
|
||||
|
||||
/* Define to 1 if you have the <getopt.h> header file. */
|
||||
#define HAVE_GETOPT_H 1
|
||||
@ -68,8 +66,11 @@
|
||||
/* Define to 1 if you have the `getopt_long' function. */
|
||||
#define HAVE_GETOPT_LONG 1
|
||||
|
||||
/* Define to 1 if you have the `gettimeofday' function. */
|
||||
#undef HAVE_GETTIMEOFDAY
|
||||
|
||||
/* Define to 1 if you have the `hasmntopt' function. */
|
||||
/* #undef HAVE_HASMNTOPT */
|
||||
#undef HAVE_HASMNTOPT
|
||||
|
||||
/* Define to 1 if you have the <inttypes.h> header file. */
|
||||
#define HAVE_INTTYPES_H 1
|
||||
@ -78,19 +79,19 @@
|
||||
#define HAVE_LIBGEN_H 1
|
||||
|
||||
/* Define to 1 if you have the <libintl.h> header file. */
|
||||
/* #undef HAVE_LIBINTL_H */
|
||||
#undef HAVE_LIBINTL_H
|
||||
|
||||
/* Define to 1 if you have the <limits.h> header file. */
|
||||
#define HAVE_LIMITS_H 1
|
||||
|
||||
/* Define to 1 if you have the <linux/fd.h> header file. */
|
||||
/* #undef HAVE_LINUX_FD_H */
|
||||
#undef HAVE_LINUX_FD_H
|
||||
|
||||
/* Define to 1 if you have the <linux/hdreg.h> header file. */
|
||||
/* #undef HAVE_LINUX_HDREG_H */
|
||||
#undef HAVE_LINUX_HDREG_H
|
||||
|
||||
/* Define to 1 if you have the <linux/major.h> header file. */
|
||||
/* #undef HAVE_LINUX_MAJOR_H */
|
||||
#undef HAVE_LINUX_MAJOR_H
|
||||
|
||||
/* Define to 1 if you have the <locale.h> header file. */
|
||||
#define HAVE_LOCALE_H 1
|
||||
@ -111,25 +112,25 @@
|
||||
#define HAVE_MEMMOVE 1
|
||||
|
||||
/* Define to 1 if you have the <memory.h> header file. */
|
||||
/* #undef HAVE_MEMORY_H */
|
||||
#undef HAVE_MEMORY_H
|
||||
|
||||
/* Define to 1 if you have the `memset' function. */
|
||||
#define HAVE_MEMSET 1
|
||||
|
||||
/* Define to 1 if you have the <mntent.h> header file. */
|
||||
/* #undef HAVE_MNTENT_H */
|
||||
#undef HAVE_MNTENT_H
|
||||
|
||||
/* Define to 1 if you have the `realpath' function. */
|
||||
/* #undef HAVE_REALPATH */
|
||||
#undef HAVE_REALPATH
|
||||
|
||||
/* Define to 1 if you have the `regcomp' function. */
|
||||
/* #undef HAVE_REGCOMP */
|
||||
#undef HAVE_REGCOMP
|
||||
|
||||
/* Define to 1 if you have the `setlocale' function. */
|
||||
#define HAVE_SETLOCALE 1
|
||||
|
||||
/* Define to 1 if you have the `setxattr' function. */
|
||||
/* #undef HAVE_SETXATTR */
|
||||
#undef HAVE_SETXATTR
|
||||
|
||||
/* Define to 1 if `stat' has the bug that it succeeds when given the
|
||||
zero-length file name argument. */
|
||||
@ -169,7 +170,7 @@
|
||||
#define HAVE_STRFTIME 1
|
||||
|
||||
/* Define to 1 if you have the <strings.h> header file. */
|
||||
/* #undef HAVE_STRINGS_H */
|
||||
#undef HAVE_STRINGS_H
|
||||
|
||||
/* Define to 1 if you have the <string.h> header file. */
|
||||
#define HAVE_STRING_H 1
|
||||
@ -187,42 +188,44 @@
|
||||
#define HAVE_STRTOUL 1
|
||||
|
||||
/* Define to 1 if `st_atim' is member of `struct stat'. */
|
||||
/* #undef HAVE_STRUCT_STAT_ST_ATIM */
|
||||
#undef HAVE_STRUCT_STAT_ST_ATIM
|
||||
|
||||
/* Define to 1 if `st_atimensec' is member of `struct stat'. */
|
||||
#undef HAVE_STRUCT_STAT_ST_ATIMENSEC
|
||||
|
||||
/* Define to 1 if `st_atimespec' is member of `struct stat'. */
|
||||
/* #undef HAVE_STRUCT_STAT_ST_ATIMESPEC */
|
||||
#undef HAVE_STRUCT_STAT_ST_ATIMESPEC
|
||||
|
||||
/* Define to 1 if `st_blocks' is member of `struct stat'. */
|
||||
#define HAVE_STRUCT_STAT_ST_BLOCKS 1
|
||||
#undef HAVE_STRUCT_STAT_ST_BLOCKS
|
||||
|
||||
/* Define to 1 if `st_rdev' is member of `struct stat'. */
|
||||
#define HAVE_STRUCT_STAT_ST_RDEV 1
|
||||
#undef HAVE_STRUCT_STAT_ST_RDEV
|
||||
|
||||
/* Define to 1 if your `struct stat' has `st_blocks'. Deprecated, use
|
||||
`HAVE_STRUCT_STAT_ST_BLOCKS' instead. */
|
||||
#define HAVE_ST_BLOCKS 1
|
||||
#define HAVE_STRUCT_STAT_ST_BLOCKS 1
|
||||
#undef HAVE_ST_BLOCKS
|
||||
|
||||
/* Define to 1 if you have the `sysconf' function. */
|
||||
/* #undef HAVE_SYSCONF */
|
||||
#define HAVE_SYSCONF 1
|
||||
|
||||
/* Define to 1 if you have the <syslog.h> header file. */
|
||||
/* #undef HAVE_SYSLOG_H */
|
||||
#undef HAVE_SYSLOG_H
|
||||
|
||||
/* Define to 1 if you have the <sys/byteorder.h> header file. */
|
||||
/* #undef HAVE_SYS_BYTEORDER_H */
|
||||
#undef HAVE_SYS_BYTEORDER_H
|
||||
|
||||
/* Define to 1 if you have the <sys/endian.h> header file. */
|
||||
/* #undef HAVE_SYS_ENDIAN_H */
|
||||
#undef HAVE_SYS_ENDIAN_H
|
||||
|
||||
/* Define to 1 if you have the <sys/ioctl.h> header file. */
|
||||
/* #undef HAVE_SYS_IOCTL_H */
|
||||
#undef HAVE_SYS_IOCTL_H
|
||||
|
||||
/* Define to 1 if you have the <sys/mkdev.h> header file. */
|
||||
/* #undef HAVE_SYS_MKDEV_H */
|
||||
#undef HAVE_SYS_MKDEV_H
|
||||
|
||||
/* Define to 1 if you have the <sys/mount.h> header file. */
|
||||
/* #undef HAVE_SYS_MOUNT_H */
|
||||
#undef HAVE_SYS_MOUNT_H
|
||||
|
||||
/* Define to 1 if you have the <sys/param.h> header file. */
|
||||
#define HAVE_SYS_PARAM_H 1
|
||||
@ -234,13 +237,13 @@
|
||||
#define HAVE_SYS_STAT_H 1
|
||||
|
||||
/* Define to 1 if you have the <sys/sysmacros.h> header file. */
|
||||
/* #undef HAVE_SYS_SYSMACROS_H */
|
||||
#undef HAVE_SYS_SYSMACROS_H
|
||||
|
||||
/* Define to 1 if you have the <sys/types.h> header file. */
|
||||
#define HAVE_SYS_TYPES_H 1
|
||||
|
||||
/* Define to 1 if you have the <sys/vfs.h> header file. */
|
||||
/* #undef HAVE_SYS_VFS_H */
|
||||
#undef HAVE_SYS_VFS_H
|
||||
|
||||
/* Define to 1 if you have the <time.h> header file. */
|
||||
#define HAVE_TIME_H 1
|
||||
@ -249,13 +252,16 @@
|
||||
#define HAVE_UNISTD_H 1
|
||||
|
||||
/* Define to 1 if you have the `utime' function. */
|
||||
/* #undef HAVE_UTIME */
|
||||
#undef HAVE_UTIME
|
||||
|
||||
/* Define to 1 if you have the `utimensat' function. */
|
||||
#undef HAVE_UTIMENSAT
|
||||
|
||||
/* Define to 1 if you have the <utime.h> header file. */
|
||||
#define HAVE_UTIME_H 1
|
||||
|
||||
/* Define to 1 if `utime(file, NULL)' sets file's timestamp to the present. */
|
||||
/* #undef HAVE_UTIME_NULL */
|
||||
#undef HAVE_UTIME_NULL
|
||||
|
||||
/* Define to 1 if you have the `vprintf' function. */
|
||||
#define HAVE_VPRINTF 1
|
||||
@ -264,27 +270,23 @@
|
||||
#define HAVE_WCHAR_H 1
|
||||
|
||||
/* Define to 1 if you have the <windows.h> header file. */
|
||||
/* #undef HAVE_WINDOWS_H */
|
||||
#undef HAVE_WINDOWS_H
|
||||
|
||||
/* Define to 1 if the system has the type `_Bool'. */
|
||||
#define HAVE__BOOL 1
|
||||
#undef HAVE__BOOL
|
||||
|
||||
/* Don't update /etc/mtab */
|
||||
/* #undef IGNORE_MTAB */
|
||||
#undef IGNORE_MTAB
|
||||
|
||||
/* Define to 1 if `lstat' dereferences a symlink specified with a trailing
|
||||
slash. */
|
||||
/* #undef LSTAT_FOLLOWS_SLASHED_SYMLINK */
|
||||
|
||||
/* Define to the sub-directory in which libtool stores uninstalled libraries.
|
||||
*/
|
||||
#define LT_OBJDIR ".libs/"
|
||||
#undef LSTAT_FOLLOWS_SLASHED_SYMLINK
|
||||
|
||||
/* Define to 1 if your C compiler doesn't accept -c and -o together. */
|
||||
/* #undef NO_MINUS_C_MINUS_O */
|
||||
#undef NO_MINUS_C_MINUS_O
|
||||
|
||||
/* Don't use default IO ops */
|
||||
/* #undef NO_NTFS_DEVICE_DEFAULT_IO_OPS */
|
||||
#undef NO_NTFS_DEVICE_DEFAULT_IO_OPS
|
||||
|
||||
/* Name of package */
|
||||
#define PACKAGE "ntfs-3g"
|
||||
@ -296,16 +298,16 @@
|
||||
#define PACKAGE_NAME "ntfs-3g"
|
||||
|
||||
/* Define to the full name and version of this package. */
|
||||
#define PACKAGE_STRING "ntfs-3g 2009.4.4AR.16"
|
||||
#define PACKAGE_STRING "ntfs-3g 2010.8.8"
|
||||
|
||||
/* Define to the one symbol short name of this package. */
|
||||
#define PACKAGE_TARNAME "ntfs-3g"
|
||||
|
||||
/* Define to the version of this package. */
|
||||
#define PACKAGE_VERSION "2009.4.4AR.16"
|
||||
#define PACKAGE_VERSION "2010.8.8"
|
||||
|
||||
/* POSIX ACL support */
|
||||
/* #undef POSIXACLS */
|
||||
#undef POSIXACLS
|
||||
|
||||
/* Define to 1 if you have the ANSI C header files. */
|
||||
#define STDC_HEADERS 1
|
||||
@ -331,59 +333,42 @@
|
||||
# define __EXTENSIONS__ 1
|
||||
#endif
|
||||
|
||||
|
||||
/* Version number of package */
|
||||
#define VERSION "2009.4.4AR.16"
|
||||
#define VERSION "2010.8.8"
|
||||
|
||||
/* Define to 1 if this is a Windows OS */
|
||||
/* #undef WINDOWS */
|
||||
#undef WINDOWS
|
||||
|
||||
/* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most
|
||||
significant byte first (like Motorola and SPARC, unlike Intel). */
|
||||
#if defined AC_APPLE_UNIVERSAL_BUILD
|
||||
# if defined __BIG_ENDIAN__
|
||||
/* Define to 1 if your processor stores words with the most significant byte
|
||||
first (like Motorola and SPARC, unlike Intel and VAX). */
|
||||
#define WORDS_BIGENDIAN 1
|
||||
# endif
|
||||
#else
|
||||
# ifndef WORDS_BIGENDIAN
|
||||
# define WORDS_BIGENDIAN 1
|
||||
# endif
|
||||
#endif
|
||||
|
||||
/* Define to 1 if your processor stores words with the least significant byte
|
||||
first (like Intel and VAX, unlike Motorola and SPARC). */
|
||||
/* #undef WORDS_LITTLEENDIAN */
|
||||
#undef WORDS_LITTLEENDIAN
|
||||
|
||||
/* Number of bits in a file offset, on hosts where this is settable. */
|
||||
/* #undef _FILE_OFFSET_BITS */
|
||||
#define _FILE_OFFSET_BITS 64
|
||||
|
||||
/* Enable GNU extensions on systems that have them. */
|
||||
#ifndef _GNU_SOURCE
|
||||
# undef _GNU_SOURCE
|
||||
#endif
|
||||
|
||||
/* Define for large files, on AIX-style hosts. */
|
||||
/* #undef _LARGE_FILES */
|
||||
|
||||
/* Define to 1 if on MINIX. */
|
||||
/* #undef _MINIX */
|
||||
|
||||
/* Define to 2 if the system does not provide POSIX.1 features except with
|
||||
this defined. */
|
||||
/* #undef _POSIX_1_SOURCE */
|
||||
|
||||
/* Define to 1 if you need to in order for `stat' and other things to work. */
|
||||
/* #undef _POSIX_SOURCE */
|
||||
#define _LARGE_FILES 1
|
||||
|
||||
/* Required define if using POSIX threads */
|
||||
/* #undef _REENTRANT */
|
||||
|
||||
/* Define to empty if `const' does not conform to ANSI C. */
|
||||
/* #undef const */
|
||||
#undef _REENTRANT
|
||||
|
||||
/* Define to `__inline__' or `__inline' if that's what the C compiler
|
||||
calls it, or to nothing if 'inline' is not supported under any name. */
|
||||
#ifndef __cplusplus
|
||||
/* #undef inline */
|
||||
#define inline __inline__
|
||||
#endif
|
||||
|
||||
/* Define to `long int' if <sys/types.h> does not define. */
|
||||
/* #undef off_t */
|
||||
#undef off_t
|
||||
|
||||
/* Define to `unsigned int' if <sys/types.h> does not define. */
|
||||
/* #undef size_t */
|
||||
#undef size_t
|
||||
|
@ -28,6 +28,18 @@
|
||||
|
||||
#ifndef NO_NTFS_DEVICE_DEFAULT_IO_OPS
|
||||
|
||||
#ifndef __CYGWIN32__
|
||||
|
||||
#ifndef GEKKO
|
||||
/* Not on Cygwin; use standard Unix style low level device operations. */
|
||||
#define ntfs_device_default_io_ops ntfs_device_unix_io_ops
|
||||
#else
|
||||
/* Wii i/o device. */
|
||||
#define ntfs_device_default_io_ops ntfs_device_gekko_io_ops
|
||||
#endif
|
||||
|
||||
#else /* __CYGWIN32__ */
|
||||
|
||||
#ifndef HDIO_GETGEO
|
||||
# define HDIO_GETGEO 0x301
|
||||
/**
|
||||
@ -53,8 +65,10 @@ struct hd_geometry {
|
||||
# define BLKBSZSET 0x40041271
|
||||
#endif
|
||||
|
||||
/* On Nintendo GameCube/Wii; use Gekko low level device operations. */
|
||||
#define ntfs_device_default_io_ops ntfs_device_gekko_io_ops
|
||||
/* On Cygwin; use Win32 low level device operations. */
|
||||
#define ntfs_device_default_io_ops ntfs_device_win32_io_ops
|
||||
|
||||
#endif /* __CYGWIN32__ */
|
||||
|
||||
|
||||
/* Forward declaration. */
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -61,6 +61,9 @@ extern ntfschar NTFS_INDEX_R[3];
|
||||
|
||||
extern u64 ntfs_inode_lookup_by_name(ntfs_inode *dir_ni,
|
||||
const ntfschar *uname, const int uname_len);
|
||||
extern u64 ntfs_inode_lookup_by_mbsname(ntfs_inode *dir_ni, const char *name);
|
||||
extern void ntfs_inode_update_mbsname(ntfs_inode *dir_ni, const char *name,
|
||||
u64 inum);
|
||||
|
||||
extern ntfs_inode *ntfs_pathname_to_inode(ntfs_volume *vol, ntfs_inode *parent,
|
||||
const char *pathname);
|
||||
@ -104,12 +107,22 @@ typedef int (*ntfs_filldir_t)(void *dirent, const ntfschar *name,
|
||||
extern int ntfs_readdir(ntfs_inode *dir_ni, s64 *pos,
|
||||
void *dirent, ntfs_filldir_t filldir);
|
||||
|
||||
int ntfs_get_ntfs_dos_name(const char *path,
|
||||
char *value, size_t size, ntfs_inode *ni);
|
||||
int ntfs_set_ntfs_dos_name(const char *path,
|
||||
const char *value, size_t size, int flags,
|
||||
ntfs_inode *ni);
|
||||
int ntfs_remove_ntfs_dos_name(const char *path, ntfs_inode *ni);
|
||||
ntfs_inode *ntfs_dir_parent_inode(ntfs_inode *ni);
|
||||
|
||||
int ntfs_get_ntfs_dos_name(ntfs_inode *ni, ntfs_inode *dir_ni,
|
||||
char *value, size_t size);
|
||||
int ntfs_set_ntfs_dos_name(ntfs_inode *ni, ntfs_inode *dir_ni,
|
||||
const char *value, size_t size, int flags);
|
||||
int ntfs_remove_ntfs_dos_name(ntfs_inode *ni, ntfs_inode *dir_ni);
|
||||
|
||||
#if CACHE_INODE_SIZE
|
||||
|
||||
struct CACHED_GENERIC;
|
||||
|
||||
extern int ntfs_dir_inode_hash(const struct CACHED_GENERIC *cached);
|
||||
extern int ntfs_dir_lookup_hash(const struct CACHED_GENERIC *cached);
|
||||
|
||||
#endif
|
||||
|
||||
#endif /* defined _NTFS_DIR_H */
|
||||
|
||||
|
@ -4,7 +4,7 @@
|
||||
* This module is part of ntfs-3g library
|
||||
*
|
||||
* Copyright (c) 2009 Martin Bene
|
||||
* Copyright (c) 2009 Jean-Pierre Andre
|
||||
* Copyright (c) 2009-2010 Jean-Pierre Andre
|
||||
*
|
||||
* This program/include file is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as published
|
||||
@ -73,8 +73,7 @@ static ntfschar logged_utility_stream_name[] = {
|
||||
* Get the ntfs EFS info into an extended attribute
|
||||
*/
|
||||
|
||||
int ntfs_get_efs_info(const char *path,
|
||||
char *value, size_t size, ntfs_inode *ni)
|
||||
int ntfs_get_efs_info(ntfs_inode *ni, char *value, size_t size)
|
||||
{
|
||||
EFS_ATTR_HEADER *efs_info;
|
||||
s64 attr_size = 0;
|
||||
@ -102,43 +101,133 @@ int ntfs_get_efs_info(const char *path,
|
||||
} else {
|
||||
if (efs_info) {
|
||||
free(efs_info);
|
||||
ntfs_log_info("Bad efs_info for file %s\n",path);
|
||||
ntfs_log_error("Bad efs_info for inode %lld\n",
|
||||
(long long)ni->mft_no);
|
||||
} else {
|
||||
ntfs_log_info("Could not get efsinfo"
|
||||
" for file %s\n", path);
|
||||
ntfs_log_error("Could not get efsinfo"
|
||||
" for inode %lld\n",
|
||||
(long long)ni->mft_no);
|
||||
}
|
||||
errno = EIO;
|
||||
attr_size = 0;
|
||||
}
|
||||
} else {
|
||||
errno = ENODATA;
|
||||
ntfs_log_info("File %s is not encrypted",path);
|
||||
ntfs_log_trace("Inode %lld is not encrypted\n",
|
||||
(long long)ni->mft_no);
|
||||
}
|
||||
}
|
||||
return (attr_size ? (int)attr_size : -errno);
|
||||
}
|
||||
|
||||
/*
|
||||
* Fix all encrypted AT_DATA attributes of an inode
|
||||
*
|
||||
* The fix may require making an attribute non resident, which
|
||||
* requires more space in the MFT record, and may cause some
|
||||
* attribute to be expelled and the full record to be reorganized.
|
||||
* When this happens, the search for data attributes has to be
|
||||
* reinitialized.
|
||||
*
|
||||
* Returns zero if successful.
|
||||
* -1 if there is a problem.
|
||||
*/
|
||||
|
||||
static int fixup_loop(ntfs_inode *ni)
|
||||
{
|
||||
ntfs_attr_search_ctx *ctx;
|
||||
ntfs_attr *na;
|
||||
ATTR_RECORD *a;
|
||||
BOOL restart;
|
||||
BOOL first;
|
||||
int cnt;
|
||||
int maxcnt;
|
||||
int res = 0;
|
||||
|
||||
maxcnt = 0;
|
||||
do {
|
||||
restart = FALSE;
|
||||
ctx = ntfs_attr_get_search_ctx(ni, NULL);
|
||||
if (!ctx) {
|
||||
ntfs_log_error("Failed to get ctx for efs\n");
|
||||
res = -1;
|
||||
}
|
||||
cnt = 0;
|
||||
while (!restart && !res
|
||||
&& !ntfs_attr_lookup(AT_DATA, NULL, 0,
|
||||
CASE_SENSITIVE, 0, NULL, 0, ctx)) {
|
||||
cnt++;
|
||||
a = ctx->attr;
|
||||
na = ntfs_attr_open(ctx->ntfs_ino, AT_DATA,
|
||||
(ntfschar*)((u8*)a + le16_to_cpu(a->name_offset)),
|
||||
a->name_length);
|
||||
if (!na) {
|
||||
ntfs_log_error("can't open DATA Attribute\n");
|
||||
res = -1;
|
||||
}
|
||||
if (na && !(ctx->attr->flags & ATTR_IS_ENCRYPTED)) {
|
||||
if (!NAttrNonResident(na)
|
||||
&& ntfs_attr_make_non_resident(na, ctx)) {
|
||||
/*
|
||||
* ntfs_attr_make_non_resident fails if there
|
||||
* is not enough space in the MFT record.
|
||||
* When this happens, force making non-resident
|
||||
* so that some other attribute is expelled.
|
||||
*/
|
||||
if (ntfs_attr_force_non_resident(na)) {
|
||||
res = -1;
|
||||
} else {
|
||||
/* make sure there is some progress */
|
||||
if (cnt <= maxcnt) {
|
||||
errno = EIO;
|
||||
ntfs_log_error("Multiple failure"
|
||||
" making non resident\n");
|
||||
res = -1;
|
||||
} else {
|
||||
ntfs_attr_put_search_ctx(ctx);
|
||||
ctx = (ntfs_attr_search_ctx*)NULL;
|
||||
restart = TRUE;
|
||||
maxcnt = cnt;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!restart && !res
|
||||
&& ntfs_efs_fixup_attribute(ctx, na)) {
|
||||
ntfs_log_error("Error in efs fixup of AT_DATA Attribute\n");
|
||||
res = -1;
|
||||
}
|
||||
}
|
||||
if (na)
|
||||
ntfs_attr_close(na);
|
||||
}
|
||||
first = FALSE;
|
||||
} while (restart && !res);
|
||||
if (ctx)
|
||||
ntfs_attr_put_search_ctx(ctx);
|
||||
return (res);
|
||||
}
|
||||
|
||||
/*
|
||||
* Set the efs data from an extended attribute
|
||||
* Warning : the new data is not checked
|
||||
* Returns 0, or -1 if there is a problem
|
||||
*/
|
||||
|
||||
int ntfs_set_efs_info(const char *path __attribute__((unused)),
|
||||
const char *value, size_t size, int flags,
|
||||
ntfs_inode *ni)
|
||||
int ntfs_set_efs_info(ntfs_inode *ni, const char *value, size_t size,
|
||||
int flags)
|
||||
|
||||
{
|
||||
int res;
|
||||
int written;
|
||||
ntfs_attr *na;
|
||||
const EFS_ATTR_HEADER *info_header;
|
||||
ntfs_attr_search_ctx *ctx;
|
||||
|
||||
res = 0;
|
||||
if (ni && value && size) {
|
||||
if (ni->flags & (FILE_ATTR_ENCRYPTED | FILE_ATTR_COMPRESSED)) {
|
||||
if (ni->flags & FILE_ATTR_ENCRYPTED) {
|
||||
ntfs_log_info("File %s already encrypted",path);
|
||||
ntfs_log_trace("Inode %lld already encrypted\n",
|
||||
(long long)ni->mft_no);
|
||||
errno = EEXIST;
|
||||
} else {
|
||||
/*
|
||||
@ -147,8 +236,8 @@ int ntfs_set_efs_info(const char *path __attribute__((unused)),
|
||||
* restored as compressed.
|
||||
* TODO : decompress first.
|
||||
*/
|
||||
ntfs_log_error("File %s cannot be encrypted and compressed\n",
|
||||
path);
|
||||
ntfs_log_error("Inode %lld cannot be encrypted and compressed\n",
|
||||
(long long)ni->mft_no);
|
||||
errno = EIO;
|
||||
}
|
||||
return -1;
|
||||
@ -207,20 +296,8 @@ int ntfs_set_efs_info(const char *path __attribute__((unused)),
|
||||
/* iterate over AT_DATA attributes */
|
||||
/* set encrypted flag, truncate attribute to match padding bytes */
|
||||
|
||||
ctx = ntfs_attr_get_search_ctx(ni, NULL);
|
||||
if (!ctx) {
|
||||
ntfs_log_error("Failed to get ctx for efs\n");
|
||||
return (-1);
|
||||
}
|
||||
while (!ntfs_attr_lookup(AT_DATA, NULL, 0,
|
||||
CASE_SENSITIVE, 0, NULL, 0, ctx)) {
|
||||
if (ntfs_efs_fixup_attribute(ctx, NULL)) {
|
||||
ntfs_log_error("Error in efs fixup of AT_DATA Attribute");
|
||||
ntfs_attr_put_search_ctx(ctx);
|
||||
return(-1);
|
||||
}
|
||||
}
|
||||
ntfs_attr_put_search_ctx(ctx);
|
||||
if (fixup_loop(ni))
|
||||
return -1;
|
||||
}
|
||||
ni->flags |= FILE_ATTR_ENCRYPTED;
|
||||
NInoSetDirty(ni);
|
||||
@ -247,15 +324,14 @@ int ntfs_set_efs_info(const char *path __attribute__((unused)),
|
||||
int ntfs_efs_fixup_attribute(ntfs_attr_search_ctx *ctx, ntfs_attr *na)
|
||||
{
|
||||
u64 newsize;
|
||||
u64 oldsize;
|
||||
le16 appended_bytes;
|
||||
u16 padding_length;
|
||||
ATTR_RECORD *a;
|
||||
ntfs_inode *ni;
|
||||
BOOL close_na = FALSE;
|
||||
BOOL close_ctx = FALSE;
|
||||
|
||||
if (!ctx && !na) {
|
||||
ntfs_log_error("neither ctx nor na specified for efs_fixup_attribute\n");
|
||||
if (!na) {
|
||||
ntfs_log_error("no na specified for efs_fixup_attribute\n");
|
||||
goto err_out;
|
||||
}
|
||||
if (!ctx) {
|
||||
@ -270,26 +346,24 @@ int ntfs_efs_fixup_attribute(ntfs_attr_search_ctx *ctx, ntfs_attr *na)
|
||||
ntfs_log_error("attr lookup for AT_DATA attribute failed in efs fixup\n");
|
||||
goto err_out;
|
||||
}
|
||||
} else {
|
||||
if (!NAttrNonResident(na)) {
|
||||
ntfs_log_error("Cannot make non resident"
|
||||
" when a context has been allocated\n");
|
||||
goto err_out;
|
||||
}
|
||||
}
|
||||
|
||||
a = ctx->attr;
|
||||
if (!na) {
|
||||
na = ntfs_attr_open(ctx->ntfs_ino, AT_DATA,
|
||||
(ntfschar*)((u8*)a + le16_to_cpu(a->name_offset)),
|
||||
a->name_length);
|
||||
if (!na) {
|
||||
ntfs_log_error("can't open DATA Attribute\n");
|
||||
return (-1);
|
||||
}
|
||||
close_na = TRUE;
|
||||
}
|
||||
/* no extra bytes are added to void attributes */
|
||||
oldsize = na->data_size;
|
||||
if (oldsize) {
|
||||
/* make sure size is valid for a raw encrypted stream */
|
||||
if ((na->data_size & 511) != 2) {
|
||||
ntfs_log_error("Bad raw encrypted stream");
|
||||
if ((oldsize & 511) != 2) {
|
||||
ntfs_log_error("Bad raw encrypted stream\n");
|
||||
goto err_out;
|
||||
}
|
||||
/* read padding length from last two bytes of attribute */
|
||||
if (ntfs_attr_pread(na, na->data_size-2, 2, &appended_bytes) != 2) {
|
||||
if (ntfs_attr_pread(na, oldsize - 2, 2, &appended_bytes) != 2) {
|
||||
ntfs_log_error("Error reading padding length\n");
|
||||
goto err_out;
|
||||
}
|
||||
@ -297,22 +371,48 @@ int ntfs_efs_fixup_attribute(ntfs_attr_search_ctx *ctx, ntfs_attr *na)
|
||||
if (padding_length > 511 || padding_length > na->data_size-2) {
|
||||
errno = EINVAL;
|
||||
ntfs_log_error("invalid padding length %d for data_size %lld\n",
|
||||
padding_length, (long long)na->data_size);
|
||||
padding_length, (long long)oldsize);
|
||||
goto err_out;
|
||||
}
|
||||
newsize = na->data_size - padding_length - 2;
|
||||
/* truncate attribute to possibly free clusters allocated
|
||||
for the last two bytes */
|
||||
if (ntfs_attr_truncate(na, na->data_size-2)) {
|
||||
newsize = oldsize - padding_length - 2;
|
||||
/*
|
||||
* truncate attribute to possibly free clusters allocated
|
||||
* for the last two bytes, but do not truncate to new size
|
||||
* to avoid losing useful data
|
||||
*/
|
||||
if (ntfs_attr_truncate(na, oldsize - 2)) {
|
||||
ntfs_log_error("Error truncating attribute\n");
|
||||
goto err_out;
|
||||
}
|
||||
} else
|
||||
newsize = 0;
|
||||
|
||||
/* Encrypted AT_DATA Attributes MUST be non-resident */
|
||||
/*
|
||||
* Encrypted AT_DATA Attributes MUST be non-resident
|
||||
* This has to be done after the attribute is resized, as
|
||||
* resizing down to zero may cause the attribute to be made
|
||||
* resident.
|
||||
*/
|
||||
if (!NAttrNonResident(na)
|
||||
&& ntfs_attr_make_non_resident(na, ctx)) {
|
||||
if (!close_ctx
|
||||
|| ntfs_attr_force_non_resident(na)) {
|
||||
ntfs_log_error("Error making DATA attribute non-resident\n");
|
||||
goto err_out;
|
||||
} else {
|
||||
/*
|
||||
* must reinitialize context after forcing
|
||||
* non-resident. We need a context for updating
|
||||
* the state, and at this point, we are sure
|
||||
* the context is not used elsewhere.
|
||||
*/
|
||||
ntfs_attr_reinit_search_ctx(ctx);
|
||||
if (ntfs_attr_lookup(AT_DATA, na->name, na->name_len,
|
||||
CASE_SENSITIVE, 0, NULL, 0, ctx)) {
|
||||
ntfs_log_error("attr lookup for AT_DATA attribute failed in efs fixup\n");
|
||||
goto err_out;
|
||||
}
|
||||
}
|
||||
}
|
||||
ni = na->ni;
|
||||
if (!na->name_len) {
|
||||
@ -321,8 +421,6 @@ int ntfs_efs_fixup_attribute(ntfs_attr_search_ctx *ctx, ntfs_attr *na)
|
||||
}
|
||||
NInoSetDirty(ni);
|
||||
NInoFileNameSetDirty(ni);
|
||||
if (close_na)
|
||||
ntfs_attr_close(na);
|
||||
|
||||
ctx->attr->data_size = cpu_to_le64(newsize);
|
||||
if (le64_to_cpu(ctx->attr->initialized_size) > newsize)
|
||||
@ -333,8 +431,6 @@ int ntfs_efs_fixup_attribute(ntfs_attr_search_ctx *ctx, ntfs_attr *na)
|
||||
|
||||
return (0);
|
||||
err_out:
|
||||
if (close_na && na)
|
||||
ntfs_attr_close(na);
|
||||
if (close_ctx && ctx)
|
||||
ntfs_attr_put_search_ctx(ctx);
|
||||
return (-1);
|
||||
|
@ -21,11 +21,10 @@
|
||||
#ifndef EFS_H
|
||||
#define EFS_H
|
||||
|
||||
int ntfs_get_efs_info(const char *path,
|
||||
char *value, size_t size, ntfs_inode *ni);
|
||||
int ntfs_set_efs_info(const char *path,
|
||||
const char *value, size_t size, int flags,
|
||||
ntfs_inode *ni);
|
||||
int ntfs_get_efs_info(ntfs_inode *ni, char *value, size_t size);
|
||||
|
||||
int ntfs_set_efs_info(ntfs_inode *ni,
|
||||
const char *value, size_t size, int flags);
|
||||
int ntfs_efs_fixup_attribute(ntfs_attr_search_ctx *ctx, ntfs_attr *na);
|
||||
|
||||
#endif /* EFS_H */
|
||||
|
@ -132,22 +132,13 @@ static int ntfs_device_gekko_io_open(struct ntfs_device *dev, int flags)
|
||||
fd->len = (fd->sectorCount * fd->sectorSize);
|
||||
fd->ino = le64_to_cpu(boot.volume_serial_number);
|
||||
|
||||
// If the device sector size is not 512 bytes then we cannot continue,
|
||||
// gekko disc I/O works on the assumption that sectors are always 512 bytes long.
|
||||
// TODO: Implement support for non-512 byte sector sizes through some fancy maths!?
|
||||
if (fd->sectorSize != BYTES_PER_SECTOR) {
|
||||
ntfs_log_error("Boot sector claims there is %i bytes per sector; expected %i\n", fd->sectorSize, BYTES_PER_SECTOR);
|
||||
errno = EIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Mark the device as read-only (if required)
|
||||
if (flags & O_RDONLY) {
|
||||
NDevSetReadOnly(dev);
|
||||
}
|
||||
|
||||
// Create the cache
|
||||
fd->cache = _NTFS_cache_constructor(fd->cachePageCount, fd->cachePageSize, interface, fd->startSector + fd->sectorCount);
|
||||
fd->cache = _NTFS_cache_constructor(fd->cachePageCount, fd->cachePageSize, interface, fd->startSector + fd->sectorCount, fd->sectorSize);
|
||||
|
||||
// Mark the device as open
|
||||
NDevSetBlock(dev);
|
||||
@ -288,29 +279,31 @@ static s64 ntfs_device_gekko_io_readbytes(struct ntfs_device *dev, s64 offset, s
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(offset < 0)
|
||||
{
|
||||
errno = EROFS;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(!count)
|
||||
return 0;
|
||||
|
||||
sec_t sec_start = (sec_t) fd->startSector;
|
||||
sec_t sec_count = 1;
|
||||
u32 buffer_offset = 0;
|
||||
u32 buffer_offset = (u32) (offset % fd->sectorSize);
|
||||
u8 *buffer = NULL;
|
||||
|
||||
// Determine the range of sectors required for this read
|
||||
if (offset > 0) {
|
||||
sec_start += (sec_t) floor((f64) offset/fd->sectorSize);
|
||||
buffer_offset = (u32) (offset % fd->sectorSize);
|
||||
sec_start += (sec_t) floor((f64) offset / (f64) fd->sectorSize);
|
||||
}
|
||||
if (count > fd->sectorSize) {
|
||||
sec_count = (sec_t) ceil((f64) count/fd->sectorSize);
|
||||
|
||||
if(buffer_offset > 0)
|
||||
sec_count += 1;
|
||||
if (buffer_offset+count > fd->sectorSize) {
|
||||
sec_count = (sec_t) ceil((f64) (buffer_offset+count) / (f64) fd->sectorSize);
|
||||
}
|
||||
|
||||
// If this read happens to be on the sector boundaries then do the read straight into the destination buffer
|
||||
|
||||
if((offset % fd->sectorSize == 0) && (count % fd->sectorSize == 0)) {
|
||||
if((buffer_offset == 0) && (count % fd->sectorSize == 0)) {
|
||||
|
||||
// Read from the device
|
||||
ntfs_log_trace("direct read from sector %d (%d sector(s) long)\n", sec_start, sec_count);
|
||||
@ -378,29 +371,30 @@ static s64 ntfs_device_gekko_io_writebytes(struct ntfs_device *dev, s64 offset,
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(!count)
|
||||
if(count < 0 || offset < 0) {
|
||||
errno = EROFS;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(count == 0)
|
||||
return 0;
|
||||
|
||||
sec_t sec_start = (sec_t) fd->startSector;
|
||||
sec_t sec_count = 1;
|
||||
u32 buffer_offset = 0;
|
||||
u32 buffer_offset = (u32) (offset % fd->sectorSize);
|
||||
u8 *buffer = NULL;
|
||||
|
||||
// Determine the range of sectors required for this write
|
||||
if (offset > 0) {
|
||||
sec_start += (sec_t) floor((f64) offset/fd->sectorSize);
|
||||
buffer_offset = (u32) (offset % fd->sectorSize);
|
||||
sec_start += (sec_t) floor((f64) offset / (f64) fd->sectorSize);
|
||||
}
|
||||
if (count > fd->sectorSize) {
|
||||
sec_count = (sec_t) ceil((f64) count/fd->sectorSize);
|
||||
|
||||
if(buffer_offset > 0)
|
||||
sec_count += 1;
|
||||
if ((buffer_offset+count) > fd->sectorSize) {
|
||||
sec_count = (sec_t) ceil((f64) (buffer_offset+count) / (f64) fd->sectorSize);
|
||||
}
|
||||
|
||||
// If this write happens to be on the sector boundaries then do the write straight to disc
|
||||
if((offset % fd->sectorSize == 0) && (count % fd->sectorSize == 0)) {
|
||||
|
||||
if((buffer_offset == 0) && (count % fd->sectorSize == 0))
|
||||
{
|
||||
// Write to the device
|
||||
ntfs_log_trace("direct write to sector %d (%d sector(s) long)\n", sec_start, sec_count);
|
||||
if (!ntfs_device_gekko_io_writesectors(dev, sec_start, sec_count, buf)) {
|
||||
@ -408,10 +402,10 @@ static s64 ntfs_device_gekko_io_writebytes(struct ntfs_device *dev, s64 offset,
|
||||
errno = EIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Else write from a buffer aligned to the sector boundaries
|
||||
} else {
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
// Allocate a buffer to hold the write data
|
||||
buffer = (u8 *) ntfs_alloc(sec_count * fd->sectorSize);
|
||||
if (!buffer) {
|
||||
@ -421,12 +415,24 @@ static s64 ntfs_device_gekko_io_writebytes(struct ntfs_device *dev, s64 offset,
|
||||
// Read the first and last sectors of the buffer from disc (if required)
|
||||
// NOTE: This is done because the data does not line up with the sector boundaries,
|
||||
// we just read in the buffer edges where the data overlaps with the rest of the disc
|
||||
if (!ntfs_device_gekko_io_readsectors(dev, sec_start, sec_count, buffer)) {
|
||||
if(buffer_offset != 0)
|
||||
{
|
||||
if (!ntfs_device_gekko_io_readsectors(dev, sec_start, 1, buffer)) {
|
||||
ntfs_log_perror("read failure @ sector %d\n", sec_start);
|
||||
ntfs_free(buffer);
|
||||
errno = EIO;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if((buffer_offset+count) % fd->sectorSize != 0)
|
||||
{
|
||||
if (!ntfs_device_gekko_io_readsectors(dev, sec_start + sec_count - 1, 1, buffer + ((sec_count-1) * fd->sectorSize))) {
|
||||
ntfs_log_perror("read failure @ sector %d\n", sec_start + sec_count - 1);
|
||||
ntfs_free(buffer);
|
||||
errno = EIO;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Copy the data into the write buffer
|
||||
memcpy(buffer + buffer_offset, buf, count);
|
||||
@ -445,7 +451,6 @@ static s64 ntfs_device_gekko_io_writebytes(struct ntfs_device *dev, s64 offset,
|
||||
}
|
||||
|
||||
// Mark the device as dirty (if we actually wrote anything)
|
||||
if (count > 0)
|
||||
NDevSetDirty(dev);
|
||||
|
||||
return count;
|
||||
@ -608,11 +613,6 @@ static int ntfs_device_gekko_io_ioctl(struct ntfs_device *dev, int request, void
|
||||
#if defined(BLKBSZSET)
|
||||
case BLKBSZSET: {
|
||||
int sectorSize = *(int*)argp;
|
||||
if (sectorSize != BYTES_PER_SECTOR) {
|
||||
ntfs_log_perror("Attempt to set sector size to an unsupported value (%i), ignored\n", sectorSize);
|
||||
errno = EOPNOTSUPP;
|
||||
return -1;
|
||||
}
|
||||
fd->sectorSize = sectorSize;
|
||||
return 0;
|
||||
}
|
||||
|
@ -26,12 +26,10 @@
|
||||
#endif
|
||||
|
||||
#include "types.h"
|
||||
#include "cache.h"
|
||||
#include "cache2.h"
|
||||
#include <gccore.h>
|
||||
#include <ogc/disc_io.h>
|
||||
|
||||
#define BYTES_PER_SECTOR 512 /* Forced by gekko disc i/o */
|
||||
|
||||
/**
|
||||
* gekko_fd - Gekko device driver descriptor
|
||||
*/
|
||||
|
@ -38,9 +38,9 @@
|
||||
#endif
|
||||
|
||||
#include "attrib.h"
|
||||
#include "collate.h"
|
||||
#include "debug.h"
|
||||
#include "index.h"
|
||||
#include "collate.h"
|
||||
#include "mst.h"
|
||||
#include "dir.h"
|
||||
#include "logging.h"
|
||||
@ -517,8 +517,13 @@ static int ntfs_ie_lookup(const void *key, const int key_len,
|
||||
* Not a perfect match, need to do full blown collation so we
|
||||
* know which way in the B+tree we have to go.
|
||||
*/
|
||||
rc = ntfs_collate(icx->ni->vol, icx->cr, key, key_len, &ie->key,
|
||||
le16_to_cpu(ie->key_length));
|
||||
if (!icx->collate) {
|
||||
ntfs_log_error("Collation function not defined\n");
|
||||
errno = EOPNOTSUPP;
|
||||
return STATUS_ERROR;
|
||||
}
|
||||
rc = icx->collate(icx->ni->vol, key, key_len,
|
||||
&ie->key, le16_to_cpu(ie->key_length));
|
||||
if (rc == NTFS_COLLATION_ERROR) {
|
||||
ntfs_log_error("Collation error. Perhaps a filename "
|
||||
"contains invalid characters?\n");
|
||||
@ -697,12 +702,12 @@ int ntfs_index_lookup(const void *key, const int key_len, ntfs_index_context *ic
|
||||
icx->vcn_size_bits = ni->vol->cluster_size_bits;
|
||||
else
|
||||
icx->vcn_size_bits = ni->vol->sector_size_bits;
|
||||
|
||||
icx->cr = ir->collation_rule;
|
||||
if (!ntfs_is_collation_rule_supported(icx->cr)) {
|
||||
/* get the appropriate collation function */
|
||||
icx->collate = ntfs_get_collate_function(ir->collation_rule);
|
||||
if (!icx->collate) {
|
||||
err = errno = EOPNOTSUPP;
|
||||
ntfs_log_perror("Unknown collation rule 0x%x",
|
||||
(unsigned)le32_to_cpu(icx->cr));
|
||||
(unsigned)le32_to_cpu(ir->collation_rule));
|
||||
goto err_out;
|
||||
}
|
||||
|
||||
|
@ -63,6 +63,9 @@
|
||||
|
||||
#define MAX_PARENT_VCN 32
|
||||
|
||||
typedef int (*COLLATE)(ntfs_volume *vol, const void *data1, int len1,
|
||||
const void *data2, int len2);
|
||||
|
||||
/**
|
||||
* struct ntfs_index_context -
|
||||
* @ni: inode containing the @entry described by this context
|
||||
@ -116,7 +119,7 @@ typedef struct {
|
||||
INDEX_ENTRY *entry;
|
||||
void *data;
|
||||
u16 data_len;
|
||||
COLLATION_RULES cr;
|
||||
COLLATE collate;
|
||||
BOOL is_in_root;
|
||||
INDEX_ROOT *ir;
|
||||
ntfs_attr_search_ctx *actx;
|
||||
|
@ -5,6 +5,7 @@
|
||||
* Copyright (c) 2002-2008 Szabolcs Szakacsits
|
||||
* Copyright (c) 2004-2007 Yura Pakhuchiy
|
||||
* Copyright (c) 2004-2005 Richard Russon
|
||||
* Copyright (c) 2009-2010 Jean-Pierre Andre
|
||||
*
|
||||
* This program/include file is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as published
|
||||
@ -39,10 +40,13 @@
|
||||
#include <sys/xattr.h>
|
||||
#endif
|
||||
|
||||
#include "param.h"
|
||||
#include "compat.h"
|
||||
#include "types.h"
|
||||
#include "attrib.h"
|
||||
#include "volume.h"
|
||||
#include "cache.h"
|
||||
#include "inode.h"
|
||||
#include "attrib.h"
|
||||
#include "debug.h"
|
||||
#include "mft.h"
|
||||
#include "attrlist.h"
|
||||
@ -152,7 +156,7 @@ static void __ntfs_inode_release(ntfs_inode *ni)
|
||||
* Return a pointer to the ntfs_inode structure on success or NULL on error,
|
||||
* with errno set to the error code.
|
||||
*/
|
||||
ntfs_inode *ntfs_inode_open(ntfs_volume *vol, const MFT_REF mref)
|
||||
static ntfs_inode *ntfs_inode_real_open(ntfs_volume *vol, const MFT_REF mref)
|
||||
{
|
||||
s64 l;
|
||||
ntfs_inode *ni = NULL;
|
||||
@ -190,10 +194,10 @@ ntfs_inode *ntfs_inode_open(ntfs_volume *vol, const MFT_REF mref)
|
||||
std_info = (STANDARD_INFORMATION *)((u8 *)ctx->attr +
|
||||
le16_to_cpu(ctx->attr->value_offset));
|
||||
ni->flags = std_info->file_attributes;
|
||||
ni->creation_time = ntfs2utc(std_info->creation_time);
|
||||
ni->last_data_change_time = ntfs2utc(std_info->last_data_change_time);
|
||||
ni->last_mft_change_time = ntfs2utc(std_info->last_mft_change_time);
|
||||
ni->last_access_time = ntfs2utc(std_info->last_access_time);
|
||||
ni->creation_time = std_info->creation_time;
|
||||
ni->last_data_change_time = std_info->last_data_change_time;
|
||||
ni->last_mft_change_time = std_info->last_mft_change_time;
|
||||
ni->last_access_time = std_info->last_access_time;
|
||||
/* JPA insert v3 extensions if present */
|
||||
/* length may be seen as 72 (v1.x) or 96 (v3.x) */
|
||||
lthle = ctx->attr->length;
|
||||
@ -205,13 +209,13 @@ ntfs_inode *ntfs_inode_open(ntfs_volume *vol, const MFT_REF mref)
|
||||
ni->usn = std_info->usn;
|
||||
} else {
|
||||
clear_nino_flag(ni, v3_Extensions);
|
||||
ni->owner_id = 0;
|
||||
ni->security_id = 0;
|
||||
ni->owner_id = const_cpu_to_le32(0);
|
||||
ni->security_id = const_cpu_to_le32(0);
|
||||
}
|
||||
/* Set attribute list information. */
|
||||
olderrno = errno;
|
||||
if (ntfs_attr_lookup(AT_ATTRIBUTE_LIST, AT_UNNAMED, 0, 0, 0, NULL, 0,
|
||||
ctx)) {
|
||||
if (ntfs_attr_lookup(AT_ATTRIBUTE_LIST, AT_UNNAMED, 0,
|
||||
CASE_SENSITIVE, 0, NULL, 0, ctx)) {
|
||||
if (errno != ENOENT)
|
||||
goto put_err_out;
|
||||
/* Attribute list attribute does not present. */
|
||||
@ -266,6 +270,7 @@ get_size:
|
||||
ni->data_size = le32_to_cpu(ctx->attr->value_length);
|
||||
ni->allocated_size = (ni->data_size + 7) & ~7;
|
||||
}
|
||||
set_nino_flag(ni,KnownSize);
|
||||
}
|
||||
ntfs_attr_put_search_ctx(ctx);
|
||||
out:
|
||||
@ -304,7 +309,8 @@ err_out:
|
||||
* EINVAL @ni is invalid (probably it is an extent inode).
|
||||
* EIO I/O error while trying to write inode to disk.
|
||||
*/
|
||||
int ntfs_inode_close(ntfs_inode *ni)
|
||||
|
||||
int ntfs_inode_real_close(ntfs_inode *ni)
|
||||
{
|
||||
int ret = -1;
|
||||
|
||||
@ -324,7 +330,7 @@ int ntfs_inode_close(ntfs_inode *ni)
|
||||
/* Is this a base inode with mapped extent inodes? */
|
||||
if (ni->nr_extents > 0) {
|
||||
while (ni->nr_extents > 0) {
|
||||
if (ntfs_inode_close(ni->extent_nis[0])) {
|
||||
if (ntfs_inode_real_close(ni->extent_nis[0])) {
|
||||
if (errno != EIO)
|
||||
errno = EBUSY;
|
||||
goto err;
|
||||
@ -364,8 +370,10 @@ int ntfs_inode_close(ntfs_inode *ni)
|
||||
/* Ignore errors, they don't really matter. */
|
||||
if (tmp_nis)
|
||||
base_ni->extent_nis = tmp_nis;
|
||||
} else if (tmp_nis)
|
||||
} else if (tmp_nis) {
|
||||
free(tmp_nis);
|
||||
base_ni->extent_nis = (ntfs_inode**)NULL;
|
||||
}
|
||||
/* Allow for error checking. */
|
||||
i = -1;
|
||||
break;
|
||||
@ -387,6 +395,154 @@ err:
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if CACHE_NIDATA_SIZE
|
||||
|
||||
/*
|
||||
* Free an inode structure when there is not more space
|
||||
* in the cache
|
||||
*/
|
||||
|
||||
void ntfs_inode_nidata_free(const struct CACHED_GENERIC *cached)
|
||||
{
|
||||
ntfs_inode_real_close(((const struct CACHED_NIDATA*)cached)->ni);
|
||||
}
|
||||
|
||||
/*
|
||||
* Compute a hash value for an inode entry
|
||||
*/
|
||||
|
||||
int ntfs_inode_nidata_hash(const struct CACHED_GENERIC *item)
|
||||
{
|
||||
return (((const struct CACHED_NIDATA*)item)->inum
|
||||
% (2*CACHE_NIDATA_SIZE));
|
||||
}
|
||||
|
||||
/*
|
||||
* inum comparing for entering/fetching from cache
|
||||
*/
|
||||
|
||||
static int idata_cache_compare(const struct CACHED_GENERIC *cached,
|
||||
const struct CACHED_GENERIC *wanted)
|
||||
{
|
||||
return (((const struct CACHED_NIDATA*)cached)->inum
|
||||
!= ((const struct CACHED_NIDATA*)wanted)->inum);
|
||||
}
|
||||
|
||||
/*
|
||||
* Invalidate an inode entry when not needed anymore.
|
||||
* The entry should have been synced, it may be reused later,
|
||||
* if it is requested before it is dropped from cache.
|
||||
*/
|
||||
|
||||
void ntfs_inode_invalidate(ntfs_volume *vol, const MFT_REF mref)
|
||||
{
|
||||
struct CACHED_NIDATA item;
|
||||
int count;
|
||||
|
||||
item.inum = MREF(mref);
|
||||
item.ni = (ntfs_inode*)NULL;
|
||||
item.pathname = (const char*)NULL;
|
||||
item.varsize = 0;
|
||||
count = ntfs_invalidate_cache(vol->nidata_cache,
|
||||
GENERIC(&item),idata_cache_compare,CACHE_FREE);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Open an inode
|
||||
*
|
||||
* When possible, an entry recorded in the cache is reused
|
||||
*
|
||||
* **NEVER REOPEN** an inode, this can lead to a duplicated
|
||||
* cache entry (hard to detect), and to an obsolete one being
|
||||
* reused. System files are however protected from being cached.
|
||||
*/
|
||||
|
||||
ntfs_inode *ntfs_inode_open(ntfs_volume *vol, const MFT_REF mref)
|
||||
{
|
||||
ntfs_inode *ni;
|
||||
#if CACHE_NIDATA_SIZE
|
||||
struct CACHED_NIDATA item;
|
||||
struct CACHED_NIDATA *cached;
|
||||
|
||||
/* fetch idata from cache */
|
||||
item.inum = MREF(mref);
|
||||
debug_double_inode(item.inum,1);
|
||||
item.pathname = (const char*)NULL;
|
||||
item.varsize = 0;
|
||||
cached = (struct CACHED_NIDATA*)ntfs_fetch_cache(vol->nidata_cache,
|
||||
GENERIC(&item),idata_cache_compare);
|
||||
if (cached) {
|
||||
ni = cached->ni;
|
||||
/* do not keep open entries in cache */
|
||||
ntfs_remove_cache(vol->nidata_cache,
|
||||
(struct CACHED_GENERIC*)cached,0);
|
||||
} else {
|
||||
ni = ntfs_inode_real_open(vol, mref);
|
||||
}
|
||||
#else
|
||||
ni = ntfs_inode_real_open(vol, mref);
|
||||
#endif
|
||||
return (ni);
|
||||
}
|
||||
|
||||
/*
|
||||
* Close an inode entry
|
||||
*
|
||||
* If cacheing is in use, the entry is synced and kept available
|
||||
* in cache for further use.
|
||||
*
|
||||
* System files (inode < 16 or having the IS_4 flag) are protected
|
||||
* against being cached.
|
||||
*/
|
||||
|
||||
int ntfs_inode_close(ntfs_inode *ni)
|
||||
{
|
||||
int res;
|
||||
#if CACHE_NIDATA_SIZE
|
||||
BOOL dirty;
|
||||
struct CACHED_NIDATA item;
|
||||
|
||||
if (ni) {
|
||||
debug_double_inode(ni->mft_no,0);
|
||||
/* do not cache system files : could lead to double entries */
|
||||
if (ni->vol && ni->vol->nidata_cache
|
||||
&& ((ni->mft_no == FILE_root)
|
||||
|| ((ni->mft_no >= FILE_first_user)
|
||||
&& !(ni->mrec->flags & MFT_RECORD_IS_4)))) {
|
||||
/* If we have dirty metadata, write it out. */
|
||||
dirty = NInoDirty(ni) || NInoAttrListDirty(ni);
|
||||
if (dirty) {
|
||||
res = ntfs_inode_sync(ni);
|
||||
/* do a real close if sync failed */
|
||||
if (res)
|
||||
ntfs_inode_real_close(ni);
|
||||
} else
|
||||
res = 0;
|
||||
|
||||
if (!res) {
|
||||
/* feed idata into cache */
|
||||
item.inum = ni->mft_no;
|
||||
item.ni = ni;
|
||||
item.pathname = (const char*)NULL;
|
||||
item.varsize = 0;
|
||||
debug_cached_inode(ni);
|
||||
ntfs_enter_cache(ni->vol->nidata_cache,
|
||||
GENERIC(&item), idata_cache_compare);
|
||||
}
|
||||
} else {
|
||||
/* cache not ready or system file, really close */
|
||||
res = ntfs_inode_real_close(ni);
|
||||
}
|
||||
} else
|
||||
res = 0;
|
||||
#else
|
||||
res = ntfs_inode_real_close(ni);
|
||||
#endif
|
||||
return (res);
|
||||
}
|
||||
|
||||
/**
|
||||
* ntfs_extent_inode_open - load an extent inode and attach it to its base
|
||||
* @base_ni: base ntfs inode
|
||||
@ -561,11 +717,11 @@ static int ntfs_inode_sync_standard_information(ntfs_inode *ni)
|
||||
std_info = (STANDARD_INFORMATION *)((u8 *)ctx->attr +
|
||||
le16_to_cpu(ctx->attr->value_offset));
|
||||
std_info->file_attributes = ni->flags;
|
||||
if (test_nino_flag(ni, TimesDirty)) {
|
||||
std_info->creation_time = utc2ntfs(ni->creation_time);
|
||||
std_info->last_data_change_time = utc2ntfs(ni->last_data_change_time);
|
||||
std_info->last_mft_change_time = utc2ntfs(ni->last_mft_change_time);
|
||||
std_info->last_access_time = utc2ntfs(ni->last_access_time);
|
||||
if (!test_nino_flag(ni, TimesSet)) {
|
||||
std_info->creation_time = ni->creation_time;
|
||||
std_info->last_data_change_time = ni->last_data_change_time;
|
||||
std_info->last_mft_change_time = ni->last_mft_change_time;
|
||||
std_info->last_access_time = ni->last_access_time;
|
||||
}
|
||||
|
||||
/* JPA update v3.x extensions, ensuring consistency */
|
||||
@ -595,12 +751,15 @@ static int ntfs_inode_sync_standard_information(ntfs_inode *ni)
|
||||
*
|
||||
* Return 0 on success or -1 on error with errno set to the error code.
|
||||
*/
|
||||
static int ntfs_inode_sync_file_name(ntfs_inode *ni)
|
||||
static int ntfs_inode_sync_file_name(ntfs_inode *ni, ntfs_inode *dir_ni)
|
||||
{
|
||||
ntfs_attr_search_ctx *ctx = NULL;
|
||||
ntfs_index_context *ictx;
|
||||
ntfs_inode *index_ni;
|
||||
FILE_NAME_ATTR *fn;
|
||||
FILE_NAME_ATTR *fnx;
|
||||
REPARSE_POINT *rpp;
|
||||
le32 reparse_tag;
|
||||
int err = 0;
|
||||
|
||||
ntfs_log_trace("Entering for inode %lld\n", (long long)ni->mft_no);
|
||||
@ -610,6 +769,17 @@ static int ntfs_inode_sync_file_name(ntfs_inode *ni)
|
||||
err = errno;
|
||||
goto err_out;
|
||||
}
|
||||
/* Collect the reparse tag, if any */
|
||||
reparse_tag = cpu_to_le32(0);
|
||||
if (ni->flags & FILE_ATTR_REPARSE_POINT) {
|
||||
if (!ntfs_attr_lookup(AT_REPARSE_POINT, NULL,
|
||||
0, CASE_SENSITIVE, 0, NULL, 0, ctx)) {
|
||||
rpp = (REPARSE_POINT*)((u8 *)ctx->attr +
|
||||
le16_to_cpu(ctx->attr->value_offset));
|
||||
reparse_tag = rpp->reparse_tag;
|
||||
}
|
||||
ntfs_attr_reinit_search_ctx(ctx);
|
||||
}
|
||||
/* Walk through all FILE_NAME attributes and update them. */
|
||||
while (!ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0, NULL, 0, ctx)) {
|
||||
fn = (FILE_NAME_ATTR *)((u8 *)ctx->attr +
|
||||
@ -624,6 +794,9 @@ static int ntfs_inode_sync_file_name(ntfs_inode *ni)
|
||||
*/
|
||||
index_ni = ni;
|
||||
} else
|
||||
if (dir_ni)
|
||||
index_ni = dir_ni;
|
||||
else
|
||||
index_ni = ntfs_inode_open(ni->vol,
|
||||
le64_to_cpu(fn->parent_directory));
|
||||
if (!index_ni) {
|
||||
@ -639,7 +812,8 @@ static int ntfs_inode_sync_file_name(ntfs_inode *ni)
|
||||
err = errno;
|
||||
ntfs_log_perror("Failed to get index ctx, inode %lld",
|
||||
(long long)index_ni->mft_no);
|
||||
if (ni != index_ni && ntfs_inode_close(index_ni) && !err)
|
||||
if ((ni != index_ni) && !dir_ni
|
||||
&& ntfs_inode_close(index_ni) && !err)
|
||||
err = errno;
|
||||
continue;
|
||||
}
|
||||
@ -658,21 +832,34 @@ static int ntfs_inode_sync_file_name(ntfs_inode *ni)
|
||||
continue;
|
||||
}
|
||||
/* Update flags and file size. */
|
||||
fn = (FILE_NAME_ATTR *)ictx->data;
|
||||
fn->file_attributes =
|
||||
(fn->file_attributes & ~FILE_ATTR_VALID_FLAGS) |
|
||||
fnx = (FILE_NAME_ATTR *)ictx->data;
|
||||
fnx->file_attributes =
|
||||
(fnx->file_attributes & ~FILE_ATTR_VALID_FLAGS) |
|
||||
(ni->flags & FILE_ATTR_VALID_FLAGS);
|
||||
fn->allocated_size = cpu_to_sle64(ni->allocated_size);
|
||||
fn->data_size = cpu_to_sle64(ni->data_size);
|
||||
if (test_nino_flag(ni, TimesDirty)) {
|
||||
fn->creation_time = utc2ntfs(ni->creation_time);
|
||||
fn->last_data_change_time = utc2ntfs(ni->last_data_change_time);
|
||||
fn->last_mft_change_time = utc2ntfs(ni->last_mft_change_time);
|
||||
fn->last_access_time = utc2ntfs(ni->last_access_time);
|
||||
if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY)
|
||||
fnx->data_size = fnx->allocated_size
|
||||
= const_cpu_to_le64(0);
|
||||
else {
|
||||
fnx->allocated_size = cpu_to_sle64(ni->allocated_size);
|
||||
fnx->data_size = cpu_to_sle64(ni->data_size);
|
||||
}
|
||||
/* update or clear the reparse tag in the index */
|
||||
fnx->reparse_point_tag = reparse_tag;
|
||||
if (!test_nino_flag(ni, TimesSet)) {
|
||||
fnx->creation_time = ni->creation_time;
|
||||
fnx->last_data_change_time = ni->last_data_change_time;
|
||||
fnx->last_mft_change_time = ni->last_mft_change_time;
|
||||
fnx->last_access_time = ni->last_access_time;
|
||||
} else {
|
||||
fnx->creation_time = fn->creation_time;
|
||||
fnx->last_data_change_time = fn->last_data_change_time;
|
||||
fnx->last_mft_change_time = fn->last_mft_change_time;
|
||||
fnx->last_access_time = fn->last_access_time;
|
||||
}
|
||||
ntfs_index_entry_mark_dirty(ictx);
|
||||
ntfs_index_ctx_put(ictx);
|
||||
if ((ni != index_ni) && ntfs_inode_close(index_ni) && !err)
|
||||
if ((ni != index_ni) && !dir_ni
|
||||
&& ntfs_inode_close(index_ni) && !err)
|
||||
err = errno;
|
||||
}
|
||||
/* Check for real error occurred. */
|
||||
@ -714,11 +901,10 @@ err_out:
|
||||
* EBUSY - Inode and/or one of its extents is busy, try again later.
|
||||
* EIO - I/O error while writing the inode (or one of its extents).
|
||||
*/
|
||||
int ntfs_inode_sync(ntfs_inode *ni)
|
||||
static int ntfs_inode_sync_in_dir(ntfs_inode *ni, ntfs_inode *dir_ni)
|
||||
{
|
||||
int ret = 0;
|
||||
int err = 0;
|
||||
|
||||
if (!ni) {
|
||||
errno = EINVAL;
|
||||
ntfs_log_error("Failed to sync NULL inode\n");
|
||||
@ -740,7 +926,7 @@ int ntfs_inode_sync(ntfs_inode *ni)
|
||||
/* Update FILE_NAME's in the index. */
|
||||
if ((ni->mrec->flags & MFT_RECORD_IN_USE) && ni->nr_extents != -1 &&
|
||||
NInoFileNameTestAndClearDirty(ni) &&
|
||||
ntfs_inode_sync_file_name(ni)) {
|
||||
ntfs_inode_sync_file_name(ni, dir_ni)) {
|
||||
if (!err || errno == EIO) {
|
||||
err = errno;
|
||||
if (err != EIO)
|
||||
@ -843,6 +1029,28 @@ sync_inode:
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ntfs_inode_sync(ntfs_inode *ni)
|
||||
{
|
||||
return (ntfs_inode_sync_in_dir(ni, (ntfs_inode*)NULL));
|
||||
}
|
||||
|
||||
/*
|
||||
* Close an inode with an open parent inode
|
||||
*/
|
||||
|
||||
int ntfs_inode_close_in_dir(ntfs_inode *ni, ntfs_inode *dir_ni)
|
||||
{
|
||||
int res;
|
||||
|
||||
res = ntfs_inode_sync_in_dir(ni, dir_ni);
|
||||
if (res) {
|
||||
if (errno != EIO)
|
||||
errno = EBUSY;
|
||||
} else
|
||||
res = ntfs_inode_close(ni);
|
||||
return (res);
|
||||
}
|
||||
|
||||
/**
|
||||
* ntfs_inode_add_attrlist - add attribute list to inode and fill it
|
||||
* @ni: opened ntfs inode to which add attribute list
|
||||
@ -1129,7 +1337,7 @@ put_err_out:
|
||||
*/
|
||||
void ntfs_inode_update_times(ntfs_inode *ni, ntfs_time_update_flags mask)
|
||||
{
|
||||
time_t now;
|
||||
ntfs_time now;
|
||||
|
||||
if (!ni) {
|
||||
ntfs_log_error("%s(): Invalid arguments.\n", __FUNCTION__);
|
||||
@ -1140,7 +1348,7 @@ void ntfs_inode_update_times(ntfs_inode *ni, ntfs_time_update_flags mask)
|
||||
NVolReadOnly(ni->vol) || !mask)
|
||||
return;
|
||||
|
||||
now = time(NULL);
|
||||
now = ntfs_current_time();
|
||||
if (mask & NTFS_UPDATE_ATIME)
|
||||
ni->last_access_time = now;
|
||||
if (mask & NTFS_UPDATE_MTIME)
|
||||
@ -1148,7 +1356,6 @@ void ntfs_inode_update_times(ntfs_inode *ni, ntfs_time_update_flags mask)
|
||||
if (mask & NTFS_UPDATE_CTIME)
|
||||
ni->last_mft_change_time = now;
|
||||
|
||||
set_nino_flag(ni, TimesDirty);
|
||||
NInoFileNameSetDirty(ni);
|
||||
NInoSetDirty(ni);
|
||||
}
|
||||
@ -1209,8 +1416,7 @@ int ntfs_inode_badclus_bad(u64 mft_no, ATTR_RECORD *attr)
|
||||
* -errno if failed
|
||||
*/
|
||||
|
||||
int ntfs_inode_get_times(const char *path __attribute__((unused)),
|
||||
char *value, size_t size, ntfs_inode *ni)
|
||||
int ntfs_inode_get_times(ntfs_inode *ni, char *value, size_t size)
|
||||
{
|
||||
ntfs_attr_search_ctx *ctx;
|
||||
STANDARD_INFORMATION *std_info;
|
||||
@ -1268,22 +1474,21 @@ int ntfs_inode_get_times(const char *path __attribute__((unused)),
|
||||
* -1 if there were an error (described by errno)
|
||||
*/
|
||||
|
||||
int ntfs_inode_set_times(const char *path __attribute__((unused)),
|
||||
const char *value, size_t size,
|
||||
int flags, ntfs_inode *ni)
|
||||
int ntfs_inode_set_times(ntfs_inode *ni, const char *value, size_t size,
|
||||
int flags)
|
||||
{
|
||||
ntfs_attr_search_ctx *ctx;
|
||||
STANDARD_INFORMATION *std_info;
|
||||
FILE_NAME_ATTR *fn;
|
||||
const u64 *times;
|
||||
le64 now;
|
||||
ntfs_time now;
|
||||
int cnt;
|
||||
int ret;
|
||||
|
||||
ret = -1;
|
||||
if ((size >= 8) && !(flags & XATTR_CREATE)) {
|
||||
times = (const u64*)value;
|
||||
now = utc2ntfs(time((time_t*)NULL));
|
||||
now = ntfs_current_time();
|
||||
/* update the standard information attribute */
|
||||
ctx = ntfs_attr_get_search_ctx(ni, NULL);
|
||||
if (ctx) {
|
||||
@ -1296,16 +1501,31 @@ int ntfs_inode_set_times(const char *path __attribute__((unused)),
|
||||
std_info = (STANDARD_INFORMATION *)((u8 *)ctx->attr +
|
||||
le16_to_cpu(ctx->attr->value_offset));
|
||||
/*
|
||||
* Do not mark times dirty to avoid
|
||||
* overwriting them when the inode is closed.
|
||||
* Mark times set to avoid overwriting
|
||||
* them when the inode is closed.
|
||||
* The inode structure must also be updated
|
||||
* (with loss of precision) because of cacheing.
|
||||
* TODO : use NTFS precision in inode, and
|
||||
* return sub-second times in getattr()
|
||||
*/
|
||||
set_nino_flag(ni, TimesSet);
|
||||
std_info->creation_time = cpu_to_le64(times[0]);
|
||||
if (size >= 16)
|
||||
ni->creation_time
|
||||
= std_info->creation_time;
|
||||
if (size >= 16) {
|
||||
std_info->last_data_change_time = cpu_to_le64(times[1]);
|
||||
if (size >= 24)
|
||||
ni->last_data_change_time
|
||||
= std_info->last_data_change_time;
|
||||
}
|
||||
if (size >= 24) {
|
||||
std_info->last_access_time = cpu_to_le64(times[2]);
|
||||
ni->last_access_time
|
||||
= std_info->last_access_time;
|
||||
}
|
||||
std_info->last_mft_change_time = now;
|
||||
ni->last_mft_change_time = now;
|
||||
ntfs_inode_mark_dirty(ctx->ntfs_ino);
|
||||
NInoFileNameSetDirty(ni);
|
||||
|
||||
/* update the file names attributes */
|
||||
ntfs_attr_reinit_search_ctx(ctx);
|
||||
@ -1315,10 +1535,6 @@ int ntfs_inode_set_times(const char *path __attribute__((unused)),
|
||||
0, NULL, 0, ctx)) {
|
||||
fn = (FILE_NAME_ATTR*)((u8 *)ctx->attr +
|
||||
le16_to_cpu(ctx->attr->value_offset));
|
||||
/*
|
||||
* Do not mark times dirty to avoid
|
||||
* overwriting them when the inode is closed.
|
||||
*/
|
||||
fn->creation_time
|
||||
= cpu_to_le64(times[0]);
|
||||
if (size >= 16)
|
||||
|
@ -32,6 +32,7 @@ typedef struct _ntfs_inode ntfs_inode;
|
||||
#include "layout.h"
|
||||
#include "support.h"
|
||||
#include "volume.h"
|
||||
#include "ntfstime.h"
|
||||
|
||||
/**
|
||||
* enum ntfs_inode_state_bits -
|
||||
@ -49,7 +50,8 @@ typedef enum {
|
||||
NI_FileNameDirty, /* 1: FILE_NAME attributes need to be updated
|
||||
in the index. */
|
||||
NI_v3_Extensions, /* 1: JPA v3.x extensions present. */
|
||||
NI_TimesDirty, /* 1: Times need to be updated */
|
||||
NI_TimesSet, /* 1: Use times which were set */
|
||||
NI_KnownSize, /* 1: Set if sizes are meaningful */
|
||||
} ntfs_inode_state_bits;
|
||||
|
||||
#define test_nino_flag(ni, flag) test_bit(NI_##flag, (ni)->state)
|
||||
@ -135,8 +137,11 @@ struct _ntfs_inode {
|
||||
* These two fields are used to sync filename index and guaranteed to be
|
||||
* correct, however value in index itself maybe wrong (windows itself
|
||||
* do not update them properly).
|
||||
* For directories, they hold the index size, provided the
|
||||
* flag KnownSize is set.
|
||||
*/
|
||||
s64 data_size; /* Data size of unnamed DATA attribute. */
|
||||
s64 data_size; /* Data size of unnamed DATA attribute
|
||||
(or INDEX_ROOT for directories) */
|
||||
s64 allocated_size; /* Allocated size stored in the filename
|
||||
index. (NOTE: Equal to allocated size of
|
||||
the unnamed data attribute for normal or
|
||||
@ -149,10 +154,10 @@ struct _ntfs_inode {
|
||||
* STANDARD_INFORMATION attribute and used to sync it and FILE_NAME
|
||||
* attribute in the index.
|
||||
*/
|
||||
time_t creation_time;
|
||||
time_t last_data_change_time;
|
||||
time_t last_mft_change_time;
|
||||
time_t last_access_time;
|
||||
ntfs_time creation_time;
|
||||
ntfs_time last_data_change_time;
|
||||
ntfs_time last_mft_change_time;
|
||||
ntfs_time last_access_time;
|
||||
/* NTFS 3.x extensions added by JPA */
|
||||
/* only if NI_v3_Extensions is set in state */
|
||||
le32 owner_id;
|
||||
@ -177,6 +182,19 @@ extern ntfs_inode *ntfs_inode_allocate(ntfs_volume *vol);
|
||||
extern ntfs_inode *ntfs_inode_open(ntfs_volume *vol, const MFT_REF mref);
|
||||
|
||||
extern int ntfs_inode_close(ntfs_inode *ni);
|
||||
extern int ntfs_inode_close_in_dir(ntfs_inode *ni, ntfs_inode *dir_ni);
|
||||
|
||||
#if CACHE_NIDATA_SIZE
|
||||
|
||||
struct CACHED_GENERIC;
|
||||
|
||||
extern int ntfs_inode_real_close(ntfs_inode *ni);
|
||||
extern void ntfs_inode_invalidate(ntfs_volume *vol, const MFT_REF mref);
|
||||
extern void ntfs_inode_nidata_free(const struct CACHED_GENERIC *cached);
|
||||
extern int ntfs_inode_nidata_hash(const struct CACHED_GENERIC *item);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
extern ntfs_inode *ntfs_extent_inode_open(ntfs_inode *base_ni,
|
||||
const MFT_REF mref);
|
||||
@ -195,9 +213,13 @@ extern int ntfs_inode_free_space(ntfs_inode *ni, int size);
|
||||
|
||||
extern int ntfs_inode_badclus_bad(u64 mft_no, ATTR_RECORD *a);
|
||||
|
||||
extern int ntfs_inode_get_times(const char *path, char *value,
|
||||
size_t size, ntfs_inode *ni);
|
||||
extern int ntfs_inode_set_times(const char *path, const char *value,
|
||||
size_t size, int flags, ntfs_inode *ni);
|
||||
extern int ntfs_inode_get_times(ntfs_inode *ni, char *value, size_t size);
|
||||
|
||||
extern int ntfs_inode_set_times(ntfs_inode *ni, const char *value,
|
||||
size_t size, int flags);
|
||||
|
||||
/* debugging */
|
||||
#define debug_double_inode(num, type)
|
||||
#define debug_cached_inode(ni)
|
||||
|
||||
#endif /* defined _NTFS_INODE_H */
|
||||
|
@ -473,7 +473,7 @@ done_zones_check:
|
||||
ntfs_log_trace("Switching zone.\n");
|
||||
pass = 1;
|
||||
if (rlpos) {
|
||||
LCN tc = tc = rl[rlpos - 1].lcn +
|
||||
LCN tc = rl[rlpos - 1].lcn +
|
||||
rl[rlpos - 1].length + NTFS_LCNALLOC_SKIP;
|
||||
|
||||
if (used_zone_pos)
|
||||
@ -609,6 +609,42 @@ out:
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Basic cluster run free
|
||||
* Returns 0 if successful
|
||||
*/
|
||||
|
||||
int ntfs_cluster_free_basic(ntfs_volume *vol, s64 lcn, s64 count)
|
||||
{
|
||||
s64 nr_freed = 0;
|
||||
int ret = -1;
|
||||
|
||||
ntfs_log_trace("Entering.\n");
|
||||
ntfs_log_trace("Dealloc lcn 0x%llx, len 0x%llx.\n",
|
||||
(long long)lcn, (long long)count);
|
||||
|
||||
if (lcn >= 0) {
|
||||
update_full_status(vol,lcn);
|
||||
if (ntfs_bitmap_clear_run(vol->lcnbmp_na, lcn,
|
||||
count)) {
|
||||
ntfs_log_perror("Cluster deallocation failed "
|
||||
"(%lld, %lld)",
|
||||
(long long)lcn,
|
||||
(long long)count);
|
||||
goto out;
|
||||
}
|
||||
nr_freed += count;
|
||||
}
|
||||
ret = 0;
|
||||
out:
|
||||
vol->free_clusters += nr_freed;
|
||||
if (vol->free_clusters > vol->nr_clusters)
|
||||
ntfs_log_error("Too many free clusters (%lld > %lld)!",
|
||||
(long long)vol->free_clusters,
|
||||
(long long)vol->nr_clusters);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* ntfs_cluster_free - free clusters on an ntfs volume
|
||||
* @vol: mounted ntfs volume on which to free the clusters
|
||||
|
@ -42,6 +42,7 @@ extern runlist *ntfs_cluster_alloc(ntfs_volume *vol, VCN start_vcn, s64 count,
|
||||
LCN start_lcn, const NTFS_CLUSTER_ALLOCATION_ZONES zone);
|
||||
|
||||
extern int ntfs_cluster_free_from_rl(ntfs_volume *vol, runlist *rl);
|
||||
extern int ntfs_cluster_free_basic(ntfs_volume *vol, s64 lcn, s64 count);
|
||||
|
||||
extern int ntfs_cluster_free(ntfs_volume *vol, ntfs_attr *na, VCN start_vcn,
|
||||
s64 count);
|
||||
|
@ -699,8 +699,8 @@ BOOL ntfs_is_logfile_clean(ntfs_attr *log_na, RESTART_PAGE_HEADER *rp)
|
||||
*/
|
||||
int ntfs_empty_logfile(ntfs_attr *na)
|
||||
{
|
||||
/*s64 pos, count;
|
||||
char buf[NTFS_BUF_SIZE];*/
|
||||
s64 pos, count;
|
||||
char buf[NTFS_BUF_SIZE];
|
||||
|
||||
ntfs_log_trace("Entering.\n");
|
||||
|
||||
@ -713,7 +713,7 @@ int ntfs_empty_logfile(ntfs_attr *na)
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*memset(buf, -1, NTFS_BUF_SIZE);
|
||||
memset(buf, -1, NTFS_BUF_SIZE);
|
||||
|
||||
pos = 0;
|
||||
while ((count = na->data_size - pos) > 0) {
|
||||
@ -729,7 +729,7 @@ int ntfs_empty_logfile(ntfs_attr *na)
|
||||
return -1;
|
||||
}
|
||||
pos += count;
|
||||
}*/
|
||||
}
|
||||
|
||||
NVolSetLogFileEmpty(na->ni->vol);
|
||||
|
||||
|
@ -1478,8 +1478,7 @@ found_free_rec:
|
||||
ni->flags = 0;
|
||||
ni->creation_time = ni->last_data_change_time =
|
||||
ni->last_mft_change_time =
|
||||
ni->last_access_time = time(NULL);
|
||||
set_nino_flag(ni, TimesDirty);
|
||||
ni->last_access_time = ntfs_current_time();
|
||||
/* Update the default mft allocation position if it was used. */
|
||||
if (!base_ni)
|
||||
vol->mft_data_pos = bit + 1;
|
||||
@ -1781,8 +1780,7 @@ found_free_rec:
|
||||
ni->flags = 0;
|
||||
ni->creation_time = ni->last_data_change_time =
|
||||
ni->last_mft_change_time =
|
||||
ni->last_access_time = time(NULL);
|
||||
set_nino_flag(ni, TimesDirty);
|
||||
ni->last_access_time = ntfs_current_time();
|
||||
/* Update the default mft allocation position if it was used. */
|
||||
if (!base_ni)
|
||||
vol->mft_data_pos = bit + 1;
|
||||
@ -1862,7 +1860,11 @@ int ntfs_mft_record_free(ntfs_volume *vol, ntfs_inode *ni)
|
||||
}
|
||||
|
||||
/* Throw away the now freed inode. */
|
||||
#if CACHE_NIDATA_SIZE
|
||||
if (!ntfs_inode_real_close(ni)) {
|
||||
#else
|
||||
if (!ntfs_inode_close(ni)) {
|
||||
#endif
|
||||
vol->free_mft_records++;
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
/**
|
||||
* misc.c : miscellaneous :
|
||||
* - dealing with errors in memory allocation
|
||||
* - data caching
|
||||
*
|
||||
* Copyright (c) 2008 Jean-Pierre Andre
|
||||
*
|
||||
@ -33,7 +32,6 @@
|
||||
#endif
|
||||
|
||||
#include "types.h"
|
||||
#include "security.h"
|
||||
#include "misc.h"
|
||||
#include "logging.h"
|
||||
|
||||
@ -61,304 +59,3 @@ void *ntfs_malloc(size_t size)
|
||||
ntfs_log_perror("Failed to malloc %lld bytes", (long long)size);
|
||||
return p;
|
||||
}
|
||||
|
||||
/*
|
||||
* General functions to deal with LRU caches
|
||||
*
|
||||
* The cached data have to be organized in a structure in which
|
||||
* the first fields must follow a mandatory pattern and further
|
||||
* fields may contain any fixed size data. They are stored in an
|
||||
* LRU list.
|
||||
*
|
||||
* A compare function must be provided for finding a wanted entry
|
||||
* in the cache. Another function may be provided for invalidating
|
||||
* an entry to facilitate multiple invalidation.
|
||||
*
|
||||
* These functions never return error codes. When there is a
|
||||
* shortage of memory, data is simply not cached.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Fetch an entry from cache
|
||||
*
|
||||
* returns the cache entry, or NULL if not available
|
||||
*/
|
||||
|
||||
struct CACHED_GENERIC *ntfs_fetch_cache(struct CACHE_HEADER *cache,
|
||||
const struct CACHED_GENERIC *wanted, cache_compare compare)
|
||||
{
|
||||
struct CACHED_GENERIC *current;
|
||||
struct CACHED_GENERIC *previous;
|
||||
|
||||
current = (struct CACHED_GENERIC*)NULL;
|
||||
if (cache) {
|
||||
/*
|
||||
* Search sequentially in LRU list
|
||||
*/
|
||||
current = cache->most_recent_entry;
|
||||
previous = (struct CACHED_GENERIC*)NULL;
|
||||
while (current
|
||||
&& compare(current, wanted)) {
|
||||
previous = current;
|
||||
current = current->next;
|
||||
}
|
||||
if (current)
|
||||
cache->hits++;
|
||||
if (current && previous) {
|
||||
/*
|
||||
* found and not at head of list, unlink from current
|
||||
* position and relink as head of list
|
||||
*/
|
||||
previous->next = current->next;
|
||||
current->next = cache->most_recent_entry;
|
||||
cache->most_recent_entry = current;
|
||||
}
|
||||
cache->reads++;
|
||||
}
|
||||
return (current);
|
||||
}
|
||||
|
||||
/*
|
||||
* Enter an inode number into cache
|
||||
* returns the cache entry or NULL if not possible
|
||||
*/
|
||||
|
||||
struct CACHED_GENERIC *ntfs_enter_cache(struct CACHE_HEADER *cache,
|
||||
const struct CACHED_GENERIC *item, cache_compare compare)
|
||||
{
|
||||
struct CACHED_GENERIC *current;
|
||||
struct CACHED_GENERIC *previous;
|
||||
struct CACHED_GENERIC *before;
|
||||
|
||||
current = (struct CACHED_GENERIC*)NULL;
|
||||
if (cache) {
|
||||
|
||||
/*
|
||||
* Search sequentially in LRU list to locate the end,
|
||||
* and find out whether the entry is already in list
|
||||
* As we normally go to the end, no statistics is
|
||||
* kept.
|
||||
*/
|
||||
current = cache->most_recent_entry;
|
||||
previous = (struct CACHED_GENERIC*)NULL;
|
||||
before = (struct CACHED_GENERIC*)NULL;
|
||||
while (current
|
||||
&& compare(current, item)) {
|
||||
before = previous;
|
||||
previous = current;
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
if (!current) {
|
||||
/*
|
||||
* Not in list, get a free entry or reuse the
|
||||
* last entry, and relink as head of list
|
||||
* Note : we assume at least three entries, so
|
||||
* before, previous and first are different when
|
||||
* an entry is reused.
|
||||
*/
|
||||
|
||||
if (cache->free_entry) {
|
||||
current = cache->free_entry;
|
||||
cache->free_entry = cache->free_entry->next;
|
||||
if (item->varsize) {
|
||||
current->variable = ntfs_malloc(
|
||||
item->varsize);
|
||||
} else
|
||||
current->variable = (void*)NULL;
|
||||
current->varsize = item->varsize;
|
||||
} else {
|
||||
before->next = (struct CACHED_GENERIC*)NULL;
|
||||
current = previous;
|
||||
if (item->varsize) {
|
||||
if (current->varsize)
|
||||
current->variable = realloc(
|
||||
current->variable,
|
||||
item->varsize);
|
||||
else
|
||||
current->variable = ntfs_malloc(
|
||||
item->varsize);
|
||||
} else {
|
||||
if (current->varsize)
|
||||
free(current->variable);
|
||||
current->variable = (void*)NULL;
|
||||
}
|
||||
current->varsize = item->varsize;
|
||||
}
|
||||
current->next = cache->most_recent_entry;
|
||||
cache->most_recent_entry = current;
|
||||
memcpy(current->fixed, item->fixed, cache->fixed_size);
|
||||
if (item->varsize) {
|
||||
if (current->variable) {
|
||||
memcpy(current->variable,
|
||||
item->variable, item->varsize);
|
||||
} else {
|
||||
/*
|
||||
* no more memory for variable part
|
||||
* recycle entry in free list
|
||||
* not an error, just uncacheable
|
||||
*/
|
||||
cache->most_recent_entry = current->next;
|
||||
current->next = cache->free_entry;
|
||||
cache->free_entry = current;
|
||||
current = (struct CACHED_GENERIC*)NULL;
|
||||
}
|
||||
} else {
|
||||
current->variable = (void*)NULL;
|
||||
current->varsize = 0;
|
||||
}
|
||||
}
|
||||
cache->writes++;
|
||||
}
|
||||
return (current);
|
||||
}
|
||||
|
||||
/*
|
||||
* Invalidate entries in cache
|
||||
*
|
||||
* Several entries may have to be invalidated (at least for inodes
|
||||
* associated to directories which have been renamed), a different
|
||||
* compare function may be provided to select entries to invalidate
|
||||
*
|
||||
* Returns the number of deleted entries, this can be used by
|
||||
* the caller to signal a cache corruption if the entry was
|
||||
* supposed to be found.
|
||||
*/
|
||||
|
||||
int ntfs_invalidate_cache(struct CACHE_HEADER *cache,
|
||||
const struct CACHED_GENERIC *item, cache_compare compare)
|
||||
{
|
||||
struct CACHED_GENERIC *current;
|
||||
struct CACHED_GENERIC *previous;
|
||||
int count;
|
||||
|
||||
current = (struct CACHED_GENERIC*)NULL;
|
||||
count = 0;
|
||||
if (cache) {
|
||||
/*
|
||||
* Search sequentially in LRU list
|
||||
*/
|
||||
current = cache->most_recent_entry;
|
||||
previous = (struct CACHED_GENERIC*)NULL;
|
||||
while (current) {
|
||||
if (!compare(current, item)) {
|
||||
/*
|
||||
* Relink into free list
|
||||
*/
|
||||
if (previous)
|
||||
previous->next = current->next;
|
||||
else
|
||||
cache->most_recent_entry = current->next;
|
||||
current->next = cache->free_entry;
|
||||
cache->free_entry = current;
|
||||
if (current->variable)
|
||||
free(current->variable);
|
||||
current->varsize = 0;
|
||||
if (previous)
|
||||
current = previous->next;
|
||||
else
|
||||
current = cache->most_recent_entry;
|
||||
count++;
|
||||
} else {
|
||||
previous = current;
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
return (count);
|
||||
}
|
||||
|
||||
/*
|
||||
* Free memory allocated to a cache
|
||||
*/
|
||||
|
||||
static void ntfs_free_cache(struct CACHE_HEADER *cache)
|
||||
{
|
||||
struct CACHED_GENERIC *entry;
|
||||
|
||||
if (cache) {
|
||||
for (entry=cache->most_recent_entry; entry; entry=entry->next)
|
||||
if (entry->variable)
|
||||
free(entry->variable);
|
||||
free(cache);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Create a cache
|
||||
*
|
||||
* Returns the cache header, or NULL if the cache could not be created
|
||||
*/
|
||||
|
||||
static struct CACHE_HEADER *ntfs_create_cache(const char *name,
|
||||
int full_item_size, int item_count)
|
||||
{
|
||||
struct CACHE_HEADER *cache;
|
||||
struct CACHED_GENERIC *p;
|
||||
struct CACHED_GENERIC *q;
|
||||
int i;
|
||||
|
||||
cache = (struct CACHE_HEADER*)
|
||||
ntfs_malloc(sizeof(struct CACHE_HEADER)
|
||||
+ item_count*full_item_size);
|
||||
if (cache) {
|
||||
cache->name = name;
|
||||
cache->fixed_size = full_item_size - sizeof(struct CACHED_GENERIC);
|
||||
cache->reads = 0;
|
||||
cache->writes = 0;
|
||||
cache->hits = 0;
|
||||
/* chain the entries, and mark an invalid entry */
|
||||
cache->most_recent_entry = (struct CACHED_GENERIC*)NULL;
|
||||
cache->free_entry = &cache->entry[0];
|
||||
p = &cache->entry[0];
|
||||
for (i=0; i<(item_count - 1); i++) {
|
||||
q = (struct CACHED_GENERIC*)((char*)p + full_item_size);
|
||||
p->next = q;
|
||||
p->variable = (void*)NULL;
|
||||
p->varsize = 0;
|
||||
p = q;
|
||||
}
|
||||
/* special for the last entry */
|
||||
p->next = (struct CACHED_GENERIC*)NULL;
|
||||
p->variable = (void*)NULL;
|
||||
p->varsize = 0;
|
||||
}
|
||||
return (cache);
|
||||
}
|
||||
|
||||
/*
|
||||
* Create all LRU caches
|
||||
*
|
||||
* No error return, if creation is not possible, cacheing will
|
||||
* just be not available
|
||||
*/
|
||||
|
||||
void ntfs_create_lru_caches(ntfs_volume *vol)
|
||||
{
|
||||
#if CACHE_INODE_SIZE
|
||||
/* inode cache */
|
||||
vol->xinode_cache = ntfs_create_cache("inode",
|
||||
sizeof(struct CACHED_INODE), CACHE_INODE_SIZE);
|
||||
#endif
|
||||
vol->securid_cache = ntfs_create_cache("securid",
|
||||
sizeof(struct CACHED_SECURID), CACHE_SECURID_SIZE);
|
||||
#if CACHE_LEGACY_SIZE
|
||||
vol->legacy_cache = ntfs_create_cache("legacy",
|
||||
sizeof(struct CACHED_PERMISSIONS_LEGACY), CACHE_LEGACY_SIZE);
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
* Free all LRU caches
|
||||
*/
|
||||
|
||||
void ntfs_free_lru_caches(ntfs_volume *vol)
|
||||
{
|
||||
#if CACHE_INODE_SIZE
|
||||
ntfs_free_cache(vol->xinode_cache);
|
||||
#endif
|
||||
ntfs_free_cache(vol->securid_cache);
|
||||
#if CACHE_LEGACY_SIZE
|
||||
ntfs_free_cache(vol->legacy_cache);
|
||||
#endif
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
/*
|
||||
* misc.h : miscellaneous exports
|
||||
* - memory allocation
|
||||
* - LRU caches
|
||||
*
|
||||
* Copyright (c) 2008 Jean-Pierre Andre
|
||||
*
|
||||
@ -24,49 +23,6 @@
|
||||
#ifndef _NTFS_MISC_H_
|
||||
#define _NTFS_MISC_H_
|
||||
|
||||
#include "volume.h"
|
||||
|
||||
struct CACHED_GENERIC {
|
||||
struct CACHED_GENERIC *next;
|
||||
void *variable;
|
||||
size_t varsize;
|
||||
void *fixed[0];
|
||||
} ;
|
||||
|
||||
struct CACHED_INODE {
|
||||
struct CACHED_INODE *next;
|
||||
const char *pathname;
|
||||
size_t varsize;
|
||||
/* above fields must match "struct CACHED_GENERIC" */
|
||||
u64 inum;
|
||||
} ;
|
||||
|
||||
typedef int (*cache_compare)(const struct CACHED_GENERIC *cached,
|
||||
const struct CACHED_GENERIC *item);
|
||||
|
||||
struct CACHE_HEADER {
|
||||
const char *name;
|
||||
struct CACHED_GENERIC *most_recent_entry;
|
||||
struct CACHED_GENERIC *free_entry;
|
||||
unsigned long reads;
|
||||
unsigned long writes;
|
||||
unsigned long hits;
|
||||
int fixed_size;
|
||||
struct CACHED_GENERIC entry[0];
|
||||
} ;
|
||||
|
||||
/* cast to generic, avoiding gcc warnings */
|
||||
#define GENERIC(pstr) ((const struct CACHED_GENERIC*)(const void*)(pstr))
|
||||
|
||||
struct CACHED_GENERIC *ntfs_fetch_cache(struct CACHE_HEADER *cache,
|
||||
const struct CACHED_GENERIC *wanted, cache_compare compare);
|
||||
struct CACHED_GENERIC *ntfs_enter_cache(struct CACHE_HEADER *cache,
|
||||
const struct CACHED_GENERIC *item, cache_compare compare);
|
||||
int ntfs_invalidate_cache(struct CACHE_HEADER *cache,
|
||||
const struct CACHED_GENERIC *item, cache_compare compare);
|
||||
void ntfs_create_lru_caches(ntfs_volume *vol);
|
||||
void ntfs_free_lru_caches(ntfs_volume *vol);
|
||||
|
||||
void *ntfs_calloc(size_t size);
|
||||
void *ntfs_malloc(size_t size);
|
||||
|
||||
|
@ -99,7 +99,7 @@ int ntfsFindPartitions (const DISC_INTERFACE *interface, sec_t **partitions)
|
||||
int i;
|
||||
|
||||
union {
|
||||
u8 buffer[BYTES_PER_SECTOR];
|
||||
u8 buffer[512];
|
||||
MASTER_BOOT_RECORD mbr;
|
||||
EXTENDED_BOOT_RECORD ebr;
|
||||
NTFS_BOOT_SECTOR boot;
|
||||
@ -419,7 +419,7 @@ bool ntfsMount (const char *name, const DISC_INTERFACE *interface, sec_t startSe
|
||||
// Sanity check
|
||||
if (!name || !interface) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialise ntfs-3g
|
||||
|
@ -139,18 +139,6 @@ extern const char *ntfsGetVolumeName (const char *name);
|
||||
*/
|
||||
extern bool ntfsSetVolumeName (const char *name, const char *volumeName);
|
||||
|
||||
/*
|
||||
typedef struct _FileInfo FileInfo;
|
||||
|
||||
struct _FileInfo
|
||||
{
|
||||
u64 offset[64];
|
||||
s64 sector[64];
|
||||
u64 count[64];
|
||||
u32 num;
|
||||
u64 filesize;
|
||||
};
|
||||
*/
|
||||
typedef int (*_ntfs_frag_append_t)(void *ff, u32 offset, u32 sector, u32 count);
|
||||
int _NTFS_get_fragments (const char *path, _ntfs_frag_append_t append_fragment, void *callback_data);
|
||||
|
||||
|
@ -402,19 +402,22 @@ int ntfs_readdir_filler (DIR_ITER *dirState, const ntfschar *name, const int nam
|
||||
|
||||
// Convert the entry name to our current local
|
||||
if (ntfsUnicodeToLocal(name, name_len, &entry_name, 0) < 0) {
|
||||
ntfs_free(entry);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(dir->first && dir->first->mref == FILE_root &&
|
||||
MREF(mref) == FILE_root && strcmp(entry_name, "..") == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// If this is not the parent or self directory reference
|
||||
if ((strcmp(entry_name, ".") != 0) && (strcmp(entry_name, "..") != 0)) {
|
||||
|
||||
// Open the entry
|
||||
ntfs_inode *ni = ntfs_pathname_to_inode(dir->vd->vol, dir->ni, entry_name);
|
||||
if (!ni) {
|
||||
ntfs_free(entry);
|
||||
if (!ni)
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Double check that this entry can be emuerated (as described by the volume descriptor)
|
||||
if (((ni->flags & FILE_ATTR_HIDDEN) && !dir->vd->showHiddenFiles) ||
|
||||
@ -429,13 +432,14 @@ int ntfs_readdir_filler (DIR_ITER *dirState, const ntfschar *name, const int nam
|
||||
}
|
||||
|
||||
// Allocate a new directory entry
|
||||
entry = ntfs_alloc(sizeof(ntfs_dir_entry));
|
||||
entry = (ntfs_dir_entry *) ntfs_alloc(sizeof(ntfs_dir_entry));
|
||||
if (!entry)
|
||||
return -1;
|
||||
|
||||
// Setup the entry
|
||||
entry->name = entry_name;
|
||||
entry->next = NULL;
|
||||
entry->mref = MREF(mref);
|
||||
|
||||
// Link the entry to the directory
|
||||
if (!dir->first) {
|
||||
|
@ -30,6 +30,7 @@
|
||||
*/
|
||||
typedef struct _ntfs_dir_entry {
|
||||
char *name;
|
||||
u64 mref;
|
||||
struct _ntfs_dir_entry *next;
|
||||
} ntfs_dir_entry;
|
||||
|
||||
|
@ -65,8 +65,8 @@ void ntfsCloseFile (ntfs_file_state *file)
|
||||
// Sync the file (and its attributes) to disc
|
||||
if(file->write)
|
||||
{
|
||||
ntfsSync(file->vd, file->ni);
|
||||
ntfsUpdateTimes(file->vd, file->ni, NTFS_UPDATE_ATIME | NTFS_UPDATE_CTIME);
|
||||
ntfsSync(file->vd, file->ni);
|
||||
}
|
||||
|
||||
if (file->read)
|
||||
|
@ -43,7 +43,7 @@ typedef struct _ntfs_file_state {
|
||||
bool compressed; /* True if file data is compressed */
|
||||
bool encrypted; /* True if file data is encryted */
|
||||
off_t pos; /* Current position within the file (in bytes) */
|
||||
size_t len; /* Total length of the file (in bytes) */
|
||||
u64 len; /* Total length of the file (in bytes) */
|
||||
struct _ntfs_file_state *prevOpenFile; /* The previous entry in a double-linked FILO list of open files */
|
||||
struct _ntfs_file_state *nextOpenFile; /* The next entry in a double-linked FILO list of open files */
|
||||
} ntfs_file_state;
|
||||
|
@ -1,6 +1,6 @@
|
||||
/**
|
||||
* ntfsfile.c - devoptab file routines for NTFS-based devices.
|
||||
*
|
||||
* Copyright (c) 2010 Miigotu
|
||||
* Copyright (c) 2009 Rhys "Shareese" Koedijk
|
||||
* Copyright (c) 2006 Michael "Chishm" Chisholm
|
||||
*
|
||||
@ -59,16 +59,23 @@ void ntfsCloseFile (ntfs_file_state *file)
|
||||
// Special case fix ups for compressed and/or encrypted files
|
||||
if (file->compressed)
|
||||
ntfs_attr_pclose(file->data_na);
|
||||
#ifdef HAVE_SETXATTR
|
||||
if (file->encrypted)
|
||||
ntfs_efs_fixup_attribute(NULL, file->data_na);
|
||||
|
||||
#endif
|
||||
// Close the file data attribute (if open)
|
||||
if (file->data_na)
|
||||
ntfs_attr_close(file->data_na);
|
||||
|
||||
// Sync the file (and its attributes) to disc
|
||||
if(file->write)
|
||||
{
|
||||
ntfsUpdateTimes(file->vd, file->ni, NTFS_UPDATE_ATIME | NTFS_UPDATE_CTIME);
|
||||
ntfsSync(file->vd, file->ni);
|
||||
}
|
||||
|
||||
if (file->read)
|
||||
ntfsUpdateTimes(file->vd, file->ni, NTFS_UPDATE_ATIME);
|
||||
|
||||
// Close the file (if open)
|
||||
if (file->ni)
|
||||
@ -204,6 +211,8 @@ int ntfs_open_r (struct _reent *r, void *fileStruct, const char *path, int flags
|
||||
file->pos = 0;
|
||||
file->len = file->data_na->data_size;
|
||||
|
||||
ntfs_log_trace("file->len %d\n", file->len);
|
||||
|
||||
// Update file times
|
||||
ntfsUpdateTimes(file->vd, file->ni, NTFS_UPDATE_ATIME);
|
||||
|
||||
@ -314,10 +323,6 @@ ssize_t ntfs_write_r (struct _reent *r, int fd, const char *ptr, size_t len)
|
||||
if (written)
|
||||
file->ni->flags |= FILE_ATTR_ARCHIVE;
|
||||
|
||||
// Update file times (if we actually wrote something)
|
||||
if (written)
|
||||
ntfsUpdateTimes(file->vd, file->ni, NTFS_UPDATE_MCTIME);
|
||||
|
||||
// Update the files data length
|
||||
file->len = file->data_na->data_size;
|
||||
|
||||
@ -346,6 +351,9 @@ int _NTFS_get_fragments (const char *path,
|
||||
if (fd != (int)file) return -12;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Sanity check
|
||||
if (!file || !file->vd || !file->ni || !file->data_na) {
|
||||
//r->_errno = EINVAL;
|
||||
@ -373,6 +381,7 @@ int _NTFS_get_fragments (const char *path,
|
||||
if (file->pos + len > file->len) {
|
||||
r->_errno = EOVERFLOW;
|
||||
len = file->len - file->pos;
|
||||
ntfs_log_trace("EOVERFLOW");
|
||||
}
|
||||
*/
|
||||
|
||||
@ -384,6 +393,7 @@ int _NTFS_get_fragments (const char *path,
|
||||
s64 ret = ntfs_attr_getfragments(file->data_na, file->pos,
|
||||
len, offset, append_fragment, callback_data);
|
||||
if (ret <= 0 || ret > len) {
|
||||
ntfsUnlock(file->vd);
|
||||
//r->_errno = errno;
|
||||
ret_val = -14;
|
||||
if (ret < 0) ret_val = ret;
|
||||
@ -401,6 +411,7 @@ int _NTFS_get_fragments (const char *path,
|
||||
ret_val = 0;
|
||||
|
||||
/*
|
||||
//ntfs_log_trace("file->pos: %d \n", (u32)file->pos);
|
||||
// Update file times (if we actually read something)
|
||||
if (read)
|
||||
ntfsUpdateTimes(file->vd, file->ni, NTFS_UPDATE_ATIME);
|
||||
@ -523,7 +534,7 @@ int ntfs_ftruncate_r (struct _reent *r, int fd, off_t len)
|
||||
|
||||
// Update file times (if we actually changed something)
|
||||
if (file->len != file->data_na->data_size)
|
||||
ntfsUpdateTimes(file->vd, file->ni, NTFS_UPDATE_MCTIME);
|
||||
ntfsUpdateTimes(file->vd, file->ni, NTFS_UPDATE_AMCTIME);
|
||||
|
||||
// Update the files data length
|
||||
file->len = file->data_na->data_size;
|
||||
|
@ -34,7 +34,6 @@
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#include "usbloader/usbstorage2.h"
|
||||
#include "ntfsinternal.h"
|
||||
#include "ntfsdir.h"
|
||||
#include "ntfsfile.h"
|
||||
@ -42,11 +41,11 @@
|
||||
#if defined(__wii__)
|
||||
#include <sdcard/wiisd_io.h>
|
||||
#include <sdcard/gcsd.h>
|
||||
|
||||
#include <ogc/usbstorage.h>
|
||||
|
||||
const INTERFACE_ID ntfs_disc_interfaces[] = {
|
||||
{ "sd", &__io_wiisd },
|
||||
{ "usb", &__io_usbstorage2 },
|
||||
{ "usb", &__io_usbstorage },
|
||||
{ "carda", &__io_gcsda },
|
||||
{ "cardb", &__io_gcsdb },
|
||||
{ NULL, NULL }
|
||||
@ -77,7 +76,7 @@ int ntfsAddDevice (const char *name, void *deviceData)
|
||||
}
|
||||
|
||||
// Allocate a devoptab for this device
|
||||
dev = ntfs_alloc(sizeof(devoptab_t) + strlen(name) + 1);
|
||||
dev = (devoptab_t *) ntfs_alloc(sizeof(devoptab_t) + strlen(name) + 1);
|
||||
if (!dev) {
|
||||
errno = ENOMEM;
|
||||
return false;
|
||||
@ -304,7 +303,7 @@ ntfs_inode *ntfsParseEntry (ntfs_vd *vd, const char *path, int reparseLevel)
|
||||
}
|
||||
|
||||
// Get the target path of this entry
|
||||
target = ntfs_make_symlink(path, ni, &attr_size);
|
||||
target = ntfs_make_symlink(ni, path, &attr_size);
|
||||
if (!target) {
|
||||
ntfsCloseEntry(vd, ni);
|
||||
return NULL;
|
||||
@ -549,7 +548,6 @@ int ntfsLink (ntfs_vd *vd, const char *old_path, const char *new_path)
|
||||
}
|
||||
|
||||
// Update entry times
|
||||
ntfsUpdateTimes(vd, ni, NTFS_UPDATE_CTIME);
|
||||
ntfsUpdateTimes(vd, dir_ni, NTFS_UPDATE_MCTIME);
|
||||
|
||||
// Sync the entry to disc
|
||||
@ -816,7 +814,7 @@ int ntfsUnicodeToLocal (const ntfschar *ins, const int ins_len, char **outs, int
|
||||
// do it manually by replacing non-ASCII characters with underscores
|
||||
if (!*outs || outs_len >= ins_len) {
|
||||
if (!*outs) {
|
||||
*outs = ntfs_alloc(ins_len + 1);
|
||||
*outs = (char *) ntfs_alloc(ins_len + 1);
|
||||
if (!*outs) {
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
|
@ -3,6 +3,7 @@
|
||||
*
|
||||
* Copyright (c) 2005 Anton Altaparmakov
|
||||
* Copyright (c) 2005 Yura Pakhuchiy
|
||||
* Copyright (c) 2010 Jean-Pierre Andre
|
||||
*
|
||||
* This program/include file is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as published
|
||||
@ -26,44 +27,95 @@
|
||||
#ifdef HAVE_TIME_H
|
||||
#include <time.h>
|
||||
#endif
|
||||
#ifdef HAVE_SYS_STAT_H
|
||||
#include <sys/stat.h>
|
||||
#endif
|
||||
#ifdef HAVE_GETTIMEOFDAY
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
#include "types.h"
|
||||
|
||||
/*
|
||||
* There are four times more conversions of internal representation
|
||||
* to ntfs representation than any other conversion, so the most
|
||||
* efficient internal representation is ntfs representation
|
||||
* (with low endianness)
|
||||
*/
|
||||
typedef sle64 ntfs_time;
|
||||
|
||||
#define NTFS_TIME_OFFSET ((s64)(369 * 365 + 89) * 24 * 3600 * 10000000)
|
||||
|
||||
/**
|
||||
* ntfs2utc - Convert an NTFS time to Unix time
|
||||
* ntfs2timespec - Convert an NTFS time to Unix time
|
||||
* @ntfs_time: An NTFS time in 100ns units since 1601
|
||||
*
|
||||
* NTFS stores times as the number of 100ns intervals since January 1st 1601 at
|
||||
* 00:00 UTC. This system will not suffer from Y2K problems until ~57000AD.
|
||||
*
|
||||
* Return: n A Unix time (number of seconds since 1970)
|
||||
* Return: A Unix time (number of seconds since 1970, and nanoseconds)
|
||||
*/
|
||||
static __inline__ time_t ntfs2utc(s64 ntfs_time)
|
||||
static __inline__ struct timespec ntfs2timespec(ntfs_time ntfstime)
|
||||
{
|
||||
return (sle64_to_cpu(ntfs_time) - (NTFS_TIME_OFFSET)) / 10000000;
|
||||
struct timespec spec;
|
||||
s64 cputime;
|
||||
|
||||
cputime = sle64_to_cpu(ntfstime);
|
||||
spec.tv_sec = (cputime - (NTFS_TIME_OFFSET)) / 10000000;
|
||||
spec.tv_nsec = (cputime - (NTFS_TIME_OFFSET)
|
||||
- (s64)spec.tv_sec*10000000)*100;
|
||||
/* force zero nsec for overflowing dates */
|
||||
if ((spec.tv_nsec < 0) || (spec.tv_nsec > 999999999))
|
||||
spec.tv_nsec = 0;
|
||||
return (spec);
|
||||
}
|
||||
|
||||
/**
|
||||
* utc2ntfs - Convert Linux time to NTFS time
|
||||
* timespec2ntfs - Convert Linux time to NTFS time
|
||||
* @utc_time: Linux time to convert to NTFS
|
||||
*
|
||||
* Convert the Linux time @utc_time to its corresponding NTFS time.
|
||||
*
|
||||
* Linux stores time in a long at present and measures it as the number of
|
||||
* 1-second intervals since 1st January 1970, 00:00:00 UTC.
|
||||
* 1-second intervals since 1st January 1970, 00:00:00 UTC
|
||||
* with a separated non-negative nanosecond value
|
||||
*
|
||||
* NTFS uses Microsoft's standard time format which is stored in a s64 and is
|
||||
* NTFS uses Microsoft's standard time format which is stored in a sle64 and is
|
||||
* measured as the number of 100 nano-second intervals since 1st January 1601,
|
||||
* 00:00:00 UTC.
|
||||
*
|
||||
* Return: n An NTFS time (100ns units since Jan 1601)
|
||||
* Return: An NTFS time (100ns units since Jan 1601)
|
||||
*/
|
||||
static __inline__ s64 utc2ntfs(time_t utc_time)
|
||||
static __inline__ ntfs_time timespec2ntfs(struct timespec spec)
|
||||
{
|
||||
/* Convert to 100ns intervals and then add the NTFS time offset. */
|
||||
return cpu_to_sle64((s64)utc_time * 10000000 + NTFS_TIME_OFFSET);
|
||||
s64 units;
|
||||
|
||||
units = (s64)spec.tv_sec * 10000000
|
||||
+ NTFS_TIME_OFFSET + spec.tv_nsec/100;
|
||||
return (cpu_to_le64(units));
|
||||
}
|
||||
|
||||
/*
|
||||
* Return the current time in ntfs format
|
||||
*/
|
||||
|
||||
static __inline__ ntfs_time ntfs_current_time(void)
|
||||
{
|
||||
struct timespec now;
|
||||
|
||||
#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_SYS_CLOCK_GETTIME)
|
||||
clock_gettime(CLOCK_REALTIME, &now);
|
||||
#elif defined(HAVE_GETTIMEOFDAY)
|
||||
struct timeval microseconds;
|
||||
|
||||
gettimeofday(µseconds, (struct timezone*)NULL);
|
||||
now.tv_sec = microseconds.tv_sec;
|
||||
now.tv_nsec = microseconds.tv_usec*1000;
|
||||
#else
|
||||
now.tv_sec = time((time_t*)NULL);
|
||||
now.tv_nsec = 0;
|
||||
#endif
|
||||
return (timespec2ntfs(now));
|
||||
}
|
||||
|
||||
#endif /* _NTFS_NTFSTIME_H */
|
||||
|
637
source/libntfs/object_id.c
Normal file
637
source/libntfs/object_id.c
Normal file
@ -0,0 +1,637 @@
|
||||
/**
|
||||
* object_id.c - Processing of object ids
|
||||
*
|
||||
* This module is part of ntfs-3g library
|
||||
*
|
||||
* Copyright (c) 2009 Jean-Pierre Andre
|
||||
*
|
||||
* This program/include file is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as published
|
||||
* by the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program/include file is distributed in the hope that it will be
|
||||
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
||||
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program (in the main directory of the NTFS-3G
|
||||
* distribution in the file COPYING); if not, write to the Free Software
|
||||
* Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_STDLIB_H
|
||||
#include <stdlib.h>
|
||||
#endif
|
||||
#ifdef HAVE_ERRNO_H
|
||||
#include <errno.h>
|
||||
#endif
|
||||
#ifdef HAVE_STRING_H
|
||||
#include <string.h>
|
||||
#endif
|
||||
#ifdef HAVE_SYS_STAT_H
|
||||
#include <sys/stat.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_SETXATTR
|
||||
#include <sys/xattr.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_SYS_SYSMACROS_H
|
||||
#include <sys/sysmacros.h>
|
||||
#endif
|
||||
|
||||
#include "types.h"
|
||||
#include "debug.h"
|
||||
#include "attrib.h"
|
||||
#include "inode.h"
|
||||
#include "dir.h"
|
||||
#include "volume.h"
|
||||
#include "mft.h"
|
||||
#include "index.h"
|
||||
#include "lcnalloc.h"
|
||||
#include "object_id.h"
|
||||
#include "logging.h"
|
||||
#include "misc.h"
|
||||
|
||||
/*
|
||||
* Endianness considerations
|
||||
*
|
||||
* According to RFC 4122, GUIDs should be printed with the most
|
||||
* significant byte first, and the six fields be compared individually
|
||||
* for ordering. RFC 4122 does not define the internal representation.
|
||||
*
|
||||
* Here we always copy disk images with no endianness change,
|
||||
* and, for indexing, GUIDs are compared as if they were a sequence
|
||||
* of four unsigned 32 bit integers.
|
||||
*
|
||||
* --------------------- begin from RFC 4122 ----------------------
|
||||
* Consider each field of the UUID to be an unsigned integer as shown
|
||||
* in the table in section Section 4.1.2. Then, to compare a pair of
|
||||
* UUIDs, arithmetically compare the corresponding fields from each
|
||||
* UUID in order of significance and according to their data type.
|
||||
* Two UUIDs are equal if and only if all the corresponding fields
|
||||
* are equal.
|
||||
*
|
||||
* UUIDs, as defined in this document, can also be ordered
|
||||
* lexicographically. For a pair of UUIDs, the first one follows the
|
||||
* second if the most significant field in which the UUIDs differ is
|
||||
* greater for the first UUID. The second precedes the first if the
|
||||
* most significant field in which the UUIDs differ is greater for
|
||||
* the second UUID.
|
||||
*
|
||||
* The fields are encoded as 16 octets, with the sizes and order of the
|
||||
* fields defined above, and with each field encoded with the Most
|
||||
* Significant Byte first (known as network byte order). Note that the
|
||||
* field names, particularly for multiplexed fields, follow historical
|
||||
* practice.
|
||||
*
|
||||
* 0 1 2 3
|
||||
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
||||
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
* | time_low |
|
||||
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
* | time_mid | time_hi_and_version |
|
||||
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
* |clk_seq_hi_res | clk_seq_low | node (0-1) |
|
||||
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
* | node (2-5) |
|
||||
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
*
|
||||
* ---------------------- end from RFC 4122 -----------------------
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
GUID object_id;
|
||||
} OBJECT_ID_INDEX_KEY;
|
||||
|
||||
typedef struct {
|
||||
le64 file_id;
|
||||
GUID birth_volume_id;
|
||||
GUID birth_object_id;
|
||||
GUID domain_id;
|
||||
} OBJECT_ID_INDEX_DATA; // known as OBJ_ID_INDEX_DATA
|
||||
|
||||
struct OBJECT_ID_INDEX { /* index entry in $Extend/$ObjId */
|
||||
INDEX_ENTRY_HEADER header;
|
||||
OBJECT_ID_INDEX_KEY key;
|
||||
OBJECT_ID_INDEX_DATA data;
|
||||
} ;
|
||||
|
||||
static ntfschar objid_index_name[] = { const_cpu_to_le16('$'),
|
||||
const_cpu_to_le16('O') };
|
||||
#ifdef HAVE_SETXATTR /* extended attributes interface required */
|
||||
|
||||
/*
|
||||
* Set the index for a new object id
|
||||
*
|
||||
* Returns 0 if success
|
||||
* -1 if failure, explained by errno
|
||||
*/
|
||||
|
||||
static int set_object_id_index(ntfs_inode *ni, ntfs_index_context *xo,
|
||||
const OBJECT_ID_ATTR *object_id)
|
||||
{
|
||||
struct OBJECT_ID_INDEX indx;
|
||||
u64 file_id_cpu;
|
||||
le64 file_id;
|
||||
le16 seqn;
|
||||
|
||||
seqn = ni->mrec->sequence_number;
|
||||
file_id_cpu = MK_MREF(ni->mft_no,le16_to_cpu(seqn));
|
||||
file_id = cpu_to_le64(file_id_cpu);
|
||||
indx.header.data_offset = const_cpu_to_le16(
|
||||
sizeof(INDEX_ENTRY_HEADER)
|
||||
+ sizeof(OBJECT_ID_INDEX_KEY));
|
||||
indx.header.data_length = const_cpu_to_le16(
|
||||
sizeof(OBJECT_ID_INDEX_DATA));
|
||||
indx.header.reservedV = const_cpu_to_le32(0);
|
||||
indx.header.length = const_cpu_to_le16(
|
||||
sizeof(struct OBJECT_ID_INDEX));
|
||||
indx.header.key_length = const_cpu_to_le16(
|
||||
sizeof(OBJECT_ID_INDEX_KEY));
|
||||
indx.header.flags = const_cpu_to_le16(0);
|
||||
indx.header.reserved = const_cpu_to_le16(0);
|
||||
|
||||
memcpy(&indx.key.object_id,object_id,sizeof(GUID));
|
||||
|
||||
indx.data.file_id = file_id;
|
||||
memcpy(&indx.data.birth_volume_id,
|
||||
&object_id->birth_volume_id,sizeof(GUID));
|
||||
memcpy(&indx.data.birth_object_id,
|
||||
&object_id->birth_object_id,sizeof(GUID));
|
||||
memcpy(&indx.data.domain_id,
|
||||
&object_id->domain_id,sizeof(GUID));
|
||||
ntfs_index_ctx_reinit(xo);
|
||||
return (ntfs_ie_add(xo,(INDEX_ENTRY*)&indx));
|
||||
}
|
||||
|
||||
#endif /* HAVE_SETXATTR */
|
||||
|
||||
/*
|
||||
* Open the $Extend/$ObjId file and its index
|
||||
*
|
||||
* Return the index context if opened
|
||||
* or NULL if an error occurred (errno tells why)
|
||||
*
|
||||
* The index has to be freed and inode closed when not needed any more.
|
||||
*/
|
||||
|
||||
static ntfs_index_context *open_object_id_index(ntfs_volume *vol)
|
||||
{
|
||||
u64 inum;
|
||||
ntfs_inode *ni;
|
||||
ntfs_inode *dir_ni;
|
||||
ntfs_index_context *xo;
|
||||
|
||||
/* do not use path_name_to inode - could reopen root */
|
||||
dir_ni = ntfs_inode_open(vol, FILE_Extend);
|
||||
ni = (ntfs_inode*)NULL;
|
||||
if (dir_ni) {
|
||||
inum = ntfs_inode_lookup_by_mbsname(dir_ni,"$ObjId");
|
||||
if (inum != (u64)-1)
|
||||
ni = ntfs_inode_open(vol, inum);
|
||||
ntfs_inode_close(dir_ni);
|
||||
}
|
||||
if (ni) {
|
||||
xo = ntfs_index_ctx_get(ni, objid_index_name, 2);
|
||||
if (!xo) {
|
||||
ntfs_inode_close(ni);
|
||||
}
|
||||
} else
|
||||
xo = (ntfs_index_context*)NULL;
|
||||
return (xo);
|
||||
}
|
||||
|
||||
#ifdef HAVE_SETXATTR /* extended attributes interface required */
|
||||
|
||||
/*
|
||||
* Merge object_id data stored in the index into
|
||||
* a full object_id struct.
|
||||
*
|
||||
* returns 0 if merging successful
|
||||
* -1 if no data could be merged. This is generally not an error
|
||||
*/
|
||||
|
||||
static int merge_index_data(ntfs_inode *ni,
|
||||
const OBJECT_ID_ATTR *objectid_attr,
|
||||
OBJECT_ID_ATTR *full_objectid)
|
||||
{
|
||||
OBJECT_ID_INDEX_KEY key;
|
||||
struct OBJECT_ID_INDEX *entry;
|
||||
ntfs_index_context *xo;
|
||||
ntfs_inode *xoni;
|
||||
int res;
|
||||
|
||||
res = -1;
|
||||
xo = open_object_id_index(ni->vol);
|
||||
if (xo) {
|
||||
memcpy(&key.object_id,objectid_attr,sizeof(GUID));
|
||||
if (!ntfs_index_lookup(&key,
|
||||
sizeof(OBJECT_ID_INDEX_KEY), xo)) {
|
||||
entry = (struct OBJECT_ID_INDEX*)xo->entry;
|
||||
/* make sure inode numbers match */
|
||||
if (entry
|
||||
&& (MREF(le64_to_cpu(entry->data.file_id))
|
||||
== ni->mft_no)) {
|
||||
memcpy(&full_objectid->birth_volume_id,
|
||||
&entry->data.birth_volume_id,
|
||||
sizeof(GUID));
|
||||
memcpy(&full_objectid->birth_object_id,
|
||||
&entry->data.birth_object_id,
|
||||
sizeof(GUID));
|
||||
memcpy(&full_objectid->domain_id,
|
||||
&entry->data.domain_id,
|
||||
sizeof(GUID));
|
||||
res = 0;
|
||||
}
|
||||
}
|
||||
xoni = xo->ni;
|
||||
ntfs_index_ctx_put(xo);
|
||||
ntfs_inode_close(xoni);
|
||||
}
|
||||
return (res);
|
||||
}
|
||||
|
||||
#endif /* HAVE_SETXATTR */
|
||||
|
||||
/*
|
||||
* Remove an object id index entry if attribute present
|
||||
*
|
||||
* Returns the size of existing object id
|
||||
* (the existing object_d is returned)
|
||||
* -1 if failure, explained by errno
|
||||
*/
|
||||
|
||||
static int remove_object_id_index(ntfs_attr *na, ntfs_index_context *xo,
|
||||
OBJECT_ID_ATTR *old_attr)
|
||||
{
|
||||
OBJECT_ID_INDEX_KEY key;
|
||||
struct OBJECT_ID_INDEX *entry;
|
||||
s64 size;
|
||||
int ret;
|
||||
|
||||
ret = na->data_size;
|
||||
if (ret) {
|
||||
/* read the existing object id attribute */
|
||||
size = ntfs_attr_pread(na, 0, sizeof(GUID), old_attr);
|
||||
if (size >= (s64)sizeof(GUID)) {
|
||||
memcpy(&key.object_id,
|
||||
&old_attr->object_id,sizeof(GUID));
|
||||
size = sizeof(GUID);
|
||||
if (!ntfs_index_lookup(&key,
|
||||
sizeof(OBJECT_ID_INDEX_KEY), xo)) {
|
||||
entry = (struct OBJECT_ID_INDEX*)xo->entry;
|
||||
memcpy(&old_attr->birth_volume_id,
|
||||
&entry->data.birth_volume_id,
|
||||
sizeof(GUID));
|
||||
memcpy(&old_attr->birth_object_id,
|
||||
&entry->data.birth_object_id,
|
||||
sizeof(GUID));
|
||||
memcpy(&old_attr->domain_id,
|
||||
&entry->data.domain_id,
|
||||
sizeof(GUID));
|
||||
size = sizeof(OBJECT_ID_ATTR);
|
||||
if (ntfs_index_rm(xo))
|
||||
ret = -1;
|
||||
}
|
||||
} else {
|
||||
ret = -1;
|
||||
errno = ENODATA;
|
||||
}
|
||||
}
|
||||
return (ret);
|
||||
}
|
||||
|
||||
#ifdef HAVE_SETXATTR /* extended attributes interface required */
|
||||
|
||||
/*
|
||||
* Update the object id and index
|
||||
*
|
||||
* The object_id attribute should have been created and the
|
||||
* non-duplication of the GUID should have been checked before.
|
||||
*
|
||||
* Returns 0 if success
|
||||
* -1 if failure, explained by errno
|
||||
* If could not remove the existing index, nothing is done,
|
||||
* If could not write the new data, no index entry is inserted
|
||||
* If failed to insert the index, data is removed
|
||||
*/
|
||||
|
||||
static int update_object_id(ntfs_inode *ni, ntfs_index_context *xo,
|
||||
const OBJECT_ID_ATTR *value, size_t size)
|
||||
{
|
||||
OBJECT_ID_ATTR old_attr;
|
||||
ntfs_attr *na;
|
||||
int oldsize;
|
||||
int written;
|
||||
int res;
|
||||
|
||||
res = 0;
|
||||
|
||||
na = ntfs_attr_open(ni, AT_OBJECT_ID, AT_UNNAMED, 0);
|
||||
if (na) {
|
||||
|
||||
/* remove the existing index entry */
|
||||
oldsize = remove_object_id_index(na,xo,&old_attr);
|
||||
if (oldsize < 0)
|
||||
res = -1;
|
||||
else {
|
||||
/* resize attribute */
|
||||
res = ntfs_attr_truncate(na, (s64)sizeof(GUID));
|
||||
/* write the object_id in attribute */
|
||||
if (!res && value) {
|
||||
written = (int)ntfs_attr_pwrite(na,
|
||||
(s64)0, (s64)sizeof(GUID),
|
||||
&value->object_id);
|
||||
if (written != (s64)sizeof(GUID)) {
|
||||
ntfs_log_error("Failed to update "
|
||||
"object id\n");
|
||||
errno = EIO;
|
||||
res = -1;
|
||||
}
|
||||
}
|
||||
/* write index part if provided */
|
||||
if (!res
|
||||
&& ((size < sizeof(OBJECT_ID_ATTR))
|
||||
|| set_object_id_index(ni,xo,value))) {
|
||||
/*
|
||||
* If cannot index, try to remove the object
|
||||
* id and log the error. There will be an
|
||||
* inconsistency if removal fails.
|
||||
*/
|
||||
ntfs_attr_rm(na);
|
||||
ntfs_log_error("Failed to index object id."
|
||||
" Possible corruption.\n");
|
||||
}
|
||||
}
|
||||
ntfs_attr_close(na);
|
||||
NInoSetDirty(ni);
|
||||
} else
|
||||
res = -1;
|
||||
return (res);
|
||||
}
|
||||
|
||||
/*
|
||||
* Add a (dummy) object id to an inode if it does not exist
|
||||
*
|
||||
* returns 0 if attribute was inserted (or already present)
|
||||
* -1 if adding failed (explained by errno)
|
||||
*/
|
||||
|
||||
static int add_object_id(ntfs_inode *ni, int flags)
|
||||
{
|
||||
int res;
|
||||
u8 dummy;
|
||||
|
||||
res = -1; /* default return */
|
||||
if (!ntfs_attr_exist(ni,AT_OBJECT_ID, AT_UNNAMED,0)) {
|
||||
if (!(flags & XATTR_REPLACE)) {
|
||||
/*
|
||||
* no object id attribute : add one,
|
||||
* apparently, this does not feed the new value in
|
||||
* Note : NTFS version must be >= 3
|
||||
*/
|
||||
if (ni->vol->major_ver >= 3) {
|
||||
res = ntfs_attr_add(ni, AT_OBJECT_ID,
|
||||
AT_UNNAMED, 0, &dummy, (s64)0);
|
||||
NInoSetDirty(ni);
|
||||
} else
|
||||
errno = EOPNOTSUPP;
|
||||
} else
|
||||
errno = ENODATA;
|
||||
} else {
|
||||
if (flags & XATTR_CREATE)
|
||||
errno = EEXIST;
|
||||
else
|
||||
res = 0;
|
||||
}
|
||||
return (res);
|
||||
}
|
||||
|
||||
#endif /* HAVE_SETXATTR */
|
||||
|
||||
/*
|
||||
* Delete an object_id index entry
|
||||
*
|
||||
* Returns 0 if success
|
||||
* -1 if failure, explained by errno
|
||||
*/
|
||||
|
||||
int ntfs_delete_object_id_index(ntfs_inode *ni)
|
||||
{
|
||||
ntfs_index_context *xo;
|
||||
ntfs_inode *xoni;
|
||||
ntfs_attr *na;
|
||||
OBJECT_ID_ATTR old_attr;
|
||||
int res;
|
||||
|
||||
res = 0;
|
||||
na = ntfs_attr_open(ni, AT_OBJECT_ID, AT_UNNAMED, 0);
|
||||
if (na) {
|
||||
/*
|
||||
* read the existing object id
|
||||
* and un-index it
|
||||
*/
|
||||
xo = open_object_id_index(ni->vol);
|
||||
if (xo) {
|
||||
if (remove_object_id_index(na,xo,&old_attr) < 0)
|
||||
res = -1;
|
||||
xoni = xo->ni;
|
||||
ntfs_index_entry_mark_dirty(xo);
|
||||
NInoSetDirty(xoni);
|
||||
ntfs_index_ctx_put(xo);
|
||||
ntfs_inode_close(xoni);
|
||||
}
|
||||
ntfs_attr_close(na);
|
||||
}
|
||||
return (res);
|
||||
}
|
||||
|
||||
#ifdef HAVE_SETXATTR /* extended attributes interface required */
|
||||
|
||||
/*
|
||||
* Get the ntfs object id into an extended attribute
|
||||
*
|
||||
* If present, the object_id from the attribute and the GUIDs
|
||||
* from the index are returned (formatted as OBJECT_ID_ATTR)
|
||||
*
|
||||
* Returns the global size (can be 0, 16 or 64)
|
||||
* and the buffer is updated if it is long enough
|
||||
*/
|
||||
|
||||
int ntfs_get_ntfs_object_id(ntfs_inode *ni, char *value, size_t size)
|
||||
{
|
||||
OBJECT_ID_ATTR full_objectid;
|
||||
OBJECT_ID_ATTR *objectid_attr;
|
||||
s64 attr_size;
|
||||
int full_size;
|
||||
|
||||
full_size = 0; /* default to no data and some error to be defined */
|
||||
if (ni) {
|
||||
objectid_attr = (OBJECT_ID_ATTR*)ntfs_attr_readall(ni,
|
||||
AT_OBJECT_ID,(ntfschar*)NULL, 0, &attr_size);
|
||||
if (objectid_attr) {
|
||||
/* restrict to only GUID present in attr */
|
||||
if (attr_size == sizeof(GUID)) {
|
||||
memcpy(&full_objectid.object_id,
|
||||
objectid_attr,sizeof(GUID));
|
||||
full_size = sizeof(GUID);
|
||||
/* get data from index, if any */
|
||||
if (!merge_index_data(ni, objectid_attr,
|
||||
&full_objectid)) {
|
||||
full_size = sizeof(OBJECT_ID_ATTR);
|
||||
}
|
||||
if (full_size <= (s64)size) {
|
||||
if (value)
|
||||
memcpy(value,&full_objectid,
|
||||
full_size);
|
||||
else
|
||||
errno = EINVAL;
|
||||
}
|
||||
} else {
|
||||
/* unexpected size, better return unsupported */
|
||||
errno = EOPNOTSUPP;
|
||||
full_size = 0;
|
||||
}
|
||||
free(objectid_attr);
|
||||
} else
|
||||
errno = ENODATA;
|
||||
}
|
||||
return (full_size ? (int)full_size : -errno);
|
||||
}
|
||||
|
||||
/*
|
||||
* Set the object id from an extended attribute
|
||||
*
|
||||
* If the size is 64, the attribute and index are set.
|
||||
* else if the size is not less than 16 only the attribute is set.
|
||||
* The object id index is set accordingly.
|
||||
*
|
||||
* Returns 0, or -1 if there is a problem
|
||||
*/
|
||||
|
||||
int ntfs_set_ntfs_object_id(ntfs_inode *ni,
|
||||
const char *value, size_t size, int flags)
|
||||
{
|
||||
OBJECT_ID_INDEX_KEY key;
|
||||
ntfs_inode *xoni;
|
||||
ntfs_index_context *xo;
|
||||
int res;
|
||||
|
||||
res = 0;
|
||||
if (ni && value && (size >= sizeof(GUID))) {
|
||||
xo = open_object_id_index(ni->vol);
|
||||
if (xo) {
|
||||
/* make sure the GUID was not used somewhere */
|
||||
memcpy(&key.object_id, value, sizeof(GUID));
|
||||
if (ntfs_index_lookup(&key,
|
||||
sizeof(OBJECT_ID_INDEX_KEY), xo)) {
|
||||
ntfs_index_ctx_reinit(xo);
|
||||
res = add_object_id(ni, flags);
|
||||
if (!res) {
|
||||
/* update value and index */
|
||||
res = update_object_id(ni,xo,
|
||||
(const OBJECT_ID_ATTR*)value,
|
||||
size);
|
||||
}
|
||||
} else {
|
||||
/* GUID is present elsewhere */
|
||||
res = -1;
|
||||
errno = EEXIST;
|
||||
}
|
||||
xoni = xo->ni;
|
||||
ntfs_index_entry_mark_dirty(xo);
|
||||
NInoSetDirty(xoni);
|
||||
ntfs_index_ctx_put(xo);
|
||||
ntfs_inode_close(xoni);
|
||||
} else {
|
||||
res = -1;
|
||||
}
|
||||
} else {
|
||||
errno = EINVAL;
|
||||
res = -1;
|
||||
}
|
||||
return (res ? -1 : 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Remove the object id
|
||||
*
|
||||
* Returns 0, or -1 if there is a problem
|
||||
*/
|
||||
|
||||
int ntfs_remove_ntfs_object_id(ntfs_inode *ni)
|
||||
{
|
||||
int res;
|
||||
int olderrno;
|
||||
ntfs_attr *na;
|
||||
ntfs_inode *xoni;
|
||||
ntfs_index_context *xo;
|
||||
int oldsize;
|
||||
OBJECT_ID_ATTR old_attr;
|
||||
|
||||
res = 0;
|
||||
if (ni) {
|
||||
/*
|
||||
* open and delete the object id
|
||||
*/
|
||||
na = ntfs_attr_open(ni, AT_OBJECT_ID,
|
||||
AT_UNNAMED,0);
|
||||
if (na) {
|
||||
/* first remove index (old object id needed) */
|
||||
xo = open_object_id_index(ni->vol);
|
||||
if (xo) {
|
||||
oldsize = remove_object_id_index(na,xo,
|
||||
&old_attr);
|
||||
if (oldsize < 0) {
|
||||
res = -1;
|
||||
} else {
|
||||
/* now remove attribute */
|
||||
res = ntfs_attr_rm(na);
|
||||
if (res
|
||||
&& (oldsize > (int)sizeof(GUID))) {
|
||||
/*
|
||||
* If we could not remove the
|
||||
* attribute, try to restore the
|
||||
* index and log the error. There
|
||||
* will be an inconsistency if
|
||||
* the reindexing fails.
|
||||
*/
|
||||
set_object_id_index(ni, xo,
|
||||
&old_attr);
|
||||
ntfs_log_error(
|
||||
"Failed to remove object id."
|
||||
" Possible corruption.\n");
|
||||
}
|
||||
}
|
||||
|
||||
xoni = xo->ni;
|
||||
ntfs_index_entry_mark_dirty(xo);
|
||||
NInoSetDirty(xoni);
|
||||
ntfs_index_ctx_put(xo);
|
||||
ntfs_inode_close(xoni);
|
||||
}
|
||||
olderrno = errno;
|
||||
ntfs_attr_close(na);
|
||||
/* avoid errno pollution */
|
||||
if (errno == ENOENT)
|
||||
errno = olderrno;
|
||||
} else {
|
||||
errno = ENODATA;
|
||||
res = -1;
|
||||
}
|
||||
NInoSetDirty(ni);
|
||||
} else {
|
||||
errno = EINVAL;
|
||||
res = -1;
|
||||
}
|
||||
return (res ? -1 : 0);
|
||||
}
|
||||
|
||||
#endif /* HAVE_SETXATTR */
|
35
source/libntfs/object_id.h
Normal file
35
source/libntfs/object_id.h
Normal file
@ -0,0 +1,35 @@
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2008 Jean-Pierre Andre
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program (in the main directory of the NTFS-3G
|
||||
* distribution in the file COPYING); if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#ifndef OBJECT_ID_H
|
||||
#define OBJECT_ID_H
|
||||
|
||||
int ntfs_get_ntfs_object_id(ntfs_inode *ni, char *value, size_t size);
|
||||
|
||||
int ntfs_set_ntfs_object_id(ntfs_inode *ni, const char *value,
|
||||
size_t size, int flags);
|
||||
int ntfs_remove_ntfs_object_id(ntfs_inode *ni);
|
||||
|
||||
int ntfs_delete_object_id_index(ntfs_inode *ni);
|
||||
|
||||
#endif /* OBJECT_ID_H */
|
82
source/libntfs/param.h
Normal file
82
source/libntfs/param.h
Normal file
@ -0,0 +1,82 @@
|
||||
/*
|
||||
* param.h - Parameter values for ntfs-3g
|
||||
*
|
||||
* Copyright (c) 2009 Jean-Pierre Andre
|
||||
*
|
||||
* This program/include file is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as published
|
||||
* by the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program/include file is distributed in the hope that it will be
|
||||
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
||||
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program (in the main directory of the NTFS-3G
|
||||
* distribution in the file COPYING); if not, write to the Free Software
|
||||
* Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#ifndef _NTFS_PARAM_H
|
||||
#define _NTFS_PARAM_H
|
||||
|
||||
#define CACHE_INODE_SIZE 32 /* inode cache, zero or >= 3 and not too big */
|
||||
#define CACHE_NIDATA_SIZE 64 /* idata cache, zero or >= 3 and not too big */
|
||||
#define CACHE_LOOKUP_SIZE 64 /* lookup cache, zero or >= 3 and not too big */
|
||||
#define CACHE_SECURID_SIZE 16 /* securid cache, zero or >= 3 and not too big */
|
||||
#define CACHE_LEGACY_SIZE 8 /* legacy cache size, zero or >= 3 and not too big */
|
||||
|
||||
#define FORCE_FORMAT_v1x 0 /* Insert security data as in NTFS v1.x */
|
||||
#define OWNERFROMACL 1 /* Get the owner from ACL (not Windows owner) */
|
||||
|
||||
/* default security sub-authorities */
|
||||
enum {
|
||||
DEFSECAUTH1 = -1153374643, /* 3141592653 */
|
||||
DEFSECAUTH2 = 589793238,
|
||||
DEFSECAUTH3 = 462843383,
|
||||
DEFSECBASE = 10000
|
||||
};
|
||||
|
||||
/*
|
||||
* Parameters for compression
|
||||
*/
|
||||
|
||||
/* default option for compression */
|
||||
#define DEFAULT_COMPRESSION FALSE
|
||||
/* (log2 of) number of clusters in a compression block for new files */
|
||||
#define STANDARD_COMPRESSION_UNIT 4
|
||||
/* maximum cluster size for allowing compression for new files */
|
||||
#define MAX_COMPRESSION_CLUSTER_SIZE 4096
|
||||
|
||||
/*
|
||||
* Permission checking modes for high level and low level
|
||||
*
|
||||
* The choices for high and low lowel are independent, they have
|
||||
* no effect on the library
|
||||
*
|
||||
* Stick to the recommended values unless you understand the consequences
|
||||
* on protection and performances. Use of cacheing is good for
|
||||
* performances, but bad on security.
|
||||
*
|
||||
* Possible values for high level :
|
||||
* 1 : no cache, kernel control (recommended)
|
||||
* 4 : no cache, file system control
|
||||
* 7 : no cache, kernel control for ACLs
|
||||
*
|
||||
* Possible values for low level :
|
||||
* 2 : no cache, kernel control
|
||||
* 3 : use kernel/fuse cache, kernel control
|
||||
* 5 : no cache, file system control (recommended)
|
||||
* 8 : no cache, kernel control for ACLs
|
||||
*
|
||||
* Use of options 7 and 8 requires a patch to fuse
|
||||
* When Posix ACLs are selected in the configure options, a value
|
||||
* of 6 is added in the mount report.
|
||||
*/
|
||||
|
||||
#define HPERMSCONFIG 1
|
||||
#define LPERMSCONFIG 5
|
||||
|
||||
#endif /* defined _NTFS_PARAM_H */
|
@ -1,8 +1,7 @@
|
||||
/**
|
||||
* reparse.c - Processing of reparse points
|
||||
*
|
||||
* This module is part of ntfs-3g library, but may also be
|
||||
* integrated in tools running over Linux or Windows
|
||||
* This module is part of ntfs-3g library
|
||||
*
|
||||
* Copyright (c) 2008-2009 Jean-Pierre Andre
|
||||
*
|
||||
@ -89,12 +88,6 @@ struct SYMLINK_REPARSE_DATA { /* reparse data for symlinks */
|
||||
char path_buffer[0]; /* above data assume this is char array */
|
||||
} ;
|
||||
|
||||
struct INODE_STACK {
|
||||
struct INODE_STACK *previous;
|
||||
struct INODE_STACK *next;
|
||||
ntfs_inode *ni;
|
||||
} ;
|
||||
|
||||
struct REPARSE_INDEX { /* index entry in $Extend/$Reparse */
|
||||
INDEX_ENTRY_HEADER header;
|
||||
REPARSE_INDEX_KEY key;
|
||||
@ -194,10 +187,10 @@ static u64 ntfs_fix_file_name(ntfs_inode *dir_ni, ntfschar *uname,
|
||||
if (entry) {
|
||||
found = &entry->key.file_name;
|
||||
if (lkup
|
||||
&& !ntfs_names_collate(find.attr.file_name,
|
||||
&& ntfs_names_are_equal(find.attr.file_name,
|
||||
find.attr.file_name_length,
|
||||
found->file_name, found->file_name_length,
|
||||
1, IGNORE_CASE,
|
||||
IGNORE_CASE,
|
||||
vol->upcase, vol->upcase_len))
|
||||
lkup = 0;
|
||||
if (!lkup) {
|
||||
@ -205,9 +198,7 @@ static u64 ntfs_fix_file_name(ntfs_inode *dir_ni, ntfschar *uname,
|
||||
* name found :
|
||||
* fix original name and return inode
|
||||
*/
|
||||
lemref = *(le64*)((char*)found->file_name
|
||||
- sizeof(INDEX_ENTRY_HEADER)
|
||||
- sizeof(FILE_NAME_ATTR));
|
||||
lemref = entry->indexed_file;
|
||||
mref = le64_to_cpu(lemref);
|
||||
for (i=0; i<found->file_name_length; i++)
|
||||
uname[i] = found->file_name[i];
|
||||
@ -271,182 +262,91 @@ static char *search_absolute(ntfs_volume *vol, ntfschar *path,
|
||||
return (target);
|
||||
}
|
||||
|
||||
/*
|
||||
* Stack the next inode in the path
|
||||
*
|
||||
* Returns the new top of stack
|
||||
* or NULL (with stack unchanged) if there is a problem
|
||||
*/
|
||||
|
||||
static struct INODE_STACK *stack_inode(struct INODE_STACK *topni,
|
||||
ntfschar *name, int len, BOOL fix)
|
||||
{
|
||||
struct INODE_STACK *curni;
|
||||
u64 inum;
|
||||
|
||||
if (fix)
|
||||
inum = ntfs_fix_file_name(topni->ni, name, len);
|
||||
else
|
||||
inum = ntfs_inode_lookup_by_name(topni->ni, name, len);
|
||||
if (inum != (u64)-1) {
|
||||
inum = MREF(inum);
|
||||
curni = (struct INODE_STACK*)malloc(sizeof(struct INODE_STACK));
|
||||
if (curni) {
|
||||
curni->ni = ntfs_inode_open(topni->ni->vol, inum);
|
||||
topni->next = curni;
|
||||
curni->previous = topni;
|
||||
curni->next = (struct INODE_STACK*)NULL;
|
||||
}
|
||||
} else
|
||||
curni = (struct INODE_STACK*)NULL;
|
||||
return (curni);
|
||||
}
|
||||
|
||||
/*
|
||||
* Destack and close the current inode in the path
|
||||
*
|
||||
* Returns the new top of stack
|
||||
* or NULL (with stack unchanged) if there is a problem
|
||||
*/
|
||||
|
||||
static struct INODE_STACK *pop_inode(struct INODE_STACK *topni)
|
||||
{
|
||||
struct INODE_STACK *curni;
|
||||
|
||||
curni = (struct INODE_STACK*)NULL;
|
||||
if (topni->previous) {
|
||||
if (!ntfs_inode_close(topni->ni)) {
|
||||
curni = topni->previous;
|
||||
free(topni);
|
||||
curni->next = (struct INODE_STACK*)NULL;
|
||||
}
|
||||
} else {
|
||||
/* ".." reached the root of fs */
|
||||
errno = ENOENT;
|
||||
}
|
||||
return (curni);
|
||||
}
|
||||
|
||||
/*
|
||||
* Search for a symbolic link along the target path,
|
||||
* with the target defined as a relative path
|
||||
*
|
||||
* Note : the path used to access the current inode, may be
|
||||
* different from the one implied in the target definition,
|
||||
* when an inode has names in several directories.
|
||||
*
|
||||
* Returns the path translated to a Linux path
|
||||
* or NULL if the path is not valid
|
||||
*/
|
||||
|
||||
static char *search_relative(ntfs_volume *vol, ntfschar *path, int count,
|
||||
const char *base, BOOL isdir)
|
||||
static char *search_relative(ntfs_inode *ni, ntfschar *path, int count)
|
||||
{
|
||||
struct INODE_STACK *topni;
|
||||
struct INODE_STACK *curni;
|
||||
char *target;
|
||||
ntfschar *unicode;
|
||||
int unisz;
|
||||
int start;
|
||||
int len;
|
||||
char *target = (char*)NULL;
|
||||
ntfs_inode *curni;
|
||||
ntfs_inode *newni;
|
||||
u64 inum;
|
||||
int pos;
|
||||
int lth;
|
||||
BOOL ok;
|
||||
int max = 32; /* safety */
|
||||
|
||||
target = (char*)NULL; /* default return */
|
||||
topni = (struct INODE_STACK*)malloc(sizeof(struct INODE_STACK));
|
||||
if (topni) {
|
||||
topni->ni = ntfs_inode_open(vol, FILE_root);
|
||||
topni->previous = (struct INODE_STACK*)NULL;
|
||||
topni->next = (struct INODE_STACK*)NULL;
|
||||
}
|
||||
if (topni && topni->ni) {
|
||||
/*
|
||||
* Process the base path
|
||||
*/
|
||||
unicode = (ntfschar*)NULL;
|
||||
unisz = ntfs_mbstoucs(base, &unicode);
|
||||
if ((unisz > 0) && unicode) {
|
||||
start = 1;
|
||||
do {
|
||||
len = 0;
|
||||
while (((start + len) < unisz)
|
||||
&& (unicode[start + len]
|
||||
!= const_cpu_to_le16('/')))
|
||||
len++;
|
||||
curni = (struct INODE_STACK*)NULL;
|
||||
if ((start + len) < unisz) {
|
||||
curni = stack_inode(topni,
|
||||
&unicode[start], len, FALSE);
|
||||
if (curni)
|
||||
topni = curni;
|
||||
} else
|
||||
curni = topni;
|
||||
start += len + 1;
|
||||
} while (curni
|
||||
&& topni->ni
|
||||
&& (topni->ni->mrec->flags
|
||||
& MFT_RECORD_IS_DIRECTORY)
|
||||
&& (start < unisz));
|
||||
free(unicode);
|
||||
if (curni
|
||||
&& topni->ni
|
||||
&& (topni->ni->mrec->flags
|
||||
& MFT_RECORD_IS_DIRECTORY)) {
|
||||
start = 0;
|
||||
do {
|
||||
len = 0;
|
||||
while (((start + len) < count)
|
||||
&& (path[start + len]
|
||||
!= const_cpu_to_le16('\\')))
|
||||
len++;
|
||||
curni = (struct INODE_STACK*)NULL;
|
||||
if ((path[start]
|
||||
== const_cpu_to_le16('.'))
|
||||
&& ((len == 1)
|
||||
|| ((len == 2)
|
||||
&& (path[start+1]
|
||||
== const_cpu_to_le16('.'))))) {
|
||||
/* leave the .. or . in the path */
|
||||
curni = topni;
|
||||
if (len == 2) {
|
||||
curni = pop_inode(topni);
|
||||
if (curni)
|
||||
topni = curni;
|
||||
}
|
||||
pos = 0;
|
||||
ok = TRUE;
|
||||
curni = ntfs_dir_parent_inode(ni);
|
||||
while (curni && ok && (pos < (count - 1)) && --max) {
|
||||
if ((count >= (pos + 2))
|
||||
&& (path[pos] == const_cpu_to_le16('.'))
|
||||
&& (path[pos+1] == const_cpu_to_le16('\\'))) {
|
||||
path[1] = const_cpu_to_le16('/');
|
||||
pos += 2;
|
||||
} else {
|
||||
curni = stack_inode(topni,
|
||||
&path[start], len, TRUE);
|
||||
if (curni)
|
||||
topni = curni;
|
||||
}
|
||||
if (topni->ni) {
|
||||
start += len;
|
||||
if (start < count)
|
||||
path[start++]
|
||||
= const_cpu_to_le16('/');
|
||||
}
|
||||
} while (curni
|
||||
&& topni->ni
|
||||
&& (topni->ni->mrec->flags
|
||||
if ((count >= (pos + 3))
|
||||
&& (path[pos] == const_cpu_to_le16('.'))
|
||||
&&(path[pos+1] == const_cpu_to_le16('.'))
|
||||
&& (path[pos+2] == const_cpu_to_le16('\\'))) {
|
||||
path[2] = const_cpu_to_le16('/');
|
||||
pos += 3;
|
||||
newni = ntfs_dir_parent_inode(curni);
|
||||
if (curni != ni)
|
||||
ntfs_inode_close(curni);
|
||||
curni = newni;
|
||||
if (!curni)
|
||||
ok = FALSE;
|
||||
} else {
|
||||
lth = 0;
|
||||
while (((pos + lth) < count)
|
||||
&& (path[pos + lth] != const_cpu_to_le16('\\')))
|
||||
lth++;
|
||||
if (lth > 0)
|
||||
inum = ntfs_fix_file_name(curni,&path[pos],lth);
|
||||
else
|
||||
inum = (u64)-1;
|
||||
if (!lth
|
||||
|| ((curni != ni)
|
||||
&& ntfs_inode_close(curni))
|
||||
|| (inum == (u64)-1))
|
||||
ok = FALSE;
|
||||
else {
|
||||
curni = ntfs_inode_open(ni->vol, MREF(inum));
|
||||
if (!curni)
|
||||
ok = FALSE;
|
||||
else {
|
||||
if (ok && ((pos + lth) < count)) {
|
||||
path[pos + lth] = const_cpu_to_le16('/');
|
||||
pos += lth + 1;
|
||||
} else {
|
||||
pos += lth;
|
||||
if ((ni->mrec->flags ^ curni->mrec->flags)
|
||||
& MFT_RECORD_IS_DIRECTORY)
|
||||
&& (start < count));
|
||||
if (curni
|
||||
&& topni->ni
|
||||
&& (topni->ni->mrec->flags
|
||||
& MFT_RECORD_IS_DIRECTORY
|
||||
? isdir : !isdir)) {
|
||||
if (ntfs_ucstombs(path, count,
|
||||
&target, 0) < 0) {
|
||||
if (target) {
|
||||
free(target);
|
||||
ok = FALSE;
|
||||
if (ntfs_inode_close(curni))
|
||||
ok = FALSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (ok && (ntfs_ucstombs(path, count, &target, 0) < 0)) {
|
||||
free(target); // needed ?
|
||||
target = (char*)NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
do {
|
||||
if (topni->ni)
|
||||
ntfs_inode_close(topni->ni);
|
||||
curni = topni;
|
||||
topni = topni->previous;
|
||||
free(curni);
|
||||
} while (topni);
|
||||
}
|
||||
return (target);
|
||||
}
|
||||
|
||||
@ -492,6 +392,62 @@ static int ntfs_drive_letter(ntfs_volume *vol, ntfschar letter)
|
||||
return (ret);
|
||||
}
|
||||
|
||||
/*
|
||||
* Do some sanity checks on reparse data
|
||||
*
|
||||
* The only general check is about the size (at least the tag must
|
||||
* be present)
|
||||
* If the reparse data looks like a junction point or symbolic
|
||||
* link, more checks can be done.
|
||||
*
|
||||
*/
|
||||
|
||||
static BOOL valid_reparse_data(ntfs_inode *ni,
|
||||
const REPARSE_POINT *reparse_attr, size_t size)
|
||||
{
|
||||
BOOL ok;
|
||||
unsigned int offs;
|
||||
unsigned int lth;
|
||||
const struct MOUNT_POINT_REPARSE_DATA *mount_point_data;
|
||||
const struct SYMLINK_REPARSE_DATA *symlink_data;
|
||||
|
||||
ok = ni && reparse_attr
|
||||
&& (size >= sizeof(REPARSE_POINT))
|
||||
&& (((size_t)le16_to_cpu(reparse_attr->reparse_data_length)
|
||||
+ sizeof(REPARSE_POINT)) == size);
|
||||
if (ok) {
|
||||
switch (reparse_attr->reparse_tag) {
|
||||
case IO_REPARSE_TAG_MOUNT_POINT :
|
||||
mount_point_data = (const struct MOUNT_POINT_REPARSE_DATA*)
|
||||
reparse_attr->reparse_data;
|
||||
offs = le16_to_cpu(mount_point_data->subst_name_offset);
|
||||
lth = le16_to_cpu(mount_point_data->subst_name_length);
|
||||
/* consistency checks */
|
||||
if (!(ni->mrec->flags & MFT_RECORD_IS_DIRECTORY)
|
||||
|| ((size_t)((sizeof(REPARSE_POINT)
|
||||
+ sizeof(struct MOUNT_POINT_REPARSE_DATA)
|
||||
+ offs + lth)) > size))
|
||||
ok = FALSE;
|
||||
break;
|
||||
case IO_REPARSE_TAG_SYMLINK :
|
||||
symlink_data = (const struct SYMLINK_REPARSE_DATA*)
|
||||
reparse_attr->reparse_data;
|
||||
offs = le16_to_cpu(symlink_data->subst_name_offset);
|
||||
lth = le16_to_cpu(symlink_data->subst_name_length);
|
||||
if ((size_t)((sizeof(REPARSE_POINT)
|
||||
+ sizeof(struct SYMLINK_REPARSE_DATA)
|
||||
+ offs + lth)) > size)
|
||||
ok = FALSE;
|
||||
break;
|
||||
default :
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!ok)
|
||||
errno = EINVAL;
|
||||
return (ok);
|
||||
}
|
||||
|
||||
/*
|
||||
* Check and translate the target of a junction point or
|
||||
* a full absolute symbolic link.
|
||||
@ -507,16 +463,12 @@ static int ntfs_drive_letter(ntfs_volume *vol, ntfschar letter)
|
||||
* or NULL if there were some problem, as described by errno
|
||||
*/
|
||||
|
||||
|
||||
static char *ntfs_get_fulllink(ntfs_volume *vol, ntfschar *junction,
|
||||
int count, const char *path, BOOL isdir)
|
||||
int count, const char *mnt_point, BOOL isdir)
|
||||
{
|
||||
char *target;
|
||||
char *fulltarget;
|
||||
int i;
|
||||
int sz;
|
||||
int level;
|
||||
const char *p;
|
||||
char *q;
|
||||
enum { DIR_JUNCTION, VOL_JUNCTION, NO_JUNCTION } kind;
|
||||
|
||||
@ -554,19 +506,11 @@ static char *ntfs_get_fulllink(ntfs_volume *vol, ntfschar *junction,
|
||||
&& !ntfs_drive_letter(vol, junction[4])) {
|
||||
target = search_absolute(vol,&junction[7],count - 7, isdir);
|
||||
if (target) {
|
||||
level = 0;
|
||||
for (p=path; *p; p++)
|
||||
if (*p == '/')
|
||||
level++;
|
||||
fulltarget = (char*)ntfs_malloc(3*level
|
||||
+ strlen(target) + 1);
|
||||
fulltarget = (char*)ntfs_malloc(strlen(mnt_point)
|
||||
+ strlen(target) + 2);
|
||||
if (fulltarget) {
|
||||
fulltarget[0] = 0;
|
||||
if (level > 1) {
|
||||
for (i=1; i<level; i++)
|
||||
strcat(fulltarget,"../");
|
||||
} else
|
||||
strcpy(fulltarget,"./");
|
||||
strcpy(fulltarget,mnt_point);
|
||||
strcat(fulltarget,"/");
|
||||
strcat(fulltarget,target);
|
||||
}
|
||||
free(target);
|
||||
@ -593,19 +537,11 @@ static char *ntfs_get_fulllink(ntfs_volume *vol, ntfschar *junction,
|
||||
&& (target[0] >= 'a')
|
||||
&& (target[0] <= 'z'))
|
||||
target[0] += 'A' - 'a';
|
||||
level = 0;
|
||||
for (p=path; *p; p++)
|
||||
if (*p == '/')
|
||||
level++;
|
||||
fulltarget = (char*)ntfs_malloc(3*level
|
||||
+ sizeof(mappingdir) + strlen(target) - 3);
|
||||
fulltarget = (char*)ntfs_malloc(strlen(mnt_point)
|
||||
+ sizeof(mappingdir) + strlen(target) + 1);
|
||||
if (fulltarget) {
|
||||
fulltarget[0] = 0;
|
||||
if (level > 1) {
|
||||
for (i=1; i<level; i++)
|
||||
strcat(fulltarget,"../");
|
||||
} else
|
||||
strcpy(fulltarget,"./");
|
||||
strcpy(fulltarget,mnt_point);
|
||||
strcat(fulltarget,"/");
|
||||
strcat(fulltarget,mappingdir);
|
||||
strcat(fulltarget,target);
|
||||
}
|
||||
@ -631,14 +567,11 @@ static char *ntfs_get_fulllink(ntfs_volume *vol, ntfschar *junction,
|
||||
*/
|
||||
|
||||
static char *ntfs_get_abslink(ntfs_volume *vol, ntfschar *junction,
|
||||
int count, const char *path, BOOL isdir)
|
||||
int count, const char *mnt_point, BOOL isdir)
|
||||
{
|
||||
char *target;
|
||||
char *fulltarget;
|
||||
int i;
|
||||
int sz;
|
||||
int level;
|
||||
const char *p;
|
||||
char *q;
|
||||
enum { FULL_PATH, ABS_PATH, REJECTED_PATH } kind;
|
||||
|
||||
@ -680,19 +613,11 @@ static char *ntfs_get_abslink(ntfs_volume *vol, ntfschar *junction,
|
||||
target = search_absolute(vol, &junction[3],
|
||||
count - 3, isdir);
|
||||
if (target) {
|
||||
level = 0;
|
||||
for (p=path; *p; p++)
|
||||
if (*p == '/')
|
||||
level++;
|
||||
fulltarget = (char*)ntfs_malloc(3*level
|
||||
+ strlen(target) + 1);
|
||||
fulltarget = (char*)ntfs_malloc(strlen(mnt_point)
|
||||
+ strlen(target) + 2);
|
||||
if (fulltarget) {
|
||||
fulltarget[0] = 0;
|
||||
if (level > 1) {
|
||||
for (i=1; i<level; i++)
|
||||
strcat(fulltarget,"../");
|
||||
} else
|
||||
strcpy(fulltarget,"./");
|
||||
strcpy(fulltarget,mnt_point);
|
||||
strcat(fulltarget,"/");
|
||||
strcat(fulltarget,target);
|
||||
}
|
||||
free(target);
|
||||
@ -716,19 +641,11 @@ static char *ntfs_get_abslink(ntfs_volume *vol, ntfschar *junction,
|
||||
&& (target[0] >= 'a')
|
||||
&& (target[0] <= 'z'))
|
||||
target[0] += 'A' - 'a';
|
||||
level = 0;
|
||||
for (p=path; *p; p++)
|
||||
if (*p == '/')
|
||||
level++;
|
||||
fulltarget = (char*)ntfs_malloc(3*level
|
||||
+ sizeof(mappingdir) + strlen(target) - 3);
|
||||
fulltarget = (char*)ntfs_malloc(strlen(mnt_point)
|
||||
+ sizeof(mappingdir) + strlen(target) + 1);
|
||||
if (fulltarget) {
|
||||
fulltarget[0] = 0;
|
||||
if (level > 1) {
|
||||
for (i=1; i<level; i++)
|
||||
strcat(fulltarget,"../");
|
||||
} else
|
||||
strcpy(fulltarget,"./");
|
||||
strcpy(fulltarget,mnt_point);
|
||||
strcat(fulltarget,"/");
|
||||
strcat(fulltarget,mappingdir);
|
||||
strcat(fulltarget,target);
|
||||
}
|
||||
@ -751,12 +668,11 @@ static char *ntfs_get_abslink(ntfs_volume *vol, ntfschar *junction,
|
||||
* or NULL if there were some problem, as described by errno
|
||||
*/
|
||||
|
||||
static char *ntfs_get_rellink(ntfs_volume *vol, ntfschar *junction,
|
||||
int count, const char *path, BOOL isdir)
|
||||
static char *ntfs_get_rellink(ntfs_inode *ni, ntfschar *junction, int count)
|
||||
{
|
||||
char *target;
|
||||
|
||||
target = search_relative(vol,junction,count,path,isdir);
|
||||
target = search_relative(ni,junction,count);
|
||||
return (target);
|
||||
}
|
||||
|
||||
@ -770,8 +686,8 @@ static char *ntfs_get_rellink(ntfs_volume *vol, ntfschar *junction,
|
||||
* symbolic link or directory junction
|
||||
*/
|
||||
|
||||
char *ntfs_make_symlink(const char *org_path,
|
||||
ntfs_inode *ni, int *pattr_size)
|
||||
char *ntfs_make_symlink(ntfs_inode *ni, const char *mnt_point,
|
||||
int *pattr_size)
|
||||
{
|
||||
s64 attr_size = 0;
|
||||
char *target;
|
||||
@ -793,26 +709,20 @@ char *ntfs_make_symlink(const char *org_path,
|
||||
vol = ni->vol;
|
||||
reparse_attr = (REPARSE_POINT*)ntfs_attr_readall(ni,
|
||||
AT_REPARSE_POINT,(ntfschar*)NULL, 0, &attr_size);
|
||||
if (reparse_attr && attr_size) {
|
||||
if (reparse_attr && attr_size
|
||||
&& valid_reparse_data(ni, reparse_attr, attr_size)) {
|
||||
switch (reparse_attr->reparse_tag) {
|
||||
case IO_REPARSE_TAG_MOUNT_POINT :
|
||||
mount_point_data = (struct MOUNT_POINT_REPARSE_DATA*)
|
||||
reparse_attr->reparse_data;
|
||||
offs = le16_to_cpu(mount_point_data->subst_name_offset);
|
||||
lth = le16_to_cpu(mount_point_data->subst_name_length);
|
||||
/* consistency checks */
|
||||
if (isdir
|
||||
&& ((le16_to_cpu(reparse_attr->reparse_data_length)
|
||||
+ 8) == attr_size)
|
||||
&& ((int)((sizeof(REPARSE_POINT)
|
||||
+ sizeof(struct MOUNT_POINT_REPARSE_DATA)
|
||||
+ offs + lth)) <= attr_size)) {
|
||||
/* reparse data consistency has been checked */
|
||||
target = ntfs_get_fulllink(vol,
|
||||
(ntfschar*)&mount_point_data->path_buffer[offs],
|
||||
lth/2, org_path, isdir);
|
||||
lth/2, mnt_point, isdir);
|
||||
if (target)
|
||||
bad = FALSE;
|
||||
}
|
||||
break;
|
||||
case IO_REPARSE_TAG_SYMLINK :
|
||||
symlink_data = (struct SYMLINK_REPARSE_DATA*)
|
||||
@ -836,19 +746,14 @@ char *ntfs_make_symlink(const char *org_path,
|
||||
else
|
||||
kind = REL_TARGET;
|
||||
p--;
|
||||
/* consistency checks */
|
||||
if (((le16_to_cpu(reparse_attr->reparse_data_length)
|
||||
+ 8) == attr_size)
|
||||
&& ((int)((sizeof(REPARSE_POINT)
|
||||
+ sizeof(struct SYMLINK_REPARSE_DATA)
|
||||
+ offs + lth)) <= attr_size)) {
|
||||
/* reparse data consistency has been checked */
|
||||
switch (kind) {
|
||||
case FULL_TARGET :
|
||||
if (!(symlink_data->flags
|
||||
& const_cpu_to_le32(1))) {
|
||||
target = ntfs_get_fulllink(vol,
|
||||
p, lth/2,
|
||||
org_path, isdir);
|
||||
mnt_point, isdir);
|
||||
if (target)
|
||||
bad = FALSE;
|
||||
}
|
||||
@ -858,7 +763,7 @@ char *ntfs_make_symlink(const char *org_path,
|
||||
& const_cpu_to_le32(1)) {
|
||||
target = ntfs_get_abslink(vol,
|
||||
p, lth/2,
|
||||
org_path, isdir);
|
||||
mnt_point, isdir);
|
||||
if (target)
|
||||
bad = FALSE;
|
||||
}
|
||||
@ -866,15 +771,13 @@ char *ntfs_make_symlink(const char *org_path,
|
||||
case REL_TARGET :
|
||||
if (symlink_data->flags
|
||||
& const_cpu_to_le32(1)) {
|
||||
target = ntfs_get_rellink(vol,
|
||||
p, lth/2,
|
||||
org_path, isdir);
|
||||
target = ntfs_get_rellink(ni,
|
||||
p, lth/2);
|
||||
if (target)
|
||||
bad = FALSE;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
free(reparse_attr);
|
||||
@ -984,8 +887,9 @@ static int remove_reparse_index(ntfs_attr *na, ntfs_index_context *xr,
|
||||
key.reparse_tag = *preparse_tag;
|
||||
/* danger on processors which require proper alignment ! */
|
||||
memcpy(&key.file_id, &file_id, 8);
|
||||
if (!ntfs_index_lookup(&key, sizeof(REPARSE_INDEX_KEY), xr))
|
||||
ret = ntfs_index_rm(xr);
|
||||
if (!ntfs_index_lookup(&key, sizeof(REPARSE_INDEX_KEY), xr)
|
||||
&& ntfs_index_rm(xr))
|
||||
ret = -1;
|
||||
} else {
|
||||
ret = -1;
|
||||
errno = ENODATA;
|
||||
@ -1005,10 +909,20 @@ static int remove_reparse_index(ntfs_attr *na, ntfs_index_context *xr,
|
||||
|
||||
static ntfs_index_context *open_reparse_index(ntfs_volume *vol)
|
||||
{
|
||||
u64 inum;
|
||||
ntfs_inode *ni;
|
||||
ntfs_inode *dir_ni;
|
||||
ntfs_index_context *xr;
|
||||
|
||||
ni = ntfs_pathname_to_inode(vol, NULL, "$Extend/$Reparse");
|
||||
/* do not use path_name_to inode - could reopen root */
|
||||
dir_ni = ntfs_inode_open(vol, FILE_Extend);
|
||||
ni = (ntfs_inode*)NULL;
|
||||
if (dir_ni) {
|
||||
inum = ntfs_inode_lookup_by_mbsname(dir_ni,"$Reparse");
|
||||
if (inum != (u64)-1)
|
||||
ni = ntfs_inode_open(vol, inum);
|
||||
ntfs_inode_close(dir_ni);
|
||||
}
|
||||
if (ni) {
|
||||
xr = ntfs_index_ctx_get(ni, reparse_index_name, 2);
|
||||
if (!xr) {
|
||||
@ -1133,8 +1047,7 @@ int ntfs_delete_reparse_index(ntfs_inode *ni)
|
||||
* and the buffer is updated if it is long enough
|
||||
*/
|
||||
|
||||
int ntfs_get_ntfs_reparse_data(const char *path __attribute__((unused)),
|
||||
char *value, size_t size, ntfs_inode *ni)
|
||||
int ntfs_get_ntfs_reparse_data(ntfs_inode *ni, char *value, size_t size)
|
||||
{
|
||||
REPARSE_POINT *reparse_attr;
|
||||
s64 attr_size;
|
||||
@ -1168,9 +1081,8 @@ int ntfs_get_ntfs_reparse_data(const char *path __attribute__((unused)),
|
||||
* Returns 0, or -1 if there is a problem
|
||||
*/
|
||||
|
||||
int ntfs_set_ntfs_reparse_data(const char *path __attribute__((unused)),
|
||||
const char *value, size_t size, int flags,
|
||||
ntfs_inode *ni)
|
||||
int ntfs_set_ntfs_reparse_data(ntfs_inode *ni,
|
||||
const char *value, size_t size, int flags)
|
||||
{
|
||||
int res;
|
||||
u8 dummy;
|
||||
@ -1178,7 +1090,7 @@ int ntfs_set_ntfs_reparse_data(const char *path __attribute__((unused)),
|
||||
ntfs_index_context *xr;
|
||||
|
||||
res = 0;
|
||||
if (ni && value && (size >= 4)) {
|
||||
if (ni && valid_reparse_data(ni, (const REPARSE_POINT*)value, size)) {
|
||||
xr = open_reparse_index(ni->vol);
|
||||
if (xr) {
|
||||
if (!ntfs_attr_exist(ni,AT_REPARSE_POINT,
|
||||
@ -1194,9 +1106,11 @@ int ntfs_set_ntfs_reparse_data(const char *path __attribute__((unused)),
|
||||
AT_REPARSE_POINT,
|
||||
AT_UNNAMED,0,&dummy,
|
||||
(s64)0);
|
||||
if (!res)
|
||||
if (!res) {
|
||||
ni->flags |=
|
||||
FILE_ATTR_REPARSE_POINT;
|
||||
NInoFileNameSetDirty(ni);
|
||||
}
|
||||
NInoSetDirty(ni);
|
||||
} else {
|
||||
errno = EOPNOTSUPP;
|
||||
@ -1237,8 +1151,7 @@ int ntfs_set_ntfs_reparse_data(const char *path __attribute__((unused)),
|
||||
* Returns 0, or -1 if there is a problem
|
||||
*/
|
||||
|
||||
int ntfs_remove_ntfs_reparse_data(const char *path __attribute__((unused)),
|
||||
ntfs_inode *ni)
|
||||
int ntfs_remove_ntfs_reparse_data(ntfs_inode *ni)
|
||||
{
|
||||
int res;
|
||||
int olderrno;
|
||||
@ -1267,6 +1180,7 @@ int ntfs_remove_ntfs_reparse_data(const char *path __attribute__((unused)),
|
||||
if (!res) {
|
||||
ni->flags &=
|
||||
~FILE_ATTR_REPARSE_POINT;
|
||||
NInoFileNameSetDirty(ni);
|
||||
} else {
|
||||
/*
|
||||
* If we could not remove the
|
||||
|
@ -24,15 +24,15 @@
|
||||
#ifndef REPARSE_H
|
||||
#define REPARSE_H
|
||||
|
||||
char *ntfs_make_symlink(const char *org_path,
|
||||
ntfs_inode *ni, int *pattr_size);
|
||||
char *ntfs_make_symlink(ntfs_inode *ni, const char *mnt_point,
|
||||
int *pattr_size);
|
||||
BOOL ntfs_possible_symlink(ntfs_inode *ni);
|
||||
|
||||
int ntfs_get_ntfs_reparse_data(const char *path,
|
||||
char *value, size_t size, ntfs_inode *ni);
|
||||
int ntfs_set_ntfs_reparse_data(const char *path, const char *value,
|
||||
size_t size, int flags, ntfs_inode *ni);
|
||||
int ntfs_remove_ntfs_reparse_data(const char *path, ntfs_inode *ni);
|
||||
int ntfs_get_ntfs_reparse_data(ntfs_inode *ni, char *value, size_t size);
|
||||
|
||||
int ntfs_set_ntfs_reparse_data(ntfs_inode *ni, const char *value,
|
||||
size_t size, int flags);
|
||||
int ntfs_remove_ntfs_reparse_data(ntfs_inode *ni);
|
||||
|
||||
int ntfs_delete_reparse_index(ntfs_inode *ni);
|
||||
|
||||
|
@ -117,18 +117,33 @@ static runlist_element *ntfs_rl_realloc(runlist_element *rl, int old_size,
|
||||
*
|
||||
* Returns the reallocated runlist
|
||||
* or NULL if reallocation was not possible (with errno set)
|
||||
* the runlist is left unchanged if the reallocation fails
|
||||
*/
|
||||
|
||||
runlist_element *ntfs_rl_extend(runlist_element *rl, int more_entries)
|
||||
runlist_element *ntfs_rl_extend(ntfs_attr *na, runlist_element *rl,
|
||||
int more_entries)
|
||||
{
|
||||
runlist_element *newrl;
|
||||
int last;
|
||||
int irl;
|
||||
|
||||
last = 0;
|
||||
while (rl[last].length)
|
||||
if (na->rl && rl) {
|
||||
irl = (int)(rl - na->rl);
|
||||
last = irl;
|
||||
while (na->rl[last].length)
|
||||
last++;
|
||||
rl = ntfs_rl_realloc(rl,last+1,last+more_entries+1);
|
||||
if (!rl)
|
||||
newrl = ntfs_rl_realloc(na->rl,last+1,last+more_entries+1);
|
||||
if (!newrl) {
|
||||
errno = ENOMEM;
|
||||
rl = (runlist_element*)NULL;
|
||||
} else
|
||||
na->rl = newrl;
|
||||
rl = &newrl[irl];
|
||||
} else {
|
||||
ntfs_log_error("Cannot extend unmapped runlist");
|
||||
errno = EIO;
|
||||
rl = (runlist_element*)NULL;
|
||||
}
|
||||
return (rl);
|
||||
}
|
||||
|
||||
@ -764,7 +779,7 @@ runlist_element *ntfs_runlists_merge(runlist_element *drl,
|
||||
* two into one, if that is possible (we check for overlap and discard the new
|
||||
* runlist if overlap present before returning NULL, with errno = ERANGE).
|
||||
*/
|
||||
runlist_element *ntfs_mapping_pairs_decompress_i(const ntfs_volume *vol,
|
||||
static runlist_element *ntfs_mapping_pairs_decompress_i(const ntfs_volume *vol,
|
||||
const ATTR_RECORD *attr, runlist_element *old_rl)
|
||||
{
|
||||
VCN vcn; /* Current vcn. */
|
||||
@ -1622,7 +1637,11 @@ int ntfs_rl_truncate(runlist **arl, const VCN start_vcn)
|
||||
|
||||
if (!arl || !*arl) {
|
||||
errno = EINVAL;
|
||||
ntfs_log_perror("rl_truncate error: arl: %p *arl: %p", arl, *arl);
|
||||
if (!arl)
|
||||
ntfs_log_perror("rl_truncate error: arl: %p", arl);
|
||||
else
|
||||
ntfs_log_perror("rl_truncate error:"
|
||||
" arl: %p *arl: %p", arl, *arl);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -49,7 +49,8 @@ struct _runlist_element {/* In memory vcn to lcn mapping structure element. */
|
||||
s64 length; /* Run length in clusters. */
|
||||
};
|
||||
|
||||
extern runlist_element *ntfs_rl_extend(runlist_element *rl, int more_entries);
|
||||
extern runlist_element *ntfs_rl_extend(ntfs_attr *na, runlist_element *rl,
|
||||
int more_entries);
|
||||
|
||||
extern LCN ntfs_rl_vcn_to_lcn(const runlist_element *rl, const VCN vcn);
|
||||
|
||||
|
@ -41,17 +41,18 @@
|
||||
#ifdef HAVE_FCNTL_H
|
||||
#include <fcntl.h>
|
||||
#endif
|
||||
#ifdef HAVE_SYS_STAT_H
|
||||
#include <sys/stat.h>
|
||||
#endif
|
||||
#ifdef HAVE_SETXATTR
|
||||
#include <sys/xattr.h>
|
||||
#endif
|
||||
#ifdef HAVE_SYS_STAT_H
|
||||
#include <sys/stat.h>
|
||||
#endif
|
||||
|
||||
#include <unistd.h>
|
||||
#include <pwd.h>
|
||||
#include <grp.h>
|
||||
|
||||
#include "param.h"
|
||||
#include "types.h"
|
||||
#include "layout.h"
|
||||
#include "attrib.h"
|
||||
@ -60,6 +61,7 @@
|
||||
#include "bitmap.h"
|
||||
#include "security.h"
|
||||
#include "acls.h"
|
||||
#include "cache.h"
|
||||
#include "misc.h"
|
||||
|
||||
/*
|
||||
@ -712,6 +714,7 @@ static le32 entersecurityattr(ntfs_volume *vol,
|
||||
INDEX_ENTRY *entry;
|
||||
INDEX_ENTRY *next;
|
||||
ntfs_index_context *xsii;
|
||||
int retries;
|
||||
ntfs_attr *na;
|
||||
int olderrno;
|
||||
|
||||
@ -756,10 +759,14 @@ static le32 entersecurityattr(ntfs_volume *vol,
|
||||
* All index blocks should be at least half full
|
||||
* so there always is a last entry but one,
|
||||
* except when creating the first entry in index root.
|
||||
* A simplified version of next(), limited to
|
||||
* current index node, could be used
|
||||
* This was however found not to be true : chkdsk
|
||||
* sometimes deletes all the (unused) keys in the last
|
||||
* index block without rebalancing the tree.
|
||||
* When this happens, a new search is restarted from
|
||||
* the smallest key.
|
||||
*/
|
||||
keyid = const_cpu_to_le32(0);
|
||||
retries = 0;
|
||||
while (entry) {
|
||||
next = ntfs_index_next(entry,xsii);
|
||||
if (next) {
|
||||
@ -775,6 +782,20 @@ static le32 entersecurityattr(ntfs_volume *vol,
|
||||
size = le32_to_cpu(psii->datasize);
|
||||
}
|
||||
entry = next;
|
||||
if (!entry && !keyid && !retries) {
|
||||
/* search failed, retry from smallest key */
|
||||
ntfs_index_ctx_reinit(xsii);
|
||||
found = !ntfs_index_lookup((char*)&keyid,
|
||||
sizeof(SII_INDEX_KEY), xsii);
|
||||
if (!found && (errno != ENOENT)) {
|
||||
ntfs_log_perror("Index $SII is broken");
|
||||
} else {
|
||||
/* restore errno */
|
||||
errno = olderrno;
|
||||
entry = xsii->entry;
|
||||
}
|
||||
retries++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!keyid) {
|
||||
@ -1080,7 +1101,6 @@ static int update_secur_descr(ntfs_volume *vol,
|
||||
|
||||
/* mark node as dirty */
|
||||
NInoSetDirty(ni);
|
||||
ntfs_inode_sync(ni); /* useful ? */
|
||||
return (res);
|
||||
}
|
||||
|
||||
@ -1105,7 +1125,7 @@ static int update_secur_descr(ntfs_volume *vol,
|
||||
* -1 if there is a problem
|
||||
*/
|
||||
|
||||
static int upgrade_secur_desc(ntfs_volume *vol, const char *path,
|
||||
static int upgrade_secur_desc(ntfs_volume *vol,
|
||||
const char *attr, ntfs_inode *ni)
|
||||
{
|
||||
int attrsz;
|
||||
@ -1116,11 +1136,11 @@ static int upgrade_secur_desc(ntfs_volume *vol, const char *path,
|
||||
/*
|
||||
* upgrade requires NTFS format v3.x
|
||||
* also refuse upgrading for special files
|
||||
* whose number is less than FILE_first_user
|
||||
*/
|
||||
|
||||
if ((vol->major_ver >= 3)
|
||||
&& (path[0] == '/')
|
||||
&& (path[1] != '$') && (path[1] != '\0')) {
|
||||
&& (ni->mft_no >= FILE_first_user)) {
|
||||
attrsz = ntfs_attr_size(attr);
|
||||
securid = setsecurityattr(vol,
|
||||
(const SECURITY_DESCRIPTOR_RELATIVE*)attr,
|
||||
@ -1151,7 +1171,6 @@ static int upgrade_secur_desc(ntfs_volume *vol, const char *path,
|
||||
res = -1;
|
||||
/* mark node as dirty */
|
||||
NInoSetDirty(ni);
|
||||
ntfs_inode_sync(ni); /* useful ? */
|
||||
} else
|
||||
res = 1;
|
||||
|
||||
@ -1788,8 +1807,7 @@ static char *retrievesecurityattr(ntfs_volume *vol, SII_INDEX_KEY id)
|
||||
* The returned descriptor is dynamically allocated and has to be freed
|
||||
*/
|
||||
|
||||
static char *getsecurityattr(ntfs_volume *vol,
|
||||
const char *path, ntfs_inode *ni)
|
||||
static char *getsecurityattr(ntfs_volume *vol, ntfs_inode *ni)
|
||||
{
|
||||
SII_INDEX_KEY securid;
|
||||
char *securattr;
|
||||
@ -1815,8 +1833,8 @@ static char *getsecurityattr(ntfs_volume *vol,
|
||||
securattr = ntfs_attr_readall(ni, AT_SECURITY_DESCRIPTOR,
|
||||
AT_UNNAMED, 0, &readallsz);
|
||||
if (securattr && !ntfs_valid_descr(securattr, readallsz)) {
|
||||
ntfs_log_error("Bad security descriptor for %s\n",
|
||||
path);
|
||||
ntfs_log_error("Bad security descriptor for inode %lld\n",
|
||||
(long long)ni->mft_no);
|
||||
free(securattr);
|
||||
securattr = (char*)NULL;
|
||||
}
|
||||
@ -1830,7 +1848,8 @@ static char *getsecurityattr(ntfs_volume *vol,
|
||||
* minimum rights, so that a real descriptor can
|
||||
* be created by chown or chmod
|
||||
*/
|
||||
ntfs_log_error("No security descriptor found for %s\n",path);
|
||||
ntfs_log_error("No security descriptor found for inode %lld\n",
|
||||
(long long)ni->mft_no);
|
||||
securattr = ntfs_build_descr(0, 0, adminsid, adminsid);
|
||||
}
|
||||
return (securattr);
|
||||
@ -1854,6 +1873,7 @@ static int access_check_posix(struct SECURITY_CONTEXT *scx,
|
||||
int groupperms;
|
||||
int mask;
|
||||
BOOL somegroup;
|
||||
BOOL needgroups;
|
||||
mode_t perms;
|
||||
int i;
|
||||
|
||||
@ -1888,9 +1908,17 @@ static int access_check_posix(struct SECURITY_CONTEXT *scx,
|
||||
} else
|
||||
perms &= 07700;
|
||||
} else {
|
||||
/* analyze designated users and get mask */
|
||||
/*
|
||||
* analyze designated users, get mask
|
||||
* and identify whether we need to check
|
||||
* the group memberships. The groups are
|
||||
* not needed when all groups have the
|
||||
* same permissions as other for the
|
||||
* requested modes.
|
||||
*/
|
||||
userperms = -1;
|
||||
groupperms = -1;
|
||||
needgroups = FALSE;
|
||||
mask = 7;
|
||||
for (i=pxdesc->acccnt-1; i>=0 ; i--) {
|
||||
pxace = &pxdesc->acl.ace[i];
|
||||
@ -1902,6 +1930,12 @@ static int access_check_posix(struct SECURITY_CONTEXT *scx,
|
||||
case POSIX_ACL_MASK :
|
||||
mask = pxace->perms & 7;
|
||||
break;
|
||||
case POSIX_ACL_GROUP_OBJ :
|
||||
case POSIX_ACL_GROUP :
|
||||
if (((pxace->perms & mask) ^ perms)
|
||||
& (request >> 6) & 7)
|
||||
needgroups = TRUE;
|
||||
break;
|
||||
default :
|
||||
break;
|
||||
}
|
||||
@ -1909,6 +1943,8 @@ static int access_check_posix(struct SECURITY_CONTEXT *scx,
|
||||
/* designated users */
|
||||
if (userperms >= 0)
|
||||
perms = (perms & 07000) + (userperms & mask);
|
||||
else if (!needgroups)
|
||||
perms &= 07007;
|
||||
else {
|
||||
/* owning group */
|
||||
if (!(~(perms >> 3) & request & mask)
|
||||
@ -1951,7 +1987,7 @@ static int access_check_posix(struct SECURITY_CONTEXT *scx,
|
||||
*/
|
||||
|
||||
static int ntfs_get_perm(struct SECURITY_CONTEXT *scx,
|
||||
const char *path, ntfs_inode * ni, mode_t request)
|
||||
ntfs_inode * ni, mode_t request)
|
||||
{
|
||||
const SECURITY_DESCRIPTOR_RELATIVE *phead;
|
||||
const struct CACHED_PERMISSIONS *cached;
|
||||
@ -1977,7 +2013,7 @@ static int ntfs_get_perm(struct SECURITY_CONTEXT *scx,
|
||||
perm = 0; /* default to no permission */
|
||||
isdir = (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY)
|
||||
!= const_cpu_to_le16(0);
|
||||
securattr = getsecurityattr(scx->vol, path, ni);
|
||||
securattr = getsecurityattr(scx->vol, ni);
|
||||
if (securattr) {
|
||||
phead = (const SECURITY_DESCRIPTOR_RELATIVE*)
|
||||
securattr;
|
||||
@ -2017,7 +2053,7 @@ static int ntfs_get_perm(struct SECURITY_CONTEXT *scx,
|
||||
&& (perm >= 0)
|
||||
&& (scx->vol->secure_flags
|
||||
& (1 << SECURITY_ADDSECURIDS))) {
|
||||
upgrade_secur_desc(scx->vol, path,
|
||||
upgrade_secur_desc(scx->vol,
|
||||
securattr, ni);
|
||||
/*
|
||||
* fetch owner and group for cacheing
|
||||
@ -2051,9 +2087,8 @@ static int ntfs_get_perm(struct SECURITY_CONTEXT *scx,
|
||||
* the caller is expected to issue a new call
|
||||
*/
|
||||
|
||||
int ntfs_get_posix_acl(struct SECURITY_CONTEXT *scx, const char *path,
|
||||
const char *name, char *value, size_t size,
|
||||
ntfs_inode *ni)
|
||||
int ntfs_get_posix_acl(struct SECURITY_CONTEXT *scx, ntfs_inode *ni,
|
||||
const char *name, char *value, size_t size)
|
||||
{
|
||||
const SECURITY_DESCRIPTOR_RELATIVE *phead;
|
||||
struct POSIX_SECURITY *pxdesc;
|
||||
@ -2076,7 +2111,7 @@ int ntfs_get_posix_acl(struct SECURITY_CONTEXT *scx, const char *path,
|
||||
if (cached)
|
||||
pxdesc = cached->pxdesc;
|
||||
else {
|
||||
securattr = getsecurityattr(scx->vol, path, ni);
|
||||
securattr = getsecurityattr(scx->vol, ni);
|
||||
isdir = (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY)
|
||||
!= const_cpu_to_le16(0);
|
||||
if (securattr) {
|
||||
@ -2107,7 +2142,7 @@ int ntfs_get_posix_acl(struct SECURITY_CONTEXT *scx, const char *path,
|
||||
&& (scx->vol->secure_flags
|
||||
& (1 << SECURITY_ADDSECURIDS))) {
|
||||
upgrade_secur_desc(scx->vol,
|
||||
path, securattr, ni);
|
||||
securattr, ni);
|
||||
}
|
||||
#if OWNERFROMACL
|
||||
uid = ntfs_find_user(scx->mapping[MAPUSERS],usid);
|
||||
@ -2185,13 +2220,10 @@ int ntfs_get_posix_acl(struct SECURITY_CONTEXT *scx, const char *path,
|
||||
* Do no use as mode of the file
|
||||
*
|
||||
* returns -1 if there is a problem
|
||||
*
|
||||
* This is only used for checking creation of DOS file names
|
||||
*/
|
||||
|
||||
static int ntfs_get_perm(struct SECURITY_CONTEXT *scx,
|
||||
const char *path, ntfs_inode *ni,
|
||||
mode_t request __attribute__((unused)))
|
||||
ntfs_inode *ni, mode_t request)
|
||||
{
|
||||
const SECURITY_DESCRIPTOR_RELATIVE *phead;
|
||||
const struct CACHED_PERMISSIONS *cached;
|
||||
@ -2203,7 +2235,7 @@ static int ntfs_get_perm(struct SECURITY_CONTEXT *scx,
|
||||
gid_t gid;
|
||||
int perm;
|
||||
|
||||
if (!scx->mapping[MAPUSERS] || !scx->uid)
|
||||
if (!scx->mapping[MAPUSERS] || (!scx->uid && !(request & S_IEXEC)))
|
||||
perm = 07777;
|
||||
else {
|
||||
/* check whether available in cache */
|
||||
@ -2216,7 +2248,7 @@ static int ntfs_get_perm(struct SECURITY_CONTEXT *scx,
|
||||
perm = 0; /* default to no permission */
|
||||
isdir = (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY)
|
||||
!= const_cpu_to_le16(0);
|
||||
securattr = getsecurityattr(scx->vol, path, ni);
|
||||
securattr = getsecurityattr(scx->vol, ni);
|
||||
if (securattr) {
|
||||
phead = (const SECURITY_DESCRIPTOR_RELATIVE*)
|
||||
securattr;
|
||||
@ -2248,7 +2280,7 @@ static int ntfs_get_perm(struct SECURITY_CONTEXT *scx,
|
||||
&& (perm >= 0)
|
||||
&& (scx->vol->secure_flags
|
||||
& (1 << SECURITY_ADDSECURIDS))) {
|
||||
upgrade_secur_desc(scx->vol, path,
|
||||
upgrade_secur_desc(scx->vol,
|
||||
securattr, ni);
|
||||
/*
|
||||
* fetch owner and group for cacheing
|
||||
@ -2267,11 +2299,25 @@ static int ntfs_get_perm(struct SECURITY_CONTEXT *scx,
|
||||
}
|
||||
}
|
||||
if (perm >= 0) {
|
||||
if (!scx->uid) {
|
||||
/* root access and execution */
|
||||
if (perm & 0111)
|
||||
perm = 07777;
|
||||
else
|
||||
perm = 0;
|
||||
} else
|
||||
if (uid == scx->uid)
|
||||
perm &= 07700;
|
||||
else
|
||||
/*
|
||||
* avoid checking group membership
|
||||
* when the requested perms for group
|
||||
* are the same as perms for other
|
||||
*/
|
||||
if ((gid == scx->gid)
|
||||
|| groupmember(scx, scx->uid, gid))
|
||||
|| ((((perm >> 3) ^ perm)
|
||||
& (request >> 6) & 7)
|
||||
&& groupmember(scx, scx->uid, gid)))
|
||||
perm &= 07070;
|
||||
else
|
||||
perm &= 07007;
|
||||
@ -2290,15 +2336,14 @@ static int ntfs_get_perm(struct SECURITY_CONTEXT *scx,
|
||||
* the caller is expected to issue a new call
|
||||
*/
|
||||
|
||||
int ntfs_get_ntfs_acl(struct SECURITY_CONTEXT *scx, const char *path,
|
||||
const char *name __attribute__((unused)),
|
||||
char *value, size_t size, ntfs_inode *ni)
|
||||
int ntfs_get_ntfs_acl(struct SECURITY_CONTEXT *scx, ntfs_inode *ni,
|
||||
char *value, size_t size)
|
||||
{
|
||||
char *securattr;
|
||||
size_t outsize;
|
||||
|
||||
outsize = 0; /* default to no data and no error */
|
||||
securattr = getsecurityattr(scx->vol, path, ni);
|
||||
securattr = getsecurityattr(scx->vol, ni);
|
||||
if (securattr) {
|
||||
outsize = ntfs_attr_size(securattr);
|
||||
if (outsize <= size) {
|
||||
@ -2315,8 +2360,7 @@ int ntfs_get_ntfs_acl(struct SECURITY_CONTEXT *scx, const char *path,
|
||||
*/
|
||||
|
||||
int ntfs_get_owner_mode(struct SECURITY_CONTEXT *scx,
|
||||
const char *path, ntfs_inode * ni,
|
||||
struct stat *stbuf)
|
||||
ntfs_inode * ni, struct stat *stbuf)
|
||||
{
|
||||
const SECURITY_DESCRIPTOR_RELATIVE *phead;
|
||||
char *securattr;
|
||||
@ -2343,7 +2387,7 @@ int ntfs_get_owner_mode(struct SECURITY_CONTEXT *scx,
|
||||
perm = -1; /* default to error */
|
||||
isdir = (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY)
|
||||
!= const_cpu_to_le16(0);
|
||||
securattr = getsecurityattr(scx->vol, path, ni);
|
||||
securattr = getsecurityattr(scx->vol, ni);
|
||||
if (securattr) {
|
||||
phead =
|
||||
(const SECURITY_DESCRIPTOR_RELATIVE*)
|
||||
@ -2379,7 +2423,7 @@ int ntfs_get_owner_mode(struct SECURITY_CONTEXT *scx,
|
||||
&& (scx->vol->secure_flags
|
||||
& (1 << SECURITY_ADDSECURIDS))) {
|
||||
upgrade_secur_desc(scx->vol,
|
||||
path, securattr, ni);
|
||||
securattr, ni);
|
||||
}
|
||||
#if OWNERFROMACL
|
||||
stbuf->st_uid = ntfs_find_user(scx->mapping[MAPUSERS],usid);
|
||||
@ -2420,8 +2464,7 @@ int ntfs_get_owner_mode(struct SECURITY_CONTEXT *scx,
|
||||
*/
|
||||
|
||||
static struct POSIX_SECURITY *inherit_posix(struct SECURITY_CONTEXT *scx,
|
||||
const char *dir_path, ntfs_inode *dir_ni,
|
||||
mode_t mode, BOOL isdir)
|
||||
ntfs_inode *dir_ni, mode_t mode, BOOL isdir)
|
||||
{
|
||||
const struct CACHED_PERMISSIONS *cached;
|
||||
const SECURITY_DESCRIPTOR_RELATIVE *phead;
|
||||
@ -2445,7 +2488,7 @@ static struct POSIX_SECURITY *inherit_posix(struct SECURITY_CONTEXT *scx,
|
||||
scx->umask,isdir);
|
||||
}
|
||||
} else {
|
||||
securattr = getsecurityattr(scx->vol, dir_path, dir_ni);
|
||||
securattr = getsecurityattr(scx->vol, dir_ni);
|
||||
if (securattr) {
|
||||
phead = (const SECURITY_DESCRIPTOR_RELATIVE*)
|
||||
securattr;
|
||||
@ -2475,7 +2518,7 @@ static struct POSIX_SECURITY *inherit_posix(struct SECURITY_CONTEXT *scx,
|
||||
if (!test_nino_flag(dir_ni, v3_Extensions)
|
||||
&& (scx->vol->secure_flags
|
||||
& (1 << SECURITY_ADDSECURIDS))) {
|
||||
upgrade_secur_desc(scx->vol, dir_path,
|
||||
upgrade_secur_desc(scx->vol,
|
||||
securattr, dir_ni);
|
||||
/*
|
||||
* fetch owner and group for cacheing
|
||||
@ -2503,8 +2546,8 @@ static struct POSIX_SECURITY *inherit_posix(struct SECURITY_CONTEXT *scx,
|
||||
*/
|
||||
|
||||
le32 ntfs_alloc_securid(struct SECURITY_CONTEXT *scx,
|
||||
uid_t uid, gid_t gid, const char *dir_path,
|
||||
ntfs_inode *dir_ni, mode_t mode, BOOL isdir)
|
||||
uid_t uid, gid_t gid, ntfs_inode *dir_ni,
|
||||
mode_t mode, BOOL isdir)
|
||||
{
|
||||
#if !FORCE_FORMAT_v1x
|
||||
const struct CACHED_SECURID *cached;
|
||||
@ -2523,7 +2566,7 @@ le32 ntfs_alloc_securid(struct SECURITY_CONTEXT *scx,
|
||||
|
||||
#if !FORCE_FORMAT_v1x
|
||||
|
||||
pxdesc = inherit_posix(scx, dir_path, dir_ni, mode, isdir);
|
||||
pxdesc = inherit_posix(scx, dir_ni, mode, isdir);
|
||||
if (pxdesc) {
|
||||
/* check whether target securid is known in cache */
|
||||
|
||||
@ -2586,7 +2629,7 @@ le32 ntfs_alloc_securid(struct SECURITY_CONTEXT *scx,
|
||||
|
||||
int ntfs_set_inherited_posix(struct SECURITY_CONTEXT *scx,
|
||||
ntfs_inode *ni, uid_t uid, gid_t gid,
|
||||
const char *dir_path, ntfs_inode *dir_ni, mode_t mode)
|
||||
ntfs_inode *dir_ni, mode_t mode)
|
||||
{
|
||||
struct POSIX_SECURITY *pxdesc;
|
||||
char *newattr;
|
||||
@ -2599,7 +2642,7 @@ int ntfs_set_inherited_posix(struct SECURITY_CONTEXT *scx,
|
||||
|
||||
res = -1;
|
||||
isdir = (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) != const_cpu_to_le16(0);
|
||||
pxdesc = inherit_posix(scx, dir_path, dir_ni, mode, isdir);
|
||||
pxdesc = inherit_posix(scx, dir_ni, mode, isdir);
|
||||
if (pxdesc) {
|
||||
usid = ntfs_find_usid(scx->mapping[MAPUSERS],uid,(SID*)&defusid);
|
||||
gsid = ntfs_find_gsid(scx->mapping[MAPGROUPS],gid,(SID*)&defgsid);
|
||||
@ -2613,7 +2656,7 @@ int ntfs_set_inherited_posix(struct SECURITY_CONTEXT *scx,
|
||||
if (newattr) {
|
||||
/* Adjust Windows read-only flag */
|
||||
res = update_secur_descr(scx->vol, newattr, ni);
|
||||
if (!res) {
|
||||
if (!res && !isdir) {
|
||||
if (mode & S_IWUSR)
|
||||
ni->flags &= ~FILE_ATTR_READONLY;
|
||||
else
|
||||
@ -2630,7 +2673,7 @@ int ntfs_set_inherited_posix(struct SECURITY_CONTEXT *scx,
|
||||
+ (pxdesc->acccnt + pxdesc->defcnt)*sizeof(struct POSIX_ACE);
|
||||
ntfs_invalidate_cache(scx->vol->legacy_cache,
|
||||
GENERIC(&legacy),
|
||||
(cache_compare)leg_compare);
|
||||
(cache_compare)leg_compare,0);
|
||||
}
|
||||
#endif
|
||||
free(newattr);
|
||||
@ -2802,10 +2845,13 @@ int ntfs_set_owner_mode(struct SECURITY_CONTEXT *scx, ntfs_inode *ni,
|
||||
res = update_secur_descr(scx->vol, newattr, ni);
|
||||
if (!res) {
|
||||
/* adjust Windows read-only flag */
|
||||
if (!isdir) {
|
||||
if (mode & S_IWUSR)
|
||||
ni->flags &= ~FILE_ATTR_READONLY;
|
||||
else
|
||||
ni->flags |= FILE_ATTR_READONLY;
|
||||
NInoFileNameSetDirty(ni);
|
||||
}
|
||||
/* update cache, for subsequent use */
|
||||
if (test_nino_flag(ni, v3_Extensions)) {
|
||||
wanted.securid = ni->security_id;
|
||||
@ -2828,7 +2874,7 @@ int ntfs_set_owner_mode(struct SECURITY_CONTEXT *scx, ntfs_inode *ni,
|
||||
#endif
|
||||
ntfs_invalidate_cache(scx->vol->legacy_cache,
|
||||
GENERIC(&legacy),
|
||||
(cache_compare)leg_compare);
|
||||
(cache_compare)leg_compare,0);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@ -2851,8 +2897,7 @@ int ntfs_set_owner_mode(struct SECURITY_CONTEXT *scx, ntfs_inode *ni,
|
||||
* if not, errno tells why
|
||||
*/
|
||||
|
||||
BOOL ntfs_allowed_as_owner(struct SECURITY_CONTEXT *scx,
|
||||
const char *path, ntfs_inode *ni)
|
||||
BOOL ntfs_allowed_as_owner(struct SECURITY_CONTEXT *scx, ntfs_inode *ni)
|
||||
{
|
||||
const struct CACHED_PERMISSIONS *cached;
|
||||
char *oldattr;
|
||||
@ -2862,25 +2907,36 @@ BOOL ntfs_allowed_as_owner(struct SECURITY_CONTEXT *scx,
|
||||
BOOL gotowner;
|
||||
int allowed;
|
||||
|
||||
gotowner = FALSE; /* default */
|
||||
processuid = scx->uid;
|
||||
/* TODO : use CAP_FOWNER process capability */
|
||||
/*
|
||||
* Always allow for root
|
||||
* Also always allow if no mapping has been defined
|
||||
*/
|
||||
if (!scx->mapping[MAPUSERS] || !processuid)
|
||||
allowed = TRUE;
|
||||
else {
|
||||
gotowner = FALSE; /* default */
|
||||
/* get the owner, either from cache or from old attribute */
|
||||
cached = fetch_cache(scx, ni);
|
||||
if (cached) {
|
||||
uid = cached->uid;
|
||||
gotowner = TRUE;
|
||||
} else {
|
||||
oldattr = getsecurityattr(scx->vol,path, ni);
|
||||
oldattr = getsecurityattr(scx->vol, ni);
|
||||
if (oldattr) {
|
||||
#if OWNERFROMACL
|
||||
usid = ntfs_acl_owner(oldattr);
|
||||
#else
|
||||
const SECURITY_DESCRIPTOR_RELATIVE *phead;
|
||||
|
||||
phead = (const SECURITY_DESCRIPTOR_RELATIVE*)oldattr;
|
||||
usid = (const SID*)&oldattr[le32_to_cpu(phead->owner)];
|
||||
phead = (const SECURITY_DESCRIPTOR_RELATIVE*)
|
||||
oldattr;
|
||||
usid = (const SID*)&oldattr
|
||||
[le32_to_cpu(phead->owner)];
|
||||
#endif
|
||||
uid = ntfs_find_user(scx->mapping[MAPUSERS],usid);
|
||||
uid = ntfs_find_user(scx->mapping[MAPUSERS],
|
||||
usid);
|
||||
gotowner = TRUE;
|
||||
free(oldattr);
|
||||
}
|
||||
@ -2893,6 +2949,7 @@ BOOL ntfs_allowed_as_owner(struct SECURITY_CONTEXT *scx,
|
||||
else
|
||||
errno = EPERM;
|
||||
}
|
||||
}
|
||||
return (allowed);
|
||||
}
|
||||
|
||||
@ -2908,9 +2965,9 @@ BOOL ntfs_allowed_as_owner(struct SECURITY_CONTEXT *scx,
|
||||
* Returns 0, or -1 if there is a problem which errno describes
|
||||
*/
|
||||
|
||||
int ntfs_set_posix_acl(struct SECURITY_CONTEXT *scx, const char *path,
|
||||
int ntfs_set_posix_acl(struct SECURITY_CONTEXT *scx, ntfs_inode *ni,
|
||||
const char *name, const char *value, size_t size,
|
||||
int flags, ntfs_inode *ni)
|
||||
int flags)
|
||||
{
|
||||
const SECURITY_DESCRIPTOR_RELATIVE *phead;
|
||||
const struct CACHED_PERMISSIONS *cached;
|
||||
@ -2950,7 +3007,7 @@ int ntfs_set_posix_acl(struct SECURITY_CONTEXT *scx, const char *path,
|
||||
(const struct POSIX_ACL*)value,count,deflt);
|
||||
}
|
||||
} else {
|
||||
oldattr = getsecurityattr(scx->vol,path, ni);
|
||||
oldattr = getsecurityattr(scx->vol, ni);
|
||||
if (oldattr) {
|
||||
phead = (const SECURITY_DESCRIPTOR_RELATIVE*)oldattr;
|
||||
#if OWNERFROMACL
|
||||
@ -3011,11 +3068,11 @@ int ntfs_set_posix_acl(struct SECURITY_CONTEXT *scx, const char *path,
|
||||
* Returns 0, or -1 if there is a problem which errno describes
|
||||
*/
|
||||
|
||||
int ntfs_remove_posix_acl(struct SECURITY_CONTEXT *scx, const char *path,
|
||||
const char *name, ntfs_inode *ni)
|
||||
int ntfs_remove_posix_acl(struct SECURITY_CONTEXT *scx, ntfs_inode *ni,
|
||||
const char *name)
|
||||
{
|
||||
return (ntfs_set_posix_acl(scx, path, name,
|
||||
(const char*)NULL, 0, 0, ni));
|
||||
return (ntfs_set_posix_acl(scx, ni, name,
|
||||
(const char*)NULL, 0, 0));
|
||||
}
|
||||
|
||||
#endif
|
||||
@ -3026,11 +3083,8 @@ int ntfs_remove_posix_acl(struct SECURITY_CONTEXT *scx, const char *path,
|
||||
* Returns 0, or -1 if there is a problem
|
||||
*/
|
||||
|
||||
int ntfs_set_ntfs_acl(struct SECURITY_CONTEXT *scx,
|
||||
const char *path __attribute__((unused)),
|
||||
const char *name __attribute__((unused)),
|
||||
const char *value, size_t size, int flags,
|
||||
ntfs_inode *ni)
|
||||
int ntfs_set_ntfs_acl(struct SECURITY_CONTEXT *scx, ntfs_inode *ni,
|
||||
const char *value, size_t size, int flags)
|
||||
{
|
||||
char *attr;
|
||||
int res;
|
||||
@ -3068,7 +3122,7 @@ int ntfs_set_ntfs_acl(struct SECURITY_CONTEXT *scx,
|
||||
legacy.varsize = 0;
|
||||
ntfs_invalidate_cache(scx->vol->legacy_cache,
|
||||
GENERIC(&legacy),
|
||||
(cache_compare)leg_compare);
|
||||
(cache_compare)leg_compare,0);
|
||||
}
|
||||
#endif
|
||||
free(attr);
|
||||
@ -3091,8 +3145,7 @@ int ntfs_set_ntfs_acl(struct SECURITY_CONTEXT *scx,
|
||||
* -1 on failure, with errno = EIO
|
||||
*/
|
||||
|
||||
int ntfs_set_mode(struct SECURITY_CONTEXT *scx,
|
||||
const char *path, ntfs_inode *ni, mode_t mode)
|
||||
int ntfs_set_mode(struct SECURITY_CONTEXT *scx, ntfs_inode *ni, mode_t mode)
|
||||
{
|
||||
const SECURITY_DESCRIPTOR_RELATIVE *phead;
|
||||
const struct CACHED_PERMISSIONS *cached;
|
||||
@ -3133,7 +3186,7 @@ int ntfs_set_mode(struct SECURITY_CONTEXT *scx,
|
||||
newpxdesc = (struct POSIX_SECURITY*)NULL;
|
||||
#endif
|
||||
} else {
|
||||
oldattr = getsecurityattr(scx->vol,path, ni);
|
||||
oldattr = getsecurityattr(scx->vol, ni);
|
||||
if (oldattr) {
|
||||
phead = (const SECURITY_DESCRIPTOR_RELATIVE*)oldattr;
|
||||
#if OWNERFROMACL
|
||||
@ -3280,7 +3333,7 @@ int ntfs_sd_add_everyone(ntfs_inode *ni)
|
||||
*/
|
||||
|
||||
int ntfs_allowed_access(struct SECURITY_CONTEXT *scx,
|
||||
const char *path, ntfs_inode *ni,
|
||||
ntfs_inode *ni,
|
||||
int accesstype) /* access type required (S_Ixxx values) */
|
||||
{
|
||||
int perm;
|
||||
@ -3288,10 +3341,6 @@ int ntfs_allowed_access(struct SECURITY_CONTEXT *scx,
|
||||
int allow;
|
||||
struct stat stbuf;
|
||||
|
||||
#if POSIXACLS
|
||||
/* shortcut, use only if Posix ACLs in use */
|
||||
if (scx->vol->secure_flags & (1 << SECURITY_DEFAULT)) return (1);
|
||||
#endif
|
||||
/*
|
||||
* Always allow for root unless execution is requested.
|
||||
* (was checked by fuse until kernel 2.6.29)
|
||||
@ -3303,7 +3352,7 @@ int ntfs_allowed_access(struct SECURITY_CONTEXT *scx,
|
||||
|| (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY))))
|
||||
allow = 1;
|
||||
else {
|
||||
perm = ntfs_get_perm(scx, path, ni, accesstype);
|
||||
perm = ntfs_get_perm(scx, ni, accesstype);
|
||||
if (perm >= 0) {
|
||||
res = EACCES;
|
||||
switch (accesstype) {
|
||||
@ -3330,7 +3379,7 @@ int ntfs_allowed_access(struct SECURITY_CONTEXT *scx,
|
||||
break;
|
||||
case S_IWRITE + S_IEXEC + S_ISVTX:
|
||||
if (perm & S_ISVTX) {
|
||||
if ((ntfs_get_owner_mode(scx,path,ni,&stbuf) >= 0)
|
||||
if ((ntfs_get_owner_mode(scx,ni,&stbuf) >= 0)
|
||||
&& (stbuf.st_uid == scx->uid))
|
||||
allow = 1;
|
||||
else
|
||||
@ -3339,6 +3388,11 @@ int ntfs_allowed_access(struct SECURITY_CONTEXT *scx,
|
||||
allow = ((perm & (S_IWUSR | S_IWGRP | S_IWOTH)) != 0)
|
||||
&& ((perm & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0);
|
||||
break;
|
||||
case S_IREAD + S_IWRITE + S_IEXEC:
|
||||
allow = ((perm & (S_IRUSR | S_IRGRP | S_IROTH)) != 0)
|
||||
&& ((perm & (S_IWUSR | S_IWGRP | S_IWOTH)) != 0)
|
||||
&& ((perm & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0);
|
||||
break;
|
||||
default :
|
||||
res = EINVAL;
|
||||
allow = 0;
|
||||
@ -3352,6 +3406,8 @@ int ntfs_allowed_access(struct SECURITY_CONTEXT *scx,
|
||||
return (allow);
|
||||
}
|
||||
|
||||
#if 0 /* not needed any more */
|
||||
|
||||
/*
|
||||
* Check whether user can access the parent directory
|
||||
* of a file in a specific way
|
||||
@ -3364,7 +3420,7 @@ int ntfs_allowed_access(struct SECURITY_CONTEXT *scx,
|
||||
* This is used for Posix ACL and checking creation of DOS file names
|
||||
*/
|
||||
|
||||
BOOL ntfs_allowed_dir_access(struct SECURITY_CONTEXT *scx,
|
||||
BOOL old_ntfs_allowed_dir_access(struct SECURITY_CONTEXT *scx,
|
||||
const char *path, int accesstype)
|
||||
{
|
||||
int allow;
|
||||
@ -3374,10 +3430,6 @@ BOOL ntfs_allowed_dir_access(struct SECURITY_CONTEXT *scx,
|
||||
ntfs_inode *dir_ni;
|
||||
struct stat stbuf;
|
||||
|
||||
#if POSIXACLS
|
||||
/* shortcut, use only if Posix ACLs in use */
|
||||
if (scx->vol->secure_flags & (1 << SECURITY_DEFAULT)) return (TRUE);
|
||||
#endif
|
||||
allow = 0;
|
||||
dirpath = strdup(path);
|
||||
if (dirpath) {
|
||||
@ -3387,7 +3439,7 @@ BOOL ntfs_allowed_dir_access(struct SECURITY_CONTEXT *scx,
|
||||
*name = 0;
|
||||
dir_ni = ntfs_pathname_to_inode(scx->vol, NULL, dirpath);
|
||||
if (dir_ni) {
|
||||
allow = ntfs_allowed_access(scx,dirpath,
|
||||
allow = ntfs_allowed_access(scx,
|
||||
dir_ni, accesstype);
|
||||
ntfs_inode_close(dir_ni);
|
||||
/*
|
||||
@ -3400,7 +3452,7 @@ BOOL ntfs_allowed_dir_access(struct SECURITY_CONTEXT *scx,
|
||||
path);
|
||||
allow = FALSE;
|
||||
if (ni) {
|
||||
allow = (ntfs_get_owner_mode(scx,path,ni,&stbuf) >= 0)
|
||||
allow = (ntfs_get_owner_mode(scx,ni,&stbuf) >= 0)
|
||||
&& (stbuf.st_uid == scx->uid);
|
||||
ntfs_inode_close(ni);
|
||||
}
|
||||
@ -3411,14 +3463,16 @@ BOOL ntfs_allowed_dir_access(struct SECURITY_CONTEXT *scx,
|
||||
return (allow); /* errno is set if not allowed */
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Define a new owner/group to a file
|
||||
*
|
||||
* returns zero if successful
|
||||
*/
|
||||
|
||||
int ntfs_set_owner(struct SECURITY_CONTEXT *scx,
|
||||
const char *path, ntfs_inode *ni, uid_t uid, gid_t gid)
|
||||
int ntfs_set_owner(struct SECURITY_CONTEXT *scx, ntfs_inode *ni,
|
||||
uid_t uid, gid_t gid)
|
||||
{
|
||||
const SECURITY_DESCRIPTOR_RELATIVE *phead;
|
||||
const struct CACHED_PERMISSIONS *cached;
|
||||
@ -3453,7 +3507,7 @@ int ntfs_set_owner(struct SECURITY_CONTEXT *scx,
|
||||
fileuid = 0;
|
||||
filegid = 0;
|
||||
mode = 0;
|
||||
oldattr = getsecurityattr(scx->vol, path, ni);
|
||||
oldattr = getsecurityattr(scx->vol, ni);
|
||||
if (oldattr) {
|
||||
isdir = (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY)
|
||||
!= const_cpu_to_le16(0);
|
||||
@ -3533,6 +3587,121 @@ int ntfs_set_owner(struct SECURITY_CONTEXT *scx,
|
||||
return (res ? -1 : 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Define new owner/group and mode to a file
|
||||
*
|
||||
* returns zero if successful
|
||||
*/
|
||||
|
||||
int ntfs_set_ownmod(struct SECURITY_CONTEXT *scx, ntfs_inode *ni,
|
||||
uid_t uid, gid_t gid, const mode_t mode)
|
||||
{
|
||||
const SECURITY_DESCRIPTOR_RELATIVE *phead;
|
||||
const struct CACHED_PERMISSIONS *cached;
|
||||
char *oldattr;
|
||||
const SID *usid;
|
||||
const SID *gsid;
|
||||
uid_t fileuid;
|
||||
uid_t filegid;
|
||||
BOOL isdir;
|
||||
int res;
|
||||
#if POSIXACLS
|
||||
const struct POSIX_SECURITY *oldpxdesc;
|
||||
struct POSIX_SECURITY *newpxdesc = (struct POSIX_SECURITY*)NULL;
|
||||
int pxsize;
|
||||
#endif
|
||||
|
||||
res = 0;
|
||||
/* get the current owner and mode from cache or security attributes */
|
||||
oldattr = (char*)NULL;
|
||||
cached = fetch_cache(scx,ni);
|
||||
if (cached) {
|
||||
fileuid = cached->uid;
|
||||
filegid = cached->gid;
|
||||
#if POSIXACLS
|
||||
oldpxdesc = cached->pxdesc;
|
||||
if (oldpxdesc) {
|
||||
/* must copy before merging */
|
||||
pxsize = sizeof(struct POSIX_SECURITY)
|
||||
+ (oldpxdesc->acccnt + oldpxdesc->defcnt)*sizeof(struct POSIX_ACE);
|
||||
newpxdesc = (struct POSIX_SECURITY*)malloc(pxsize);
|
||||
if (newpxdesc) {
|
||||
memcpy(newpxdesc, oldpxdesc, pxsize);
|
||||
if (ntfs_merge_mode_posix(newpxdesc, mode))
|
||||
res = -1;
|
||||
} else
|
||||
res = -1;
|
||||
}
|
||||
#endif
|
||||
} else {
|
||||
fileuid = 0;
|
||||
filegid = 0;
|
||||
oldattr = getsecurityattr(scx->vol, ni);
|
||||
if (oldattr) {
|
||||
isdir = (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY)
|
||||
!= const_cpu_to_le16(0);
|
||||
phead = (const SECURITY_DESCRIPTOR_RELATIVE*)
|
||||
oldattr;
|
||||
gsid = (const SID*)
|
||||
&oldattr[le32_to_cpu(phead->group)];
|
||||
#if OWNERFROMACL
|
||||
usid = ntfs_acl_owner(oldattr);
|
||||
#else
|
||||
usid = (const SID*)
|
||||
&oldattr[le32_to_cpu(phead->owner)];
|
||||
#endif
|
||||
#if POSIXACLS
|
||||
newpxdesc = ntfs_build_permissions_posix(scx->mapping, oldattr,
|
||||
usid, gsid, isdir);
|
||||
if (!newpxdesc || ntfs_merge_mode_posix(newpxdesc, mode))
|
||||
res = -1;
|
||||
else {
|
||||
fileuid = ntfs_find_user(scx->mapping[MAPUSERS],usid);
|
||||
filegid = ntfs_find_group(scx->mapping[MAPGROUPS],gsid);
|
||||
}
|
||||
#endif
|
||||
free(oldattr);
|
||||
} else
|
||||
res = -1;
|
||||
}
|
||||
if (!res) {
|
||||
/* check requested by root */
|
||||
/* or chgrp requested by owner to an owned group */
|
||||
if (!scx->uid
|
||||
|| ((((int)uid < 0) || (uid == fileuid))
|
||||
&& ((gid == scx->gid) || groupmember(scx, scx->uid, gid))
|
||||
&& (fileuid == scx->uid))) {
|
||||
/* replace by the new usid and gsid */
|
||||
/* or reuse old gid and sid for cacheing */
|
||||
if ((int)uid < 0)
|
||||
uid = fileuid;
|
||||
if ((int)gid < 0)
|
||||
gid = filegid;
|
||||
#if POSIXACLS
|
||||
res = ntfs_set_owner_mode(scx, ni, uid, gid,
|
||||
mode, newpxdesc);
|
||||
#else
|
||||
res = ntfs_set_owner_mode(scx, ni, uid, gid, mode);
|
||||
#endif
|
||||
} else {
|
||||
res = -1; /* neither owner nor root */
|
||||
errno = EPERM;
|
||||
}
|
||||
} else {
|
||||
/*
|
||||
* Should not happen : a default descriptor is generated
|
||||
* by getsecurityattr() when there are none
|
||||
*/
|
||||
ntfs_log_error("File has no security descriptor\n");
|
||||
res = -1;
|
||||
errno = EIO;
|
||||
}
|
||||
#if POSIXACLS
|
||||
free(newpxdesc);
|
||||
#endif
|
||||
return (res ? -1 : 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Build a security id for a descriptor inherited from
|
||||
* parent directory the Windows way
|
||||
@ -3670,7 +3839,7 @@ static le32 build_inherited_id(struct SECURITY_CONTEXT *scx,
|
||||
*/
|
||||
|
||||
le32 ntfs_inherited_id(struct SECURITY_CONTEXT *scx,
|
||||
const char *dir_path, ntfs_inode *dir_ni, BOOL fordir)
|
||||
ntfs_inode *dir_ni, BOOL fordir)
|
||||
{
|
||||
struct CACHED_PERMISSIONS *cached;
|
||||
char *parentattr;
|
||||
@ -3693,7 +3862,7 @@ le32 ntfs_inherited_id(struct SECURITY_CONTEXT *scx,
|
||||
* Note : if parent directory has no id, it is not cacheable
|
||||
*/
|
||||
if (!securid) {
|
||||
parentattr = getsecurityattr(scx->vol, dir_path, dir_ni);
|
||||
parentattr = getsecurityattr(scx->vol, dir_ni);
|
||||
if (parentattr) {
|
||||
securid = build_inherited_id(scx,
|
||||
parentattr, fordir);
|
||||
@ -3799,14 +3968,13 @@ static int link_group_members(struct SECURITY_CONTEXT *scx)
|
||||
return (res);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Apply default single user mapping
|
||||
* returns zero if successful
|
||||
*/
|
||||
|
||||
static int ntfs_do_default_mapping(struct SECURITY_CONTEXT *scx,
|
||||
const SID *usid)
|
||||
uid_t uid, gid_t gid, const SID *usid)
|
||||
{
|
||||
struct MAPPING *usermapping;
|
||||
struct MAPPING *groupmapping;
|
||||
@ -3824,10 +3992,10 @@ static int ntfs_do_default_mapping(struct SECURITY_CONTEXT *scx,
|
||||
groupmapping = (struct MAPPING*)ntfs_malloc(sizeof(struct MAPPING));
|
||||
if (groupmapping) {
|
||||
usermapping->sid = sid;
|
||||
usermapping->xid = scx->uid;
|
||||
usermapping->xid = uid;
|
||||
usermapping->next = (struct MAPPING*)NULL;
|
||||
groupmapping->sid = sid;
|
||||
groupmapping->xid = scx->uid;
|
||||
groupmapping->xid = gid;
|
||||
groupmapping->next = (struct MAPPING*)NULL;
|
||||
scx->mapping[MAPUSERS] = usermapping;
|
||||
scx->mapping[MAPGROUPS] = groupmapping;
|
||||
@ -3836,7 +4004,6 @@ static int ntfs_do_default_mapping(struct SECURITY_CONTEXT *scx,
|
||||
}
|
||||
}
|
||||
return (res);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
@ -3878,6 +4045,8 @@ static BOOL check_mapping(const struct MAPPING *usermapping,
|
||||
|
||||
#endif
|
||||
|
||||
#if 0 /* not used any more */
|
||||
|
||||
/*
|
||||
* Try and apply default single user mapping
|
||||
* returns zero if successful
|
||||
@ -3894,12 +4063,13 @@ static int ntfs_default_mapping(struct SECURITY_CONTEXT *scx)
|
||||
res = -1;
|
||||
ni = ntfs_pathname_to_inode(scx->vol, NULL, "/.");
|
||||
if (ni) {
|
||||
securattr = getsecurityattr(scx->vol,"/.",ni);
|
||||
securattr = getsecurityattr(scx->vol, ni);
|
||||
if (securattr) {
|
||||
phead = (const SECURITY_DESCRIPTOR_RELATIVE*)securattr;
|
||||
usid = (SID*)&securattr[le32_to_cpu(phead->owner)];
|
||||
if (ntfs_is_user_sid(usid))
|
||||
res = ntfs_do_default_mapping(scx,usid);
|
||||
res = ntfs_do_default_mapping(scx,
|
||||
scx->uid, scx->gid, usid);
|
||||
free(securattr);
|
||||
}
|
||||
ntfs_inode_close(ni);
|
||||
@ -3907,6 +4077,8 @@ static int ntfs_default_mapping(struct SECURITY_CONTEXT *scx)
|
||||
return (res);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Basic read from a user mapping file on another volume
|
||||
*/
|
||||
@ -3939,7 +4111,8 @@ static int localread(void *fileid, char *buf, size_t size, off_t offs)
|
||||
* (failure should not be interpreted as an error)
|
||||
*/
|
||||
|
||||
int ntfs_build_mapping(struct SECURITY_CONTEXT *scx, const char *usermap_path)
|
||||
int ntfs_build_mapping(struct SECURITY_CONTEXT *scx, const char *usermap_path,
|
||||
BOOL allowdef)
|
||||
{
|
||||
struct MAPLIST *item;
|
||||
struct MAPLIST *firstitem;
|
||||
@ -3947,6 +4120,22 @@ int ntfs_build_mapping(struct SECURITY_CONTEXT *scx, const char *usermap_path)
|
||||
struct MAPPING *groupmapping;
|
||||
ntfs_inode *ni;
|
||||
int fd;
|
||||
static struct {
|
||||
u8 revision;
|
||||
u8 levels;
|
||||
be16 highbase;
|
||||
be32 lowbase;
|
||||
le32 level1;
|
||||
le32 level2;
|
||||
le32 level3;
|
||||
le32 level4;
|
||||
le32 level5;
|
||||
} defmap = {
|
||||
1, 5, const_cpu_to_be16(0), const_cpu_to_be32(5),
|
||||
const_cpu_to_le32(21),
|
||||
const_cpu_to_le32(DEFSECAUTH1), const_cpu_to_le32(DEFSECAUTH2),
|
||||
const_cpu_to_le32(DEFSECAUTH3), const_cpu_to_le32(DEFSECBASE)
|
||||
} ;
|
||||
|
||||
/* be sure not to map anything until done */
|
||||
scx->mapping[MAPUSERS] = (struct MAPPING*)NULL;
|
||||
@ -3986,9 +4175,10 @@ int ntfs_build_mapping(struct SECURITY_CONTEXT *scx, const char *usermap_path)
|
||||
firstitem = item;
|
||||
}
|
||||
} else {
|
||||
/* no mapping file, try default mapping */
|
||||
if (scx->uid && scx->gid) {
|
||||
if (!ntfs_default_mapping(scx))
|
||||
/* no mapping file, try a default mapping */
|
||||
if (allowdef) {
|
||||
if (!ntfs_do_default_mapping(scx,
|
||||
0, 0, (const SID*)&defmap))
|
||||
ntfs_log_info("Using default user mapping\n");
|
||||
}
|
||||
}
|
||||
@ -4002,8 +4192,7 @@ int ntfs_build_mapping(struct SECURITY_CONTEXT *scx, const char *usermap_path)
|
||||
* The attribute is returned according to cpu endianness
|
||||
*/
|
||||
|
||||
int ntfs_get_ntfs_attrib(const char *path __attribute__((unused)),
|
||||
char *value, size_t size, ntfs_inode *ni)
|
||||
int ntfs_get_ntfs_attrib(ntfs_inode *ni, char *value, size_t size)
|
||||
{
|
||||
u32 attrib;
|
||||
size_t outsize;
|
||||
@ -4035,9 +4224,8 @@ int ntfs_get_ntfs_attrib(const char *path __attribute__((unused)),
|
||||
* Returns 0, or -1 if there is a problem
|
||||
*/
|
||||
|
||||
int ntfs_set_ntfs_attrib(const char *path __attribute__((unused)),
|
||||
const char *value, size_t size, int flags,
|
||||
ntfs_inode *ni)
|
||||
int ntfs_set_ntfs_attrib(ntfs_inode *ni,
|
||||
const char *value, size_t size, int flags)
|
||||
{
|
||||
u32 attrib;
|
||||
le32 settable;
|
||||
@ -4073,6 +4261,7 @@ int ntfs_set_ntfs_attrib(const char *path __attribute__((unused)),
|
||||
if (!res) {
|
||||
ni->flags = (ni->flags & ~settable)
|
||||
| (cpu_to_le32(attrib) & settable);
|
||||
NInoFileNameSetDirty(ni);
|
||||
NInoSetDirty(ni);
|
||||
}
|
||||
} else
|
||||
@ -4490,7 +4679,7 @@ int ntfs_get_file_security(struct SECURITY_API *scapi,
|
||||
if (scapi && (scapi->magic == MAGIC_API)) {
|
||||
ni = ntfs_pathname_to_inode(scapi->security.vol, NULL, path);
|
||||
if (ni) {
|
||||
attr = getsecurityattr(scapi->security.vol, path, ni);
|
||||
attr = getsecurityattr(scapi->security.vol, ni);
|
||||
if (attr) {
|
||||
if (feedsecurityattr(attr,selection,
|
||||
buf,buflen,psize)) {
|
||||
@ -4559,7 +4748,7 @@ int ntfs_set_file_security(struct SECURITY_API *scapi,
|
||||
NULL, path);
|
||||
if (ni) {
|
||||
oldattr = getsecurityattr(scapi->security.vol,
|
||||
path, ni);
|
||||
ni);
|
||||
if (oldattr) {
|
||||
if (mergesecurityattr(
|
||||
scapi->security.vol,
|
||||
@ -4950,7 +5139,7 @@ struct SECURITY_API *ntfs_initialize_file_security(const char *device,
|
||||
scx->pseccache = &scapi->seccache;
|
||||
scx->vol->secure_flags = 0;
|
||||
/* accept no mapping and no $Secure */
|
||||
ntfs_build_mapping(scx,(const char*)NULL);
|
||||
ntfs_build_mapping(scx,(const char*)NULL,TRUE);
|
||||
ntfs_open_secure(vol);
|
||||
} else {
|
||||
if (scapi)
|
||||
|
@ -34,6 +34,9 @@
|
||||
#define POSIXACLS 0
|
||||
#endif
|
||||
|
||||
typedef u16 be16;
|
||||
typedef u32 be32;
|
||||
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
#define const_cpu_to_be16(x) ((((x) & 255L) << 8) + (((x) >> 8) & 255L))
|
||||
#define const_cpu_to_be32(x) ((((x) & 255L) << 24) + (((x) & 0xff00L) << 8) \
|
||||
@ -79,6 +82,7 @@ struct CACHED_PERMISSIONS {
|
||||
|
||||
struct CACHED_PERMISSIONS_LEGACY {
|
||||
struct CACHED_PERMISSIONS_LEGACY *next;
|
||||
struct CACHED_PERMISSIONS_LEGACY *previous;
|
||||
void *variable;
|
||||
size_t varsize;
|
||||
/* above fields must match "struct CACHED_GENERIC" */
|
||||
@ -92,6 +96,7 @@ struct CACHED_PERMISSIONS_LEGACY {
|
||||
|
||||
struct CACHED_SECURID {
|
||||
struct CACHED_SECURID *next;
|
||||
struct CACHED_SECURID *previous;
|
||||
void *variable;
|
||||
size_t varsize;
|
||||
/* above fields must match "struct CACHED_GENERIC" */
|
||||
@ -238,28 +243,29 @@ extern int ntfs_sd_add_everyone(ntfs_inode *ni);
|
||||
extern le32 ntfs_security_hash(const SECURITY_DESCRIPTOR_RELATIVE *sd,
|
||||
const u32 len);
|
||||
|
||||
int ntfs_build_mapping(struct SECURITY_CONTEXT *scx, const char *usermap_path);
|
||||
int ntfs_build_mapping(struct SECURITY_CONTEXT *scx, const char *usermap_path,
|
||||
BOOL allowdef);
|
||||
int ntfs_get_owner_mode(struct SECURITY_CONTEXT *scx,
|
||||
const char *path, ntfs_inode *ni, struct stat*);
|
||||
int ntfs_set_mode(struct SECURITY_CONTEXT *scx,
|
||||
const char *path, ntfs_inode *ni, mode_t mode);
|
||||
BOOL ntfs_allowed_as_owner(struct SECURITY_CONTEXT *scx,
|
||||
const char *path, ntfs_inode *ni);
|
||||
int ntfs_allowed_access(struct SECURITY_CONTEXT *scx, const char *path,
|
||||
ntfs_inode *ni, struct stat*);
|
||||
int ntfs_set_mode(struct SECURITY_CONTEXT *scx, ntfs_inode *ni, mode_t mode);
|
||||
BOOL ntfs_allowed_as_owner(struct SECURITY_CONTEXT *scx, ntfs_inode *ni);
|
||||
int ntfs_allowed_access(struct SECURITY_CONTEXT *scx,
|
||||
ntfs_inode *ni, int accesstype);
|
||||
BOOL ntfs_allowed_dir_access(struct SECURITY_CONTEXT *scx,
|
||||
BOOL old_ntfs_allowed_dir_access(struct SECURITY_CONTEXT *scx,
|
||||
const char *path, int accesstype);
|
||||
|
||||
#if POSIXACLS
|
||||
le32 ntfs_alloc_securid(struct SECURITY_CONTEXT *scx,
|
||||
uid_t uid, gid_t gid, const char *dir_path,
|
||||
ntfs_inode *dir_ni, mode_t mode, BOOL isdir);
|
||||
uid_t uid, gid_t gid, ntfs_inode *dir_ni,
|
||||
mode_t mode, BOOL isdir);
|
||||
#else
|
||||
le32 ntfs_alloc_securid(struct SECURITY_CONTEXT *scx,
|
||||
uid_t uid, gid_t gid, mode_t mode, BOOL isdir);
|
||||
#endif
|
||||
int ntfs_set_owner(struct SECURITY_CONTEXT *scx,
|
||||
const char *path, ntfs_inode *ni, uid_t uid, gid_t gid);
|
||||
int ntfs_set_owner(struct SECURITY_CONTEXT *scx, ntfs_inode *ni,
|
||||
uid_t uid, gid_t gid);
|
||||
int ntfs_set_ownmod(struct SECURITY_CONTEXT *scx,
|
||||
ntfs_inode *ni, uid_t uid, gid_t gid, mode_t mode);
|
||||
#if POSIXACLS
|
||||
int ntfs_set_owner_mode(struct SECURITY_CONTEXT *scx,
|
||||
ntfs_inode *ni, uid_t uid, gid_t gid,
|
||||
@ -269,7 +275,7 @@ int ntfs_set_owner_mode(struct SECURITY_CONTEXT *scx,
|
||||
ntfs_inode *ni, uid_t uid, gid_t gid, mode_t mode);
|
||||
#endif
|
||||
le32 ntfs_inherited_id(struct SECURITY_CONTEXT *scx,
|
||||
const char *dir_path, ntfs_inode *dir_ni, BOOL fordir);
|
||||
ntfs_inode *dir_ni, BOOL fordir);
|
||||
int ntfs_open_secure(ntfs_volume *vol);
|
||||
void ntfs_close_secure(struct SECURITY_CONTEXT *scx);
|
||||
|
||||
@ -277,28 +283,25 @@ void ntfs_close_secure(struct SECURITY_CONTEXT *scx);
|
||||
|
||||
int ntfs_set_inherited_posix(struct SECURITY_CONTEXT *scx,
|
||||
ntfs_inode *ni, uid_t uid, gid_t gid,
|
||||
const char *dir_path, ntfs_inode *dir_ni, mode_t mode);
|
||||
int ntfs_get_posix_acl(struct SECURITY_CONTEXT *scx, const char *path,
|
||||
const char *name, char *value, size_t size,
|
||||
ntfs_inode *ni);
|
||||
int ntfs_set_posix_acl(struct SECURITY_CONTEXT *scx, const char *path,
|
||||
ntfs_inode *dir_ni, mode_t mode);
|
||||
int ntfs_get_posix_acl(struct SECURITY_CONTEXT *scx, ntfs_inode *ni,
|
||||
const char *name, char *value, size_t size);
|
||||
int ntfs_set_posix_acl(struct SECURITY_CONTEXT *scx, ntfs_inode *ni,
|
||||
const char *name, const char *value, size_t size,
|
||||
int flags, ntfs_inode *ni);
|
||||
int ntfs_remove_posix_acl(struct SECURITY_CONTEXT *scx, const char *path,
|
||||
const char *name, ntfs_inode *ni);
|
||||
int flags);
|
||||
int ntfs_remove_posix_acl(struct SECURITY_CONTEXT *scx, ntfs_inode *ni,
|
||||
const char *name);
|
||||
#endif
|
||||
|
||||
int ntfs_get_ntfs_acl(struct SECURITY_CONTEXT *scx, const char *path,
|
||||
const char *name, char *value, size_t size,
|
||||
ntfs_inode *ni);
|
||||
int ntfs_set_ntfs_acl(struct SECURITY_CONTEXT *scx, const char *path,
|
||||
const char *name, const char *value, size_t size,
|
||||
int flags, ntfs_inode *ni);
|
||||
int ntfs_get_ntfs_attrib(const char *path,
|
||||
char *value, size_t size, ntfs_inode *ni);
|
||||
int ntfs_set_ntfs_attrib(const char *path,
|
||||
const char *value, size_t size, int flags,
|
||||
ntfs_inode *ni);
|
||||
int ntfs_get_ntfs_acl(struct SECURITY_CONTEXT *scx, ntfs_inode *ni,
|
||||
char *value, size_t size);
|
||||
int ntfs_set_ntfs_acl(struct SECURITY_CONTEXT *scx, ntfs_inode *ni,
|
||||
const char *value, size_t size, int flags);
|
||||
|
||||
int ntfs_get_ntfs_attrib(ntfs_inode *ni, char *value, size_t size);
|
||||
int ntfs_set_ntfs_attrib(ntfs_inode *ni,
|
||||
const char *value, size_t size, int flags);
|
||||
|
||||
|
||||
/*
|
||||
* Security API for direct access to security descriptors
|
||||
|
@ -87,8 +87,8 @@ typedef sle64 leLSN;
|
||||
* Cygwin has a collision between our BOOL and <windef.h>'s
|
||||
* As long as this file will be included after <windows.h> were fine.
|
||||
*/
|
||||
#ifndef _WINDEF_H
|
||||
#ifndef GEKKO
|
||||
#ifndef _WINDEF_H
|
||||
/**
|
||||
* enum BOOL - These are just to make the code more readable...
|
||||
*/
|
||||
@ -112,8 +112,8 @@ typedef enum {
|
||||
ONE = 1,
|
||||
#endif
|
||||
} BOOL;
|
||||
#endif /* GEKKO */
|
||||
#endif /* defined _WINDEF_H */
|
||||
#endif /* defined GECKO */
|
||||
|
||||
/**
|
||||
* enum IGNORE_CASE_BOOL -
|
||||
|
@ -132,37 +132,30 @@ BOOL ntfs_names_are_equal(const ntfschar *s1, size_t s1_len,
|
||||
TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* ntfs_names_collate - collate two Unicode names
|
||||
/*
|
||||
* ntfs_names_full_collate() fully collate two Unicode names
|
||||
*
|
||||
* @name1: first Unicode name to compare
|
||||
* @name1_len: length of first Unicode name to compare
|
||||
* @name2: second Unicode name to compare
|
||||
* @name2_len: length of second Unicode name to compare
|
||||
* @err_val: if @name1 contains an invalid character return this value
|
||||
* @ic: either CASE_SENSITIVE or IGNORE_CASE
|
||||
* @upcase: upcase table (ignored if @ic is CASE_SENSITIVE)
|
||||
* @upcase_len: upcase table size (ignored if @ic is CASE_SENSITIVE)
|
||||
*
|
||||
* ntfs_names_collate() collates two Unicode names and returns:
|
||||
*
|
||||
* -1 if the first name collates before the second one,
|
||||
* 0 if the names match,
|
||||
* 1 if the second name collates before the first one, or
|
||||
* @err_val if an invalid character is found in @name1 during the comparison.
|
||||
*
|
||||
* The following characters are considered invalid: '"', '*', '<', '>' and '?'.
|
||||
*
|
||||
* A few optimizations made by JPA
|
||||
*/
|
||||
|
||||
int ntfs_names_collate(const ntfschar *name1, const u32 name1_len,
|
||||
int ntfs_names_full_collate(const ntfschar *name1, const u32 name1_len,
|
||||
const ntfschar *name2, const u32 name2_len,
|
||||
const int err_val __attribute__((unused)),
|
||||
const IGNORE_CASE_BOOL ic, const ntfschar *upcase,
|
||||
const u32 upcase_len)
|
||||
{
|
||||
u32 cnt;
|
||||
ntfschar c1, c2;
|
||||
u16 c1, c2;
|
||||
u16 u1, u2;
|
||||
|
||||
#ifdef DEBUG
|
||||
if (!name1 || !name2 || (ic && (!upcase || !upcase_len))) {
|
||||
@ -171,39 +164,71 @@ int ntfs_names_collate(const ntfschar *name1, const u32 name1_len,
|
||||
}
|
||||
#endif
|
||||
cnt = min(name1_len, name2_len);
|
||||
/* JPA average loop count is 8 */
|
||||
if (cnt > 0) {
|
||||
if (ic)
|
||||
/* JPA this loop in 76% cases */
|
||||
if (ic == CASE_SENSITIVE) {
|
||||
do {
|
||||
c1 = le16_to_cpu(*name1);
|
||||
name1++;
|
||||
c2 = le16_to_cpu(*name2);
|
||||
name2++;
|
||||
if (c1 < upcase_len)
|
||||
c1 = le16_to_cpu(upcase[c1]);
|
||||
if (c2 < upcase_len)
|
||||
c2 = le16_to_cpu(upcase[c2]);
|
||||
} while ((c1 == c2) && --cnt);
|
||||
else
|
||||
} while (--cnt && (c1 == c2));
|
||||
u1 = c1;
|
||||
u2 = c2;
|
||||
if (u1 < upcase_len)
|
||||
u1 = le16_to_cpu(upcase[u1]);
|
||||
if (u2 < upcase_len)
|
||||
u2 = le16_to_cpu(upcase[u2]);
|
||||
if ((u1 == u2) && cnt)
|
||||
do {
|
||||
/* JPA this loop in 24% cases */
|
||||
c1 = le16_to_cpu(*name1);
|
||||
u1 = le16_to_cpu(*name1);
|
||||
name1++;
|
||||
c2 = le16_to_cpu(*name2);
|
||||
u2 = le16_to_cpu(*name2);
|
||||
name2++;
|
||||
} while ((c1 == c2) && --cnt);
|
||||
if (u1 < upcase_len)
|
||||
u1 = le16_to_cpu(upcase[u1]);
|
||||
if (u2 < upcase_len)
|
||||
u2 = le16_to_cpu(upcase[u2]);
|
||||
} while ((u1 == u2) && --cnt);
|
||||
if (u1 < u2)
|
||||
return -1;
|
||||
if (u1 > u2)
|
||||
return 1;
|
||||
if (name1_len < name2_len)
|
||||
return -1;
|
||||
if (name1_len > name2_len)
|
||||
return 1;
|
||||
if (c1 < c2)
|
||||
return -1;
|
||||
if (c1 > c2)
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
do {
|
||||
u1 = c1 = le16_to_cpu(*name1);
|
||||
name1++;
|
||||
u2 = c2 = le16_to_cpu(*name2);
|
||||
name2++;
|
||||
if (u1 < upcase_len)
|
||||
u1 = le16_to_cpu(upcase[u1]);
|
||||
if (u2 < upcase_len)
|
||||
u2 = le16_to_cpu(upcase[u2]);
|
||||
} while ((u1 == u2) && --cnt);
|
||||
if (u1 < u2)
|
||||
return -1;
|
||||
if (u1 > u2)
|
||||
return 1;
|
||||
if (name1_len < name2_len)
|
||||
return -1;
|
||||
if (name1_len == name2_len)
|
||||
return 0;
|
||||
if (name1_len > name2_len)
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
if (name1_len < name2_len)
|
||||
return -1;
|
||||
if (name1_len > name2_len)
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* ntfs_ucsncmp - compare two little endian Unicode strings
|
||||
@ -264,7 +289,7 @@ int ntfs_ucsncmp(const ntfschar *s1, const ntfschar *s2, size_t n)
|
||||
int ntfs_ucsncasecmp(const ntfschar *s1, const ntfschar *s2, size_t n,
|
||||
const ntfschar *upcase, const u32 upcase_size)
|
||||
{
|
||||
ntfschar c1, c2;
|
||||
u16 c1, c2;
|
||||
size_t i;
|
||||
|
||||
#ifdef DEBUG
|
||||
@ -357,13 +382,28 @@ void ntfs_name_upcase(ntfschar *name, u32 name_len, const ntfschar *upcase,
|
||||
const u32 upcase_len)
|
||||
{
|
||||
u32 i;
|
||||
ntfschar u;
|
||||
u16 u;
|
||||
|
||||
for (i = 0; i < name_len; i++)
|
||||
if ((u = le16_to_cpu(name[i])) < upcase_len)
|
||||
name[i] = upcase[u];
|
||||
}
|
||||
|
||||
/**
|
||||
* ntfs_name_locase - Map a Unicode name to its lowercase equivalent
|
||||
*/
|
||||
void ntfs_name_locase(ntfschar *name, u32 name_len, const ntfschar *locase,
|
||||
const u32 locase_len)
|
||||
{
|
||||
u32 i;
|
||||
u16 u;
|
||||
|
||||
if (locase)
|
||||
for (i = 0; i < name_len; i++)
|
||||
if ((u = le16_to_cpu(name[i])) < locase_len)
|
||||
name[i] = locase[u];
|
||||
}
|
||||
|
||||
/**
|
||||
* ntfs_file_value_upcase - Convert a filename to upper case
|
||||
* @file_name_attr:
|
||||
@ -381,31 +421,6 @@ void ntfs_file_value_upcase(FILE_NAME_ATTR *file_name_attr,
|
||||
file_name_attr->file_name_length, upcase, upcase_len);
|
||||
}
|
||||
|
||||
/**
|
||||
* ntfs_file_values_compare - Which of two filenames should be listed first
|
||||
* @file_name_attr1:
|
||||
* @file_name_attr2:
|
||||
* @err_val:
|
||||
* @ic:
|
||||
* @upcase:
|
||||
* @upcase_len:
|
||||
*
|
||||
* Description...
|
||||
*
|
||||
* Returns:
|
||||
*/
|
||||
int ntfs_file_values_compare(const FILE_NAME_ATTR *file_name_attr1,
|
||||
const FILE_NAME_ATTR *file_name_attr2,
|
||||
const int err_val, const IGNORE_CASE_BOOL ic,
|
||||
const ntfschar *upcase, const u32 upcase_len)
|
||||
{
|
||||
return ntfs_names_collate((ntfschar*)&file_name_attr1->file_name,
|
||||
file_name_attr1->file_name_length,
|
||||
(ntfschar*)&file_name_attr2->file_name,
|
||||
file_name_attr2->file_name_length,
|
||||
err_val, ic, upcase, upcase_len);
|
||||
}
|
||||
|
||||
/*
|
||||
NTFS uses Unicode (UTF-16LE [NTFS-3G uses UCS-2LE, which is enough
|
||||
for now]) for path names, but the Unicode code points need to be
|
||||
@ -505,7 +520,7 @@ static int ntfs_utf16_to_utf8(const ntfschar *ins, const int ins_len,
|
||||
|
||||
char *t;
|
||||
int i, size, ret = -1;
|
||||
ntfschar halfpair;
|
||||
int halfpair;
|
||||
|
||||
halfpair = 0;
|
||||
if (!*outs)
|
||||
@ -614,6 +629,7 @@ static int utf8_to_utf16_size(const char *s)
|
||||
while ((byte = *((const unsigned char *)s++))) {
|
||||
if (++count >= PATH_MAX)
|
||||
goto fail;
|
||||
if (byte >= 0xc0) {
|
||||
if (byte >= 0xF5) {
|
||||
errno = EILSEQ;
|
||||
goto out;
|
||||
@ -634,6 +650,7 @@ static int utf8_to_utf16_size(const char *s)
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
}
|
||||
ret = count;
|
||||
out:
|
||||
return ret;
|
||||
@ -663,8 +680,6 @@ static int utf8_to_unicode(u32 *wc, const char *s)
|
||||
} else if (byte < 0xc2) {
|
||||
goto fail;
|
||||
} else if (byte < 0xE0) {
|
||||
if (strlen(s) < 2)
|
||||
goto fail;
|
||||
if ((s[1] & 0xC0) == 0x80) {
|
||||
*wc = ((u32)(byte & 0x1F) << 6)
|
||||
| ((u32)(s[1] & 0x3F));
|
||||
@ -673,8 +688,6 @@ static int utf8_to_unicode(u32 *wc, const char *s)
|
||||
goto fail;
|
||||
/* three-byte */
|
||||
} else if (byte < 0xF0) {
|
||||
if (strlen(s) < 3)
|
||||
goto fail;
|
||||
if (((s[1] & 0xC0) == 0x80) && ((s[2] & 0xC0) == 0x80)) {
|
||||
*wc = ((u32)(byte & 0x0F) << 12)
|
||||
| ((u32)(s[1] & 0x3F) << 6)
|
||||
@ -693,8 +706,6 @@ static int utf8_to_unicode(u32 *wc, const char *s)
|
||||
goto fail;
|
||||
/* four-byte */
|
||||
} else if (byte < 0xF5) {
|
||||
if (strlen(s) < 4)
|
||||
goto fail;
|
||||
if (((s[1] & 0xC0) == 0x80) && ((s[2] & 0xC0) == 0x80)
|
||||
&& ((s[3] & 0xC0) == 0x80)) {
|
||||
*wc = ((u32)(byte & 0x07) << 18)
|
||||
@ -737,6 +748,7 @@ static int ntfs_utf8_to_utf16(const char *ins, ntfschar **outs)
|
||||
#endif /* defined(__APPLE__) || defined(__DARWIN__) */
|
||||
const char *t = ins;
|
||||
u32 wc;
|
||||
BOOL allocated;
|
||||
ntfschar *outpos;
|
||||
int shorts, ret = -1;
|
||||
|
||||
@ -744,18 +756,30 @@ static int ntfs_utf8_to_utf16(const char *ins, ntfschar **outs)
|
||||
if (shorts < 0)
|
||||
goto fail;
|
||||
|
||||
allocated = FALSE;
|
||||
if (!*outs) {
|
||||
*outs = ntfs_malloc((shorts + 1) * sizeof(ntfschar));
|
||||
if (!*outs)
|
||||
goto fail;
|
||||
allocated = TRUE;
|
||||
}
|
||||
|
||||
outpos = *outs;
|
||||
|
||||
while(1) {
|
||||
int m = utf8_to_unicode(&wc, t);
|
||||
if (m < 0)
|
||||
if (m <= 0) {
|
||||
if (m < 0) {
|
||||
/* do not leave space allocated if failed */
|
||||
if (allocated) {
|
||||
free(*outs);
|
||||
*outs = (ntfschar*)NULL;
|
||||
}
|
||||
goto fail;
|
||||
}
|
||||
*outpos++ = const_cpu_to_le16(0);
|
||||
break;
|
||||
}
|
||||
if (wc < 0x10000)
|
||||
*outpos++ = cpu_to_le16(wc);
|
||||
else {
|
||||
@ -763,8 +787,6 @@ static int ntfs_utf8_to_utf16(const char *ins, ntfschar **outs)
|
||||
*outpos++ = cpu_to_le16((wc >> 10) + 0xd800);
|
||||
*outpos++ = cpu_to_le16((wc & 0x3ff) + 0xdc00);
|
||||
}
|
||||
if (m == 0)
|
||||
break;
|
||||
t += m;
|
||||
}
|
||||
|
||||
@ -1028,6 +1050,61 @@ err_out:
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Turn a UTF8 name uppercase
|
||||
*
|
||||
* Returns an allocated uppercase name which has to be freed by caller
|
||||
* or NULL if there is an error (described by errno)
|
||||
*/
|
||||
|
||||
char *ntfs_uppercase_mbs(const char *low,
|
||||
const ntfschar *upcase, u32 upcase_size)
|
||||
{
|
||||
int size;
|
||||
char *upp;
|
||||
u32 wc;
|
||||
int n;
|
||||
const char *s;
|
||||
char *t;
|
||||
|
||||
size = strlen(low);
|
||||
upp = (char*)ntfs_malloc(3*size + 1);
|
||||
if (upp) {
|
||||
s = low;
|
||||
t = upp;
|
||||
do {
|
||||
n = utf8_to_unicode(&wc, s);
|
||||
if (n > 0) {
|
||||
if (wc < upcase_size)
|
||||
wc = le16_to_cpu(upcase[wc]);
|
||||
if (wc < 0x80)
|
||||
*t++ = wc;
|
||||
else if (wc < 0x800) {
|
||||
*t++ = (0xc0 | ((wc >> 6) & 0x3f));
|
||||
*t++ = 0x80 | (wc & 0x3f);
|
||||
} else if (wc < 0x10000) {
|
||||
*t++ = 0xe0 | (wc >> 12);
|
||||
*t++ = 0x80 | ((wc >> 6) & 0x3f);
|
||||
*t++ = 0x80 | (wc & 0x3f);
|
||||
} else {
|
||||
*t++ = 0xf0 | ((wc >> 18) & 7);
|
||||
*t++ = 0x80 | ((wc >> 12) & 63);
|
||||
*t++ = 0x80 | ((wc >> 6) & 0x3f);
|
||||
*t++ = 0x80 | (wc & 0x3f);
|
||||
}
|
||||
s += n;
|
||||
}
|
||||
} while (n > 0);
|
||||
if (n < 0) {
|
||||
free(upp);
|
||||
upp = (char*)NULL;
|
||||
errno = EILSEQ;
|
||||
}
|
||||
*t = 0;
|
||||
}
|
||||
return (upp);
|
||||
}
|
||||
|
||||
/**
|
||||
* ntfs_upcase_table_build - build the default upcase table for NTFS
|
||||
* @uc: destination buffer where to store the built table
|
||||
@ -1099,6 +1176,38 @@ void ntfs_upcase_table_build(ntfschar *uc, u32 uc_len)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Build a table for converting to lower case
|
||||
*
|
||||
* This is only meaningful when there is a single lower case
|
||||
* character leading to an upper case one, and currently the
|
||||
* only exception is the greek letter sigma which has a single
|
||||
* upper case glyph (code U+03A3), but two lower case glyphs
|
||||
* (code U+03C3 and U+03C2, the latter to be used at the end
|
||||
* of a word). In the following implementation the upper case
|
||||
* sigma will be lowercased as U+03C3.
|
||||
*/
|
||||
|
||||
ntfschar *ntfs_locase_table_build(const ntfschar *uc, u32 uc_cnt)
|
||||
{
|
||||
ntfschar *lc;
|
||||
u32 upp;
|
||||
u32 i;
|
||||
|
||||
lc = (ntfschar*)ntfs_malloc(uc_cnt*sizeof(ntfschar));
|
||||
if (lc) {
|
||||
for (i=0; i<uc_cnt; i++)
|
||||
lc[i] = cpu_to_le16(i);
|
||||
for (i=0; i<uc_cnt; i++) {
|
||||
upp = le16_to_cpu(uc[i]);
|
||||
if ((upp != i) && (upp < uc_cnt))
|
||||
lc[upp] = cpu_to_le16(i);
|
||||
}
|
||||
} else
|
||||
ntfs_log_error("Could not build the locase table\n");
|
||||
return (lc);
|
||||
}
|
||||
|
||||
/**
|
||||
* ntfs_str2ucs - convert a string to a valid NTFS file name
|
||||
* @s: input string
|
||||
@ -1170,7 +1279,9 @@ BOOL ntfs_forbidden_chars(const ntfschar *name, int len)
|
||||
| (1L << ('>' - 0x20))
|
||||
| (1L << ('?' - 0x20));
|
||||
|
||||
forbidden = (len == 0) || (le16_to_cpu(name[len-1]) == ' ');
|
||||
forbidden = (len == 0)
|
||||
|| (le16_to_cpu(name[len-1]) == ' ')
|
||||
|| (le16_to_cpu(name[len-1]) == '.');
|
||||
for (i=0; i<len; i++) {
|
||||
ch = le16_to_cpu(name[i]);
|
||||
if ((ch < 0x20)
|
||||
|
@ -30,9 +30,9 @@ extern BOOL ntfs_names_are_equal(const ntfschar *s1, size_t s1_len,
|
||||
const ntfschar *s2, size_t s2_len, const IGNORE_CASE_BOOL ic,
|
||||
const ntfschar *upcase, const u32 upcase_size);
|
||||
|
||||
extern int ntfs_names_collate(const ntfschar *name1, const u32 name1_len,
|
||||
extern int ntfs_names_full_collate(const ntfschar *name1, const u32 name1_len,
|
||||
const ntfschar *name2, const u32 name2_len,
|
||||
const int err_val, const IGNORE_CASE_BOOL ic,
|
||||
const IGNORE_CASE_BOOL ic,
|
||||
const ntfschar *upcase, const u32 upcase_len);
|
||||
|
||||
extern int ntfs_ucsncmp(const ntfschar *s1, const ntfschar *s2, size_t n);
|
||||
@ -47,19 +47,21 @@ extern ntfschar *ntfs_ucsndup(const ntfschar *s, u32 maxlen);
|
||||
extern void ntfs_name_upcase(ntfschar *name, u32 name_len,
|
||||
const ntfschar *upcase, const u32 upcase_len);
|
||||
|
||||
extern void ntfs_file_value_upcase(FILE_NAME_ATTR *file_name_attr,
|
||||
const ntfschar *upcase, const u32 upcase_len);
|
||||
extern void ntfs_name_locase(ntfschar *name, u32 name_len,
|
||||
const ntfschar *locase, const u32 locase_len);
|
||||
|
||||
extern int ntfs_file_values_compare(const FILE_NAME_ATTR *file_name_attr1,
|
||||
const FILE_NAME_ATTR *file_name_attr2,
|
||||
const int err_val, const IGNORE_CASE_BOOL ic,
|
||||
extern void ntfs_file_value_upcase(FILE_NAME_ATTR *file_name_attr,
|
||||
const ntfschar *upcase, const u32 upcase_len);
|
||||
|
||||
extern int ntfs_ucstombs(const ntfschar *ins, const int ins_len, char **outs,
|
||||
int outs_len);
|
||||
extern int ntfs_mbstoucs(const char *ins, ntfschar **outs);
|
||||
|
||||
extern char *ntfs_uppercase_mbs(const char *low,
|
||||
const ntfschar *upcase, u32 upcase_len);
|
||||
|
||||
extern void ntfs_upcase_table_build(ntfschar *uc, u32 uc_len);
|
||||
extern ntfschar *ntfs_locase_table_build(const ntfschar *uc, u32 uc_cnt);
|
||||
|
||||
extern ntfschar *ntfs_str2ucs(const char *s, int *len);
|
||||
|
||||
|
@ -4,6 +4,7 @@
|
||||
* Copyright (c) 2000-2006 Anton Altaparmakov
|
||||
* Copyright (c) 2002-2009 Szabolcs Szakacsits
|
||||
* Copyright (c) 2004-2005 Richard Russon
|
||||
* Copyright (c) 2010 Jean-Pierre Andre
|
||||
*
|
||||
* This program/include file is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as published
|
||||
@ -65,6 +66,7 @@
|
||||
#include "logfile.h"
|
||||
#include "dir.h"
|
||||
#include "logging.h"
|
||||
#include "cache.h"
|
||||
#include "misc.h"
|
||||
|
||||
const char *ntfs_home =
|
||||
@ -200,6 +202,7 @@ static int __ntfs_volume_release(ntfs_volume *v)
|
||||
ntfs_free_lru_caches(v);
|
||||
free(v->vol_name);
|
||||
free(v->upcase);
|
||||
if (v->locase) free(v->locase);
|
||||
free(v->attrdef);
|
||||
free(v);
|
||||
|
||||
@ -486,7 +489,14 @@ ntfs_volume *ntfs_volume_startup(struct ntfs_device *dev, unsigned long flags)
|
||||
|
||||
ntfs_upcase_table_build(vol->upcase,
|
||||
vol->upcase_len * sizeof(ntfschar));
|
||||
/* Default with no locase table and case sensitive file names */
|
||||
vol->locase = (ntfschar*)NULL;
|
||||
NVolSetCaseSensitive(vol);
|
||||
|
||||
/* by default, all files are shown and not marked hidden */
|
||||
NVolSetShowSysFiles(vol);
|
||||
NVolSetShowHidFiles(vol);
|
||||
NVolClearHideDotFiles(vol);
|
||||
if (flags & MS_RDONLY)
|
||||
NVolSetReadOnly(vol);
|
||||
|
||||
@ -760,6 +770,70 @@ out:
|
||||
return errno ? -1 : 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Make sure a LOGGED_UTILITY_STREAM attribute named "$TXF_DATA"
|
||||
* on the root directory is resident.
|
||||
* When it is non-resident, the partition cannot be mounted on Vista
|
||||
* (see http://support.microsoft.com/kb/974729)
|
||||
*
|
||||
* We take care to avoid this situation, however this can be a
|
||||
* consequence of having used an older version (including older
|
||||
* Windows version), so we had better fix it.
|
||||
*
|
||||
* Returns 0 if unneeded or successful
|
||||
* -1 if there was an error, explained by errno
|
||||
*/
|
||||
|
||||
static int fix_txf_data(ntfs_volume *vol)
|
||||
{
|
||||
void *txf_data;
|
||||
s64 txf_data_size;
|
||||
ntfs_inode *ni;
|
||||
ntfs_attr *na;
|
||||
int res;
|
||||
|
||||
res = 0;
|
||||
ntfs_log_debug("Loading root directory\n");
|
||||
ni = ntfs_inode_open(vol, FILE_root);
|
||||
if (!ni) {
|
||||
ntfs_log_perror("Failed to open root directory");
|
||||
res = -1;
|
||||
} else {
|
||||
/* Get the $TXF_DATA attribute */
|
||||
na = ntfs_attr_open(ni, AT_LOGGED_UTILITY_STREAM, TXF_DATA, 9);
|
||||
if (na) {
|
||||
if (NAttrNonResident(na)) {
|
||||
/*
|
||||
* Fix the attribute by truncating, then
|
||||
* rewriting it.
|
||||
*/
|
||||
ntfs_log_debug("Making $TXF_DATA resident\n");
|
||||
txf_data = ntfs_attr_readall(ni,
|
||||
AT_LOGGED_UTILITY_STREAM,
|
||||
TXF_DATA, 9, &txf_data_size);
|
||||
if (txf_data) {
|
||||
if (ntfs_attr_truncate(na, 0)
|
||||
|| (ntfs_attr_pwrite(na, 0,
|
||||
txf_data_size, txf_data)
|
||||
!= txf_data_size))
|
||||
res = -1;
|
||||
free(txf_data);
|
||||
}
|
||||
if (res)
|
||||
ntfs_log_error("Failed to make $TXF_DATA resident\n");
|
||||
else
|
||||
ntfs_log_error("$TXF_DATA made resident\n");
|
||||
}
|
||||
ntfs_attr_close(na);
|
||||
}
|
||||
if (ntfs_inode_close(ni)) {
|
||||
ntfs_log_perror("Failed to close root");
|
||||
res = -1;
|
||||
}
|
||||
}
|
||||
return (res);
|
||||
}
|
||||
|
||||
/**
|
||||
* ntfs_device_mount - open ntfs volume
|
||||
* @dev: device to open
|
||||
@ -1045,9 +1119,9 @@ ntfs_volume *ntfs_device_mount(struct ntfs_device *dev, unsigned long flags)
|
||||
goto error_exit;
|
||||
|
||||
for (j = 0; j < (s32)u; j++) {
|
||||
ntfschar uc = le16_to_cpu(vname[j]);
|
||||
u16 uc = le16_to_cpu(vname[j]);
|
||||
if (uc > 0xff)
|
||||
uc = (ntfschar)'_';
|
||||
uc = (u16)'_';
|
||||
vol->vol_name[j] = (char)uc;
|
||||
}
|
||||
vol->vol_name[u] = '\0';
|
||||
@ -1111,6 +1185,9 @@ ntfs_volume *ntfs_device_mount(struct ntfs_device *dev, unsigned long flags)
|
||||
goto error_exit;
|
||||
}
|
||||
}
|
||||
/* make $TXF_DATA resident if present on the root directory */
|
||||
if (!NVolReadOnly(vol) && fix_txf_data(vol))
|
||||
goto error_exit;
|
||||
|
||||
return vol;
|
||||
io_error_exit:
|
||||
@ -1126,6 +1203,58 @@ error_exit:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Set appropriate flags for showing NTFS metafiles
|
||||
* or files marked as hidden.
|
||||
* Not set in ntfs_mount() to avoid breaking existing tools.
|
||||
*/
|
||||
|
||||
int ntfs_set_shown_files(ntfs_volume *vol,
|
||||
BOOL show_sys_files, BOOL show_hid_files,
|
||||
BOOL hide_dot_files)
|
||||
{
|
||||
int res;
|
||||
|
||||
res = -1;
|
||||
if (vol) {
|
||||
NVolClearShowSysFiles(vol);
|
||||
NVolClearShowHidFiles(vol);
|
||||
NVolClearHideDotFiles(vol);
|
||||
if (show_sys_files)
|
||||
NVolSetShowSysFiles(vol);
|
||||
if (show_hid_files)
|
||||
NVolSetShowHidFiles(vol);
|
||||
if (hide_dot_files)
|
||||
NVolSetHideDotFiles(vol);
|
||||
res = 0;
|
||||
}
|
||||
if (res)
|
||||
ntfs_log_error("Failed to set file visibility\n");
|
||||
return (res);
|
||||
}
|
||||
|
||||
/*
|
||||
* Set ignore case mode
|
||||
*/
|
||||
|
||||
int ntfs_set_ignore_case(ntfs_volume *vol)
|
||||
{
|
||||
int res;
|
||||
|
||||
res = -1;
|
||||
if (vol && vol->upcase) {
|
||||
vol->locase = ntfs_locase_table_build(vol->upcase,
|
||||
vol->upcase_len);
|
||||
if (vol->locase) {
|
||||
NVolClearCaseSensitive(vol);
|
||||
res = 0;
|
||||
}
|
||||
}
|
||||
if (res)
|
||||
ntfs_log_error("Failed to set ignore_case mode\n");
|
||||
return (res);
|
||||
}
|
||||
|
||||
/**
|
||||
* ntfs_mount - open ntfs volume
|
||||
* @name: name of device/file to open
|
||||
@ -1432,7 +1561,7 @@ error_exit:
|
||||
*
|
||||
* Return 0 if successful and -1 if not with errno set to the error code.
|
||||
*/
|
||||
int ntfs_volume_write_flags(ntfs_volume *vol, const u16 flags)
|
||||
int ntfs_volume_write_flags(ntfs_volume *vol, const le16 flags)
|
||||
{
|
||||
ATTR_RECORD *a;
|
||||
VOLUME_INFORMATION *c;
|
||||
|
@ -42,10 +42,6 @@
|
||||
#include <mntent.h>
|
||||
#endif
|
||||
|
||||
#define CACHE_INODE_SIZE 32 /* inode cache, zero or >= 3 and not too big */
|
||||
#define CACHE_SECURID_SIZE 16 /* securid cache, zero or >= 3 and not too big */
|
||||
#define CACHE_LEGACY_SIZE 8 /* legacy cache size, zero or >= 3 and not too big */
|
||||
|
||||
/*
|
||||
* Under Cygwin, DJGPP and FreeBSD we do not have MS_RDONLY,
|
||||
* so we define them ourselves.
|
||||
@ -65,6 +61,7 @@
|
||||
/* Forward declaration */
|
||||
typedef struct _ntfs_volume ntfs_volume;
|
||||
|
||||
#include "param.h"
|
||||
#include "types.h"
|
||||
#include "support.h"
|
||||
#include "device.h"
|
||||
@ -110,6 +107,10 @@ typedef enum {
|
||||
NV_ReadOnly, /* 1: Volume is read-only. */
|
||||
NV_CaseSensitive, /* 1: Volume is mounted case-sensitive. */
|
||||
NV_LogFileEmpty, /* 1: $logFile journal is empty. */
|
||||
NV_ShowSysFiles, /* 1: Show NTFS metafiles. */
|
||||
NV_ShowHidFiles, /* 1: Show files marked hidden. */
|
||||
NV_HideDotFiles, /* 1: Set hidden flag on dot files */
|
||||
NV_Compression, /* 1: allow compression */
|
||||
} ntfs_volume_state_bits;
|
||||
|
||||
#define test_nvol_flag(nv, flag) test_bit(NV_##flag, (nv)->state)
|
||||
@ -128,6 +129,22 @@ typedef enum {
|
||||
#define NVolSetLogFileEmpty(nv) set_nvol_flag(nv, LogFileEmpty)
|
||||
#define NVolClearLogFileEmpty(nv) clear_nvol_flag(nv, LogFileEmpty)
|
||||
|
||||
#define NVolShowSysFiles(nv) test_nvol_flag(nv, ShowSysFiles)
|
||||
#define NVolSetShowSysFiles(nv) set_nvol_flag(nv, ShowSysFiles)
|
||||
#define NVolClearShowSysFiles(nv) clear_nvol_flag(nv, ShowSysFiles)
|
||||
|
||||
#define NVolShowHidFiles(nv) test_nvol_flag(nv, ShowHidFiles)
|
||||
#define NVolSetShowHidFiles(nv) set_nvol_flag(nv, ShowHidFiles)
|
||||
#define NVolClearShowHidFiles(nv) clear_nvol_flag(nv, ShowHidFiles)
|
||||
|
||||
#define NVolHideDotFiles(nv) test_nvol_flag(nv, HideDotFiles)
|
||||
#define NVolSetHideDotFiles(nv) set_nvol_flag(nv, HideDotFiles)
|
||||
#define NVolClearHideDotFiles(nv) clear_nvol_flag(nv, HideDotFiles)
|
||||
|
||||
#define NVolCompression(nv) test_nvol_flag(nv, Compression)
|
||||
#define NVolSetCompression(nv) set_nvol_flag(nv, Compression)
|
||||
#define NVolClearCompression(nv) clear_nvol_flag(nv, Compression)
|
||||
|
||||
/*
|
||||
* NTFS version 1.1 and 1.2 are used by Windows NT4.
|
||||
* NTFS version 2.x is used by Windows 2000 Beta
|
||||
@ -159,7 +176,7 @@ struct _ntfs_volume {
|
||||
ntfs_inode *vol_ni; /* ntfs_inode structure for FILE_Volume. */
|
||||
u8 major_ver; /* Ntfs major version of volume. */
|
||||
u8 minor_ver; /* Ntfs minor version of volume. */
|
||||
u16 flags; /* Bit array of VOLUME_* flags. */
|
||||
le16 flags; /* Bit array of VOLUME_* flags. */
|
||||
|
||||
u16 sector_size; /* Byte size of a sector. */
|
||||
u8 sector_size_bits; /* Log(2) of the byte size of a sector. */
|
||||
@ -220,6 +237,9 @@ struct _ntfs_volume {
|
||||
FILE_UpCase. */
|
||||
u32 upcase_len; /* Length in Unicode characters of the upcase
|
||||
table. */
|
||||
ntfschar *locase; /* Lower case equivalents of all 65536 2-byte
|
||||
Unicode characters. Only if option
|
||||
case_ignore is set. */
|
||||
|
||||
ATTR_DEF *attrdef; /* Attribute definitions. Obtained from
|
||||
FILE_AttrDef. */
|
||||
@ -235,6 +255,12 @@ struct _ntfs_volume {
|
||||
#if CACHE_INODE_SIZE
|
||||
struct CACHE_HEADER *xinode_cache;
|
||||
#endif
|
||||
#if CACHE_NIDATA_SIZE
|
||||
struct CACHE_HEADER *nidata_cache;
|
||||
#endif
|
||||
#if CACHE_LOOKUP_SIZE
|
||||
struct CACHE_HEADER *lookup_cache;
|
||||
#endif
|
||||
#if CACHE_SECURID_SIZE
|
||||
struct CACHE_HEADER *securid_cache;
|
||||
#endif
|
||||
@ -261,14 +287,17 @@ extern int ntfs_version_is_supported(ntfs_volume *vol);
|
||||
extern int ntfs_volume_check_hiberfile(ntfs_volume *vol, int verbose);
|
||||
extern int ntfs_logfile_reset(ntfs_volume *vol);
|
||||
|
||||
extern int ntfs_volume_write_flags(ntfs_volume *vol, const u16 flags);
|
||||
extern int ntfs_volume_write_flags(ntfs_volume *vol, const le16 flags);
|
||||
|
||||
extern int ntfs_volume_error(int err);
|
||||
extern void ntfs_mount_error(const char *vol, const char *mntpoint, int err);
|
||||
|
||||
extern int ntfs_volume_get_free_space(ntfs_volume *vol);
|
||||
|
||||
extern int ntfs_set_shown_files(ntfs_volume *vol,
|
||||
BOOL show_sys_files, BOOL show_hid_files, BOOL hide_dot_files);
|
||||
extern int ntfs_set_locale(void);
|
||||
extern int ntfs_set_ignore_case(ntfs_volume *vol);
|
||||
|
||||
#endif /* defined _NTFS_VOLUME_H */
|
||||
|
||||
|
384
source/main.cpp
384
source/main.cpp
@ -51,6 +51,7 @@ extern "C"
|
||||
#include "usbloader/usbstorage2.h"
|
||||
#include "memory/mem2.h"
|
||||
#include "lstub.h"
|
||||
#include "usbloader/usbstorage2.h"
|
||||
|
||||
extern bool geckoinit;
|
||||
extern bool textVideoInit;
|
||||
@ -65,172 +66,19 @@ extern char headlessID[8];
|
||||
FreeTypeGX *fontSystem=0;
|
||||
FreeTypeGX *fontClock=0;
|
||||
PartList partitions;
|
||||
|
||||
u8 dbvideo =0;
|
||||
|
||||
static void BootUpProblems()
|
||||
{
|
||||
s32 ret2;
|
||||
|
||||
// load main font from file, or default to built-in font
|
||||
fontSystem = new FreeTypeGX();
|
||||
fontSystem->loadFont(NULL, font_ttf, font_ttf_size, 0);
|
||||
fontSystem->setCompatibilityMode(FTGX_COMPATIBILITY_DEFAULT_TEVOP_GX_PASSCLR | FTGX_COMPATIBILITY_DEFAULT_VTXDESC_GX_NONE);
|
||||
|
||||
GuiImageData bootimageData(gxlogo_png);
|
||||
GuiImage bootimage(&bootimageData);
|
||||
GuiText boottext(NULL, 20, (GXColor) {255, 255, 255, 255}
|
||||
);
|
||||
boottext.SetPosition(200, 240-1.2*bootimage.GetHeight()/2+250);
|
||||
bootimage.SetPosition(320-1.2*bootimage.GetWidth()/2, 240-1.2*bootimage.GetHeight()/2);
|
||||
bootimage.SetScale(1.2);
|
||||
|
||||
GuiImageData usbimageData(usbport_png);
|
||||
GuiImage usbimage(&usbimageData);
|
||||
usbimage.SetPosition(400,300);
|
||||
usbimage.SetScale(.7);
|
||||
usbimage.SetAlpha(200);
|
||||
|
||||
time_t curtime;
|
||||
time_t endtime = time(0) + 30;
|
||||
do
|
||||
{
|
||||
/*ret2 = IOS_ReloadIOSsafe(249);
|
||||
if (ret2 < 0) {
|
||||
ret2 = IOS_ReloadIOSsafe(222);
|
||||
SDCard_Init();
|
||||
load_ehc_module();
|
||||
SDCard_deInit();
|
||||
if(ret2 <0) {
|
||||
boottext.SetText("ERROR: cIOS could not be loaded!");
|
||||
bootimage.Draw();
|
||||
boottext.Draw();
|
||||
Menu_Render();
|
||||
sleep(5);
|
||||
SYS_ResetSystem(SYS_RETURNTOMENU, 0, 0);
|
||||
}
|
||||
}*/
|
||||
USBDevice_deInit();
|
||||
USBDevice_Init();
|
||||
ret2 = WBFS_Init(WBFS_DEVICE_USB);
|
||||
if (ret2 >= 0)
|
||||
{
|
||||
boottext.SetText("Loading...");
|
||||
bootimage.Draw();
|
||||
boottext.Draw();
|
||||
Menu_Render();
|
||||
break;
|
||||
}
|
||||
curtime = time(0);
|
||||
boottext.SetTextf("Waiting for your slow USB Device: %i secs...", int(endtime-curtime));
|
||||
while(curtime == time(0))
|
||||
{
|
||||
boottext.Draw();
|
||||
bootimage.Draw();
|
||||
if (endtime-curtime<15)usbimage.Draw();
|
||||
Menu_Render();
|
||||
}
|
||||
} while((endtime-time(0)) > 0);
|
||||
|
||||
/*if(ret2 < 0) {
|
||||
boottext.SetText("ERROR: USB device could not be loaded!");
|
||||
usbimage.Draw();
|
||||
bootimage.Draw();
|
||||
boottext.Draw();
|
||||
Menu_Render();
|
||||
SYS_ResetSystem(SYS_RETURNTOMENU, 0, 0);
|
||||
}*/
|
||||
|
||||
///delete font to load up custom if set
|
||||
if(fontSystem)
|
||||
{
|
||||
delete fontSystem;
|
||||
fontSystem = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
unsigned int *xfb = NULL;
|
||||
|
||||
void InitTextVideo ()
|
||||
{
|
||||
gprintf("\nInitTextVideo ()");
|
||||
if (textVideoInit)
|
||||
{
|
||||
gprintf("...0");
|
||||
return;
|
||||
}
|
||||
dbvideo=1;
|
||||
VIDEO_Init();
|
||||
// get default video mode
|
||||
GXRModeObj *vmode = VIDEO_GetPreferredMode(NULL);
|
||||
|
||||
// widescreen fix
|
||||
VIDEO_Configure (vmode);
|
||||
|
||||
// Allocate the video buffers
|
||||
xfb = (u32 *) MEM_K0_TO_K1 (SYS_AllocateFramebuffer (vmode));
|
||||
|
||||
// A console is always useful while debugging
|
||||
console_init (xfb, 20, 64, vmode->fbWidth, vmode->xfbHeight, vmode->fbWidth * 2);
|
||||
|
||||
// Clear framebuffers etc.
|
||||
VIDEO_ClearFrameBuffer (vmode, xfb, COLOR_BLACK);
|
||||
VIDEO_SetNextFramebuffer (xfb);
|
||||
|
||||
VIDEO_SetBlack (FALSE);
|
||||
VIDEO_Flush ();
|
||||
VIDEO_WaitVSync ();
|
||||
if (vmode->viTVMode & VI_NON_INTERLACE)
|
||||
VIDEO_WaitVSync ();
|
||||
|
||||
//send console output to the gecko
|
||||
if (geckoinit)CON_EnableGecko(1, true);
|
||||
textVideoInit = true;
|
||||
gprintf("...1");
|
||||
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
MEM2_init(48);
|
||||
PAD_Init();
|
||||
InitVideo();
|
||||
setlocale(LC_ALL, "en.UTF-8");
|
||||
geckoinit = InitGecko();
|
||||
|
||||
if (hbcStubAvailable() || geckoinit)
|
||||
{
|
||||
InitTextVideo();
|
||||
}
|
||||
|
||||
// DEBUG_Init(GDBSTUB_DEVICE_USB, 1);
|
||||
//_break();
|
||||
|
||||
__exception_setreload(5); //auto reset code dump nobody gives us codedump info anyways.
|
||||
|
||||
gprintf("\n\n------------------");
|
||||
gprintf("\nUSB Loader GX rev%s",GetRev());
|
||||
gprintf("\nmain(%d", argc);
|
||||
for (int i=0;i<argc;i++)
|
||||
gprintf(", %s",argv[i]?argv[i]:"<NULL>");
|
||||
gprintf(")");
|
||||
|
||||
// This part is added, because we need a identify patched ios
|
||||
// printf("\n\tReloading into ios 236");
|
||||
if (IOS_ReloadIOSsafe(236) < 0)
|
||||
{
|
||||
// printf("\n\tIOS 236 not found, reloading into 36");
|
||||
IOS_ReloadIOSsafe(36);
|
||||
}
|
||||
__exception_setreload(5);
|
||||
|
||||
printf("\n\tStarting up");
|
||||
|
||||
MEM2_init(36); // Initialize 36 MB
|
||||
MEM2_takeBigOnes(true);
|
||||
|
||||
s32 ret;
|
||||
bool startupproblem = false;
|
||||
|
||||
bool bootDevice_found=false;
|
||||
if (argc >= 1)
|
||||
{
|
||||
@ -242,144 +90,15 @@ main(int argc, char *argv[])
|
||||
bootDevice_found = true;
|
||||
}
|
||||
|
||||
printf("\n\tInitializing controllers");
|
||||
|
||||
/** PAD_Init has to be before InitVideo don't move that **/
|
||||
PAD_Init(); // initialize PAD/WPAD
|
||||
|
||||
printf("\n\tInitialize USB (wake up)");
|
||||
|
||||
USBDevice_Init(); // seems enough to wake up some HDDs if they are in sleep mode when the loader starts (tested with WD MyPassport Essential 2.5")
|
||||
|
||||
gprintf("\n\tChecking for stub IOS");
|
||||
ios222rev = getIOSrev(0x00000001000000dell);
|
||||
ios249rev = getIOSrev(0x00000001000000f9ll);
|
||||
|
||||
//if we don't like either of the cIOS then scram
|
||||
if (!(ios222rev==4 || ios222rev==5 || (ios249rev>=9 && ios249rev<65280)))
|
||||
{
|
||||
InitTextVideo();
|
||||
printf("\x1b[2J");
|
||||
if ((ios222rev < 0 && ios222rev != WII_EINSTALL) && (ios249rev < 0 && ios249rev != WII_EINSTALL))
|
||||
{
|
||||
printf("\n\n\n\tWARNING!");
|
||||
printf("\n\tUSB Loader GX needs unstubbed cIOS 222 v4 or 249 v9+");
|
||||
printf("\n\n\tWe cannot determine the versions on your system,\n\tsince you have no patched ios 36 or 236 installed.");
|
||||
printf("\n\tTherefor, if loading of USB Loader GX fails, you\n\tprobably have installed the 4.2 update,");
|
||||
printf("\n\tand you should go figure out how to get some cios action going on\n\tin your Wii.");
|
||||
printf("\n\n\tThis message will show every time.");
|
||||
sleep(5);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("\n\n\n\tERROR!");
|
||||
printf("\n\tUSB Loader GX needs unstubbed cIOS 222 v4 or 249 v9+");
|
||||
printf("\n\n\tI found \n\t\t222 = %d%s",ios222rev,ios222rev==65280?" (Stubbed by 4.2 update)":"");
|
||||
printf("\n\t\t249 = %d%s",ios249rev,ios249rev==65280?" (Stubbed by 4.2 update)":"");
|
||||
printf("\n\n\tGo figure out how to get some cIOS action going on\n\tin your Wii and come back and see me.");
|
||||
|
||||
sleep(15);
|
||||
printf("\n\n\tBye");
|
||||
|
||||
USBDevice_deInit();
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n\tReloading ios 249...");
|
||||
ret = IOS_ReloadIOSsafe(249);
|
||||
|
||||
printf("%d", ret);
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
printf("\n\tIOS 249 failed, reloading ios 222...");
|
||||
ret = IOS_ReloadIOSsafe(222);
|
||||
printf("%d", ret);
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
printf("\n\tIOS 222 failed, reloading ios 250...");
|
||||
ret = IOS_ReloadIOSsafe(250);
|
||||
printf("%d", ret);
|
||||
|
||||
if(ret < 0)
|
||||
{
|
||||
printf("\n\tIOS 250 failed, reloading ios 223...");
|
||||
ret = IOS_ReloadIOSsafe(223);
|
||||
printf("%d", ret);
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
printf("\n\tERROR: cIOS could not be loaded!\n");
|
||||
sleep(5);
|
||||
SYS_ResetSystem(SYS_RETURNTOMENU, 0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
//Let's use libogc sd/usb for config loading
|
||||
printf("\n\tInitialize sd card");
|
||||
SDCard_Init();
|
||||
printf("\n\tLoad ehc module");
|
||||
load_ehc_module();
|
||||
printf("\n\tdeinit sd card");
|
||||
SDCard_deInit();
|
||||
}
|
||||
|
||||
printf("\n\tInit wbfs...");
|
||||
ret = WBFS_Init(WBFS_DEVICE_USB);
|
||||
printf("%d", ret);
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
printf("\n\tYou have issues with a slow disc, or a difficult disc\n\tReloading 222...");
|
||||
ret = IOS_ReloadIOSsafe(222);
|
||||
printf("%d", ret);
|
||||
/*if(ret < 0) {
|
||||
// printf("\n\tSleeping for 4 seconds");
|
||||
// sleep(4);
|
||||
|
||||
InitVideo(); // Initialise video
|
||||
Menu_Render();
|
||||
BootUpProblems();
|
||||
startupproblem = true;
|
||||
ret = 1;
|
||||
}*/
|
||||
printf("\n\tInitialize sd card");
|
||||
SDCard_Init();
|
||||
printf("\n\tLoad ehc module");
|
||||
load_ehc_module();
|
||||
printf("\n\tdeinit sd card");
|
||||
SDCard_deInit();
|
||||
|
||||
printf("\n\tInitialize wbfs...");
|
||||
USBDevice_deInit();
|
||||
USBDevice_Init();
|
||||
ret = WBFS_Init(WBFS_DEVICE_USB);
|
||||
printf("%d", ret);
|
||||
|
||||
if(ret < 0)
|
||||
{
|
||||
// printf("\n\tSleeping for 4 seconds");
|
||||
// sleep(4);
|
||||
InitVideo(); // Initialise video
|
||||
Menu_Render();
|
||||
BootUpProblems();
|
||||
startupproblem = true;
|
||||
ret = 1;
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n\tInitialize sd card");
|
||||
SDCard_Init(); // mount SD for loading cfg's
|
||||
|
||||
//this should have already been done by now in order to WBFS_Init().
|
||||
printf("\n\tInitialize usb device");
|
||||
USBDevice_Init(); // and mount USB:/
|
||||
USBDevice_Init();
|
||||
|
||||
if (!bootDevice_found)
|
||||
{
|
||||
printf("\n\tSearch for configuration file");
|
||||
|
||||
//try USB
|
||||
//left in all the dol and elf files in this check in case this is the first time running the app and they dont have the config
|
||||
if (checkfile((char*) "USB:/config/GXglobal.cfg") || (checkfile((char*) "USB:/apps/usbloader_gx/boot.elf"))
|
||||
@ -390,82 +109,39 @@ main(int argc, char *argv[])
|
||||
printf("\n\tConfiguration file is on %s", bootDevice);
|
||||
}
|
||||
|
||||
// Try opening and closing the configuration file here
|
||||
// to prevent a crash dump later on - giantpune
|
||||
char GXGlobal_cfg[26];
|
||||
sprintf(GXGlobal_cfg, "%s/config/GXGlobal.cfg", bootDevice);
|
||||
FILE *fp = fopen(GXGlobal_cfg, "r");
|
||||
if (fp)
|
||||
{
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
gettextCleanUp();
|
||||
printf("\n\tLoading configuration...");
|
||||
CFG_Load();
|
||||
printf("done");
|
||||
// gprintf("\n\tbootDevice = %s",bootDevice);
|
||||
|
||||
/* Load Custom IOS */
|
||||
if ((Settings.cios == ios222 && IOS_GetVersion() != 222) ||
|
||||
(Settings.cios == ios223 && IOS_GetVersion() != 223))
|
||||
{
|
||||
printf("\n\tReloading IOS to config setting (%d)...", Settings.cios == ios222 ? 222 : 223);
|
||||
SDCard_deInit();// unmount SD for reloading IOS
|
||||
USBDevice_deInit();// unmount USB for reloading IOS
|
||||
USBStorage2_Deinit();
|
||||
ret = IOS_ReloadIOSsafe(Settings.cios == ios222 ? 222 : 223);
|
||||
printf("%d", ret);
|
||||
SDCard_Init();
|
||||
load_ehc_module();
|
||||
if (ret < 0)
|
||||
|
||||
// This part is added, because we need a identify patched ios
|
||||
//! pune please replace this with your magic patch functions - Dimok
|
||||
if (IOS_ReloadIOSsafe(236) < 0)
|
||||
IOS_ReloadIOSsafe(36);
|
||||
|
||||
printf("\n\tCheck for an existing cIOS");
|
||||
CheckForCIOS();
|
||||
|
||||
// Let's load the cIOS now
|
||||
if(LoadAppCIOS() < 0)
|
||||
{
|
||||
SDCard_deInit();
|
||||
Settings.cios = ios249;
|
||||
ret = IOS_ReloadIOSsafe(249);
|
||||
// now mount SD:/ //no need to keep mindlessly mounting and unmounting SD card
|
||||
SDCard_Init();
|
||||
}
|
||||
|
||||
USBDevice_Init(); // and mount USB:/
|
||||
WBFS_Init(WBFS_DEVICE_USB);
|
||||
} else if ((Settings.cios == ios249 && IOS_GetVersion() != 249) ||
|
||||
(Settings.cios == ios250 && IOS_GetVersion() != 250))
|
||||
{
|
||||
|
||||
printf("\n\tReloading IOS to config setting (%d)...", ios249 ? 249 : 250);
|
||||
SDCard_deInit(); // unmount SD for reloading IOS
|
||||
USBDevice_deInit(); // unmount USB for reloading IOS
|
||||
USBStorage2_Deinit();
|
||||
ret = IOS_ReloadIOSsafe(ios249 ? 249 : 250);
|
||||
printf("%d", ret);
|
||||
if (ret < 0)
|
||||
{
|
||||
Settings.cios = ios222;
|
||||
ret = IOS_ReloadIOSsafe(222);
|
||||
SDCard_Init();
|
||||
load_ehc_module();
|
||||
}
|
||||
|
||||
else SDCard_Init(); // now mount SD:/ //no need to keep mindlessly mounting and unmounting SD card
|
||||
USBDevice_Init(); // and mount USB:/
|
||||
WBFS_Init(WBFS_DEVICE_USB);
|
||||
}
|
||||
|
||||
// Partition_GetList(&partitions);
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
printf("\nERROR: cIOS could not be loaded!");
|
||||
printf("\n\tERROR: No cIOS could be loaded. Exiting....");
|
||||
sleep(5);
|
||||
exit(0);
|
||||
//SYS_ResetSystem(SYS_RETURNTOMENU, 0, 0);
|
||||
Sys_BackToLoader();
|
||||
}
|
||||
//gprintf("\n\tcIOS = %u (Rev %u)",IOS_GetVersion(), IOS_GetRevision());//don't need gprintf if sending console shit to gecko, too
|
||||
|
||||
printf("\n\tcIOS = %u (Rev %u)",IOS_GetVersion(), IOS_GetRevision());
|
||||
|
||||
// printf("Sleeping for 5 seconds\n");
|
||||
// sleep(5);
|
||||
// Init WBFS
|
||||
int ret = WBFS_Init(WBFS_DEVICE_USB);
|
||||
if (ret < 0)
|
||||
{
|
||||
printf("\nERROR: No WBFS drive mounted.");
|
||||
}
|
||||
|
||||
//if a ID was passed via args copy it and try to boot it after the partition is mounted
|
||||
//its not really a headless mode. more like hairless.
|
||||
@ -478,9 +154,7 @@ main(int argc, char *argv[])
|
||||
//! Init the rest of the System
|
||||
Sys_Init();
|
||||
Wpad_Init();
|
||||
if(!startupproblem)
|
||||
InitVideo();
|
||||
InitAudio(); // Initialize audio
|
||||
InitAudio();
|
||||
|
||||
WPAD_SetDataFormat(WPAD_CHAN_ALL,WPAD_FMT_BTNS_ACC_IR);
|
||||
WPAD_SetVRes(WPAD_CHAN_ALL, screenwidth, screenheight);
|
||||
|
@ -1,17 +1,27 @@
|
||||
|
||||
#include "mem2.h"
|
||||
#include "mem2alloc.h"
|
||||
#include "mem2alloc.hpp"
|
||||
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MEM2_PRIORITY_SIZE 0x40
|
||||
#define MEM2_PRIORITY_SIZE 2097152 //2MB
|
||||
|
||||
// Forbid the use of MEM2 through malloc
|
||||
u32 MALLOC_MEM2 = 0;
|
||||
|
||||
static CMEM2Alloc g_mem2gp;
|
||||
|
||||
static bool g_bigGoesToMem2 = false;
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
void MEM2_takeBigOnes(bool b)
|
||||
{
|
||||
g_bigGoesToMem2 = b;
|
||||
}
|
||||
|
||||
void MEM2_init(unsigned int mem2Size)
|
||||
{
|
||||
g_mem2gp.init(mem2Size);
|
||||
@ -22,41 +32,31 @@ void MEM2_cleanup(void)
|
||||
g_mem2gp.cleanup();
|
||||
}
|
||||
|
||||
extern "C" void *MEM2_alloc(unsigned int s)
|
||||
void *MEM2_alloc(unsigned int s)
|
||||
{
|
||||
return g_mem2gp.allocate(s);
|
||||
}
|
||||
|
||||
extern "C" void MEM2_free(void *p)
|
||||
void MEM2_free(void *p)
|
||||
{
|
||||
g_mem2gp.release(p);
|
||||
}
|
||||
|
||||
extern "C" void *MEM2_realloc(void *p, unsigned int s)
|
||||
void *MEM2_realloc(void *p, unsigned int s)
|
||||
{
|
||||
return g_mem2gp.reallocate(p, s);
|
||||
}
|
||||
|
||||
extern "C" unsigned int MEM2_usableSize(void *p)
|
||||
unsigned int MEM2_usableSize(void *p)
|
||||
{
|
||||
return CMEM2Alloc::usableSize(p);
|
||||
}
|
||||
|
||||
// Give priority to MEM2 for big allocations
|
||||
// Used for saving some space in malloc, which is required for 2 reasons :
|
||||
// - decent speed on small and frequent allocations
|
||||
// - newlib uses its malloc internally (for *printf for example) so it should always have some memory left
|
||||
bool g_bigGoesToMem2 = false;
|
||||
|
||||
void MEM2_takeBigOnes(bool b)
|
||||
unsigned int MEM2_freesize()
|
||||
{
|
||||
g_bigGoesToMem2 = b;
|
||||
return g_mem2gp.FreeSize();
|
||||
}
|
||||
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
extern __typeof(malloc) __real_malloc;
|
||||
extern __typeof(calloc) __real_calloc;
|
||||
extern __typeof(realloc) __real_realloc;
|
||||
@ -133,9 +133,12 @@ void __wrap_free(void *p)
|
||||
if(!p)
|
||||
return;
|
||||
|
||||
if (((u32)p & 0x10000000) != 0) {
|
||||
if (((u32)p & 0x10000000) != 0)
|
||||
{
|
||||
MEM2_free(p);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
__real_free(p);
|
||||
}
|
||||
}
|
||||
@ -185,4 +188,4 @@ size_t __wrap_malloc_usable_size(void *p)
|
||||
return __real_malloc_usable_size(p);
|
||||
}
|
||||
|
||||
}
|
||||
} ///extern "C"
|
||||
|
@ -1,26 +1,24 @@
|
||||
// 2 MEM2 allocators, one for general purpose, one for covers
|
||||
// Aligned and padded to 32 bytes, as required by many functions
|
||||
|
||||
#ifndef __MEM2_HPP
|
||||
#define __MEM2_HPP
|
||||
#ifndef __MEM2_H_
|
||||
#define __MEM2_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void *MEM2_alloc(unsigned int s);
|
||||
void *MEM2_realloc(void *p, unsigned int s);
|
||||
void MEM2_free(void *p);
|
||||
unsigned int MEM2_usableSize(void *p);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
void MEM2_init(unsigned int mem2Size);
|
||||
void MEM2_cleanup(void);
|
||||
void MEM2_takeBigOnes(bool b);
|
||||
void *MEM2_alloc(unsigned int s);
|
||||
void *MEM2_realloc(void *p, unsigned int s);
|
||||
void MEM2_free(void *p);
|
||||
unsigned int MEM2_usableSize(void *p);
|
||||
unsigned int MEM2_freesize();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // !defined(__MEM2_HPP)
|
||||
#endif // !defined(__MEM2_H_)
|
||||
|
@ -1,5 +1,5 @@
|
||||
|
||||
#include "mem2alloc.h"
|
||||
#include "mem2alloc.hpp"
|
||||
|
||||
#include <ogc/system.h>
|
||||
#include <algorithm>
|
||||
@ -18,8 +18,8 @@ void CMEM2Alloc::init(unsigned int size)
|
||||
{
|
||||
m_baseAddress = (SBlock *) (((u32)SYS_GetArena2Lo() + 31) & ~31);
|
||||
m_endAddress = (SBlock *) ((char *)m_baseAddress + std::min(size * 0x100000, SYS_GetArena2Size() & ~31));
|
||||
if (m_endAddress > (SBlock *)0x93000000) // See loader/disc.c
|
||||
m_endAddress = (SBlock *)0x93000000;
|
||||
if (m_endAddress > (SBlock *) 0x93300000) //rest is reserved for usb/usb2/network and other stuff... (0xE0000 bytes)
|
||||
m_endAddress = (SBlock *) 0x93300000;
|
||||
SYS_SetArena2Lo(m_endAddress);
|
||||
LWP_MutexInit(&m_mutex, 0);
|
||||
}
|
||||
@ -36,9 +36,9 @@ void CMEM2Alloc::cleanup(void)
|
||||
LWP_MutexDestroy(m_mutex);
|
||||
m_mutex = 0;
|
||||
m_first = 0;
|
||||
// // Try to release the range we took through SYS functions
|
||||
// if (SYS_GetArena2Lo() == m_endAdress)
|
||||
// SYS_SetArena2Lo(m_baseAddress);
|
||||
// Try to release the range we took through SYS functions
|
||||
if (SYS_GetArena2Lo() == m_endAddress)
|
||||
SYS_SetArena2Lo(m_baseAddress);
|
||||
m_baseAddress = 0;
|
||||
m_endAddress = 0;
|
||||
}
|
||||
@ -118,9 +118,16 @@ void CMEM2Alloc::release(void *p)
|
||||
{
|
||||
if (p == 0)
|
||||
return;
|
||||
|
||||
LockMutex lock(m_mutex);
|
||||
SBlock *i = (SBlock *)p - 1;
|
||||
i->f = true;
|
||||
|
||||
// If there are no other blocks following yet,
|
||||
// set the remaining size to free size. - Dimok
|
||||
if(i->next == 0)
|
||||
i->s = m_endAddress - i - 1;
|
||||
|
||||
// Merge with previous block
|
||||
if (i->prev != 0 && i->prev->f)
|
||||
{
|
||||
@ -150,12 +157,17 @@ void *CMEM2Alloc::reallocate(void *p, unsigned int s)
|
||||
s = 1;
|
||||
if (p == 0)
|
||||
return allocate(s);
|
||||
|
||||
i = (SBlock *)p - 1;
|
||||
s = (s - 1) / sizeof (SBlock) + 1;
|
||||
{
|
||||
LockMutex lock(m_mutex);
|
||||
|
||||
//out of memory /* Dimok */
|
||||
if (i + s + 1 >= m_endAddress)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Last block
|
||||
if (i->next == 0 && i + s + 1 < m_endAddress)
|
||||
@ -164,7 +176,7 @@ void *CMEM2Alloc::reallocate(void *p, unsigned int s)
|
||||
return p;
|
||||
}
|
||||
// Size <= current size + next block
|
||||
if (i->s < s && i->next->f && i->s + i->next->s + 1 >= s)
|
||||
if (i->next != 0 && i->s < s && i->next->f && i->s + i->next->s + 1 >= s)
|
||||
{
|
||||
// Merge
|
||||
i->s += i->next->s + 1;
|
||||
@ -199,3 +211,28 @@ void *CMEM2Alloc::reallocate(void *p, unsigned int s)
|
||||
release(p);
|
||||
return n;
|
||||
}
|
||||
|
||||
unsigned int CMEM2Alloc::FreeSize()
|
||||
{
|
||||
LockMutex lock(m_mutex);
|
||||
|
||||
if (m_first == 0)
|
||||
return (const char *) m_endAddress - (const char *) m_baseAddress;
|
||||
|
||||
SBlock *i;
|
||||
unsigned int size = 0;
|
||||
|
||||
for(i = m_first; i != 0; i = i->next)
|
||||
{
|
||||
if(i->f && i->next != 0)
|
||||
size += i->s;
|
||||
|
||||
else if(i->f && i->next == 0)
|
||||
size += m_endAddress - i - 1;
|
||||
|
||||
else if(!i->f && i->next == 0)
|
||||
size += m_endAddress - i - i->s - 1;
|
||||
}
|
||||
|
||||
return size*sizeof(SBlock);
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ public:
|
||||
void forceEndAddress(void *newAddr) { m_endAddress = (SBlock *)newAddr; }
|
||||
void *getEndAddress(void) const { return m_endAddress; }
|
||||
void info(void *&address, unsigned int &size) const { address = m_baseAddress; size = (const char *)m_endAddress - (const char *)m_baseAddress; }
|
||||
unsigned int FreeSize();
|
||||
//
|
||||
CMEM2Alloc(void) : m_baseAddress(0), m_endAddress(0), m_first(0), m_mutex(0) { }
|
||||
private:
|
@ -350,10 +350,8 @@ int MainMenu(int menu) {
|
||||
gettextCleanUp();
|
||||
|
||||
if(dbvideo)
|
||||
{
|
||||
InitVideodebug ();
|
||||
//printf("\n\n\n\n\n");
|
||||
}
|
||||
|
||||
if (mountMethod==3)
|
||||
{
|
||||
struct discHdr *header = &gameList[gameSelected];
|
||||
@ -372,13 +370,13 @@ int MainMenu(int menu) {
|
||||
WII_LaunchTitle(0x0000000100000100ULL);
|
||||
}
|
||||
|
||||
else if (boothomebrew == 1) {
|
||||
if (boothomebrew == 1) {
|
||||
gprintf("\nBootHomebrew");
|
||||
BootHomebrew(Settings.selected_homebrew);
|
||||
}
|
||||
else if (boothomebrew == 2) {
|
||||
gprintf("\nBootHomebrewFromMenu");
|
||||
BootHomebrewFromMem();
|
||||
gprintf("\nBootHomebrew from Menu");
|
||||
BootHomebrew();
|
||||
}
|
||||
else {
|
||||
gprintf("\n\tSettings.partition:%d",Settings.partition);
|
||||
|
@ -577,7 +577,7 @@ int MenuDiscList() {
|
||||
w.Append(&sdcardBtn);
|
||||
w.Append(&poweroffBtn);
|
||||
w.Append(&gameInfo);
|
||||
if (Settings.godmode && load_from_fs != PART_FS_NTFS)
|
||||
if (Settings.godmode)
|
||||
w.Append(&installBtn);
|
||||
w.Append(&homeBtn);
|
||||
w.Append(&settingsBtn);
|
||||
@ -800,17 +800,9 @@ int MenuDiscList() {
|
||||
gameCarousel->SetFocus(1);
|
||||
}
|
||||
} else if (installBtn.GetState() == STATE_CLICKED) {
|
||||
gprintf("\n\tinstallBtn clicked");
|
||||
if (load_from_fs == PART_FS_NTFS)
|
||||
{
|
||||
choice = 0;
|
||||
WindowPrompt(tr("Install not possible"), tr("You are using NTFS filesystem. Due to possible write errors to a NTFS partition, installing a game is not possible."), tr("OK"));
|
||||
}
|
||||
else
|
||||
choice = WindowPrompt(tr("Install a game"),0,tr("Yes"),tr("No"));
|
||||
if (choice == 1)
|
||||
{
|
||||
if (load_from_fs == PART_FS_NTFS)
|
||||
menu = MENU_INSTALL;
|
||||
break;
|
||||
}
|
||||
@ -825,7 +817,7 @@ int MenuDiscList() {
|
||||
gameCarousel->SetFocus(1);
|
||||
}
|
||||
}
|
||||
} else if ((covert & 0x2)&&(covert!=covertOld) && load_from_fs != PART_FS_NTFS) {
|
||||
} else if ((covert & 0x2)&&(covert!=covertOld)) {
|
||||
gprintf("\n\tNew Disc Detected");
|
||||
choice = WindowPrompt(tr("New Disc Detected"),0,tr("Install"),tr("Mount DVD drive"),tr("Cancel"));
|
||||
if (choice == 1) {
|
||||
|
@ -12,8 +12,10 @@
|
||||
#include "audio.h"
|
||||
#include "menu.h"
|
||||
#include "fatmounter.h"
|
||||
#include "gecko.h"
|
||||
#include "sys.h"
|
||||
#include "wpad.h"
|
||||
#include "lstub.h"
|
||||
|
||||
extern char game_partition[6];
|
||||
extern u8 load_from_fs;
|
||||
@ -197,7 +199,8 @@ void Sys_LoadMenu(void) {
|
||||
}
|
||||
|
||||
void Sys_BackToLoader(void) {
|
||||
if (*((u32*) 0x80001800)) {
|
||||
|
||||
if (hbcStubAvailable()) {
|
||||
_ExitApp();
|
||||
exit(0);
|
||||
}
|
||||
@ -264,6 +267,85 @@ s32 IOS_ReloadIOSsafe(int ios)
|
||||
return r;
|
||||
}
|
||||
|
||||
s32 CheckForCIOS()
|
||||
{
|
||||
gprintf("\n\tChecking for stub IOS");
|
||||
s32 ret = 1;
|
||||
ios222rev = getIOSrev(0x00000001000000dell);
|
||||
ios249rev = getIOSrev(0x00000001000000f9ll);
|
||||
|
||||
//if we don't like either of the cIOS then scram
|
||||
if (!((ios222rev >= 4 && ios222rev < 65280) || (ios249rev >=9 && ios249rev < 65280)))
|
||||
{
|
||||
printf("\x1b[2J");
|
||||
if ((ios222rev < 0 && ios222rev != WII_EINSTALL) && (ios249rev < 0 && ios249rev != WII_EINSTALL)) {
|
||||
printf("\n\n\n\tWARNING!");
|
||||
printf("\n\tUSB Loader GX needs unstubbed cIOS 222 v4 or 249 v9+");
|
||||
printf("\n\n\tWe cannot determine the versions on your system,\n\tsince you have no patched ios 36 or 236 installed.");
|
||||
printf("\n\tTherefor, if loading of USB Loader GX fails, you\n\tprobably have installed the 4.2 update,");
|
||||
printf("\n\tand you should go figure out how to get some cios action going on\n\tin your Wii.");
|
||||
printf("\n\n\tThis message will show every time.");
|
||||
sleep(5);
|
||||
} else {
|
||||
printf("\n\n\n\tERROR!");
|
||||
printf("\n\tUSB Loader GX needs unstubbed cIOS 222 v4 or 249 v9+");
|
||||
printf("\n\n\tI found \n\t\t222 = %d%s",ios222rev,ios222rev==65280?" (Stubbed by 4.2 update)":"");
|
||||
printf("\n\t\t249 = %d%s",ios249rev,ios249rev==65280?" (Stubbed by 4.2 update)":"");
|
||||
printf("\n\n\tGo figure out how to get some cIOS action going on\n\tin your Wii and come back and see me.");
|
||||
|
||||
sleep(15);
|
||||
printf("\n\n\tBye");
|
||||
|
||||
USBDevice_deInit();
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int LoadAppCIOS()
|
||||
{
|
||||
s32 ret = 1;
|
||||
/* Load Custom IOS */
|
||||
SDCard_deInit();// unmount SD for reloading IOS
|
||||
USBDevice_deInit();// unmount USB for reloading IOS
|
||||
USBStorage2_Deinit();
|
||||
|
||||
if (Settings.cios == ios222 && IOS_GetVersion() != 222)
|
||||
{
|
||||
printf("\n\tReloading IOS to config setting (222)...");
|
||||
ret = IOS_ReloadIOSsafe(222);
|
||||
printf("%d", ret);
|
||||
if (ret < 0)
|
||||
{
|
||||
Settings.cios = ios249;
|
||||
IOS_ReloadIOSsafe(249);
|
||||
}
|
||||
}
|
||||
|
||||
if ((Settings.cios == ios249 && IOS_GetVersion() != 249)
|
||||
|| (Settings.cios == ios250 && IOS_GetVersion() != 250))
|
||||
{
|
||||
printf("\n\tReloading IOS to config setting (%d)...", (Settings.cios == ios249) ? 249 : 250);
|
||||
ret = IOS_ReloadIOSsafe((Settings.cios == ios249) ? 249 : 250);
|
||||
printf("%d", ret);
|
||||
if (ret < 0) {
|
||||
Settings.cios = ios222;
|
||||
ret = IOS_ReloadIOSsafe(222);
|
||||
}
|
||||
}
|
||||
|
||||
SDCard_Init();
|
||||
if(IOS_GetVersion() == 222)
|
||||
load_ehc_module();
|
||||
|
||||
USBDevice_Init();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
#include <time.h>
|
||||
|
||||
void ScreenShot()
|
||||
|
@ -16,7 +16,8 @@ int Sys_IosReload(int IOS);
|
||||
bool Sys_IsHermes();
|
||||
s32 IOS_ReloadIOSsafe(int ios);
|
||||
void ScreenShot();
|
||||
|
||||
s32 CheckForCIOS();
|
||||
int LoadAppCIOS();
|
||||
void ShowMemInfo();
|
||||
extern s32 ios222rev;
|
||||
extern s32 ios223rev;
|
||||
|
@ -177,9 +177,6 @@ InitVideo () {
|
||||
xfb[0] = (u32 *) MEM_K0_TO_K1 (SYS_AllocateFramebuffer (vmode));
|
||||
xfb[1] = (u32 *) MEM_K0_TO_K1 (SYS_AllocateFramebuffer (vmode));
|
||||
|
||||
// A console is always useful while debugging
|
||||
console_init (xfb[0], 20, 64, vmode->fbWidth, vmode->xfbHeight, vmode->fbWidth * 2);
|
||||
|
||||
// Clear framebuffers etc.
|
||||
VIDEO_ClearFrameBuffer (vmode, xfb[0], COLOR_BLACK);
|
||||
VIDEO_ClearFrameBuffer (vmode, xfb[1], COLOR_BLACK);
|
||||
@ -197,6 +194,9 @@ InitVideo () {
|
||||
StartGX();
|
||||
ResetVideo_Menu();
|
||||
// Finally, the video is up and ready for use :)
|
||||
|
||||
// A console is always useful while debugging
|
||||
console_init (xfb[0], 80, 100, 500, 350, vmode->fbWidth * 2);
|
||||
}
|
||||
static unsigned int *xfbDB = NULL;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user