Skip to content

Commit

Permalink
Media detection code. Also untested. :/
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Apr 22, 2007
1 parent f88f7f1 commit 0c39812
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 10 deletions.
32 changes: 22 additions & 10 deletions CMakeLists.txt
Expand Up @@ -242,16 +242,28 @@ IF(MOJOSETUP_NEED_ZLIB)
ENDIF(MOJOSETUP_INTERNAL_ZLIB)
ENDIF(MOJOSETUP_NEED_ZLIB)

IF(UNIX AND NOT MACOSX)
CHECK_LIBRARY_EXISTS("dl" "dlopen" "" HAVE_LIBDL)
IF(HAVE_LIBDL)
SET(OPTIONAL_LIBS ${OPTIONAL_LIBS} dl)
ENDIF(HAVE_LIBDL)
CHECK_LIBRARY_EXISTS("m" "sin" "" HAVE_LIBM)
IF(HAVE_LIBM)
SET(OPTIONAL_LIBS ${OPTIONAL_LIBS} m)
ENDIF(HAVE_LIBM)
ENDIF(UNIX AND NOT MACOSX)
IF(UNIX)
CHECK_INCLUDE_FILE(sys/ucred.h HAVE_UCRED_H)
IF(HAVE_UCRED_H)
ADD_DEFINITIONS(-DMOJOSETUP_HAVE_SYS_UCRED_H=1)
ENDIF(HAVE_UCRED_H)

CHECK_INCLUDE_FILE(mntent.h HAVE_MNTENT_H)
IF(HAVE_MNTENT_H)
ADD_DEFINITIONS(-DMOJOSETUP_HAVE_MNTENT_H=1)
ENDIF(HAVE_MNTENT_H)

IF(NOT MACOSX)
CHECK_LIBRARY_EXISTS("dl" "dlopen" "" HAVE_LIBDL)
IF(HAVE_LIBDL)
SET(OPTIONAL_LIBS ${OPTIONAL_LIBS} dl)
ENDIF(HAVE_LIBDL)
CHECK_LIBRARY_EXISTS("m" "sin" "" HAVE_LIBM)
IF(HAVE_LIBM)
SET(OPTIONAL_LIBS ${OPTIONAL_LIBS} m)
ENDIF(HAVE_LIBM)
ENDIF(NOT MACOSX)
ENDIF(UNIX)

OPTION(MOJOSETUP_BUILD_LUAC "Build separate Lua compiler" TRUE)
IF(MOJOSETUP_BUILD_LUAC)
Expand Down
42 changes: 42 additions & 0 deletions lua_glue.c
Expand Up @@ -453,6 +453,47 @@ static int luahook_cmdlinestr(lua_State *L)
} // luahook_cmdlinestr


static int luahook_findmedia(lua_State *L)
{
// Let user specify overrides of directories to act as drives.
// This is good if for some reason MojoSetup can't find them, or they
// want to pretend a copy on the filesystem is really a CD, etc.
// Put your anti-piracy crap in your OWN program. :)
//
// You can specify with command lines or environment variables, command
// lines taking precedence:
// MOJOSETUP_MEDIA0=/my/patch ./installer --media1=/over/there
//
// --media and MOJOSETUP_MEDIA are checked first, then --media0 and
// MOJOSETUP_MEDIA0, etc until you find the media or run out of
// overrides.
//
// After the overrides, we ask the platform layer to find the media.

const char *unique = luaL_checkstring(L, 1);
const char *override = cmdlinestr("media", "MOJOSETUP_MEDIA", NULL);
char *physical = NULL;
char cmdbuf[64];
char envrbuf[64];
int i = 0;

do
{
if ( (override) && (MojoPlatform_exists(override, unique)) )
return retvalString(L, override);

snprintf(cmdbuf, sizeof (cmdbuf), "media%d", i);
snprintf(envrbuf, sizeof (envrbuf), "MOJOSETUP_MEDIA%d", i);
} while ((override = cmdlinestr(cmdbuf, envrbuf, NULL)) != NULL);

// No override. Try platform layer for real media...
physical = MojoPlatform_findMedia(unique);
retvalString(L, physical); // may push nil.
free(physical);
return 1;
} // luahook_findmedia


