Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
cmake_minimum_required(VERSION 2.8)
project(fatcat)

OPTION(DEFINE_WIN "Compiling windows" OFF)
if(MINGW)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++ -s")
endif()

set(SOURCES
core/FatEntry.cpp
Expand All @@ -20,20 +22,18 @@ set(SOURCES
analysis/FatChains.cpp
analysis/FatSearch.cpp
analysis/FatWalk.cpp

xgetopt/xgetopt.cpp
xgetopt/stdafx.cpp
xgetopt/argcargv.cpp

xgetopt/xgetopt.cpp
xgetopt/stdafx.cpp
xgetopt/argcargv.cpp

mingw/utimes.cpp
)

foreach(SOURCE ${SOURCES})
set(ALL_SOURCES ${ALL_SOURCES} src/${SOURCE})
endforeach()

IF(DEFINE_WIN)
add_definitions(-D__WIN__)
ENDIF(DEFINE_WIN)

include_directories("${CMAKE_SOURCE_DIR}/src")

add_executable(fatcat "src/fatcat.cpp" ${ALL_SOURCES})
Expand Down
24 changes: 19 additions & 5 deletions build.bat
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
if not exist build md build
cd build
git clean -fdx && cmake .. -DDEFINE_WIN=ON && "c:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe" fatcat.sln /p:OutDir=bin
cd ..
pause
@echo off

if exist build rmdir /S /Q build
mkdir build
pushd build

set MINGW_BIN=C:\MinGW\bin
if not exist %MINGW_BIN% goto _msvc

set PATH=%MINGW_BIN%;%PATH%
cmake .. -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=MINSIZEREL && mingw32-make
goto _the_end

:_msvc
cmake .. && start "" fatcat.sln

:_the_end
popd
pause
5 changes: 0 additions & 5 deletions src/FatUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
#include <string>
#include <algorithm>
#include <functional>
#ifdef __WIN__
#include <ctype.h>
#else

#endif

using namespace std;

Expand Down
13 changes: 5 additions & 8 deletions src/analysis/FatExtract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
#include <string.h>
#include <string>
#include <sys/stat.h>
#ifndef __WIN__
#ifdef _WIN32
#include <mingw/utimes.h>
#else
#include <sys/time.h>
#endif
#include <sys/types.h>
#include "FatExtract.h"
#ifdef __WIN__
#include <windows.h>
#endif

using namespace std;

Expand All @@ -23,7 +22,7 @@ FatExtract::FatExtract(FatSystem &system)
void FatExtract::onDirectory(FatEntry &parent, FatEntry &entry, string name)
{
string directory = targetDirectory + "/" + name;
#ifdef __WIN__
#ifdef _WIN32
CreateDirectory(directory.c_str(), NULL);
#else
mkdir(directory.c_str(), 0755);
Expand All @@ -42,11 +41,10 @@ void FatExtract::onEntry(FatEntry &parent, FatEntry &entry, string name)

string target = targetDirectory + name;
cout << "Extracting " << name << " to " << target << endl;
FILE *output = fopen(target.c_str(), "w+");
FILE *output = fopen(target.c_str(), "wb+");
system.readFile(entry.cluster, entry.size, output, contiguous);
fclose(output);

#ifndef __WIN__
time_t mtime = entry.changeDate.timestamp();
if (mtime == (time_t)-1) {
// Files on FAT can have dates up to 2107 year inclusive (which is
Expand All @@ -68,7 +66,6 @@ void FatExtract::onEntry(FatEntry &parent, FatEntry &entry, string name)
<< ": " << strerror(errno) << endl;
}
}
#endif
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/core/FatDate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ string FatDate::pretty()
return string(buffer);
}

