Merge pull request #10 from Antidote/file-dir-api

File and Dir API
This commit is contained in:
Phillip 2015-05-20 22:54:27 -07:00
commit 7785983093
26 changed files with 3250 additions and 68 deletions

View File

@ -61,3 +61,5 @@ win32 {
headerFiles.files = $$PWD/include/*
INSTALLS += libFiles headerFiles
}

View File

@ -21,7 +21,10 @@ SOURCES += \
$$PWD/src/LZ77/LZLookupTable.cpp \
$$PWD/src/LZ77/LZType10.cpp \
$$PWD/src/LZ77/LZType11.cpp \
$$PWD/src/LZ77/LZBase.cpp
$$PWD/src/LZ77/LZBase.cpp \
$$PWD/src/Athena/FileInfo.cpp \
$$PWD/src/Athena/Dir.cpp \
$$PWD/src/gekko_support.c
win32:SOURCES += \
$$PWD/src/win32_largefilewrapper.c
@ -56,11 +59,13 @@ HEADERS += \
$$PWD/include/utf8.h \
$$PWD/include/utf8/checked.h \
$$PWD/include/utf8/core.h \
$$PWD/include/utf8/unchecked.h
$$PWD/include/utf8/unchecked.h \
$$PWD/include/Athena/FileInfo.hpp \
$$PWD/include/Athena/Dir.hpp \
$$PWD/include/gekko_support.h
win32:HEADERS += \
$$PWD/include/win32_largefilewrapper.h
mac:HEADERS += \
$$PWD/include/osx_largefilewrapper.h

2413
Doxyfile Normal file

File diff suppressed because it is too large Load Diff

View File

@ -3,10 +3,6 @@ SOURCES += \
$$PWD/compress.c \
$$PWD/crc32.c \
$$PWD/deflate.c \
$$PWD/gzclose.c \
$$PWD/gzlib.c \
$$PWD/gzread.c \
$$PWD/gzwrite.c \
$$PWD/infback.c \
$$PWD/inffast.c \
$$PWD/inflate.c \

34
include/Athena/Dir.hpp Normal file
View File

@ -0,0 +1,34 @@
#ifndef DIR_HPP
#define DIR_HPP
#include "Athena/FileInfo.hpp"
#include <stdio.h>
namespace Athena
{
class Dir
{
public:
explicit Dir(const std::string& path);
std::string absolutePath() const;
static inline std::string absolutePath(const std::string& path)
{ return Dir(path).absolutePath(); }
bool isDir() const;
static bool isDir(const std::string dir)
{ return Dir(dir).isDir(); }
std::vector<FileInfo> files() const;
bool cd(const std::string& path);
bool rm(const std::string& path);
bool touch();
static bool mkdir(const std::string& dir, mode_t mode = 0755);
static bool mkpath(const std::string& path, mode_t mode = 0755);
private:
std::string m_path;
};
}
#endif // DIR_HPP

View File

@ -0,0 +1,59 @@
#ifndef FILEINFO_HPP
#define FILEINFO_HPP
#include <string>
#include "Athena/Global.hpp"
namespace Athena
{
class FileInfo
{
public:
explicit FileInfo(const std::string& path = std::string());
std::string absolutePath() const;
static inline std::string absolutePath(const std::string& lnk)
{ return FileInfo(lnk).absolutePath(); }
std::string absoluteFilePath() const;
static inline std::string absoluteFilePath(const std::string& path)
{ return FileInfo(path).absoluteFilePath(); }
std::string filename() const;
static inline std::string filename(const std::string path)
{ return FileInfo(path).filename(); }
std::string path() const;
static inline std::string path(const std::string path)
{ return FileInfo(path).path(); }
std::string extension() const;
static inline std::string extension(const std::string path)
{ return FileInfo(path).extension(); }
atUint64 size() const;
static inline atUint64 size(const std::string path)
{ return FileInfo(path).size(); }
bool exists() const;
static inline bool exists(const std::string& path)
{ return FileInfo(path).exists(); }
bool isLink() const;
static inline bool isLink(const std::string& lnk)
{ return FileInfo(lnk).isLink(); }
bool isFile() const;
static inline bool isFile(const std::string& path)
{ return FileInfo(path).isFile(); }
bool touch() const;
static inline bool touch(const std::string& path)
{ return FileInfo(path).touch(); }
private:
std::string m_path;
};
}
#endif // FILEINFO_HPP

View File

@ -18,7 +18,7 @@
#include "Athena/IStreamReader.hpp"
#include <string>
#include <cstdio>
#include <stdio.h>
namespace Athena
{

View File

@ -17,6 +17,7 @@
#define FILEWRITER_HPP
#include "Athena/IStreamWriter.hpp"
#include <stdio.h>
namespace Athena
{
@ -25,17 +26,16 @@ namespace io
class FileWriter : public IStreamWriter
{
public:
FileWriter(const std::string& filename);
FileWriter(const std::string& filename, bool overwrite = true);
virtual ~FileWriter();
void setEndian(Endian endian);
Endian endian() const;
bool isBigEndian() const;
bool isLittleEndian() const;
void open();
void open(bool overwrite = true);
void close();
bool isOpen() const;
bool save();
void seek(atInt64 pos, SeekOrigin origin = SeekOrigin::Current);
inline void seekAlign32() {seek(ROUND_UP_32(position()), SeekOrigin::Begin);}
bool atEnd() const;

View File

@ -40,6 +40,15 @@
# endif
#endif
#ifdef GEKKO
#include "gekko_support.h"
typedef struct stat stat64_t;
#define stat64 stat
#else
typedef struct stat64 stat64_t;
#endif
#ifndef aDebug
#define aDebug() \
std::cout << __FILE__ << "(" << __LINE__ << ") " << AT_PRETTY_FUNCTION << ": "

View File

@ -75,13 +75,13 @@ inline atInt64 swap64(atInt64 val)
inline atUint64 swapU64(atUint64 val) {return (atUint64)swap64(val);}
inline float swapFloat(float val)
{
atInt32 ival = swap32(*((atInt32*)(&val)));
return *((float*)(&ival));
atInt32 ival = swap64(static_cast<atInt32>(val));
return static_cast<float>(ival);
}
inline double swapDouble(double val)
{
atInt64 ival = swap64(*((atInt64*)(&val)));
return *((double*)(&ival));
atInt64 ival = swap64(static_cast<atInt64>(val));
return static_cast<double>(ival);
}
inline atInt16 LittleInt16(atInt16& val)
{
@ -203,8 +203,9 @@ inline double BigDouble(double& val)
return val;
}
void fillRandom(atUint8* rndArea, atUint64 count);
std::vector<std::string> split(const std::string& s, char delim);
void fillRandom(atUint8 * rndArea, atUint64 count);
std::vector<std::string> split(const std::string &s, char delim);
atUint64 rand64();
std::string join(const std::vector<std::string>& elems, const std::string& delims);
void tolower(std::string& str);
void toupper(std::string& str);

View File

@ -166,19 +166,61 @@ public:
*/
bool isFile() const;
/*!
* \brief addChild
* \param file
*/
void addChild(WiiFile* file);
/*!
* \brief children
* \return
*/
std::vector<WiiFile*> children();
/*!
* \brief child
* \param name
* \return
*/
WiiFile* child(const std::string& name);
/*!
* \brief removeChild
* \param name
*/
void removeChild(const std::string& name);
/*!
* \brief removeChild
* \param file
*/
void removeChild(WiiFile* file);
/*!
* \brief parent
* \return
*/
WiiFile* parent();
/*!
* \brief setParent
* \param parent
*/
void setParent(WiiFile* parent);
/*!
* \brief fileCount
* \return
*/
atUint32 fileCount();
/*!
* \brief allChildren
* \return
*/
std::vector<WiiFile*> allChildren();
/*!
* \brief fullpath
* \return
*/
std::string fullpath();
protected:
private:

21
include/gekko_support.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef GEKKO_SUPPORT_H
#define GEKKO_SUPPORT_H
#ifdef GEKKO
#include <stdarg.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
char *
realpath(const char *path, char *resolved);
int
vsnprintf (char *s, size_t n, const char *format, va_list ap);
#ifdef __cplusplus
}
#endif
#endif
#endif // GEKKO_SUPPORT_H

