📄 stage1.cpp
字号:
/* ***** BEGIN LICENSE BLOCK ***** * Source last modified: $Id: stage1.cpp,v 1.1.2.2 2004/07/09 02:02:43 hubbe Exp $ * * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved. * * The contents of this file, and the files included with this file, * are subject to the current version of the RealNetworks Public * Source License (the "RPSL") available at * http://www.helixcommunity.org/content/rpsl unless you have licensed * the file under the current version of the RealNetworks Community * Source License (the "RCSL") available at * http://www.helixcommunity.org/content/rcsl, in which case the RCSL * will apply. You may also obtain the license terms directly from * RealNetworks. You may not use this file except in compliance with * the RPSL or, if you have a valid RCSL with RealNetworks applicable * to this file, the RCSL. Please see the applicable RPSL or RCSL for * the rights, obligations and limitations governing use of the * contents of the file. * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL") in which case the provisions of the GPL are applicable * instead of those above. If you wish to allow use of your version of * this file only under the terms of the GPL, and not to allow others * to use your version of this file under the terms of either the RPSL * or RCSL, indicate your decision by deleting the provisions above * and replace them with the notice and other provisions required by * the GPL. If you do not delete the provisions above, a recipient may * use your version of this file under the terms of any one of the * RPSL, the RCSL or the GPL. * * This file is part of the Helix DNA Technology. RealNetworks is the * developer of the Original Code and owns the copyrights in the * portions it created. * * This file, and the files included with this file, is distributed * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET * ENJOYMENT OR NON-INFRINGEMENT. * * Technology Compatibility Kit Test Suite(s) Location: * http://www.helixcommunity.org/content/tck * * Contributor(s): * * ***** END LICENSE BLOCK ***** *//*********************************************************************** * stage1.cpp * * A simple self-extracting installer * *********************************************************************** * * Create the tarball (or whatever archive type you're using). * Determine the size of the tarball. * Define ARCHIVE_SIZE in archive_info.h to be the tarball size. * Define ARCHIVE_TYPE_TGZ (or one of the others) in archive_info.h * if the tarball is a .tgz archive. * Compile this program. * When run, it untarrs the tarball and attempts to run a second-stage * installer inside it. * The second-stage installer may be called setup (binary), setup.sh * bash shell), setup.pl (perl), or setup.py (python), and it will * run the first it finds and exit. * * KEEP THIS THING GENERIC AND VERY SIMPLE , putting package-specific items * in the second-stage installer. The second-stage installer * is responsible for all cleanup. * ***********************************************************************/#include "hxtypes.h"#ifdef _WIN32#include <process.h>#include <direct.h>#ifndef mkdir#define mkdir(n,p) \ mkdir(n)#endif /* mkdir */#elif defined _UNIX#if defined(_FREEBSD) || defined(_OPENBSD) || defined(_NETBSD) || defined(_MAC_UNIX)#define TCGETS TIOCGETA#define TCSETS TIOCSETA#elif defined _AIX43#include <sys/termio.h>#endif /* _FREEBSD || _OPENBSD || _NETBSD || _MAC_UNIX */#include <termios.h>#include <unistd.h>#include <sys/ioctl.h>#endif /* _WIN32 */#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include "package_info.h"#include "stage1.h"#include "prodinstlib.h"/*********************************************************************** * Defines */#ifdef _WIN32#define PATH_SEP "\\"#else#define PATH_SEP "/"#endif#ifndef DESTDIR#define DESTDIR "." PATH_SEP "hxsetup"#endif#ifndef SECOND_STAGE_PROG#define SECOND_STAGE_PROG "Bin" PATH_SEP "setup"#endif#ifndef TMP_EXTRACT_PROG#define TMP_EXTRACT_PROG "xxextract.tmp"#endif#ifndef TRUE#define TRUE 1#endif#ifndef FALSE#define FALSE 0#endif/*********************************************************************** * Globals */char* g_szTmpDir = (char*)DESTDIR;char* g_szProg=0;#ifdef _CONSOLE struct termios g_oldtty;#endif //_CONSOLEStage1::Stage1() : m_ulInstFlags(0){ memset(m_szUserPass, 0, MAX_PASS);}Stage1::~Stage1(){}#ifdef _CONSOLE/************************************************************************ * Stage1::Main - first stage executable of the self-extracting installer * * Basic idea: * Open the binary file we're currently executing (argv[0]). * Seek to the end minus the size of the archive (.tar.gz/whatever) file. * Read the data and pipe it to the extraction utility. * Run the second-stage installer if found in the archive. */intStage1::Main(int nArchiveSize, int nArchiveProgSize, char* szArchiveType, int argc, char* argv[], const char* szPassword){ int nRet = 0; char* pArchiveData = new char[nArchiveSize + 1]; char* pArchiveProgData = new char[nArchiveProgSize + 1]; FILE* fp=0; int nBytes=0; char szCmd[1024]; bool bUsePipe = TRUE; const char* szTmpArchive=0; char szExtractProg[1024]; szExtractProg[0] = '\0'; m_szPassword = szPassword; ParseCmdLine(argc, argv); // Check the password if(m_szPassword && strcmp(m_szUserPass, m_szPassword) != 0 && ((m_ulInstFlags & INST_NON_INTERACTIVE) || !PasswordPrompt())) { Bail(0, NULL); } if(!(m_ulInstFlags & INST_SILENT)) { printf("Extracting files for %s installation...", PackageInfo::Name()); fflush(0); } fp = fopen(argv[0], "r"); // if archive prog is included in the archive, extract it if (nArchiveProgSize) { fseek(fp, -(nArchiveSize + nArchiveProgSize), SEEK_END); nBytes = fread(pArchiveData, sizeof(char), nArchiveProgSize, fp); if (nBytes != nArchiveProgSize) { if(!(m_ulInstFlags & INST_SILENT)) { printf("\nError: short read (%d/%d bytes) (%s)\n", nBytes, nArchiveProgSize, strerror(errno)); } nRet = -1; } unlink(TMP_EXTRACT_PROG); FILE* fp2 = fopen(TMP_EXTRACT_PROG, "w"); fwrite(pArchiveData, sizeof(char), nArchiveProgSize, fp2); fclose (fp2); chmod (TMP_EXTRACT_PROG, 0755); char szCurDir[1024]; memset (szCurDir, 0, 1024); getcwd(szCurDir, 1023); sprintf(szExtractProg, "%s%s%s", szCurDir, PATH_SEP, TMP_EXTRACT_PROG); } // read the archive file (.rzt, .tgz, whatever) fseek(fp, -(nArchiveSize), SEEK_END); nBytes = fread(pArchiveData, sizeof(char), nArchiveSize, fp); fclose(fp); if (nBytes != nArchiveSize) { if(!(m_ulInstFlags & INST_SILENT)) { printf("\nError: short read (%d/%d bytes) (%s)\n", nBytes, nArchiveSize, strerror(errno)); } nRet = -1; } system ("rm -rf " DESTDIR); if (mkdir(DESTDIR, 0700) != 0) Bail(-1, "\nError: mkdir failed"); if (chmod(DESTDIR, 0700) != 0) Bail(-1, "\nError: chmod failed");#if defined(_OPENBSD) const char* szTarOpts = "xf";#else const char* szTarOpts = "xof";#endif if (strcmp(szArchiveType, "bz2") == 0) { // TAR+BZIP2 // should work on any unix system with tar sprintf(szCmd, "%s -dc | (cd %s; tar %s -)", szExtractProg, DESTDIR, szTarOpts); } else if (strcmp(szArchiveType, "tgz") == 0) { // TAR+GZIP // should work on any unix system with gunzip and tar sprintf(szCmd, "gunzip -c | (cd %s; tar %s -)", DESTDIR, szTarOpts); } else if (strcmp(szArchiveType, "tz") == 0) { // TAR+COMPRESS // archives are pretty big but should work on any unix system with tar sprintf(szCmd, "uncompress | (cd %s; tar %s -)", DESTDIR, szTarOpts); } else if (strcmp(szArchiveType, "zip") == 0) { // ZIP // XXX untested, experimental, unfinished, don't use yet... bUsePipe = FALSE; szTmpArchive = "rsarchive.zip"; sprintf(szCmd, "cd %s; %s %s; rm -f %s\n", DESTDIR, szExtractProg, szTmpArchive, szTmpArchive); } else { Bail (-1, "\nError: Internal error -- unknown archive type\n"); } if (bUsePipe) { fp = popen(szCmd, "w"); } else { char szFile[1024]; sprintf (szFile, "%s%s%s", DESTDIR, PATH_SEP, szTmpArchive);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -