2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2007-03-03 04:04:22 +00:00
|
|
|
/** @file fileio.cpp Standard In/Out file operations */
|
2007-03-01 01:24:44 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2008-08-31 10:50:05 +00:00
|
|
|
#include "fileio_func.h"
|
2005-07-21 18:44:27 +00:00
|
|
|
#include "variables.h"
|
2007-02-08 23:46:25 +00:00
|
|
|
#include "debug.h"
|
2007-03-12 15:25:33 +00:00
|
|
|
#include "fios.h"
|
2008-01-07 14:23:25 +00:00
|
|
|
#include "string_func.h"
|
2008-06-24 09:05:24 +00:00
|
|
|
#include "tar_type.h"
|
2007-06-17 15:48:57 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
2007-03-18 18:02:24 +00:00
|
|
|
#include <pwd.h>
|
2007-03-12 15:25:33 +00:00
|
|
|
#endif
|
2007-09-15 15:21:17 +00:00
|
|
|
#include <sys/stat.h>
|
2009-01-16 16:29:07 +00:00
|
|
|
#include <algorithm>
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/*************************************************/
|
|
|
|
/* FILE IO ROUTINES ******************************/
|
|
|
|
/*************************************************/
|
|
|
|
|
|
|
|
#define FIO_BUFFER_SIZE 512
|
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct Fio {
|
2007-10-29 23:02:31 +00:00
|
|
|
byte *buffer, *buffer_end; ///< position pointer in local buffer and last valid byte of buffer
|
2008-05-08 13:20:54 +00:00
|
|
|
size_t pos; ///< current (system) position in file
|
2007-10-29 23:02:31 +00:00
|
|
|
FILE *cur_fh; ///< current file handle
|
|
|
|
const char *filename; ///< current filename
|
|
|
|
FILE *handles[MAX_FILE_SLOTS]; ///< array of file handles we can have open
|
|
|
|
byte buffer_start[FIO_BUFFER_SIZE]; ///< local buffer when read from file
|
|
|
|
const char *filenames[MAX_FILE_SLOTS]; ///< array of filenames we (should) have open
|
2008-01-22 07:27:06 +00:00
|
|
|
char *shortnames[MAX_FILE_SLOTS];///< array of short names for spriteloader's use
|
2007-02-08 23:46:25 +00:00
|
|
|
#if defined(LIMITED_FDS)
|
2007-10-29 23:02:31 +00:00
|
|
|
uint open_handles; ///< current amount of open handles
|
|
|
|
uint usage_count[MAX_FILE_SLOTS]; ///< count how many times this file has been opened
|
2007-02-08 23:46:25 +00:00
|
|
|
#endif /* LIMITED_FDS */
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
static Fio _fio;
|
|
|
|
|
2007-03-01 01:24:44 +00:00
|
|
|
/* Get current position in file */
|
2008-05-27 21:41:00 +00:00
|
|
|
size_t FioGetPos()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-05-05 22:35:33 +00:00
|
|
|
return _fio.pos + (_fio.buffer - _fio.buffer_end);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-01-22 07:27:06 +00:00
|
|
|
const char *FioGetFilename(uint8 slot)
|
2007-06-13 16:21:11 +00:00
|
|
|
{
|
2008-01-22 07:27:06 +00:00
|
|
|
return _fio.shortnames[slot];
|
2007-06-13 16:21:11 +00:00
|
|
|
}
|
|
|
|
|
2008-05-27 21:41:00 +00:00
|
|
|
void FioSeekTo(size_t pos, int mode)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
if (mode == SEEK_CUR) pos += FioGetPos();
|
|
|
|
_fio.buffer = _fio.buffer_end = _fio.buffer_start + FIO_BUFFER_SIZE;
|
2006-07-31 22:26:11 +00:00
|
|
|
_fio.pos = pos;
|
|
|
|
fseek(_fio.cur_fh, _fio.pos, SEEK_SET);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-02-08 23:46:25 +00:00
|
|
|
#if defined(LIMITED_FDS)
|
|
|
|
static void FioRestoreFile(int slot)
|
|
|
|
{
|
|
|
|
/* Do we still have the file open, or should we reopen it? */
|
|
|
|
if (_fio.handles[slot] == NULL) {
|
2007-07-28 12:45:27 +00:00
|
|
|
DEBUG(misc, 6, "Restoring file '%s' in slot '%d' from disk", _fio.filenames[slot], slot);
|
|
|
|
FioOpenFile(slot, _fio.filenames[slot]);
|
2007-02-08 23:46:25 +00:00
|
|
|
}
|
|
|
|
_fio.usage_count[slot]++;
|
|
|
|
}
|
|
|
|
#endif /* LIMITED_FDS */
|
|
|
|
|
2007-03-01 01:24:44 +00:00
|
|
|
/* Seek to a file and a position */
|
2008-05-27 23:10:42 +00:00
|
|
|
void FioSeekToFile(uint8 slot, size_t pos)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-02-08 23:46:25 +00:00
|
|
|
FILE *f;
|
|
|
|
#if defined(LIMITED_FDS)
|
|
|
|
/* Make sure we have this file open */
|
2007-09-13 18:22:34 +00:00
|
|
|
FioRestoreFile(slot);
|
2007-02-08 23:46:25 +00:00
|
|
|
#endif /* LIMITED_FDS */
|
2007-09-13 18:22:34 +00:00
|
|
|
f = _fio.handles[slot];
|
2004-08-09 17:04:08 +00:00
|
|
|
assert(f != NULL);
|
|
|
|
_fio.cur_fh = f;
|
2007-09-13 18:22:34 +00:00
|
|
|
_fio.filename = _fio.filenames[slot];
|
|
|
|
FioSeekTo(pos, SEEK_SET);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
byte FioReadByte()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
if (_fio.buffer == _fio.buffer_end) {
|
2008-05-05 21:54:05 +00:00
|
|
|
_fio.buffer = _fio.buffer_start;
|
2008-05-05 22:35:33 +00:00
|
|
|
size_t size = fread(_fio.buffer, 1, FIO_BUFFER_SIZE, _fio.cur_fh);
|
|
|
|
_fio.pos += size;
|
|
|
|
_fio.buffer_end = _fio.buffer_start + size;
|
|
|
|
|
|
|
|
if (size == 0) return 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return *_fio.buffer++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FioSkipBytes(int n)
|
|
|
|
{
|
2006-02-01 07:36:15 +00:00
|
|
|
for (;;) {
|
2004-08-09 17:04:08 +00:00
|
|
|
int m = min(_fio.buffer_end - _fio.buffer, n);
|
|
|
|
_fio.buffer += m;
|
|
|
|
n -= m;
|
|
|
|
if (n == 0) break;
|
|
|
|
FioReadByte();
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
uint16 FioReadWord()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
byte b = FioReadByte();
|
|
|
|
return (FioReadByte() << 8) | b;
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
uint32 FioReadDword()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
uint b = FioReadWord();
|
|
|
|
return (FioReadWord() << 16) | b;
|
|
|
|
}
|
|
|
|
|
2008-05-08 13:20:54 +00:00
|
|
|
void FioReadBlock(void *ptr, size_t size)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
FioSeekTo(FioGetPos(), SEEK_SET);
|
2008-05-05 21:54:05 +00:00
|
|
|
_fio.pos += fread(ptr, 1, size, _fio.cur_fh);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-16 12:40:25 +00:00
|
|
|
static inline void FioCloseFile(int slot)
|
|
|
|
{
|
|
|
|
if (_fio.handles[slot] != NULL) {
|
2005-01-23 13:09:35 +00:00
|
|
|
fclose(_fio.handles[slot]);
|
2008-01-22 07:27:06 +00:00
|
|
|
|
|
|
|
free(_fio.shortnames[slot]);
|
|
|
|
_fio.shortnames[slot] = NULL;
|
|
|
|
|
2005-01-16 12:40:25 +00:00
|
|
|
_fio.handles[slot] = NULL;
|
2007-02-08 23:46:25 +00:00
|
|
|
#if defined(LIMITED_FDS)
|
|
|
|
_fio.open_handles--;
|
|
|
|
#endif /* LIMITED_FDS */
|
2005-01-16 12:40:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void FioCloseAll()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2005-01-16 12:40:25 +00:00
|
|
|
for (i = 0; i != lengthof(_fio.handles); i++)
|
|
|
|
FioCloseFile(i);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-02-08 23:46:25 +00:00
|
|
|
#if defined(LIMITED_FDS)
|
|
|
|
static void FioFreeHandle()
|
|
|
|
{
|
|
|
|
/* If we are about to open a file that will exceed the limit, close a file */
|
|
|
|
if (_fio.open_handles + 1 == LIMITED_FDS) {
|
|
|
|
uint i, count;
|
|
|
|
int slot;
|
|
|
|
|
|
|
|
count = UINT_MAX;
|
|
|
|
slot = -1;
|
|
|
|
/* Find the file that is used the least */
|
|
|
|
for (i = 0; i < lengthof(_fio.handles); i++) {
|
|
|
|
if (_fio.handles[i] != NULL && _fio.usage_count[i] < count) {
|
|
|
|
count = _fio.usage_count[i];
|
|
|
|
slot = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(slot != -1);
|
2007-07-28 12:45:27 +00:00
|
|
|
DEBUG(misc, 6, "Closing filehandler '%s' in slot '%d' because of fd-limit", _fio.filenames[slot], slot);
|
2007-02-08 23:46:25 +00:00
|
|
|
FioCloseFile(slot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* LIMITED_FDS */
|
|
|
|
|
2007-03-12 15:40:12 +00:00
|
|
|
void FioOpenFile(int slot, const char *filename)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
#if defined(LIMITED_FDS)
|
|
|
|
FioFreeHandle();
|
|
|
|
#endif /* LIMITED_FDS */
|
|
|
|
f = FioFOpenFile(filename);
|
2008-06-05 20:54:52 +00:00
|
|
|
if (f == NULL) usererror("Cannot open file '%s'", filename);
|
2007-09-13 18:22:34 +00:00
|
|
|
uint32 pos = ftell(f);
|
2007-03-12 15:40:12 +00:00
|
|
|
|
|
|
|
FioCloseFile(slot); // if file was opened before, close it
|
|
|
|
_fio.handles[slot] = f;
|
2007-06-13 16:21:11 +00:00
|
|
|
_fio.filenames[slot] = filename;
|
2008-01-22 07:27:06 +00:00
|
|
|
|
|
|
|
/* Store the filename without path and extension */
|
|
|
|
const char *t = strrchr(filename, PATHSEPCHAR);
|
|
|
|
_fio.shortnames[slot] = strdup(t == NULL ? filename : t);
|
|
|
|
char *t2 = strrchr(_fio.shortnames[slot], '.');
|
|
|
|
if (t2 != NULL) *t2 = '\0';
|
|
|
|
strtolower(_fio.shortnames[slot]);
|
|
|
|
|
2007-03-12 15:40:12 +00:00
|
|
|
#if defined(LIMITED_FDS)
|
|
|
|
_fio.usage_count[slot] = 0;
|
|
|
|
_fio.open_handles++;
|
|
|
|
#endif /* LIMITED_FDS */
|
2007-09-13 18:22:34 +00:00
|
|
|
FioSeekToFile(slot, pos);
|
2007-03-12 15:40:12 +00:00
|
|
|
}
|
|
|
|
|
2007-06-17 15:48:57 +00:00
|
|
|
const char *_subdirs[NUM_SUBDIRS] = {
|
|
|
|
"",
|
|
|
|
"save" PATHSEP,
|
|
|
|
"save" PATHSEP "autosave" PATHSEP,
|
|
|
|
"scenario" PATHSEP,
|
|
|
|
"scenario" PATHSEP "heightmap" PATHSEP,
|
|
|
|
"gm" PATHSEP,
|
|
|
|
"data" PATHSEP,
|
2009-01-12 17:11:45 +00:00
|
|
|
"lang" PATHSEP,
|
|
|
|
"ai" PATHSEP,
|
2009-01-14 15:20:06 +00:00
|
|
|
"ai" PATHSEP "library" PATHSEP,
|
2007-06-17 15:48:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const char *_searchpaths[NUM_SEARCHPATHS];
|
2007-09-16 18:10:52 +00:00
|
|
|
TarList _tar_list;
|
|
|
|
TarFileList _tar_filelist;
|
2007-06-17 15:48:57 +00:00
|
|
|
|
2008-07-13 16:09:00 +00:00
|
|
|
typedef std::map<std::string, std::string> TarLinkList;
|
|
|
|
static TarLinkList _tar_linklist; ///< List of directory links
|
|
|
|
|
2007-03-12 15:40:12 +00:00
|
|
|
/**
|
|
|
|
* Check whether the given file exists
|
|
|
|
* @param filename the file to try for existance
|
2007-06-17 15:48:57 +00:00
|
|
|
* @param subdir the subdirectory to look in
|
2007-03-12 15:40:12 +00:00
|
|
|
* @return true if and only if the file can be opened
|
|
|
|
*/
|
2007-06-17 15:48:57 +00:00
|
|
|
bool FioCheckFileExists(const char *filename, Subdirectory subdir)
|
2007-03-12 15:40:12 +00:00
|
|
|
{
|
2007-06-17 15:48:57 +00:00
|
|
|
FILE *f = FioFOpenFile(filename, "rb", subdir);
|
2007-03-12 15:40:12 +00:00
|
|
|
if (f == NULL) return false;
|
|
|
|
|
2007-09-16 18:10:52 +00:00
|
|
|
FioFCloseFile(f);
|
2007-03-12 15:40:12 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-09-16 18:10:52 +00:00
|
|
|
/**
|
|
|
|
* Close a file in a safe way.
|
|
|
|
*/
|
|
|
|
void FioFCloseFile(FILE *f)
|
|
|
|
{
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2007-06-17 15:48:57 +00:00
|
|
|
char *FioGetFullPath(char *buf, size_t buflen, Searchpath sp, Subdirectory subdir, const char *filename)
|
2005-12-10 11:16:45 +00:00
|
|
|
{
|
2007-06-17 15:48:57 +00:00
|
|
|
assert(subdir < NUM_SUBDIRS);
|
|
|
|
assert(sp < NUM_SEARCHPATHS);
|
|
|
|
|
2007-06-17 21:26:57 +00:00
|
|
|
snprintf(buf, buflen, "%s%s%s", _searchpaths[sp], _subdirs[subdir], filename);
|
2007-06-17 15:48:57 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *FioFindFullPath(char *buf, size_t buflen, Subdirectory subdir, const char *filename)
|
|
|
|
{
|
|
|
|
Searchpath sp;
|
|
|
|
assert(subdir < NUM_SUBDIRS);
|
|
|
|
|
|
|
|
FOR_ALL_SEARCHPATHS(sp) {
|
|
|
|
FioGetFullPath(buf, buflen, sp, subdir, filename);
|
|
|
|
if (FileExists(buf)) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *FioAppendDirectory(char *buf, size_t buflen, Searchpath sp, Subdirectory subdir)
|
|
|
|
{
|
|
|
|
assert(subdir < NUM_SUBDIRS);
|
|
|
|
assert(sp < NUM_SEARCHPATHS);
|
|
|
|
|
|
|
|
snprintf(buf, buflen, "%s%s", _searchpaths[sp], _subdirs[subdir]);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *FioGetDirectory(char *buf, size_t buflen, Subdirectory subdir)
|
|
|
|
{
|
|
|
|
Searchpath sp;
|
|
|
|
|
|
|
|
/* Find and return the first valid directory */
|
|
|
|
FOR_ALL_SEARCHPATHS(sp) {
|
|
|
|
char *ret = FioAppendDirectory(buf, buflen, sp, subdir);
|
|
|
|
if (FileExists(buf)) return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Could not find the directory, fall back to a base path */
|
|
|
|
ttd_strlcpy(buf, _personal_dir, buflen);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2007-09-13 18:46:29 +00:00
|
|
|
FILE *FioFOpenFileSp(const char *filename, const char *mode, Searchpath sp, Subdirectory subdir, size_t *filesize)
|
2007-06-17 15:48:57 +00:00
|
|
|
{
|
|
|
|
#if defined(WIN32) && defined(UNICODE)
|
|
|
|
/* fopen is implemented as a define with ellipses for
|
|
|
|
* Unicode support (prepend an L). As we are not sending
|
|
|
|
* a string, but a variable, it 'renames' the variable,
|
|
|
|
* so make that variable to makes it compile happily */
|
|
|
|
wchar_t Lmode[5];
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, mode, -1, Lmode, lengthof(Lmode));
|
|
|
|
#endif
|
|
|
|
FILE *f = NULL;
|
2005-12-10 11:16:45 +00:00
|
|
|
char buf[MAX_PATH];
|
|
|
|
|
2007-06-17 20:09:05 +00:00
|
|
|
if (subdir == NO_DIRECTORY) {
|
2008-11-02 11:20:15 +00:00
|
|
|
strecpy(buf, filename, lastof(buf));
|
2007-06-08 21:16:17 +00:00
|
|
|
} else {
|
2007-06-17 15:48:57 +00:00
|
|
|
snprintf(buf, lengthof(buf), "%s%s%s", _searchpaths[sp], _subdirs[subdir], filename);
|
2007-04-04 12:03:10 +00:00
|
|
|
}
|
2005-12-10 11:16:45 +00:00
|
|
|
|
2007-09-24 19:15:42 +00:00
|
|
|
#if defined(WIN32)
|
2007-09-25 14:15:38 +00:00
|
|
|
if (mode[0] == 'r' && GetFileAttributes(OTTD2FS(buf)) == INVALID_FILE_ATTRIBUTES) return NULL;
|
2007-09-24 19:15:42 +00:00
|
|
|
#endif
|
|
|
|
|
2007-06-17 15:48:57 +00:00
|
|
|
f = fopen(buf, mode);
|
2005-12-10 11:16:45 +00:00
|
|
|
#if !defined(WIN32)
|
|
|
|
if (f == NULL) {
|
2008-05-04 21:47:36 +00:00
|
|
|
strtolower(buf + ((subdir == NO_DIRECTORY) ? 0 : strlen(_searchpaths[sp]) - 1));
|
2007-06-17 15:48:57 +00:00
|
|
|
f = fopen(buf, mode);
|
2005-12-10 11:16:45 +00:00
|
|
|
}
|
|
|
|
#endif
|
2007-09-13 18:46:29 +00:00
|
|
|
if (f != NULL && filesize != NULL) {
|
|
|
|
/* Find the size of the file */
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
*filesize = ftell(f);
|
|
|
|
fseek(f, 0, SEEK_SET);
|
|
|
|
}
|
2007-06-17 15:48:57 +00:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2007-09-16 18:10:52 +00:00
|
|
|
FILE *FioFOpenFileTar(TarFileListEntry *entry, size_t *filesize)
|
2007-09-14 22:25:00 +00:00
|
|
|
{
|
2008-07-23 21:42:55 +00:00
|
|
|
FILE *f = fopen(entry->tar_filename, "rb");
|
2007-09-14 22:25:00 +00:00
|
|
|
assert(f != NULL);
|
|
|
|
|
2007-09-16 18:10:52 +00:00
|
|
|
fseek(f, entry->position, SEEK_SET);
|
|
|
|
if (filesize != NULL) *filesize = entry->size;
|
|
|
|
return f;
|
2007-09-14 22:25:00 +00:00
|
|
|
}
|
|
|
|
|
2007-06-17 15:48:57 +00:00
|
|
|
/** Opens OpenTTD files somewhere in a personal or global directory */
|
2007-09-13 18:50:42 +00:00
|
|
|
FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize)
|
2007-06-17 15:48:57 +00:00
|
|
|
{
|
|
|
|
FILE *f = NULL;
|
|
|
|
Searchpath sp;
|
|
|
|
|
2007-06-17 20:09:05 +00:00
|
|
|
assert(subdir < NUM_SUBDIRS || subdir == NO_DIRECTORY);
|
2007-06-17 15:48:57 +00:00
|
|
|
|
|
|
|
FOR_ALL_SEARCHPATHS(sp) {
|
2007-09-13 18:50:42 +00:00
|
|
|
f = FioFOpenFileSp(filename, mode, sp, subdir, filesize);
|
2007-06-17 20:09:05 +00:00
|
|
|
if (f != NULL || subdir == NO_DIRECTORY) break;
|
2007-06-17 15:48:57 +00:00
|
|
|
}
|
2007-09-16 18:10:52 +00:00
|
|
|
|
2007-09-14 22:25:00 +00:00
|
|
|
/* We can only use .tar in case of data-dir, and read-mode */
|
2009-03-04 09:53:31 +00:00
|
|
|
if (f == NULL && mode[0] == 'r') {
|
2008-07-13 16:09:00 +00:00
|
|
|
static const uint MAX_RESOLVED_LENGTH = 2 * (100 + 100 + 155) + 1; // Enough space to hold two filenames plus link. See 'TarHeader'.
|
|
|
|
char resolved_name[MAX_RESOLVED_LENGTH];
|
|
|
|
|
2007-09-16 18:10:52 +00:00
|
|
|
/* Filenames in tars are always forced to be lowercase */
|
2009-04-10 20:37:05 +00:00
|
|
|
strecpy(resolved_name, filename, lastof(resolved_name));
|
2008-07-13 16:09:00 +00:00
|
|
|
strtolower(resolved_name);
|
|
|
|
|
2008-08-13 14:52:54 +00:00
|
|
|
size_t resolved_len = strlen(resolved_name);
|
2008-07-13 16:09:00 +00:00
|
|
|
|
|
|
|
/* Resolve ONE directory link */
|
|
|
|
for (TarLinkList::iterator link = _tar_linklist.begin(); link != _tar_linklist.end(); link++) {
|
|
|
|
const std::string &src = link->first;
|
2008-08-13 14:52:54 +00:00
|
|
|
size_t len = src.length();
|
2008-07-13 19:44:05 +00:00
|
|
|
if (resolved_len >= len && resolved_name[len - 1] == PATHSEPCHAR && strncmp(src.c_str(), resolved_name, len) == 0) {
|
2008-07-13 16:09:00 +00:00
|
|
|
/* Apply link */
|
|
|
|
char resolved_name2[MAX_RESOLVED_LENGTH];
|
|
|
|
const std::string &dest = link->second;
|
2009-04-10 20:37:05 +00:00
|
|
|
strecpy(resolved_name2, &(resolved_name[len]), lastof(resolved_name2));
|
|
|
|
strecpy(resolved_name, dest.c_str(), lastof(resolved_name));
|
|
|
|
strecpy(&(resolved_name[dest.length()]), resolved_name2, lastof(resolved_name));
|
2008-07-13 16:09:00 +00:00
|
|
|
break; // Only resolve one level
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TarFileList::iterator it = _tar_filelist.find(resolved_name);
|
2007-09-16 18:10:52 +00:00
|
|
|
if (it != _tar_filelist.end()) {
|
|
|
|
f = FioFOpenFileTar(&((*it).second), filesize);
|
2007-09-14 22:25:00 +00:00
|
|
|
}
|
|
|
|
}
|
2005-12-10 11:16:45 +00:00
|
|
|
|
2007-12-04 18:42:08 +00:00
|
|
|
/* Sometimes a full path is given. To support
|
|
|
|
* the 'subdirectory' must be 'removed'. */
|
|
|
|
if (f == NULL && subdir != NO_DIRECTORY) {
|
|
|
|
f = FioFOpenFile(filename, mode, NO_DIRECTORY, filesize);
|
|
|
|
}
|
|
|
|
|
2005-12-10 11:16:45 +00:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2007-03-12 15:25:33 +00:00
|
|
|
/**
|
|
|
|
* Create a directory with the given name
|
|
|
|
* @param name the new name of the directory
|
|
|
|
*/
|
|
|
|
void FioCreateDirectory(const char *name)
|
|
|
|
{
|
|
|
|
#if defined(WIN32) || defined(WINCE)
|
|
|
|
CreateDirectory(OTTD2FS(name), NULL);
|
|
|
|
#elif defined(OS2) && !defined(__INNOTEK_LIBC__)
|
|
|
|
mkdir(OTTD2FS(name));
|
2007-11-18 18:28:32 +00:00
|
|
|
#elif defined(__MORPHOS__) || defined(__AMIGAOS__)
|
|
|
|
char buf[MAX_PATH];
|
|
|
|
ttd_strlcpy(buf, name, MAX_PATH);
|
|
|
|
|
|
|
|
size_t len = strlen(name) - 1;
|
|
|
|
if (buf[len] == '/') {
|
|
|
|
buf[len] = '\0'; // Kill pathsep, so mkdir() will not fail
|
|
|
|
}
|
|
|
|
|
|
|
|
mkdir(OTTD2FS(buf), 0755);
|
2007-03-12 15:25:33 +00:00
|
|
|
#else
|
|
|
|
mkdir(OTTD2FS(name), 0755);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appends, if necessary, the path separator character to the end of the string.
|
|
|
|
* It does not add the path separator to zero-sized strings.
|
|
|
|
* @param buf string to append the separator to
|
|
|
|
* @param buflen the length of the buf
|
|
|
|
*/
|
|
|
|
void AppendPathSeparator(char *buf, size_t buflen)
|
|
|
|
{
|
|
|
|
size_t s = strlen(buf);
|
|
|
|
|
|
|
|
/* Length of string + path separator + '\0' */
|
|
|
|
if (s != 0 && buf[s - 1] != PATHSEPCHAR && s + 2 < buflen) {
|
2007-03-12 15:40:12 +00:00
|
|
|
buf[s] = PATHSEPCHAR;
|
|
|
|
buf[s + 1] = '\0';
|
2007-03-12 15:25:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-08 21:16:17 +00:00
|
|
|
/**
|
|
|
|
* Allocates and files a variable with the full path
|
|
|
|
* based on the given directory.
|
|
|
|
* @param dir the directory to base the path on
|
|
|
|
* @return the malloced full path
|
|
|
|
*/
|
|
|
|
char *BuildWithFullPath(const char *dir)
|
|
|
|
{
|
|
|
|
char *dest = MallocT<char>(MAX_PATH);
|
|
|
|
ttd_strlcpy(dest, dir, MAX_PATH);
|
|
|
|
|
|
|
|
/* Check if absolute or relative path */
|
|
|
|
const char *s = strchr(dest, PATHSEPCHAR);
|
|
|
|
|
|
|
|
/* Add absolute path */
|
|
|
|
if (s == NULL || dest != s) {
|
2008-05-05 21:54:05 +00:00
|
|
|
if (getcwd(dest, MAX_PATH) == NULL) *dest = '\0';
|
2007-06-08 21:16:17 +00:00
|
|
|
AppendPathSeparator(dest, MAX_PATH);
|
|
|
|
ttd_strlcat(dest, dir, MAX_PATH);
|
|
|
|
}
|
|
|
|
AppendPathSeparator(dest, MAX_PATH);
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
const char *FioTarFirstDir(const char *tarname)
|
|
|
|
{
|
|
|
|
TarList::iterator it = _tar_list.find(tarname);
|
|
|
|
if (it == _tar_list.end()) return NULL;
|
|
|
|
return (*it).second.dirname;
|
|
|
|
}
|
|
|
|
|
2009-01-16 14:56:33 +00:00
|
|
|
static void TarAddLink(const std::string &srcParam, const std::string &destParam)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2009-01-16 14:56:33 +00:00
|
|
|
std::string src = srcParam;
|
|
|
|
std::string dest = destParam;
|
|
|
|
/* Tar internals assume lowercase */
|
|
|
|
std::transform(src.begin(), src.end(), src.begin(), tolower);
|
|
|
|
std::transform(dest.begin(), dest.end(), dest.begin(), tolower);
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
TarFileList::iterator dest_file = _tar_filelist.find(dest);
|
|
|
|
if (dest_file != _tar_filelist.end()) {
|
|
|
|
/* Link to file. Process the link like the destination file. */
|
|
|
|
_tar_filelist.insert(TarFileList::value_type(src, dest_file->second));
|
|
|
|
} else {
|
2009-03-15 00:32:18 +00:00
|
|
|
/* Destination file not found. Assume 'link to directory'
|
|
|
|
* Append PATHSEPCHAR to 'src' and 'dest' if needed */
|
2009-01-12 17:11:45 +00:00
|
|
|
const std::string src_path = ((*src.rbegin() == PATHSEPCHAR) ? src : src + PATHSEPCHAR);
|
|
|
|
const std::string dst_path = (dest.length() == 0 ? "" : ((*dest.rbegin() == PATHSEPCHAR) ? dest : dest + PATHSEPCHAR));
|
|
|
|
_tar_linklist.insert(TarLinkList::value_type(src_path, dst_path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FioTarAddLink(const char *src, const char *dest)
|
|
|
|
{
|
|
|
|
TarAddLink(src, dest);
|
|
|
|
}
|
|
|
|
|
2008-07-13 16:09:00 +00:00
|
|
|
/**
|
|
|
|
* Simplify filenames from tars.
|
|
|
|
* Replace '/' by PATHSEPCHAR, and force 'name' to lowercase.
|
|
|
|
* @param name Filename to process.
|
|
|
|
*/
|
|
|
|
static void SimplifyFileName(char *name)
|
|
|
|
{
|
|
|
|
/* Force lowercase */
|
|
|
|
strtolower(name);
|
|
|
|
|
|
|
|
/* Tar-files always have '/' path-seperator, but we want our PATHSEPCHAR */
|
|
|
|
#if (PATHSEPCHAR != '/')
|
|
|
|
for (char *n = name; *n != '\0'; n++) if (*n == '/') *n = PATHSEPCHAR;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-01-17 16:53:32 +00:00
|
|
|
bool TarListAddFile(const char *filename)
|
2007-09-14 22:25:00 +00:00
|
|
|
{
|
2007-09-16 18:10:52 +00:00
|
|
|
/* The TAR-header, repeated for every file */
|
|
|
|
typedef struct TarHeader {
|
|
|
|
char name[100]; ///< Name of the file
|
|
|
|
char mode[8];
|
|
|
|
char uid[8];
|
|
|
|
char gid[8];
|
|
|
|
char size[12]; ///< Size of the file, in ASCII
|
|
|
|
char mtime[12];
|
|
|
|
char chksum[8];
|
|
|
|
char typeflag;
|
|
|
|
char linkname[100];
|
|
|
|
char magic[6];
|
|
|
|
char version[2];
|
|
|
|
char uname[32];
|
|
|
|
char gname[32];
|
|
|
|
char devmajor[8];
|
|
|
|
char devminor[8];
|
|
|
|
char prefix[155]; ///< Path of the file
|
|
|
|
|
|
|
|
char unused[12];
|
|
|
|
} TarHeader;
|
|
|
|
|
|
|
|
/* Check if we already seen this file */
|
|
|
|
TarList::iterator it = _tar_list.find(filename);
|
|
|
|
if (it != _tar_list.end()) return false;
|
|
|
|
|
|
|
|
FILE *f = fopen(filename, "rb");
|
|
|
|
assert(f != NULL);
|
|
|
|
|
2008-07-23 21:42:55 +00:00
|
|
|
const char *dupped_filename = strdup(filename);
|
|
|
|
_tar_list[filename].filename = dupped_filename;
|
2009-01-12 17:11:45 +00:00
|
|
|
_tar_list[filename].dirname = NULL;
|
2007-09-16 18:10:52 +00:00
|
|
|
|
2008-07-13 16:09:00 +00:00
|
|
|
TarLinkList links; ///< Temporary list to collect links
|
|
|
|
|
2007-09-16 18:10:52 +00:00
|
|
|
TarHeader th;
|
|
|
|
char buf[sizeof(th.name) + 1], *end;
|
|
|
|
char name[sizeof(th.prefix) + 1 + sizeof(th.name) + 1];
|
2008-07-13 16:09:00 +00:00
|
|
|
char link[sizeof(th.linkname) + 1];
|
|
|
|
char dest[sizeof(th.prefix) + 1 + sizeof(th.name) + 1 + 1 + sizeof(th.linkname) + 1];
|
2008-05-08 13:20:54 +00:00
|
|
|
size_t num = 0, pos = 0;
|
2007-09-16 18:10:52 +00:00
|
|
|
|
|
|
|
/* Make a char of 512 empty bytes */
|
|
|
|
char empty[512];
|
|
|
|
memset(&empty[0], 0, sizeof(empty));
|
|
|
|
|
2008-07-12 14:49:43 +00:00
|
|
|
for (;;) { // Note: feof() always returns 'false' after 'fseek()'. Cool, isn't it?
|
|
|
|
size_t num_bytes_read = fread(&th, 1, 512, f);
|
|
|
|
if (num_bytes_read != 512) break;
|
|
|
|
pos += num_bytes_read;
|
2007-09-16 18:10:52 +00:00
|
|
|
|
|
|
|
/* Check if we have the new tar-format (ustar) or the old one (a lot of zeros after 'link' field) */
|
|
|
|
if (strncmp(th.magic, "ustar", 5) != 0 && memcmp(&th.magic, &empty[0], 512 - offsetof(TarHeader, magic)) != 0) {
|
|
|
|
/* If we have only zeros in the block, it can be an end-of-file indicator */
|
|
|
|
if (memcmp(&th, &empty[0], 512) == 0) continue;
|
|
|
|
|
|
|
|
DEBUG(misc, 0, "The file '%s' isn't a valid tar-file", filename);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
name[0] = '\0';
|
2008-05-08 13:20:54 +00:00
|
|
|
size_t len = 0;
|
2007-09-16 18:10:52 +00:00
|
|
|
|
|
|
|
/* The prefix contains the directory-name */
|
|
|
|
if (th.prefix[0] != '\0') {
|
|
|
|
memcpy(name, th.prefix, sizeof(th.prefix));
|
|
|
|
name[sizeof(th.prefix)] = '\0';
|
|
|
|
len = strlen(name);
|
|
|
|
name[len] = PATHSEPCHAR;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the name of the file in a safe way at the end of 'name' */
|
|
|
|
memcpy(&name[len], th.name, sizeof(th.name));
|
|
|
|
name[len + sizeof(th.name)] = '\0';
|
|
|
|
|
|
|
|
/* Calculate the size of the file.. for some strange reason this is stored as a string */
|
|
|
|
memcpy(buf, th.size, sizeof(th.size));
|
|
|
|
buf[sizeof(th.size)] = '\0';
|
2009-05-10 17:27:25 +00:00
|
|
|
size_t skip = strtoul(buf, &end, 8);
|
2007-09-16 18:10:52 +00:00
|
|
|
|
2008-07-13 16:09:00 +00:00
|
|
|
switch (th.typeflag) {
|
|
|
|
case '\0':
|
|
|
|
case '0': { // regular file
|
|
|
|
/* Ignore empty files */
|
|
|
|
if (skip == 0) break;
|
2007-09-16 18:10:52 +00:00
|
|
|
|
2008-07-13 16:09:00 +00:00
|
|
|
if (strlen(name) == 0) break;
|
2007-09-16 18:10:52 +00:00
|
|
|
|
2008-07-13 16:09:00 +00:00
|
|
|
/* Store this entry in the list */
|
|
|
|
TarFileListEntry entry;
|
2008-07-23 21:42:55 +00:00
|
|
|
entry.tar_filename = dupped_filename;
|
|
|
|
entry.size = skip;
|
|
|
|
entry.position = pos;
|
2008-07-13 16:09:00 +00:00
|
|
|
|
|
|
|
/* Convert to lowercase and our PATHSEPCHAR */
|
|
|
|
SimplifyFileName(name);
|
2007-09-16 18:10:52 +00:00
|
|
|
|
2009-05-10 17:27:25 +00:00
|
|
|
DEBUG(misc, 6, "Found file in tar: %s (" PRINTF_SIZE " bytes, " PRINTF_SIZE " offset)", name, skip, pos);
|
2008-07-13 16:09:00 +00:00
|
|
|
if (_tar_filelist.insert(TarFileList::value_type(name, entry)).second) num++;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case '1': // hard links
|
|
|
|
case '2': { // symbolic links
|
|
|
|
/* Copy the destination of the link in a safe way at the end of 'linkname' */
|
|
|
|
memcpy(link, th.linkname, sizeof(th.linkname));
|
|
|
|
link[sizeof(th.linkname)] = '\0';
|
|
|
|
|
|
|
|
if (strlen(name) == 0 || strlen(link) == 0) break;
|
|
|
|
|
|
|
|
/* Convert to lowercase and our PATHSEPCHAR */
|
|
|
|
SimplifyFileName(name);
|
|
|
|
SimplifyFileName(link);
|
|
|
|
|
|
|
|
/* Only allow relative links */
|
|
|
|
if (link[0] == PATHSEPCHAR) {
|
|
|
|
DEBUG(misc, 1, "Ignoring absolute link in tar: %s -> %s", name, link);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process relative path.
|
|
|
|
* Note: The destination of links must not contain any directory-links. */
|
2009-04-10 20:37:05 +00:00
|
|
|
strecpy(dest, name, lastof(dest));
|
2008-07-13 16:09:00 +00:00
|
|
|
char *destpos = strrchr(dest, PATHSEPCHAR);
|
|
|
|
if (destpos == NULL) destpos = dest;
|
|
|
|
*destpos = '\0';
|
|
|
|
|
|
|
|
char *pos = link;
|
|
|
|
while (*pos != '\0') {
|
|
|
|
char *next = strchr(link, PATHSEPCHAR);
|
|
|
|
if (next == NULL) next = pos + strlen(pos);
|
|
|
|
|
|
|
|
/* Skip '.' (current dir) */
|
|
|
|
if (next != pos + 1 || pos[0] != '.') {
|
|
|
|
if (next == pos + 2 && pos[0] == '.' && pos[1] == '.') {
|
|
|
|
/* level up */
|
|
|
|
if (dest[0] == '\0') {
|
|
|
|
DEBUG(misc, 1, "Ignoring link pointing outside of data directory: %s -> %s", name, link);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Truncate 'dest' after last PATHSEPCHAR.
|
|
|
|
* This assumes, that the truncated part is a real directory and not a link */
|
|
|
|
destpos = strrchr(dest, PATHSEPCHAR);
|
|
|
|
if (destpos == NULL) destpos = dest;
|
|
|
|
} else {
|
|
|
|
/* Append at end of 'dest' */
|
|
|
|
if (destpos != dest) *(destpos++) = PATHSEPCHAR;
|
2008-09-29 16:27:02 +00:00
|
|
|
strncpy(destpos, pos, next - pos); // Safe as we do '\0'-termination ourselves
|
2008-07-13 16:09:00 +00:00
|
|
|
destpos += next - pos;
|
|
|
|
}
|
|
|
|
*destpos = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Store links in temporary list */
|
|
|
|
DEBUG(misc, 6, "Found link in tar: %s -> %s", name, dest);
|
|
|
|
links.insert(TarLinkList::value_type(name, dest));
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
case '5': // directory
|
|
|
|
/* Convert to lowercase and our PATHSEPCHAR */
|
|
|
|
SimplifyFileName(name);
|
|
|
|
|
|
|
|
/* Store the first directory name we detect */
|
|
|
|
DEBUG(misc, 6, "Found dir in tar: %s", name);
|
|
|
|
if (_tar_list[filename].dirname == NULL) _tar_list[filename].dirname = strdup(name);
|
|
|
|
break;
|
|
|
|
|
2008-07-13 16:09:00 +00:00
|
|
|
default:
|
|
|
|
/* Ignore other types */
|
|
|
|
break;
|
|
|
|
}
|
2007-09-16 18:10:52 +00:00
|
|
|
|
|
|
|
/* Skip to the next block.. */
|
2007-11-19 20:40:14 +00:00
|
|
|
skip = Align(skip, 512);
|
2007-09-16 18:10:52 +00:00
|
|
|
fseek(f, skip, SEEK_CUR);
|
|
|
|
pos += skip;
|
2007-09-14 22:25:00 +00:00
|
|
|
}
|
|
|
|
|
2009-05-10 17:27:25 +00:00
|
|
|
DEBUG(misc, 1, "Found tar '%s' with " PRINTF_SIZE " new files", filename, num);
|
2007-09-16 18:10:52 +00:00
|
|
|
fclose(f);
|
2007-09-14 22:25:00 +00:00
|
|
|
|
2008-07-13 16:09:00 +00:00
|
|
|
/* Resolve file links and store directory links.
|
|
|
|
* We restrict usage of links to two cases:
|
|
|
|
* 1) Links to directories:
|
|
|
|
* Both the source path and the destination path must NOT contain any further links.
|
|
|
|
* When resolving files at most one directory link is resolved.
|
|
|
|
* 2) Links to files:
|
|
|
|
* The destination path must NOT contain any links.
|
|
|
|
* The source path may contain one directory link.
|
|
|
|
*/
|
|
|
|
for (TarLinkList::iterator link = links.begin(); link != links.end(); link++) {
|
|
|
|
const std::string &src = link->first;
|
|
|
|
const std::string &dest = link->second;
|
2009-01-12 17:11:45 +00:00
|
|
|
TarAddLink(src, dest);
|
2008-07-13 16:09:00 +00:00
|
|
|
}
|
|
|
|
|
2007-09-14 22:25:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-05-08 13:20:54 +00:00
|
|
|
static int ScanPathForTarFiles(const char *path, size_t basepath_length)
|
2007-09-14 22:25:00 +00:00
|
|
|
{
|
|
|
|
extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
|
|
|
|
|
|
|
|
uint num = 0;
|
|
|
|
struct stat sb;
|
|
|
|
struct dirent *dirent;
|
|
|
|
DIR *dir;
|
|
|
|
|
|
|
|
if (path == NULL || (dir = ttd_opendir(path)) == NULL) return 0;
|
|
|
|
|
|
|
|
while ((dirent = readdir(dir)) != NULL) {
|
|
|
|
const char *d_name = FS2OTTD(dirent->d_name);
|
|
|
|
char filename[MAX_PATH];
|
|
|
|
|
|
|
|
if (!FiosIsValidFile(path, dirent, &sb)) continue;
|
|
|
|
|
|
|
|
snprintf(filename, lengthof(filename), "%s%s", path, d_name);
|
|
|
|
|
2008-12-05 18:02:04 +00:00
|
|
|
if (S_ISDIR(sb.st_mode)) {
|
2007-09-14 22:25:00 +00:00
|
|
|
/* Directory */
|
|
|
|
if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue;
|
|
|
|
AppendPathSeparator(filename, lengthof(filename));
|
|
|
|
num += ScanPathForTarFiles(filename, basepath_length);
|
2008-12-05 18:02:04 +00:00
|
|
|
} else if (S_ISREG(sb.st_mode)) {
|
2007-09-14 22:25:00 +00:00
|
|
|
/* File */
|
|
|
|
char *ext = strrchr(filename, '.');
|
|
|
|
|
|
|
|
/* If no extension or extension isn't .tar, skip the file */
|
|
|
|
if (ext == NULL) continue;
|
|
|
|
if (strcasecmp(ext, ".tar") != 0) continue;
|
|
|
|
|
|
|
|
if (TarListAddFile(filename)) num++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2007-09-15 15:21:17 +00:00
|
|
|
void ScanForTarFiles()
|
2007-09-14 22:25:00 +00:00
|
|
|
{
|
|
|
|
Searchpath sp;
|
|
|
|
char path[MAX_PATH];
|
|
|
|
uint num = 0;
|
|
|
|
|
|
|
|
DEBUG(misc, 1, "Scanning for tars");
|
|
|
|
FOR_ALL_SEARCHPATHS(sp) {
|
|
|
|
FioAppendDirectory(path, MAX_PATH, sp, DATA_DIR);
|
|
|
|
num += ScanPathForTarFiles(path, strlen(path));
|
2009-01-12 17:11:45 +00:00
|
|
|
FioAppendDirectory(path, MAX_PATH, sp, AI_DIR);
|
|
|
|
num += ScanPathForTarFiles(path, strlen(path));
|
2009-01-15 18:15:12 +00:00
|
|
|
FioAppendDirectory(path, MAX_PATH, sp, AI_LIBRARY_DIR);
|
|
|
|
num += ScanPathForTarFiles(path, strlen(path));
|
2009-03-04 09:53:31 +00:00
|
|
|
FioAppendDirectory(path, MAX_PATH, sp, SCENARIO_DIR);
|
|
|
|
num += ScanPathForTarFiles(path, strlen(path));
|
2007-09-14 22:25:00 +00:00
|
|
|
}
|
|
|
|
DEBUG(misc, 1, "Scan complete, found %d files", num);
|
|
|
|
}
|
|
|
|
|
2007-09-15 15:21:17 +00:00
|
|
|
#if defined(WIN32) || defined(WINCE)
|
|
|
|
/**
|
|
|
|
* Determine the base (personal dir and game data dir) paths
|
|
|
|
* @param exe the path from the current path to the executable
|
|
|
|
* @note defined in the OS related files (os2.cpp, win32.cpp, unix.cpp etc)
|
|
|
|
*/
|
|
|
|
extern void DetermineBasePaths(const char *exe);
|
|
|
|
#else /* defined(WIN32) || defined(WINCE) */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the working directory to the path of the give executable.
|
|
|
|
* For OSX application bundles '.app' is the required extension of the bundle,
|
|
|
|
* so when we crop the path to there, when can remove the name of the bundle
|
|
|
|
* in the same way we remove the name from the executable name.
|
|
|
|
* @param exe the path to the executable
|
|
|
|
*/
|
|
|
|
void ChangeWorkingDirectory(const char *exe)
|
|
|
|
{
|
|
|
|
#ifdef WITH_COCOA
|
|
|
|
char *app_bundle = strchr(exe, '.');
|
|
|
|
while (app_bundle != NULL && strncasecmp(app_bundle, ".app", 4) != 0) app_bundle = strchr(&app_bundle[1], '.');
|
|
|
|
|
|
|
|
if (app_bundle != NULL) app_bundle[0] = '\0';
|
|
|
|
#endif /* WITH_COCOA */
|
2009-05-24 21:09:00 +00:00
|
|
|
char *s = const_cast<char *>(strrchr(exe, PATHSEPCHAR));
|
2007-09-15 15:21:17 +00:00
|
|
|
if (s != NULL) {
|
|
|
|
*s = '\0';
|
2008-11-26 13:12:45 +00:00
|
|
|
#if defined(__DJGPP__)
|
|
|
|
/* If we want to go to the root, we can't use cd C:, but we must use '/' */
|
|
|
|
if (s[-1] == ':') chdir("/");
|
|
|
|
#endif
|
2008-05-05 21:54:05 +00:00
|
|
|
if (chdir(exe) != 0) DEBUG(misc, 0, "Directory with the binary does not exist?");
|
2007-09-15 15:21:17 +00:00
|
|
|
*s = PATHSEPCHAR;
|
|
|
|
}
|
|
|
|
#ifdef WITH_COCOA
|
|
|
|
if (app_bundle != NULL) app_bundle[0] = '.';
|
|
|
|
#endif /* WITH_COCOA */
|
|
|
|
}
|
|
|
|
|
2007-03-12 15:25:33 +00:00
|
|
|
/**
|
|
|
|
* Determine the base (personal dir and game data dir) paths
|
2007-03-17 11:36:04 +00:00
|
|
|
* @param exe the path to the executable
|
2007-03-12 15:25:33 +00:00
|
|
|
*/
|
2007-03-17 11:36:04 +00:00
|
|
|
void DetermineBasePaths(const char *exe)
|
|
|
|
{
|
2007-06-17 15:48:57 +00:00
|
|
|
char tmp[MAX_PATH];
|
2009-03-16 20:03:37 +00:00
|
|
|
#if defined(__MORPHOS__) || defined(__AMIGA__) || defined(DOS) || defined(OS2) || !defined(WITH_PERSONAL_DIR)
|
2007-06-21 14:32:27 +00:00
|
|
|
_searchpaths[SP_PERSONAL_DIR] = NULL;
|
|
|
|
#else
|
2007-03-17 11:36:04 +00:00
|
|
|
const char *homedir = getenv("HOME");
|
|
|
|
|
|
|
|
if (homedir == NULL) {
|
|
|
|
const struct passwd *pw = getpwuid(getuid());
|
2007-06-17 15:48:57 +00:00
|
|
|
homedir = (pw == NULL) ? "" : pw->pw_dir;
|
2007-03-17 11:36:04 +00:00
|
|
|
}
|
|
|
|
|
2007-06-17 15:48:57 +00:00
|
|
|
snprintf(tmp, MAX_PATH, "%s" PATHSEP "%s", homedir, PERSONAL_DIR);
|
|
|
|
AppendPathSeparator(tmp, MAX_PATH);
|
|
|
|
|
|
|
|
_searchpaths[SP_PERSONAL_DIR] = strdup(tmp);
|
|
|
|
#endif
|
2008-01-13 17:45:29 +00:00
|
|
|
|
|
|
|
#if defined(WITH_SHARED_DIR)
|
|
|
|
snprintf(tmp, MAX_PATH, "%s", SHARED_DIR);
|
|
|
|
AppendPathSeparator(tmp, MAX_PATH);
|
|
|
|
_searchpaths[SP_SHARED_DIR] = strdup(tmp);
|
|
|
|
#else
|
2007-06-17 15:48:57 +00:00
|
|
|
_searchpaths[SP_SHARED_DIR] = NULL;
|
2008-01-13 17:45:29 +00:00
|
|
|
#endif
|
2007-06-17 15:48:57 +00:00
|
|
|
|
2007-06-21 14:32:27 +00:00
|
|
|
#if defined(__MORPHOS__) || defined(__AMIGA__)
|
|
|
|
_searchpaths[SP_WORKING_DIR] = NULL;
|
|
|
|
#else
|
2008-05-05 21:54:05 +00:00
|
|
|
if (getcwd(tmp, MAX_PATH) == NULL) *tmp = '\0';
|
2007-06-17 15:48:57 +00:00
|
|
|
AppendPathSeparator(tmp, MAX_PATH);
|
|
|
|
_searchpaths[SP_WORKING_DIR] = strdup(tmp);
|
2007-06-21 14:32:27 +00:00
|
|
|
#endif
|
2007-06-17 15:48:57 +00:00
|
|
|
|
|
|
|
/* Change the working directory to that one of the executable */
|
2009-05-24 20:29:04 +00:00
|
|
|
ChangeWorkingDirectory(exe);
|
2008-05-05 21:54:05 +00:00
|
|
|
if (getcwd(tmp, MAX_PATH) == NULL) *tmp = '\0';
|
2007-06-17 15:48:57 +00:00
|
|
|
AppendPathSeparator(tmp, MAX_PATH);
|
|
|
|
_searchpaths[SP_BINARY_DIR] = strdup(tmp);
|
|
|
|
|
2009-03-16 20:03:37 +00:00
|
|
|
#if defined(__MORPHOS__) || defined(__AMIGA__) || defined(DOS) || defined(OS2)
|
2007-06-21 14:32:27 +00:00
|
|
|
_searchpaths[SP_INSTALLATION_DIR] = NULL;
|
|
|
|
#else
|
2007-06-17 15:48:57 +00:00
|
|
|
snprintf(tmp, MAX_PATH, "%s", GLOBAL_DATA_DIR);
|
|
|
|
AppendPathSeparator(tmp, MAX_PATH);
|
|
|
|
_searchpaths[SP_INSTALLATION_DIR] = strdup(tmp);
|
2007-06-21 14:32:27 +00:00
|
|
|
#endif
|
2007-06-17 15:48:57 +00:00
|
|
|
#ifdef WITH_COCOA
|
|
|
|
extern void cocoaSetApplicationBundleDir();
|
|
|
|
cocoaSetApplicationBundleDir();
|
|
|
|
#else
|
|
|
|
_searchpaths[SP_APPLICATION_BUNDLE_DIR] = NULL;
|
|
|
|
#endif
|
2007-03-17 11:36:04 +00:00
|
|
|
}
|
|
|
|
#endif /* defined(WIN32) || defined(WINCE) */
|
2007-03-12 15:25:33 +00:00
|
|
|
|
2007-06-17 15:48:57 +00:00
|
|
|
char *_personal_dir;
|
|
|
|
|
2007-03-12 15:25:33 +00:00
|
|
|
/**
|
|
|
|
* Acquire the base paths (personal dir and game data dir),
|
|
|
|
* fill all other paths (save dir, autosave dir etc) and
|
|
|
|
* make the save and scenario directories.
|
2007-06-17 15:48:57 +00:00
|
|
|
* @param exe the path from the current path to the executable
|
2007-03-12 15:25:33 +00:00
|
|
|
*/
|
2007-03-17 11:36:04 +00:00
|
|
|
void DeterminePaths(const char *exe)
|
2007-03-12 15:25:33 +00:00
|
|
|
{
|
2007-03-17 11:36:04 +00:00
|
|
|
DetermineBasePaths(exe);
|
2007-03-12 15:25:33 +00:00
|
|
|
|
2007-06-17 15:48:57 +00:00
|
|
|
Searchpath sp;
|
|
|
|
FOR_ALL_SEARCHPATHS(sp) DEBUG(misc, 4, "%s added as search path", _searchpaths[sp]);
|
|
|
|
|
2007-06-17 21:26:57 +00:00
|
|
|
if (_config_file != NULL) {
|
|
|
|
_personal_dir = strdup(_config_file);
|
|
|
|
char *end = strrchr(_personal_dir , PATHSEPCHAR);
|
|
|
|
if (end == NULL) {
|
|
|
|
_personal_dir[0] = '\0';
|
|
|
|
} else {
|
|
|
|
end[1] = '\0';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
char personal_dir[MAX_PATH];
|
|
|
|
FioFindFullPath(personal_dir, lengthof(personal_dir), BASE_DIR, "openttd.cfg");
|
|
|
|
|
|
|
|
if (FileExists(personal_dir)) {
|
|
|
|
char *end = strrchr(personal_dir, PATHSEPCHAR);
|
|
|
|
if (end != NULL) end[1] = '\0';
|
|
|
|
_personal_dir = strdup(personal_dir);
|
|
|
|
_config_file = str_fmt("%sopenttd.cfg", _personal_dir);
|
|
|
|
} else {
|
|
|
|
static const Searchpath new_openttd_cfg_order[] = {
|
|
|
|
SP_PERSONAL_DIR, SP_BINARY_DIR, SP_WORKING_DIR, SP_SHARED_DIR, SP_INSTALLATION_DIR
|
|
|
|
};
|
|
|
|
|
|
|
|
for (uint i = 0; i < lengthof(new_openttd_cfg_order); i++) {
|
|
|
|
if (IsValidSearchPath(new_openttd_cfg_order[i])) {
|
|
|
|
_personal_dir = strdup(_searchpaths[new_openttd_cfg_order[i]]);
|
|
|
|
_config_file = str_fmt("%sopenttd.cfg", _personal_dir);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-06-17 15:48:57 +00:00
|
|
|
}
|
2007-03-12 15:25:33 +00:00
|
|
|
|
2007-06-17 21:26:57 +00:00
|
|
|
DEBUG(misc, 3, "%s found as personal directory", _personal_dir);
|
2007-03-12 15:25:33 +00:00
|
|
|
|
2007-06-17 15:48:57 +00:00
|
|
|
_highscore_file = str_fmt("%shs.dat", _personal_dir);
|
|
|
|
_log_file = str_fmt("%sopenttd.log", _personal_dir);
|
|
|
|
|
|
|
|
/* Make the necessary folders */
|
2007-11-18 18:28:32 +00:00
|
|
|
#if !defined(__MORPHOS__) && !defined(__AMIGA__) && defined(WITH_PERSONAL_DIR)
|
2007-06-17 15:48:57 +00:00
|
|
|
FioCreateDirectory(_personal_dir);
|
2007-11-18 18:28:32 +00:00
|
|
|
#endif
|
|
|
|
|
2009-03-04 14:25:53 +00:00
|
|
|
static const Subdirectory default_subdirs[] = {
|
2009-03-04 23:12:15 +00:00
|
|
|
SAVE_DIR, AUTOSAVE_DIR, SCENARIO_DIR, HEIGHTMAP_DIR
|
2009-03-04 14:25:53 +00:00
|
|
|
};
|
2007-03-12 15:25:33 +00:00
|
|
|
|
2009-03-04 14:25:53 +00:00
|
|
|
for (uint i = 0; i < lengthof(default_subdirs); i++) {
|
|
|
|
char *dir = str_fmt("%s%s", _personal_dir, FioGetSubdirectory(default_subdirs[i]));
|
|
|
|
FioCreateDirectory(dir);
|
|
|
|
free(dir);
|
|
|
|
}
|
2009-01-16 16:32:48 +00:00
|
|
|
|
2009-01-17 16:53:32 +00:00
|
|
|
/* If we have network we make a directory for the autodownloading of content */
|
|
|
|
_searchpaths[SP_AUTODOWNLOAD_DIR] = str_fmt("%s%s", _personal_dir, "content_download" PATHSEP);
|
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
FioCreateDirectory(_searchpaths[SP_AUTODOWNLOAD_DIR]);
|
|
|
|
|
|
|
|
/* Create the directory for each of the types of content */
|
|
|
|
const Subdirectory dirs[] = { SCENARIO_DIR, HEIGHTMAP_DIR, DATA_DIR, AI_DIR, AI_LIBRARY_DIR };
|
|
|
|
for (uint i = 0; i < lengthof(dirs); i++) {
|
|
|
|
char *tmp = str_fmt("%s%s", _searchpaths[SP_AUTODOWNLOAD_DIR], FioGetSubdirectory(dirs[i]));
|
|
|
|
FioCreateDirectory(tmp);
|
|
|
|
free(tmp);
|
|
|
|
}
|
|
|
|
#else /* ENABLE_NETWORK */
|
|
|
|
/* If we don't have networking, we don't need to make the directory. But
|
|
|
|
* if it exists we keep it, otherwise remove it from the search paths. */
|
|
|
|
if (!FileExists(_searchpaths[SP_AUTODOWNLOAD_DIR])) {
|
|
|
|
free((void*)_searchpaths[SP_AUTODOWNLOAD_DIR]);
|
|
|
|
_searchpaths[SP_AUTODOWNLOAD_DIR] = NULL;
|
|
|
|
}
|
|
|
|
#endif /* ENABLE_NETWORK */
|
|
|
|
|
2009-01-16 16:32:48 +00:00
|
|
|
ScanForTarFiles();
|
2007-03-12 15:25:33 +00:00
|
|
|
}
|
2007-06-12 15:46:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanitizes a filename, i.e. removes all illegal characters from it.
|
|
|
|
* @param filename the "\0" terminated filename
|
|
|
|
*/
|
|
|
|
void SanitizeFilename(char *filename)
|
|
|
|
{
|
|
|
|
for (; *filename != '\0'; filename++) {
|
|
|
|
switch (*filename) {
|
|
|
|
/* The following characters are not allowed in filenames
|
|
|
|
* on at least one of the supported operating systems: */
|
|
|
|
case ':': case '\\': case '*': case '?': case '/':
|
2007-06-22 19:30:59 +00:00
|
|
|
case '<': case '>': case '|': case '"':
|
2007-06-12 15:46:34 +00:00
|
|
|
*filename = '_';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-12-25 11:26:07 +00:00
|
|
|
|
|
|
|
void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize)
|
|
|
|
{
|
2009-02-24 20:59:17 +00:00
|
|
|
FILE *in = fopen(filename, "rb");
|
2007-12-25 11:26:07 +00:00
|
|
|
if (in == NULL) return NULL;
|
|
|
|
|
|
|
|
fseek(in, 0, SEEK_END);
|
2009-02-24 20:59:17 +00:00
|
|
|
size_t len = ftell(in);
|
2007-12-25 11:26:07 +00:00
|
|
|
fseek(in, 0, SEEK_SET);
|
2009-02-24 20:59:17 +00:00
|
|
|
if (len > maxsize) {
|
2007-12-25 11:26:07 +00:00
|
|
|
fclose(in);
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-02-24 20:59:17 +00:00
|
|
|
byte *mem = MallocT<byte>(len + 1);
|
2007-12-25 11:26:07 +00:00
|
|
|
mem[len] = 0;
|
|
|
|
if (fread(mem, len, 1, in) != 1) {
|
|
|
|
fclose(in);
|
|
|
|
free(mem);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
fclose(in);
|
|
|
|
|
|
|
|
*lenp = len;
|
|
|
|
return mem;
|
|
|
|
}
|
2008-08-31 08:38:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scan a single directory (and recursively it's children) and add
|
|
|
|
* any graphics sets that are found.
|
|
|
|
* @param extension the extension of files to search for.
|
|
|
|
* @param path full path we're currently at
|
|
|
|
* @param basepath_length from where in the path are we 'based' on the search path
|
|
|
|
*/
|
2009-03-04 00:17:51 +00:00
|
|
|
static uint ScanPath(FileScanner *fs, const char *extension, const char *path, size_t basepath_length, bool recursive)
|
2008-08-31 08:38:30 +00:00
|
|
|
{
|
|
|
|
extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
|
|
|
|
|
|
|
|
uint num = 0;
|
|
|
|
struct stat sb;
|
|
|
|
struct dirent *dirent;
|
|
|
|
DIR *dir;
|
|
|
|
|
|
|
|
if (path == NULL || (dir = ttd_opendir(path)) == NULL) return 0;
|
|
|
|
|
|
|
|
while ((dirent = readdir(dir)) != NULL) {
|
|
|
|
const char *d_name = FS2OTTD(dirent->d_name);
|
|
|
|
char filename[MAX_PATH];
|
|
|
|
|
|
|
|
if (!FiosIsValidFile(path, dirent, &sb)) continue;
|
|
|
|
|
|
|
|
snprintf(filename, lengthof(filename), "%s%s", path, d_name);
|
|
|
|
|
2008-11-26 01:07:49 +00:00
|
|
|
if (S_ISDIR(sb.st_mode)) {
|
2008-08-31 08:38:30 +00:00
|
|
|
/* Directory */
|
2009-03-04 00:17:51 +00:00
|
|
|
if (!recursive) continue;
|
2008-08-31 08:38:30 +00:00
|
|
|
if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue;
|
|
|
|
AppendPathSeparator(filename, lengthof(filename));
|
2009-03-04 00:17:51 +00:00
|
|
|
num += ScanPath(fs, extension, filename, basepath_length, recursive);
|
2008-11-26 01:07:49 +00:00
|
|
|
} else if (S_ISREG(sb.st_mode)) {
|
2008-08-31 08:38:30 +00:00
|
|
|
/* File */
|
2009-03-04 00:19:42 +00:00
|
|
|
if (extension != NULL) {
|
|
|
|
char *ext = strrchr(filename, '.');
|
2008-08-31 08:38:30 +00:00
|
|
|
|
2009-03-04 00:19:42 +00:00
|
|
|
/* If no extension or extension isn't .grf, skip the file */
|
|
|
|
if (ext == NULL) continue;
|
|
|
|
if (strcasecmp(ext, extension) != 0) continue;
|
|
|
|
}
|
2008-08-31 08:38:30 +00:00
|
|
|
|
|
|
|
if (fs->AddFile(filename, basepath_length)) num++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scan the given tar and add graphics sets when it finds one.
|
|
|
|
* @param extension the extension of files to search for.
|
|
|
|
* @param tar the tar to search in.
|
|
|
|
*/
|
|
|
|
static uint ScanTar(FileScanner *fs, const char *extension, TarFileList::iterator tar)
|
|
|
|
{
|
|
|
|
uint num = 0;
|
|
|
|
const char *filename = (*tar).first.c_str();
|
|
|
|
|
2009-03-04 00:19:42 +00:00
|
|
|
if (extension != NULL) {
|
|
|
|
const char *ext = strrchr(filename, '.');
|
|
|
|
|
|
|
|
/* If no extension or extension isn't .grf, skip the file */
|
|
|
|
if (ext == NULL) return false;
|
|
|
|
if (strcasecmp(ext, extension) != 0) return false;
|
|
|
|
}
|
2008-08-31 08:38:30 +00:00
|
|
|
|
|
|
|
if (fs->AddFile(filename, 0)) num++;
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scan for files with the given extention in the given search path.
|
|
|
|
* @param extension the extension of files to search for.
|
2008-10-14 19:27:08 +00:00
|
|
|
* @param sd the sub directory to search in.
|
2008-08-31 08:38:30 +00:00
|
|
|
* @param tars whether to search in the tars too.
|
|
|
|
* @return the number of found files, i.e. the number of times that
|
|
|
|
* AddFile returned true.
|
|
|
|
*/
|
2009-03-04 00:17:51 +00:00
|
|
|
uint FileScanner::Scan(const char *extension, Subdirectory sd, bool tars, bool recursive)
|
2008-08-31 08:38:30 +00:00
|
|
|
{
|
|
|
|
Searchpath sp;
|
|
|
|
char path[MAX_PATH];
|
|
|
|
TarFileList::iterator tar;
|
|
|
|
uint num = 0;
|
|
|
|
|
|
|
|
FOR_ALL_SEARCHPATHS(sp) {
|
|
|
|
FioAppendDirectory(path, MAX_PATH, sp, sd);
|
2009-03-04 00:17:51 +00:00
|
|
|
num += ScanPath(this, extension, path, strlen(path), recursive);
|
2008-08-31 08:38:30 +00:00
|
|
|
}
|
2009-01-16 16:14:01 +00:00
|
|
|
|
|
|
|
if (tars) {
|
|
|
|
FOR_ALL_TARS(tar) {
|
|
|
|
num += ScanTar(this, extension, tar);
|
|
|
|
}
|
2008-08-31 08:38:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
2009-01-17 16:53:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Scan for files with the given extention in the given search path.
|
|
|
|
* @param extension the extension of files to search for.
|
|
|
|
* @param directory the sub directory to search in.
|
|
|
|
* @return the number of found files, i.e. the number of times that
|
|
|
|
* AddFile returned true.
|
|
|
|
*/
|
2009-03-04 00:17:51 +00:00
|
|
|
uint FileScanner::Scan(const char *extension, const char *directory, bool recursive)
|
2009-01-17 16:53:32 +00:00
|
|
|
{
|
|
|
|
char path[MAX_PATH];
|
|
|
|
strecpy(path, directory, lastof(path));
|
|
|
|
AppendPathSeparator(path, lengthof(path));
|
2009-03-04 00:17:51 +00:00
|
|
|
return ScanPath(this, extension, path, strlen(path), recursive);
|
2009-01-17 16:53:32 +00:00
|
|
|
}
|