version 1.3, 2015/04/20 18:10:09
|
version 1.4, 2015/04/20 18:19:50
|
|
|
*/ | */ |
| |
#include "paths.h" | #include "paths.h" |
#include "strings.h" |
#include <pal/strings.h> |
#include "process.h" | #include "process.h" |
#include "io.h" |
/* #include <pal/format.h> */ |
|
#include <pal/format.h> |
|
#include <pal/file.h> |
| |
#if defined(CONFIG_OS_WINDOWS) | #if defined(CONFIG_OS_WINDOWS) |
# include <windows.h> | # include <windows.h> |
|
|
{ "authdir", CONFIG_AUTHDIR, MI_FALSE }, | { "authdir", CONFIG_AUTHDIR, MI_FALSE }, |
{ "httpsendtracefile", HTTPSENDTRACEFILE, MI_FALSE }, | { "httpsendtracefile", HTTPSENDTRACEFILE, MI_FALSE }, |
{ "httprecvtracefile", HTTPRECVTRACEFILE, MI_FALSE }, | { "httprecvtracefile", HTTPRECVTRACEFILE, MI_FALSE }, |
|
{ "srcdir", CONFIG_SRCDIR, MI_FALSE }, |
}; | }; |
| |
|
#include <pal/ownedmemory.h> |
|
|
|
static char* Strdup_InOwnedMemory( |
|
_In_z_ const char* s) |
|
{ |
|
size_t sLen = 0; |
|
char *newMemory = NULL; |
|
if(!s) return NULL; |
|
|
|
sLen = Strlen(s); |
|
newMemory = (char *)OwnedMemory_Alloc(sizeof(char) * (sLen + 1)); |
|
|
|
if(!newMemory) |
|
return NULL; |
|
|
|
Strlcpy(newMemory, s, sLen + 1); |
|
|
|
return newMemory; |
|
} |
|
|
#if defined(CONFIG_OS_WINDOWS) | #if defined(CONFIG_OS_WINDOWS) |
static int _GetFullProgramPath( | static int _GetFullProgramPath( |
const char* programName, | const char* programName, |
char path[MAX_PATH_SIZE]) |
_Pre_writable_size_(PAL_MAX_PATH_SIZE) char path[PAL_MAX_PATH_SIZE]) |
{ | { |
/* Get full path of the current module (library or program). */ | /* Get full path of the current module (library or program). */ |
{ | { |
DWORD r = GetModuleFileNameA(NULL, path, MAX_PATH_SIZE); |
DWORD r = GetModuleFileNameA(NULL, path, PAL_MAX_PATH_SIZE); |
| |
if (r < 1 || r >= MAX_PATH_SIZE) |
if (r < 1 || r >= PAL_MAX_PATH_SIZE) |
return -1; | return -1; |
} | } |
| |
|
|
{ | { |
end[1] = '\0'; | end[1] = '\0'; |
| |
if (Strlcat(path, programName, MAX_PATH_SIZE) >= MAX_PATH_SIZE) |
if (Strlcat(path, programName, PAL_MAX_PATH_SIZE) >= PAL_MAX_PATH_SIZE) |
return -1; | return -1; |
} | } |
else | else |
|
|
#endif /* defined(CONFIG_OS_WINDOWS) */ | #endif /* defined(CONFIG_OS_WINDOWS) */ |
| |
#if defined(CONFIG_OS_WINDOWS) | #if defined(CONFIG_OS_WINDOWS) |
static int _ResolvePrefixPath(char path[MAX_PATH_SIZE]) |
static int _ResolvePrefixPath(_Pre_writable_size_(PAL_MAX_PATH_SIZE) _Null_terminated_ char path[PAL_MAX_PATH_SIZE]) |
{ | { |
char* p; | char* p; |
| |
|
|
/* Work down directory hierarchy, looking for '.prefix' file */ | /* Work down directory hierarchy, looking for '.prefix' file */ |
for (;;) | for (;;) |
{ | { |
char buf[MAX_PATH_SIZE]; |
char buf[PAL_MAX_PATH_SIZE]; |
| |
/* Format path to .prefix file */ | /* Format path to .prefix file */ |
Strlcpy(buf, path, sizeof(buf)); | Strlcpy(buf, path, sizeof(buf)); |
|
|
char uuid[36]; | char uuid[36]; |
| |
/* Open .prefix file */ | /* Open .prefix file */ |
is = Fopen(buf, "rb"); |
is = File_Open(buf, "rb"); |
if (!is) | if (!is) |
continue; | continue; |
| |
/* Read UUID from file */ | /* Read UUID from file */ |
if (fread(uuid, 1, sizeof(uuid), is) != sizeof(uuid)) | if (fread(uuid, 1, sizeof(uuid), is) != sizeof(uuid)) |
{ | { |
fclose(is); |
File_Close(is); |
continue; | continue; |
} | } |
| |
/* Check whether UUID is the expected one */ | /* Check whether UUID is the expected one */ |
if (memcmp(uuid, UUID, sizeof(uuid)) != 0) | if (memcmp(uuid, UUID, sizeof(uuid)) != 0) |
{ | { |
fclose(is); |
File_Close(is); |
continue; | continue; |
} | } |
| |
/* Success! */ | /* Success! */ |
fclose(is); |
File_Close(is); |
return 0; | return 0; |
} | } |
| |
|
|
standalone directory, outisde of 'source' */ | standalone directory, outisde of 'source' */ |
| |
/* Get current directory */ | /* Get current directory */ |
if (_getcwd(path, MAX_PATH_SIZE) == 0) |
if (_getcwd(path, PAL_MAX_PATH_SIZE) == 0) |
return -1; | return -1; |
| |
/* Translate backward to forward slashes */ | /* Translate backward to forward slashes */ |
|
|
/* Work down directory hierarchy, looking for '.prefix' file */ | /* Work down directory hierarchy, looking for '.prefix' file */ |
for (;;) | for (;;) |
{ | { |
char buf[MAX_PATH_SIZE]; |
char buf[PAL_MAX_PATH_SIZE]; |
|
|
|
if (!path || *path == 0) |
|
break; |
| |
/* Format path to .prefix file */ | /* Format path to .prefix file */ |
Strlcpy(buf, path, sizeof(buf)); | Strlcpy(buf, path, sizeof(buf)); |
|
|
char uuid[36]; | char uuid[36]; |
| |
/* Open .prefix file */ | /* Open .prefix file */ |
is = Fopen(buf, "rb"); |
is = File_Open(buf, "rb"); |
if (!is) | if (!is) |
continue; | continue; |
| |
/* Read UUID from file */ | /* Read UUID from file */ |
if (fread(uuid, 1, sizeof(uuid), is) != sizeof(uuid)) | if (fread(uuid, 1, sizeof(uuid), is) != sizeof(uuid)) |
{ | { |
fclose(is); |
File_Close(is); |
continue; | continue; |
} | } |
| |
/* Check whether UUID is the expected one */ | /* Check whether UUID is the expected one */ |
if (memcmp(uuid, UUID, sizeof(uuid)) != 0) | if (memcmp(uuid, UUID, sizeof(uuid)) != 0) |
{ | { |
fclose(is); |
File_Close(is); |
continue; | continue; |
} | } |
| |
/* Success! */ | /* Success! */ |
fclose(is); |
File_Close(is); |
return 0; | return 0; |
} | } |
| |
|
|
} | } |
#endif /* defined(CONFIG_OS_WINDOWS) */ | #endif /* defined(CONFIG_OS_WINDOWS) */ |
| |
const char* GetPath(PathID id) |
#if defined(CONFIG_OS_WINDOWS) |
|
static char s_ServerProgramPath[PAL_MAX_PATH_SIZE] = ""; |
|
static char s_PrefixPath[PAL_MAX_PATH_SIZE] = ""; |
|
static char s_SrcDirPath[PAL_MAX_PATH_SIZE] = ""; |
|
#endif |
|
|
|
const char* OMI_GetPath(PathID id) |
{ | { |
int i = ((int)id) % MI_COUNT(_paths); | int i = ((int)id) % MI_COUNT(_paths); |
| |
|
|
/* Look for Windows programs in same directory as this module */ | /* Look for Windows programs in same directory as this module */ |
if (id == ID_SERVERPROGRAM) | if (id == ID_SERVERPROGRAM) |
{ | { |
static char path[MAX_PATH_SIZE]; |
if (s_ServerProgramPath[0] == '\0') |
|
|
if (path[0] == '\0') |
|
{ | { |
if (_GetFullProgramPath("omiserver", path) != 0) |
if (_GetFullProgramPath("omiserver", s_ServerProgramPath) != 0) |
return NULL; | return NULL; |
} | } |
| |
return path; |
return s_ServerProgramPath; |
} | } |
else if (id == ID_PREFIX) | else if (id == ID_PREFIX) |
{ | { |
static char path[MAX_PATH_SIZE]; |
if (s_PrefixPath[0] == '\0') |
|
|
if (path[0] == '\0') |
|
{ | { |
if (_ResolvePrefixPath(path) != 0) |
if (_ResolvePrefixPath(s_PrefixPath) != 0) |
return CONFIG_PREFIX; | return CONFIG_PREFIX; |
} | } |
| |
return path; |
return s_PrefixPath; |
|
} |
|
else if (id == ID_SRCDIR) |
|
{ |
|
if (s_SrcDirPath[0] == '\0') |
|
{ |
|
if (_ResolvePrefixPath(s_SrcDirPath) != 0) |
|
return CONFIG_SRCDIR; |
} | } |
| |
/* Although id is defined in enumeration and will always be less than |
return s_SrcDirPath; |
* MI_COUNT(_paths), VS prefast tool is compalining about buffer overrun |
} |
|
|
|
/* Although id is defined in an enumeration and will always be less than |
|
* MI_COUNT(_paths), VS prefast tool compalins about buffer overrun |
*/ | */ |
#endif | #endif |
| |
#if defined(CONFIG_OS_WINDOWS) | #if defined(CONFIG_OS_WINDOWS) |
if (_paths[i].str[0] == '.' && !_paths[i].dynamic) | if (_paths[i].str[0] == '.' && !_paths[i].dynamic) |
{ | { |
char buf[MAX_PATH_SIZE]; |
char buf[PAL_MAX_PATH_SIZE]; |
Strlcpy(buf, GetPath(ID_PREFIX), sizeof(buf)); |
Strlcpy(buf, OMI_GetPath(ID_PREFIX), sizeof buf); |
Strlcat(buf, _paths[i].str + 1, sizeof(buf)); |
Strlcat(buf, _paths[i].str + 1, sizeof buf); |
_paths[i].str = Strdup(buf); |
_paths[i].str = Strdup_InOwnedMemory(buf); |
_paths[i].dynamic = MI_TRUE; |
_paths[i].dynamic = (_paths[i].str != NULL); |
} | } |
#endif | #endif |
| |
|
|
| |
} | } |
| |
|
int CreateLogFileNameWithPrefix(_In_z_ const char *prefix, _Pre_writable_size_(PAL_MAX_PATH_SIZE) PAL_Char finalPath[PAL_MAX_PATH_SIZE]) |
|
{ |
|
char path[PAL_MAX_PATH_SIZE]; |
|
|
|
if (Strlcpy(path, OMI_GetPath(ID_LOGDIR), PAL_MAX_PATH_SIZE) >= PAL_MAX_PATH_SIZE) |
|
{ |
|
return -1; |
|
} |
|
|
|
Strlcat(path, "/", PAL_MAX_PATH_SIZE); |
|
Strlcat(path, prefix, PAL_MAX_PATH_SIZE); |
|
Strlcat(path, ".log", PAL_MAX_PATH_SIZE); |
|
#if defined(CONFIG_ENABLE_WCHAR) |
|
TcsStrlcpy(finalPath, path, MI_COUNT(path)); |
|
#else |
|
Strlcpy(finalPath, path, MI_COUNT(path)); |
|
#endif |
|
|
|
return 0; |
|
} |
|
|
int SetPath(PathID id, const char* path) | int SetPath(PathID id, const char* path) |
{ | { |
/* First prepend 'destdir' to all other paths */ | /* First prepend 'destdir' to all other paths */ |
|
|
| |
for (i = 0; i < MI_COUNT(_paths); i++) | for (i = 0; i < MI_COUNT(_paths); i++) |
{ | { |
char buf[MAX_PATH_SIZE]; |
char buf[PAL_MAX_PATH_SIZE]; |
| |
if (i == (size_t)ID_DESTDIR) | if (i == (size_t)ID_DESTDIR) |
continue; | continue; |
|
|
/* Set the path */ | /* Set the path */ |
{ | { |
size_t i = (size_t)((int)id) % MI_COUNT(_paths); | size_t i = (size_t)((int)id) % MI_COUNT(_paths); |
char* str = Strdup(path); |
char* str = Strdup_InOwnedMemory(path); |
| |
if (!str) | if (!str) |
return -1; | return -1; |
| |
if (_paths[i].dynamic) | if (_paths[i].dynamic) |
free(_paths[i].str); |
OwnedMemory_Free(_paths[i].str); |
| |
_paths[i].str = str; | _paths[i].str = str; |
_paths[i].dynamic = MI_TRUE; | _paths[i].dynamic = MI_TRUE; |
|
|
for (i = 0; i < MI_COUNT(_paths); i++) | for (i = 0; i < MI_COUNT(_paths); i++) |
{ | { |
PathID id = (PathID)i; | PathID id = (PathID)i; |
printf("%s=%s\n", _paths[i].nickname, GetPath(id)); |
Tprintf(PAL_T("%s=%s\n"), scs(_paths[i].nickname), scs(OMI_GetPath(id))); |
} | } |
} | } |
| |
|
|
return MI_FALSE; | return MI_FALSE; |
} | } |
| |
|
_Use_decl_annotations_ |
|
char* TempPath(char path[PAL_MAX_PATH_SIZE], const char* file) |
|
{ |
|
if (Strlcpy(path, OMI_GetPath(ID_TMPDIR), PAL_MAX_PATH_SIZE) >= PAL_MAX_PATH_SIZE) |
|
return NULL; |
|
|
|
if (Strlcat(path, "/", PAL_MAX_PATH_SIZE) >= PAL_MAX_PATH_SIZE) |
|
return NULL; |
|
|
|
if (Strlcat(path, file, PAL_MAX_PATH_SIZE) >= PAL_MAX_PATH_SIZE) |
|
return NULL; |
|
|
|
return path; |
|
} |
|
|
|
_Use_decl_annotations_ |
|
TChar* TSTempPath(TChar path[PAL_MAX_PATH_SIZE], const char* file) |
|
{ |
|
if (TcsStrlcpy(path, OMI_GetPath(ID_TMPDIR), PAL_MAX_PATH_SIZE) >= PAL_MAX_PATH_SIZE) |
|
return NULL; |
|
if (TcsStrlcat(path, "/", PAL_MAX_PATH_SIZE) >= PAL_MAX_PATH_SIZE) |
|
return NULL; |
|
|
|
if (TcsStrlcat(path, file, PAL_MAX_PATH_SIZE) >= PAL_MAX_PATH_SIZE) |
|
return NULL; |
|
|
|
return path; |
|
} |
|
|
END_EXTERNC | END_EXTERNC |