Skip to content

Commit

Permalink
Merge branch 'master' into debian
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregwar committed Jan 18, 2022
2 parents d05669b + 99cb99f commit 1665af4
Show file tree
Hide file tree
Showing 22 changed files with 1,030 additions and 294 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
language: cpp
arch:
- amd64
- ppc64le

compiler:
- gcc
Expand Down
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
cmake_minimum_required(VERSION 2.8)
project(fatcat)

OPTION(DEFINE_WIN "Compiling windows" OFF)

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

xgetopt/xgetopt.cpp
xgetopt/stdafx.cpp
xgetopt/argcargv.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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ Fatcat takes an image as argument:
fatcat disk.img [options]
```

**NOTE: according to your build, you might have to specify options before ``disk.img``, i.e
``fatcat [options] disk.img``**

You can specify an offset in the file with `-O`, this could be useful if there is
multiple partitions on a block devices, for instance:

Expand Down
5 changes: 5 additions & 0 deletions build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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
7 changes: 7 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh
if [ ! -d build ]; then
mkdir build
fi
cd build
git clean -fdx && cmake .. && make
cd ..
2 changes: 1 addition & 1 deletion man/fatcat.1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ npage for fatcat
..

.\" Contact g.passault@gmail.com to correct errors or typos.
.TH man 1 "07 August 2014" "1.0.5" "fatcat man page"
.TH man 1 "28 October 2017" "1.0.6" "fatcat man page"
.SH NAME
fatcat \- FAT filesystem explore, extract, repair, and forensic tool
.SH SYNOPSIS
Expand Down
31 changes: 18 additions & 13 deletions src/FatUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
#include <string>
#include <algorithm>
#include <functional>
#ifdef __WIN__
#include <ctype.h>
#else

#endif

using namespace std;

Expand All @@ -26,20 +31,20 @@ using namespace std;
buffer[x+3] = ((l)>>24)&0xff;

// trim from start
static inline std::string ltrim(std::string s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
static inline string ltrim(string s) {
s.erase(s.begin(), find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace))));
return s;
}

// trim from end
static inline std::string rtrim(std::string s) {
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
static inline string rtrim(string s) {
s.erase(find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(), s.end());
return s;
}

// trim from both ends
static inline std::string trim(std::string s) {
return ltrim(rtrim(s));
static inline string trim(string s) {
return ltrim(rtrim(s));
}

// split a string into vector
Expand All @@ -65,17 +70,17 @@ static inline string prettySize(unsigned long long bytes)
n++;
}

ostringstream oss;
oss << size << units[n];
ostringstream oss;
oss << size << units[n];

return oss.str();
return oss.str();
}

static inline std::string strtolower(std::string myString)
static inline string strtolower(string myString)
{
const int length = myString.length();
for(int i=0; i!=length; ++i) {
myString[i] = std::tolower(myString[i]);
myString[i] = tolower(myString[i]);
}

return myString;
Expand Down
40 changes: 38 additions & 2 deletions src/analysis/FatExtract.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
#include <errno.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <sys/stat.h>
#ifndef __WIN__
#include <sys/time.h>
#endif
#include <sys/types.h>
#include "FatExtract.h"
#ifdef __WIN__
#include <windows.h>
#endif

using namespace std;

FatExtract::FatExtract(FatSystem &system)
: FatWalk(system)
{
}

void FatExtract::onDirectory(FatEntry &parent, FatEntry &entry, string name)
{
string directory = targetDirectory + "/" + name;
#ifdef __WIN__
CreateDirectory(directory.c_str(), NULL);
#else
mkdir(directory.c_str(), 0755);
#endif
}

void FatExtract::onEntry(FatEntry &parent, FatEntry &entry, string name)
Expand All @@ -33,9 +45,33 @@ void FatExtract::onEntry(FatEntry &parent, FatEntry &entry, string name)
FILE *output = fopen(target.c_str(), "w+");
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
// more than 2038), so it's theoretically possible that on a dated
// system with 4-byte time_t some dates cannot be represented.
// Too bad.
cerr << "! Unable to set file timestamps for " << name
<< ": value cannot be represented" << endl;
} else {
struct timeval times[2];
// Modification time
times[1].tv_sec = mtime;
times[1].tv_usec = 0;
// Access time
times[0] = times[1];

if (utimes(target.c_str(), times) != 0) {
cerr << "! Unable to set file timestamps for " << name
<< ": " << strerror(errno) << endl;
}
}
#endif
}
}

void FatExtract::extract(unsigned int cluster, string directory, bool erased)
{
walkErased = erased;
Expand Down
35 changes: 35 additions & 0 deletions src/core/FatDate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <sstream>
#include <iostream>
#include <string>
#include <time.h>

#include <FatUtils.h>
#include "FatDate.h"
Expand Down Expand Up @@ -33,3 +34,37 @@ 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
* current timezone not in UTC. So what this function returns depends on
* the current timezone.
*/
time_t FatDate::timestamp() const
{
struct tm tm;
tm.tm_sec = s; // Seconds (0-60)
tm.tm_min = i; // Minutes (0-59)
tm.tm_hour = h; // Hours (0-23)
tm.tm_mday = d; // Day of the month (1-31)
tm.tm_mon = m - 1; // Month (0-11)
tm.tm_year = y - 1900; // Year - 1900

// A negative value of tm_isdst means that mktime() should (use timezone
// information and system databases to) attempt to determine whether DST
// is in effect at the specified time.
tm.tm_isdst = -1; // Daylight saving time

return mktime(&tm);
}
#endif

string FatDate::isoFormat()
{
char buffer[128];
sprintf(buffer, "%04d-%02d-%02dT%02d:%02d:%02d", y, m, d, h, i, s);

return string(buffer);
}
8 changes: 8 additions & 0 deletions src/core/FatDate.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#define _FATCAT_FATDATE_H

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

using namespace std;

Expand All @@ -15,6 +18,11 @@ class FatDate
int y, m, d;

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

string isoFormat();
};

#endif // _FATCAT_FATDATE_H
27 changes: 16 additions & 11 deletions src/core/FatEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,27 @@ string FatEntry::getFilename()
if (longName != "") {
return longName;
} else {
string name;
string ext = trim(shortName.substr(8,3));
string base = trim(shortName.substr(0,8));
return getShortFilename();
}
}

if (isErased()) {
base = base.substr(1);
}
string FatEntry::getShortFilename()
{
string name;
string ext = trim(shortName.substr(8,3));
string base = trim(shortName.substr(0,8));

name = base;
if (isErased()) {
base = base.substr(1);
}

if (ext != "") {
name += "." + ext;
}
name = base;

return name;
if (ext != "") {
name += "." + ext;
}

return name;
}

bool FatEntry::isDirectory()
Expand Down
1 change: 1 addition & 0 deletions src/core/FatEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class FatEntry
FatEntry();

string getFilename();
string getShortFilename();
bool isDirectory();
bool isHidden();
bool isErased();
Expand Down
Loading

0 comments on commit 1665af4

Please sign in to comment.