static int luahook_gui_start(lua_State *L)
{
const char *title = luaL_checkstring(L, 1);
Expand Down Expand Up @@ -912,6 +953,7 @@ boolean MojoLua_initLua(void)
set_cfunc(luaState, luahook_cmdlinestr, "cmdlinestr");
set_cfunc(luaState, luahook_collectgarbage, "collectgarbage");
set_cfunc(luaState, luahook_debugger, "debugger");
set_cfunc(luaState, luahook_findmedia, "findmedia");
set_string(luaState, locale, "locale");
set_string(luaState, PLATFORM_NAME, "platform");
set_string(luaState, PLATFORM_ARCH, "arch");
Expand Down
7 changes: 7 additions & 0 deletions platform.h
Expand Up @@ -30,6 +30,12 @@ boolean MojoPlatform_unlink(const char *fname);
// NULL if path couldn't be resolved.
char *MojoPlatform_realpath(const char *path);

// !!! FIXME: comment me.
boolean MojoPlatform_exists(const char *dir, const char *fname);

// !!! FIXME: comment me.
char *MojoPlatform_findMedia(const char *uniquefile);

// Wrappers for Unix dlopen/dlsym/dlclose, sort of. Instead of a filename,
// these take a memory buffer for the library. If you can't load this
// directly in RAM, the platform should write it to a temporary file first,
Expand All @@ -40,6 +46,7 @@ void *MojoPlatform_dlopen(const uint8 *img, size_t len);
void *MojoPlatform_dlsym(void *lib, const char *sym);
void MojoPlatform_dlclose(void *lib);


// Put a line of text to the the system log, whatever that might be on a
// given platform. (str) is a complete line, but won't end with any newline
// characters. You should supply if needed.
Expand Down
67 changes: 67 additions & 0 deletions platform/unix.c
Expand Up @@ -9,9 +9,21 @@
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/utsname.h>
#include <sys/mount.h>
#include <time.h>
#include <unistd.h>

#if MOJOSETUP_HAVE_SYS_UCRED_H
# ifdef MOJOSETUP_HAVE_MNTENT_H
# undef MOJOSETUP_HAVE_MNTENT_H /* don't do both... */
# endif
# include <sys/ucred.h>
#endif

#if MOJOSETUP_HAVE_MNTENT_H
# include <mntent.h>
#endif

#if PLATFORM_BEOS
#define DLOPEN_ARGS 0
void *beos_dlopen(const char *fname, int unused);
Expand Down Expand Up @@ -435,6 +447,61 @@ boolean MojoPlatform_unlink(const char *fname)
} // MojoPlatform_unlink


boolean MojoPlatform_exists(const char *dir, const char *fname)
{
boolean retval = false;
if (fname == NULL)
retval = (access(dir, F_OK) != -1);
else
{
const size_t len = strlen(dir) + strlen(fname) + 2;
char *buf = (char *) xmalloc(strlen(dir) + strlen(fname) + 2);
snprintf(buf, len, "%s/%s", dir, fname);
retval = (access(buf, F_OK) != -1);
free(buf);
} // else
return retval;
} // MojoPlatform_exists


char *MojoPlatform_findMedia(const char *uniquefile)
{
#if MOJOSETUP_HAVE_SYS_UCRED_H
int i = 0;
struct statfs *mntbufp = NULL;
int mounts = getmntinfo(&mntbufp, MNT_WAIT);
for (i = 0; i < mounts; i++)
{
const char *mnt = mntbufp[i].f_mntonname;
if (MojoPlatform_exists(mnt, uniquefile))
return xstrdup(mnt);
} // for

#elif MOJOSETUP_HAVE_MNTENT_H
FILE *mounts = setmntent("/etc/mtab", "r");
if (mounts != NULL)
{
struct mntent *ent = NULL;
while ((ent = getmntent(mounts)) != NULL)
{
const char *mnt = ent->mnt_dir;
if (MojoPlatform_exists(mnt, uniquefile))
{
endmntent(mounts);
return xstrdup(mnt);
} // if
} // while
endmntent(mounts);
} // if

#else
# warning No mountpoint detection on this platform...
#endif

return NULL;
} // MojoPlatform_findMedia


void MojoPlatform_log(const char *str)
{
printf("%s\n", str);
Expand Down

0 comments on commit 0c39812

Please sign in to comment.