View File

@ -14,10 +14,10 @@
// along with libAthena. If not, see <http://www.gnu.org/licenses/>
#include "Athena/Checksums.hpp"
#include "Athena/FileReader.hpp"
#include <iostream>
#include <iomanip>
namespace Athena
{
namespace Checksums

View File

@ -358,32 +358,18 @@ atUint32 simpleEnc(const atUint8* src, atInt32 size, atInt32 pos, atUint32* pMat
atUint32 decompressLZ77(const atUint8* src, atUint32 srcLen, atUint8** dst)
{
LZBase* lzCodec;
if (*(atUint8*)src == 0x11)
lzCodec = new LZType11;
else
lzCodec = new LZType10;
return LZType11().decompress(src, dst, srcLen);
atUint32 retLength = lzCodec->decompress(src, dst, srcLen);
delete lzCodec;
return retLength;
return LZType10(2).decompress(src, dst, srcLen);
}
atUint32 compressLZ77(const atUint8* src, atUint32 srcLen, atUint8** dst, bool extended)
{
LZBase* lzCodec;
if (extended)
lzCodec = new LZType11;
else
lzCodec = new LZType10(2);
return LZType11().compress(src, dst, srcLen);
atUint32 retLength = lzCodec->compress(src, dst, srcLen);
delete lzCodec;
return retLength;
return LZType10(2).compress(src, dst, srcLen);
}
} // Compression

96
src/Athena/Dir.cpp Normal file
View File

@ -0,0 +1,96 @@
#include "Athena/Dir.hpp"
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#ifdef _MSC_VER
#define stat64 __stat64
#define realpath(__name, __resolved) _fullpath((__name), (__resolved), 4096)
#endif
namespace Athena
{
Dir::Dir(const std::string &path)
: m_path(path)
{
}
std::string Dir::absolutePath() const
{
return FileInfo(m_path).absoluteFilePath();
}
bool Dir::isDir() const
{
stat64_t st;
int e = stat64(m_path.c_str(), &st);
if (e < 0)
return false;
return (S_ISDIR(st.st_mode));
}
bool Dir::cd(const std::string& path)
{
Dir tmp(path);
if (tmp.isDir())
{
m_path = path;
return true;
}
return false;
}
bool Dir::rm(const std::string& path)
{
return !(remove((m_path + "/" + path).c_str()) < 0);
}
bool Dir::touch()
{
srand(time(NULL));
atUint64 tmp = utility::rand64();
std::string tmpFile = utility::sprintf("%" PRIX64 ".tmp", tmp);
bool ret = FileInfo(m_path + "/" + tmpFile).touch();
if (ret)
return rm(tmpFile);
return false;
}
bool Dir::mkdir(const std::string& dir, mode_t mode)
{
return !(::mkdir(dir.c_str(), mode) < 0);
}
bool Dir::mkpath(const std::string& path, mode_t mode)
{
std::vector<std::string> dirs = utility::split(path, '/');
if (dirs.empty())
dirs = utility::split(path, '\\');
if (dirs.empty())
return false;
bool ret = false;
std::string newPath;
for (const std::string& dir : dirs)
{
if (dir.size() == 2 && dir[1] == ':')
{
newPath += dir + "//";
continue;
}
newPath += "/" + dir;
ret = mkdir(newPath, mode);
}
// we only care if the last directory was created
return ret;
}
}

197
src/Athena/FileInfo.cpp Normal file
View File

@ -0,0 +1,197 @@
#include "Athena/FileInfo.hpp"
#include "Athena/Utility.hpp"
#include "Athena/FileWriter.hpp"
#include "Athena/FileReader.hpp"
#include <sys/time.h>
#include <time.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <limits.h>
#include <stdlib.h>
#if !(defined(HW_DOL) || defined(HW_RVL) || defined(_WIN32))
#include <fcntl.h>
#endif
#ifdef _WIN32
#include <windows.h>
#include <wchar.h>
#endif
#ifdef _MSC_VER
#include <functional>
#include <locale>
#define stat64 __stat64
#define realpath(__name, __resolved) _fullpath((__name), (__resolved), 4096)
#endif
namespace Athena
{
FileInfo::FileInfo(const std::string& path)
: m_path(path)
{
}
std::string FileInfo::absolutePath() const
{
std::string path = absoluteFilePath();
size_t pos = path.find_last_of('/');
if (pos == std::string::npos)
pos = path.find_last_of('\\');
if (pos == std::string::npos)
return path;
return path.substr(0, pos+1);
}
std::string FileInfo::absoluteFilePath() const
{
char ret[4096];
realpath(m_path.c_str(), ret);
return ret;
}
std::string FileInfo::filename() const
{
size_t pos = m_path.find_last_of('/');
if (pos == std::string::npos)
pos = m_path.find_last_of('\\');
if (pos == std::string::npos)
return m_path;
return m_path.substr(pos + 1);
}
std::string FileInfo::extension() const
{
size_t pos = m_path.find_last_of('.');
if (pos == std::string::npos)
return std::string();
return m_path.substr(pos + 1);
}
atUint64 FileInfo::size() const
{
return utility::fileSize(m_path);
}
bool FileInfo::exists() const
{
stat64_t st;
int e = stat64(m_path.c_str(), &st);
if (e < 0)
return false;
return (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode));
}
bool FileInfo::isLink() const
{
stat64_t st;
int e = stat64(m_path.c_str(), &st);
if (e < 0)
return false;
return (S_ISLNK(st.st_mode));
}
bool FileInfo::isFile() const
{
stat64_t st;
int e = stat64(m_path.c_str(), &st);
if (e < 0)
return false;
return (S_ISREG(st.st_mode));
}
bool FileInfo::touch() const
{
#if defined(__GNUC__) && !(defined(HW_DOL) || defined(HW_RVL) || defined(GEKKO))
stat64_t st;
if (stat64(m_path.c_str(), &st) < 0) {
(void)Athena::io::FileWriter(m_path);
return true;
}
if (utimes(m_path.c_str(), NULL) < 0) {
return false;
}
#elif defined(_WIN32)
FILETIME modtime;
SYSTEMTIME st;
HANDLE fh;
wchar_t date[80], time[80];
fh = CreateFileW(path, GENERIC_READ | FILE_WRITE_ATTRIBUTES, 0, NULL, CREATE_NEW, 0, NULL);
if (fh == INVALID_HANDLE_VALUE)
return false;
/*
* Use GetFileTime() to get the file modification time.
*/
if (GetFileTime(fh, NULL, NULL, &modtime) == 0)
{
CloseHandle(fh);
return false;
}
FileTimeToSystemTime(&modtime, &st);
if (GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, NULL, date, sizeof date / sizeof date[0]) == 0 ||
GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, NULL, time, sizeof time / sizeof time[0]) == 0)
{
CloseHandle(fh);
return false;
}
/*
* Use SetFileTime() to change the file modification time
* to the current time.
*/
GetSystemTime(&st);
if (GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, NULL, date, sizeof date / sizeof date[0]) == 0 ||
GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, NULL, time, sizeof time / sizeof time[0]) == 0)
{
CloseHandle(fh);
return false;
}
SystemTimeToFileTime(&st, &modtime);
if (SetFileTime(fh, NULL, NULL, &modtime) == 0)
{
CloseHandle(fh);
return false;
}
CloseHandle(fh);
#elif (defined(HW_RVL) || defined(HW_DOL)) && defined(GEKKO)
// Generic portable version, not extremely reliable but does work
atUint64 val = 0xCDCDCDCDCD;
try
{
Athena::io::FileReader reader(m_path.c_str());
val = reader.readUint64();
}
catch(...)
{
}
try
{
Athena::io::FileWriter writer(m_path, false);
if (val != 0xCDCDCDCDCD)
writer.writeUint64(val);
}
catch(...)
{
return false;
}
#endif
return true;
}
}