#ifndef __WIN__
/**
* Returns date as a number of seconds elapsed since the Epoch,
* 1970-01-01 00:00:00 +0000 (UTC). FAT dates are considered to be in the
Expand All @@ -59,7 +58,6 @@ time_t FatDate::timestamp() const

return mktime(&tm);
}
#endif

string FatDate::isoFormat()
{
Expand Down
4 changes: 0 additions & 4 deletions src/core/FatDate.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
#define _FATCAT_FATDATE_H

#include <string>
#ifndef __WIN__
#include <time.h>
#endif

using namespace std;

Expand All @@ -18,9 +16,7 @@ class FatDate
int y, m, d;

string pretty();
#ifndef __WIN__
time_t timestamp() const;
#endif

string isoFormat();
};
Expand Down
75 changes: 57 additions & 18 deletions src/core/FatSystem.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
#ifdef __WIN__
#include <io.h>
#else
#include <iostream>
#include <stdio.h>
#include <set>
#ifndef _WIN32
#include <unistd.h>
#endif
#include <time.h>
#include <string>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <set>
#endif

#include <FatUtils.h>
#include "FatFilename.h"
Expand All @@ -38,10 +36,18 @@ FatSystem::FatSystem(string filename_, unsigned long long globalOffset_, OutputF
rootEntries(0)
{
this->_outputFormat = outputFormat_;
#ifdef _WIN32
fd = CreateFile(filename.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
#else
fd = open(filename.c_str(), O_RDONLY|O_LARGEFILE);
#endif
writeMode = false;

#ifdef _WIN32
if (fd == INVALID_HANDLE_VALUE) {
#else
if (fd < 0) {
#endif
ostringstream oss;
oss << "! Unable to open the input file: " << filename << " for reading";

Expand All @@ -63,10 +69,17 @@ void FatSystem::enableCache()

void FatSystem::enableWrite()
{
#ifdef _WIN32
CloseHandle(fd);
fd = CreateFile(filename.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

if (fd == INVALID_HANDLE_VALUE) {
#else
close(fd);
fd = open(filename.c_str(), O_RDWR|O_LARGEFILE);

if (fd < 0) {
#endif
ostringstream oss;
oss << "! Unable to open the input file: " << filename << " for writing";

Expand All @@ -78,7 +91,11 @@ void FatSystem::enableWrite()

FatSystem::~FatSystem()
{
#ifdef _WIN32
CloseHandle(fd);
#else
close(fd);
#endif
}

/**
Expand All @@ -90,6 +107,16 @@ int FatSystem::readData(unsigned long long address, char *buffer, int size)
cerr << "! Trying to read outside the disk" << endl;
}

#ifdef _WIN32
DWORD bytesRead;

LARGE_INTEGER offset;
offset.QuadPart = globalOffset+address;

SetFilePointerEx(fd, offset, NULL, FILE_BEGIN);
ReadFile(fd, buffer, size, &bytesRead, NULL);
return bytesRead;
#else
lseek64(fd, globalOffset+address, SEEK_SET);

int n;
Expand All @@ -104,6 +131,7 @@ int FatSystem::readData(unsigned long long address, char *buffer, int size)
} while ((size>0) && (n>0));

return n;
#endif
}

int FatSystem::writeData(unsigned long long address, const char *buffer, int size)
Expand All @@ -112,6 +140,16 @@ int FatSystem::writeData(unsigned long long address, const char *buffer, int siz
throw string("Trying to write data while write mode is disabled");
}

#ifdef _WIN32
DWORD bytesWritten;

LARGE_INTEGER offset;
offset.QuadPart = globalOffset+address;

SetFilePointerEx(fd, offset, NULL, FILE_BEGIN);
WriteFile(fd, buffer, size, &bytesWritten, NULL);
return bytesWritten;
#else
lseek64(fd, globalOffset+address, SEEK_SET);

int n;
Expand All @@ -126,6 +164,7 @@ int FatSystem::writeData(unsigned long long address, const char *buffer, int siz
} while ((size>0) && (n>0));

return n;
#endif
}

/**
Expand Down Expand Up @@ -199,12 +238,12 @@ void FatSystem::parseHeader()
unsigned int FatSystem::nextCluster(unsigned int cluster, int fat)
{
int bytes = (bits == 32 ? 4 : 2);
#ifndef __WIN__
#ifndef _MSC_VER
char buffer[bytes];
#else
char *buffer = new char[bytes];
__try
{
char *buffer = new char[bytes];
__try
{
#endif

if (!validCluster(cluster)) {
Expand Down Expand Up @@ -249,7 +288,7 @@ unsigned int FatSystem::nextCluster(unsigned int cluster, int fat)
}
}
}
#ifdef __WIN__
#ifdef _MSC_VER
}
__finally
{
Expand All @@ -264,7 +303,7 @@ __finally
bool FatSystem::writeNextCluster(unsigned int cluster, unsigned int next, int fat)
{
int bytes = (bits == 32 ? 4 : 2);
#ifndef __WIN__
#ifndef _MSC_VER
char buffer[bytes];
#else
char *buffer = new char[bytes];
Expand Down Expand Up @@ -296,7 +335,7 @@ bool FatSystem::writeNextCluster(unsigned int cluster, unsigned int next, int fa
}

return writeData(offset, buffer, bytes) == bytes;
#ifdef __WIN__
#ifdef _MSC_VER
}
__finally
{
Expand Down Expand Up @@ -594,7 +633,7 @@ void FatSystem::readFile(unsigned int cluster, unsigned int size, FILE *f, bool
if (toRead > bytesPerCluster || size < 0) {
toRead = bytesPerCluster;
}
#ifndef __WIN__
#ifndef _MSC_VER
char buffer[bytesPerCluster];
#else
char *buffer = new char[bytesPerCluster];
Expand Down Expand Up @@ -636,7 +675,7 @@ void FatSystem::readFile(unsigned int cluster, unsigned int size, FILE *f, bool
cluster = currentCluster+1;
}
}
#ifdef __WIN__
#ifdef _MSC_VER
}
__finally
{
Expand Down Expand Up @@ -865,7 +904,7 @@ void FatSystem::rewriteUnallocated(bool random)
srand(time(NULL));
for (int cluster=0; cluster<totalClusters; cluster++) {
if (freeCluster(cluster)) {
#ifndef __WIN__
#ifndef _MSC_VER
char buffer[bytesPerCluster];
#else
char *buffer = new char[bytesPerCluster];
Expand All @@ -881,7 +920,7 @@ void FatSystem::rewriteUnallocated(bool random)
}
writeData(clusterAddress(cluster), buffer, sizeof(buffer));
total++;
#ifdef __WIN__
#ifdef _MSC_VER
}
__finally
{
Expand Down
12 changes: 8 additions & 4 deletions src/core/FatSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include <map>
#include <vector>
#include <string>
#ifdef _WIN32
#include <windows.h>
#endif

#include "FatEntry.h"
#include "FatPath.h"
Expand All @@ -13,10 +16,7 @@
#define O_LARGEFILE 0
#define lseek64 lseek
#endif
#ifdef __WIN__
#define O_LARGEFILE 0
#define lseek64 lseek
#endif

using namespace std;

// Last cluster
Expand Down Expand Up @@ -114,7 +114,11 @@ class FatSystem
// File descriptor
string filename;
unsigned long long globalOffset;
#ifdef _WIN32
HANDLE fd;
#else
int fd;
#endif
bool writeMode;

// Header values
Expand Down
Loading