View File

@ -41,7 +41,8 @@ FileReader::FileReader(const std::string& filename)
FileReader::~FileReader()
{
close();
if (isOpen())
close();
}
std::string FileReader::filename() const

View File

@ -31,7 +31,7 @@ namespace Athena
{
namespace io
{
FileWriter::FileWriter(const std::string& filename)
FileWriter::FileWriter(const std::string& filename, bool overwrite)
: m_filename(filename),
m_fileHandle(NULL),
m_endian(Endian::LittleEndian),
@ -39,7 +39,7 @@ FileWriter::FileWriter(const std::string& filename)
m_bitShift(0),
m_bitValid(false)
{
open();
open(overwrite);
}
FileWriter::~FileWriter()
@ -68,9 +68,12 @@ bool FileWriter::isLittleEndian() const
return (m_endian == Endian::LittleEndian);
}
void FileWriter::open()
void FileWriter::open(bool overwrite)
{
m_fileHandle = fopen(m_filename.c_str(), "w+b");
if (overwrite)
m_fileHandle = fopen(m_filename.c_str(), "w+b");
else
m_fileHandle = fopen(m_filename.c_str(), "r+b");
if (!m_fileHandle)
THROW_FILE_NOT_FOUND_EXCEPTION(m_filename);
@ -94,11 +97,6 @@ bool FileWriter::isOpen() const
return m_fileHandle != NULL;
}
bool FileWriter::save()
{
return true;
}
void FileWriter::seek(atInt64 pos, SeekOrigin origin)
{
if (fseeko64(m_fileHandle, pos, (int)origin) != 0)

View File

@ -499,11 +499,7 @@ void MemoryReader::loadData()
rewind(in);
length = utility::fileSize(m_filepath);
#ifdef HW_RVL
m_data = (Uint8*)memalign(32, length);
#else
m_data = new atUint8[length];
#endif
atUint64 done = 0;
atUint64 blocksize = BLOCKSZ;

View File

@ -495,12 +495,7 @@ void MemoryWriter::resize(atUint64 newSize)
THROW_INVALID_OPERATION_EXCEPTION("Stream::resize() -> New size cannot be less to the old size.");
// Allocate and copy new buffer
#ifdef HW_RVL
Uint8* newArray = (Uint8*)memalign(32, newSize);
#else
atUint8* newArray = new atUint8[newSize];
#endif
memset(newArray, 0, newSize);
if (m_data)
@ -508,11 +503,7 @@ void MemoryWriter::resize(atUint64 newSize)
memcpy(newArray, m_data, m_length);
// Delete the old one
#ifdef HW_RVL
free(m_data);
#else
delete[] m_data;
#endif
}
// Swap the pointer and size out for the new ones.

View File

@ -19,10 +19,13 @@
#include <stdlib.h>
#include <sstream>
#include <algorithm>
#include <cstdarg>
#include <stdarg.h>
#include <iterator>
#include <cstdio>
#include <sys/types.h>
#include <sys/stat.h>
#include <Athena/Exception.hpp>
#include <random>
#ifdef _MSC_VER
#include <functional>
@ -37,7 +40,7 @@ namespace utility
bool isSystemBigEndian()
{
static atUint8* test = (atUint8*)"\xFE\xFF";
static const atUint8* test = (atUint8*)"\xFE\xFF";
return (*(atUint16*)test == 0xFEFF);
}
@ -118,7 +121,7 @@ bool parseBool(const std::string& boolean, bool* valid)
std::string val = boolean;
// compare must be case insensitive
// This is the cleanest solution since I only need to do it once
std::transform(val.begin(), val.end(), val.begin(), ::tolower);
tolower(val);
// Check for true first
if (!val.compare("true") || !val.compare("1") || !val.compare("yes") || !val.compare("on"))
@ -170,7 +173,7 @@ int countChar(const std::string& str, const char chr, int* lastOccur)
atUint64 fileSize(const std::string& filename)
{
struct stat64 st;
stat64_t st;
stat64(filename.c_str(), &st);
return st.st_size;
}
@ -202,5 +205,15 @@ std::string& trim(std::string& s)
return s;
}
atUint64 rand64()
{
// Combine 4 parts of low 16-bit of each rand()
atUint64 r0 = (atUint64)rand() << 48;
atUint64 r1 = (atUint64)rand() << 48 >> 16;
atUint64 r2 = (atUint64)rand() << 48 >> 32;
atUint64 r3 = (atUint64)rand() << 48 >> 48;
return r0 | r1 | r2 | r3;
}
} // utility
} // Athena

View File

@ -23,6 +23,7 @@
#include "Athena/IOException.hpp"
#include "Athena/InvalidOperationException.hpp"
#include "Athena/InvalidDataException.hpp"
#include "Athena/FileWriter.hpp"
#include "md5.h"
#include "aes.h"
#include "ec.h"
@ -74,7 +75,7 @@ WiiSave* WiiSaveReader::readSave()
if (bkMagic != 0x426B0001)
THROW_INVALID_DATA_EXCEPTION_RETURN(nullptr, "Invalid BacKup header magic");
atUint32 ngId = base::readUint32();
/*atUint32 ngId =*/ base::readUint32();
atUint32 numFiles = base::readUint32();
/*int fileSize =*/ base::readUint32();
@ -124,13 +125,14 @@ WiiBanner* WiiSaveReader::readBanner()
atUint8 permissions;
atUint8 md5[16];
atUint8 md5Calc[16];
atUint8 tmpIV[26];
atUint8 tmpIV[16];
memcpy(tmpIV, SD_IV, 16);
std::cout << "Decrypting: banner.bin...";
aes_set_key(SD_KEY);
aes_decrypt(tmpIV, data, dec, 0xF0C0);
std::cout << "done" << std::endl;
memset(md5, 0, 16);
memset(md5Calc, 0, 16);
// Read in the MD5 sum
@ -328,7 +330,7 @@ void WiiSaveReader::readCerts(atUint32 totalSize)
WiiFile* WiiSaveReader::buildTree(std::vector<WiiFile*> files)
{
// This is simply a virtual root that will contain all the other nodes
WiiFile* root = new WiiFile("/");
WiiFile* root = new WiiFile("");
root->setType(WiiFile::Directory);
for (WiiFile* f : files)

View File

@ -1,7 +1,7 @@
#include "LZ77/LZLookupTable.hpp"
#include "LZ77/LZType10.hpp"
#include <Athena/MemoryWriter.hpp>
#include <memory.h>
#include <string.h>
LZType10::LZType10(atInt32 MinimumOffset, atInt32 SlidingWindow, atInt32 MinimumMatch, atInt32 BlockSize)
: LZBase(MinimumOffset, SlidingWindow, MinimumMatch, BlockSize)

View File

@ -1,7 +1,7 @@
#include "LZ77/LZLookupTable.hpp"
#include "LZ77/LZType11.hpp"
#include <Athena/MemoryWriter.hpp>
#include <memory.h>
#include <string.h>
LZType11::LZType11(atInt32 minimumOffset, atInt32 slidingWindow, atInt32 minimumMatch, atInt32 blockSize)

317
src/gekko_support.c Normal file
View File

@ -0,0 +1,317 @@
#if defined(GEKKO)
#include "gekko_support.h"
#define SYMLOOP_MAX 8
#include <sys/stat.h>
#include <sys/param.h>
#include <stddef.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
/* A slightly modified copy of this file exists in libexec/ld.so */
/*
* char *realpath(const char *path, char resolved[PATH_MAX]);
*
* Find the real name of path, by removing all ".", ".." and symlink
* components. Returns (resolved) on success, or (NULL) on failure,
* in which case the path which caused trouble is left in (resolved).
*/
char *
realpath(const char *path, char *resolved)
{
struct stat sb;
char *p, *q, *s;
size_t left_len, resolved_len;
unsigned symlinks;
int serrno, slen, mem_allocated;
char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
if (path[0] == '\0') {
errno = ENOENT;
return (NULL);
}
serrno = errno;
if (resolved == NULL) {
resolved = malloc(PATH_MAX);
if (resolved == NULL)
return (NULL);
mem_allocated = 1;
} else
mem_allocated = 0;
symlinks = 0;
if (path[0] == '/') {
resolved[0] = '/';
resolved[1] = '\0';
if (path[1] == '\0')
return (resolved);
resolved_len = 1;
left_len = strlcpy(left, path + 1, sizeof(left));
} else {
if (getcwd(resolved, PATH_MAX) == NULL) {
if (mem_allocated)
free(resolved);
else
strlcpy(resolved, ".", PATH_MAX);
return (NULL);
}
resolved_len = strlen(resolved);
left_len = strlcpy(left, path, sizeof(left));
}
if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) {
errno = ENAMETOOLONG;
goto err;
}
/*
* Iterate over path components in `left'.
*/
while (left_len != 0) {
/*
* Extract the next path component and adjust `left'
* and its length.
*/
p = strchr(left, '/');
s = p ? p : left + left_len;
if (s - left >= sizeof(next_token)) {
errno = ENAMETOOLONG;
goto err;
}
memcpy(next_token, left, s - left);
next_token[s - left] = '\0';
left_len -= s - left;
if (p != NULL)
memmove(left, s + 1, left_len + 1);
if (resolved[resolved_len - 1] != '/') {
if (resolved_len + 1 >= PATH_MAX) {
errno = ENAMETOOLONG;
goto err;
}
resolved[resolved_len++] = '/';
resolved[resolved_len] = '\0';
}
if (next_token[0] == '\0')
continue;
else if (strcmp(next_token, ".") == 0)
continue;
else if (strcmp(next_token, "..") == 0) {
/*
* Strip the last path component except when we have
* single "/"
*/
if (resolved_len > 1) {
resolved[resolved_len - 1] = '\0';
q = strrchr(resolved, '/') + 1;
*q = '\0';
resolved_len = q - resolved;
}
continue;
}
/*
* Append the next path component and lstat() it. If
* lstat() fails we still can return successfully if
* there are no more path components left.
*/
resolved_len = strlcat(resolved, next_token, PATH_MAX);
if (resolved_len >= PATH_MAX) {
errno = ENAMETOOLONG;
goto err;
}
if (lstat(resolved, &sb) != 0) {
if (errno == ENOENT && p == NULL) {
errno = serrno;
return (resolved);
}
goto err;
}
if (S_ISLNK(sb.st_mode)) {
if (symlinks++ > SYMLOOP_MAX) {
errno = ELOOP;
goto err;
}
slen = readlink(resolved, symlink, sizeof(symlink) - 1);
if (slen < 0)
goto err;
symlink[slen] = '\0';
if (symlink[0] == '/') {
resolved[1] = 0;
resolved_len = 1;
} else if (resolved_len > 1) {
/* Strip the last path component. */
resolved[resolved_len - 1] = '\0';
q = strrchr(resolved, '/') + 1;
*q = '\0';
resolved_len = q - resolved;
}
/*
* If there are any path components left, then
* append them to symlink. The result is placed
* in `left'.
*/
if (p != NULL) {
if (symlink[slen - 1] != '/') {
if (slen + 1 >= sizeof(symlink)) {
errno = ENAMETOOLONG;
goto err;
}
symlink[slen] = '/';
symlink[slen + 1] = 0;
}
left_len = strlcat(symlink, left, sizeof(symlink));
if (left_len >= sizeof(left)) {
errno = ENAMETOOLONG;
goto err;
}
}
left_len = strlcpy(left, symlink, sizeof(left));
}
}
/*
* Remove trailing slash except when the resolved pathname
* is a single "/".
*/
if (resolved_len > 1 && resolved[resolved_len - 1] == '/')
resolved[resolved_len - 1] = '\0';
return (resolved);
err:
if (mem_allocated)
free(resolved);
return (NULL);
}
static int
int_vasprintf (result, format, args)
char **result;
const char *format;
va_list *args;
{
const char *p = format;
/* Add one to make sure that it is never zero, which might cause malloc
to return NULL. */
int total_width = strlen (format) + 1;
va_list ap;
memcpy ((void*) &ap, (void*) args, sizeof (va_list));
while (*p != '\0')
{
if (*p++ == '%')
{
while (strchr ("-+ #0", *p))
++p;
if (*p == '*')
{
++p;
total_width += abs (va_arg (ap, int));
}
else
total_width += strtoul (p, (char**)&p, 10);
if (*p == '.')
{
++p;
if (*p == '*')
{
++p;
total_width += abs (va_arg (ap, int));
}
else
total_width += strtoul (p, (char**)&p, 10);
}
while (strchr ("hlL", *p))
++p;
/* Should be big enough for any format specifier except %s and floats. */
total_width += 30;
switch (*p)
{
case 'd':
case 'i':
case 'o':
case 'u':
case 'x':
case 'X':
case 'c':
(void) va_arg (ap, int);
break;
case 'f':
case 'e':
case 'E':
case 'g':
case 'G':
(void) va_arg (ap, double);
/* Since an ieee double can have an exponent of 307, we'll
make the buffer wide enough to cover the gross case. */
total_width += 307;
break;
case 's':
total_width += strlen (va_arg (ap, char *));
break;
case 'p':
case 'n':
(void) va_arg (ap, char *);
break;
}
}
}
#ifdef TEST
global_total_width = total_width;
#endif
*result = malloc (total_width);
if (*result != NULL)
return vsprintf (*result, format, *args);
else
return 0;
}
int
vasprintf (result, format, args)
char **result;
const char *format;
#if defined (_BSD_VA_LIST_) && defined (__FreeBSD__)
_BSD_VA_LIST_ args;
#else
va_list args;
#endif
{
return int_vasprintf (result, format, &args);
}
int
vsnprintf (char *s, size_t n, const char *format, va_list ap)
{
char *buf = 0;
int result = vasprintf (&buf, format, ap);
if (!buf)
return -1;
if (result < 0)
{
free (buf);
return -1;
}
result = strlen (buf);
if (n > 0)
{
if ((long) n > result)
memcpy (s, buf, result+1);
else
{
memcpy (s, buf, n-1);
s[n - 1] = 0;
}
}
free (buf);
return result;
}
#endif

View File

@ -1,3 +1,4 @@
#ifdef __APPLE__
#include "osx_largefilewrapper.h"
#include <stdio.h>
@ -10,4 +11,6 @@ off_t ftello64(FILE* fp)
{
return ftello(fp);
}
#endif