2007-03-22 10:30:00 -07:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Application Update.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Benjamin Smedberg <benjamin@smedbergs.us>
|
|
|
|
*
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2005
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Darin Fisher <darin@meer.net>
|
2008-06-23 12:06:37 -07:00
|
|
|
* Robert Strong <robert.bugzilla@gmail.com>
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. 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 MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
/**
|
2011-04-11 21:23:27 -07:00
|
|
|
* Manifest Format
|
2007-03-22 10:30:00 -07:00
|
|
|
* ---------------
|
|
|
|
*
|
|
|
|
* contents = 1*( line )
|
|
|
|
* line = method LWS *( param LWS ) CRLF
|
|
|
|
* CRLF = "\r\n"
|
|
|
|
* LWS = 1*( " " | "\t" )
|
2011-04-11 21:23:27 -07:00
|
|
|
*
|
|
|
|
* Available methods for the different manifest files:
|
|
|
|
*
|
|
|
|
* update.manifest
|
|
|
|
* ---------------
|
|
|
|
* method = "add" | "add-if" | "patch" | "patch-if" | "remove"
|
|
|
|
*
|
|
|
|
* updatev2.manifest
|
|
|
|
* -----------------
|
2012-01-18 14:10:38 -08:00
|
|
|
* method = "add" | "add-if" | "patch" | "patch-if" | "remove" |
|
2011-04-11 21:23:27 -07:00
|
|
|
* "rmdir" | "rmrfdir" | type
|
|
|
|
*
|
|
|
|
* 'type' is the update type (e.g. complete or partial) and when present MUST
|
|
|
|
* be the first entry in the update manifest. The type is used to support
|
|
|
|
* downgrades by causing the actions defined in precomplete to be performed.
|
|
|
|
*
|
|
|
|
* precomplete
|
|
|
|
* -----------
|
2012-01-18 14:10:38 -08:00
|
|
|
* method = "remove" | "rmdir"
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
#include "bspatch.h"
|
|
|
|
#include "progressui.h"
|
|
|
|
#include "archivereader.h"
|
|
|
|
#include "errors.h"
|
|
|
|
#include "bzlib.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2012-01-04 20:19:14 -08:00
|
|
|
#include "updatelogging.h"
|
|
|
|
|
2009-10-06 16:39:51 -07:00
|
|
|
// Amount of the progress bar to use in each of the 3 update stages,
|
|
|
|
// should total 100.0.
|
|
|
|
#define PROGRESS_PREPARE_SIZE 20.0f
|
|
|
|
#define PROGRESS_EXECUTE_SIZE 75.0f
|
|
|
|
#define PROGRESS_FINISH_SIZE 5.0f
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#if defined(XP_MACOSX)
|
2010-05-22 14:10:31 -07:00
|
|
|
// These functions are defined in launchchild_osx.mm
|
2007-03-22 10:30:00 -07:00
|
|
|
void LaunchChild(int argc, char **argv);
|
2010-05-22 14:10:31 -07:00
|
|
|
void LaunchMacPostProcess(const char* aAppExe);
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _O_BINARY
|
|
|
|
# define _O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef NULL
|
|
|
|
# define NULL (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SSIZE_MAX
|
|
|
|
# define SSIZE_MAX LONG_MAX
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// We want to use execv to invoke the callback executable on platforms where
|
|
|
|
// we were launched using execv. See nsUpdateDriver.cpp.
|
|
|
|
#if defined(XP_UNIX) && !defined(XP_MACOSX)
|
|
|
|
#define USE_EXECV
|
|
|
|
#endif
|
|
|
|
|
2009-11-13 13:11:20 -08:00
|
|
|
#ifdef XP_WIN
|
2012-01-04 20:19:15 -08:00
|
|
|
#include "updatehelper.h"
|
2012-01-04 20:19:14 -08:00
|
|
|
|
2009-11-13 13:11:20 -08:00
|
|
|
// Closes the handle if valid and if the updater is elevated returns with the
|
|
|
|
// return code specified. This prevents multiple launches of the callback
|
|
|
|
// application by preventing the elevated process from launching the callback.
|
|
|
|
#define EXIT_WHEN_ELEVATED(path, handle, retCode) \
|
|
|
|
{ \
|
|
|
|
if (handle != INVALID_HANDLE_VALUE) { \
|
|
|
|
CloseHandle(handle); \
|
|
|
|
} \
|
|
|
|
if (_waccess(path, F_OK) == 0 && NS_tremove(path) != 0) { \
|
|
|
|
return retCode; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// This variable lives in libbz2. It's declared in bzlib_private.h, so we just
|
|
|
|
// declare it here to avoid including that entire header file.
|
2008-03-12 04:13:09 -07:00
|
|
|
#if (__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
|
|
|
|
extern "C" __attribute__((visibility("default"))) unsigned int BZ2_crc32Table[256];
|
2008-09-07 23:21:07 -07:00
|
|
|
#elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
|
|
|
|
extern "C" __global unsigned int BZ2_crc32Table[256];
|
2008-03-12 04:13:09 -07:00
|
|
|
#else
|
2007-03-22 10:30:00 -07:00
|
|
|
extern "C" unsigned int BZ2_crc32Table[256];
|
2008-03-12 04:13:09 -07:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static unsigned int
|
|
|
|
crc32(const unsigned char *buf, unsigned int len)
|
|
|
|
{
|
|
|
|
unsigned int crc = 0xffffffffL;
|
|
|
|
|
|
|
|
const unsigned char *end = buf + len;
|
|
|
|
for (; buf != end; ++buf)
|
|
|
|
crc = (crc << 8) ^ BZ2_crc32Table[(crc >> 24) ^ *buf];
|
|
|
|
|
|
|
|
crc = ~crc;
|
|
|
|
return crc;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2009-07-16 15:21:09 -07:00
|
|
|
// A simple stack based container for a FILE struct that closes the
|
2007-03-22 10:30:00 -07:00
|
|
|
// file descriptor from its destructor.
|
2009-07-16 15:21:09 -07:00
|
|
|
class AutoFile
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
2009-07-16 15:21:09 -07:00
|
|
|
AutoFile(FILE* file = NULL)
|
|
|
|
: mFile(file) {
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-07-16 15:21:09 -07:00
|
|
|
~AutoFile() {
|
|
|
|
if (mFile != NULL)
|
|
|
|
fclose(mFile);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-07-16 15:21:09 -07:00
|
|
|
AutoFile &operator=(FILE* file) {
|
|
|
|
if (mFile != 0)
|
|
|
|
fclose(mFile);
|
|
|
|
mFile = file;
|
2007-03-22 10:30:00 -07:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2009-07-16 15:21:09 -07:00
|
|
|
operator FILE*() {
|
|
|
|
return mFile;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-04-11 21:23:27 -07:00
|
|
|
|
2009-07-21 00:25:37 -07:00
|
|
|
FILE* get() {
|
|
|
|
return mFile;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
2009-07-16 15:21:09 -07:00
|
|
|
FILE* mFile;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
typedef void (* ThreadFunc)(void *param);
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <process.h>
|
|
|
|
|
|
|
|
class Thread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int Run(ThreadFunc func, void *param)
|
|
|
|
{
|
|
|
|
mThreadFunc = func;
|
|
|
|
mThreadParam = param;
|
|
|
|
|
2009-07-16 15:21:09 -07:00
|
|
|
unsigned int threadID;
|
2011-04-11 21:23:27 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
mThread = (HANDLE) _beginthreadex(NULL, 0, ThreadMain, this, 0, &threadID);
|
2011-04-11 21:23:27 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return mThread ? 0 : -1;
|
|
|
|
}
|
|
|
|
int Join()
|
|
|
|
{
|
|
|
|
WaitForSingleObject(mThread, INFINITE);
|
|
|
|
CloseHandle(mThread);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
static unsigned __stdcall ThreadMain(void *p)
|
|
|
|
{
|
|
|
|
Thread *self = (Thread *) p;
|
|
|
|
self->mThreadFunc(self->mThreadParam);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
HANDLE mThread;
|
|
|
|
ThreadFunc mThreadFunc;
|
|
|
|
void *mThreadParam;
|
|
|
|
};
|
|
|
|
|
|
|
|
#elif defined(XP_UNIX)
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
class Thread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int Run(ThreadFunc func, void *param)
|
|
|
|
{
|
|
|
|
return pthread_create(&thr, NULL, (void* (*)(void *)) func, param);
|
|
|
|
}
|
|
|
|
int Join()
|
|
|
|
{
|
|
|
|
void *result;
|
|
|
|
return pthread_join(thr, &result);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
pthread_t thr;
|
|
|
|
};
|
|
|
|
|
|
|
|
#elif defined(XP_OS2)
|
|
|
|
|
|
|
|
class Thread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int Run(ThreadFunc func, void *param)
|
|
|
|
{
|
|
|
|
mThreadFunc = func;
|
|
|
|
mThreadParam = param;
|
|
|
|
|
|
|
|
mThread = _beginthread(ThreadMain, NULL, 16384, (void *)this);
|
2011-04-11 21:23:27 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return mThread ? 0 : -1;
|
|
|
|
}
|
|
|
|
int Join()
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
waitpid(mThread, &status, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
static void ThreadMain(void *p)
|
|
|
|
{
|
|
|
|
Thread *self = (Thread *) p;
|
|
|
|
self->mThreadFunc(self->mThreadParam);
|
|
|
|
}
|
|
|
|
int mThread;
|
|
|
|
ThreadFunc mThreadFunc;
|
|
|
|
void *mThreadParam;
|
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
#error "Unsupported platform"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2009-11-13 13:11:20 -08:00
|
|
|
static NS_tchar* gSourcePath;
|
|
|
|
static ArchiveReader gArchiveReader;
|
2010-05-22 14:10:31 -07:00
|
|
|
static bool gSucceeded = false;
|
2009-11-13 13:11:20 -08:00
|
|
|
|
|
|
|
#ifdef XP_WIN
|
2011-04-11 21:23:27 -07:00
|
|
|
// The current working directory specified in the command line.
|
2009-07-29 23:01:36 -07:00
|
|
|
static NS_tchar* gDestPath;
|
2011-04-11 21:23:27 -07:00
|
|
|
static NS_tchar gCallbackRelPath[MAXPATHLEN];
|
|
|
|
static NS_tchar gCallbackBackupPath[MAXPATHLEN];
|
2007-07-30 13:31:26 -07:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
static const NS_tchar kWhitespace[] = NS_T(" \t");
|
|
|
|
static const NS_tchar kNL[] = NS_T("\r\n");
|
|
|
|
static const NS_tchar kQuote[] = NS_T("\"");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-23 15:31:32 -07:00
|
|
|
static inline size_t
|
|
|
|
mmin(size_t a, size_t b)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return (a > b) ? b : a;
|
|
|
|
}
|
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
static NS_tchar*
|
|
|
|
mstrtok(const NS_tchar *delims, NS_tchar **str)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!*str || !**str)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// skip leading "whitespace"
|
2011-04-11 21:23:27 -07:00
|
|
|
NS_tchar *ret = *str;
|
|
|
|
const NS_tchar *d;
|
2007-03-22 10:30:00 -07:00
|
|
|
do {
|
2011-04-11 21:23:27 -07:00
|
|
|
for (d = delims; *d != NS_T('\0'); ++d) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (*ret == *d) {
|
|
|
|
++ret;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (*d);
|
|
|
|
|
|
|
|
if (!*ret) {
|
|
|
|
*str = ret;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
NS_tchar *i = ret;
|
2007-03-22 10:30:00 -07:00
|
|
|
do {
|
2011-04-11 21:23:27 -07:00
|
|
|
for (d = delims; *d != NS_T('\0'); ++d) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (*i == *d) {
|
2011-04-11 21:23:27 -07:00
|
|
|
*i = NS_T('\0');
|
2007-03-22 10:30:00 -07:00
|
|
|
*str = ++i;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
} while (*i);
|
|
|
|
|
|
|
|
*str = NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-07-29 23:01:36 -07:00
|
|
|
#ifdef XP_WIN
|
2011-04-11 21:23:27 -07:00
|
|
|
/**
|
|
|
|
* Coverts a relative update path to a full path for Windows.
|
|
|
|
*
|
|
|
|
* @param relpath
|
|
|
|
* The relative path to convert to a full path.
|
|
|
|
* @return valid filesystem full path or NULL memory allocation fails.
|
|
|
|
*/
|
2009-07-29 23:01:36 -07:00
|
|
|
static NS_tchar*
|
2011-04-11 21:23:27 -07:00
|
|
|
get_full_path(const NS_tchar *relpath)
|
2009-07-29 23:01:36 -07:00
|
|
|
{
|
2011-04-11 21:23:27 -07:00
|
|
|
size_t lendestpath = NS_tstrlen(gDestPath);
|
|
|
|
size_t lenrelpath = NS_tstrlen(relpath);
|
|
|
|
NS_tchar *s = (NS_tchar *) malloc((lendestpath + lenrelpath + 1) * sizeof(NS_tchar));
|
2009-07-29 23:01:36 -07:00
|
|
|
if (!s)
|
|
|
|
return NULL;
|
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
NS_tchar *c = s;
|
|
|
|
|
|
|
|
NS_tstrcpy(c, gDestPath);
|
|
|
|
c += lendestpath;
|
|
|
|
NS_tstrcat(c, relpath);
|
|
|
|
c += lenrelpath;
|
|
|
|
*c = NS_T('\0');
|
|
|
|
c++;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the platform specific path and performs simple checks to the path. If
|
|
|
|
* the path checks don't pass NULL will be returned.
|
|
|
|
*
|
|
|
|
* @param line
|
|
|
|
* The line from the manifest that contains the path.
|
|
|
|
* @param isdir
|
|
|
|
* Whether the path is a directory path. Defaults to false.
|
|
|
|
* @return valid filesystem path or NULL if the path checks fail.
|
|
|
|
*/
|
|
|
|
static NS_tchar*
|
|
|
|
get_valid_path(NS_tchar **line, bool isdir = false)
|
|
|
|
{
|
|
|
|
NS_tchar *path = mstrtok(kQuote, line);
|
|
|
|
if (!path) {
|
|
|
|
LOG(("get_valid_path: unable to determine path: " LOG_S "\n", line));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All paths must be relative from the current working directory
|
|
|
|
if (path[0] == NS_T('/')) {
|
|
|
|
LOG(("get_valid_path: path must be relative: " LOG_S "\n", path));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
// All paths must be relative from the current working directory
|
|
|
|
if (path[0] == NS_T('\\') || path[1] == NS_T(':')) {
|
|
|
|
LOG(("get_valid_path: path must be relative: " LOG_S "\n", path));
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-07-29 23:01:36 -07:00
|
|
|
#endif
|
2011-04-11 21:23:27 -07:00
|
|
|
|
|
|
|
if (isdir) {
|
|
|
|
// Directory paths must have a trailing forward slash.
|
|
|
|
if (path[NS_tstrlen(path) - 1] != NS_T('/')) {
|
|
|
|
LOG(("get_valid_path: directory paths must have a trailing forward " \
|
|
|
|
"slash: " LOG_S "\n", path));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the trailing forward slash because stat on Windows will return
|
|
|
|
// ENOENT if the path has a trailing slash.
|
|
|
|
path[NS_tstrlen(path) - 1] = NS_T('\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't allow relative paths that resolve to a parent directory.
|
|
|
|
if (NS_tstrstr(path, NS_T("..")) != NULL) {
|
|
|
|
LOG(("get_valid_path: paths must not contain '..': " LOG_S "\n", path));
|
|
|
|
return NULL;
|
2009-11-13 13:11:20 -08:00
|
|
|
}
|
2011-04-11 21:23:27 -07:00
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NS_tchar*
|
|
|
|
get_quoted_path(const NS_tchar *path)
|
|
|
|
{
|
|
|
|
size_t lenQuote = NS_tstrlen(kQuote);
|
|
|
|
size_t lenPath = NS_tstrlen(path);
|
|
|
|
size_t len = lenQuote + lenPath + lenQuote + 1;
|
|
|
|
|
|
|
|
NS_tchar *s = (NS_tchar *) malloc(len * sizeof(NS_tchar));
|
|
|
|
if (!s)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
NS_tchar *c = s;
|
|
|
|
NS_tstrcpy(c, kQuote);
|
|
|
|
c += lenQuote;
|
|
|
|
NS_tstrcat(c, path);
|
|
|
|
c += lenPath;
|
|
|
|
NS_tstrcat(c, kQuote);
|
|
|
|
c += lenQuote;
|
2009-07-29 23:01:36 -07:00
|
|
|
*c = NS_T('\0');
|
|
|
|
c++;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ensure_write_permissions(const NS_tchar *path)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
#ifdef XP_WIN
|
2009-07-29 23:01:36 -07:00
|
|
|
(void) _wchmod(path, _S_IREAD | _S_IWRITE);
|
2007-03-22 10:30:00 -07:00
|
|
|
#else
|
|
|
|
struct stat fs;
|
|
|
|
if (!stat(path, &fs) && !(fs.st_mode & S_IWUSR)) {
|
|
|
|
(void)chmod(path, fs.st_mode | S_IWUSR);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-07-29 23:01:36 -07:00
|
|
|
static int ensure_remove(const NS_tchar *path)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
ensure_write_permissions(path);
|
2009-07-29 23:01:36 -07:00
|
|
|
int rv = NS_tremove(path);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (rv)
|
2010-11-18 17:20:44 -08:00
|
|
|
LOG(("ensure_remove: failed to remove file: " LOG_S ", rv: %d, err: %d\n",
|
|
|
|
path, rv, errno));
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
static FILE* ensure_open(const NS_tchar *path, const NS_tchar *flags, unsigned int options)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
ensure_write_permissions(path);
|
2009-07-29 23:01:36 -07:00
|
|
|
FILE* f = NS_tfopen(path, flags);
|
|
|
|
if (NS_tchmod(path, options) != 0) {
|
2009-11-13 13:11:32 -08:00
|
|
|
if (f != NULL) {
|
|
|
|
fclose(f);
|
|
|
|
}
|
2009-07-20 15:53:46 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
struct stat ss;
|
2009-07-29 23:01:36 -07:00
|
|
|
if (NS_tstat(path, &ss) != 0 || ss.st_mode != options) {
|
2009-11-13 13:11:32 -08:00
|
|
|
if (f != NULL) {
|
|
|
|
fclose(f);
|
|
|
|
}
|
2009-07-20 15:53:46 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return f;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the directory containing this file exists.
|
2009-07-29 23:01:36 -07:00
|
|
|
static int ensure_parent_dir(const NS_tchar *path)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
int rv = OK;
|
|
|
|
|
2009-07-29 23:01:36 -07:00
|
|
|
NS_tchar *slash = (NS_tchar *) NS_tstrrchr(path, NS_T('/'));
|
|
|
|
if (slash) {
|
|
|
|
*slash = NS_T('\0');
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = ensure_parent_dir(path);
|
|
|
|
if (rv == OK) {
|
2009-07-29 23:01:36 -07:00
|
|
|
rv = NS_tmkdir(path, 0755);
|
2011-04-11 21:23:27 -07:00
|
|
|
// If the directory already exists, then ignore the error.
|
2007-03-22 10:30:00 -07:00
|
|
|
if (rv < 0 && errno != EEXIST) {
|
2010-11-18 17:20:44 -08:00
|
|
|
LOG(("ensure_parent_dir: failed to create directory: " LOG_S ", " \
|
|
|
|
"err: %d\n", path, errno));
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = WRITE_ERROR;
|
|
|
|
} else {
|
|
|
|
rv = OK;
|
|
|
|
}
|
|
|
|
}
|
2009-07-29 23:01:36 -07:00
|
|
|
*slash = NS_T('/');
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2010-11-18 17:20:44 -08:00
|
|
|
// Renames the specified file to the new file specified. If the destination file
|
|
|
|
// exists it is removed.
|
|
|
|
static int rename_file(const NS_tchar *spath, const NS_tchar *dpath)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
int rv = ensure_parent_dir(dpath);
|
|
|
|
if (rv)
|
|
|
|
return rv;
|
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
struct stat spathInfo;
|
|
|
|
rv = NS_tstat(spath, &spathInfo);
|
|
|
|
if (rv) {
|
|
|
|
LOG(("rename_file: failed to read file status info: " LOG_S ", " \
|
|
|
|
"err: %d\n", spath, errno));
|
2007-03-22 10:30:00 -07:00
|
|
|
return READ_ERROR;
|
|
|
|
}
|
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
if (!S_ISREG(spathInfo.st_mode)) {
|
|
|
|
LOG(("rename_file: path present, but not a file: " LOG_S ", err: %d\n",
|
|
|
|
spath, errno));
|
|
|
|
return UNEXPECTED_ERROR;
|
|
|
|
}
|
|
|
|
|
2010-11-18 17:20:44 -08:00
|
|
|
if (!NS_taccess(dpath, F_OK)) {
|
|
|
|
if (ensure_remove(dpath)) {
|
|
|
|
LOG(("rename_file: destination file exists and could not be " \
|
|
|
|
"removed: " LOG_S "\n", dpath));
|
2007-03-22 10:30:00 -07:00
|
|
|
return WRITE_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2010-11-18 17:20:44 -08:00
|
|
|
|
|
|
|
if (NS_trename(spath, dpath) != 0) {
|
|
|
|
LOG(("rename_file: failed to rename file - src: " LOG_S ", " \
|
|
|
|
"dst:" LOG_S ", err: %d\n", spath, dpath, errno));
|
|
|
|
return WRITE_ERROR;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2010-11-18 17:20:44 -08:00
|
|
|
// Create a backup of the specified file by renaming it.
|
2009-07-29 23:01:36 -07:00
|
|
|
static int backup_create(const NS_tchar *path)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-07-29 23:01:36 -07:00
|
|
|
NS_tchar backup[MAXPATHLEN];
|
2010-09-20 21:41:38 -07:00
|
|
|
NS_tsnprintf(backup, sizeof(backup)/sizeof(backup[0]),
|
|
|
|
NS_T("%s" BACKUP_EXT), path);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-11-18 17:20:44 -08:00
|
|
|
return rename_file(path, backup);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-11-18 17:20:44 -08:00
|
|
|
// Rename the backup of the specified file that was created by renaming it back
|
|
|
|
// to the original file.
|
2009-07-29 23:01:36 -07:00
|
|
|
static int backup_restore(const NS_tchar *path)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-07-29 23:01:36 -07:00
|
|
|
NS_tchar backup[MAXPATHLEN];
|
2010-09-20 21:41:38 -07:00
|
|
|
NS_tsnprintf(backup, sizeof(backup)/sizeof(backup[0]),
|
|
|
|
NS_T("%s" BACKUP_EXT), path);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-11-18 17:20:44 -08:00
|
|
|
if (NS_taccess(backup, F_OK)) {
|
|
|
|
LOG(("backup_restore: backup file doesn't exist: " LOG_S "\n", backup));
|
|
|
|
return OK;
|
2009-11-13 13:11:32 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-11-18 17:20:44 -08:00
|
|
|
return rename_file(backup, path);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-11-18 17:20:44 -08:00
|
|
|
// Discard the backup of the specified file that was created by renaming it.
|
2009-07-29 23:01:36 -07:00
|
|
|
static int backup_discard(const NS_tchar *path)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-07-29 23:01:36 -07:00
|
|
|
NS_tchar backup[MAXPATHLEN];
|
2010-09-20 21:41:38 -07:00
|
|
|
NS_tsnprintf(backup, sizeof(backup)/sizeof(backup[0]),
|
|
|
|
NS_T("%s" BACKUP_EXT), path);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-11-18 17:20:44 -08:00
|
|
|
// Nothing to discard
|
|
|
|
if (NS_taccess(backup, F_OK)) {
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
int rv = ensure_remove(backup);
|
2011-04-11 21:23:27 -07:00
|
|
|
#if defined(XP_WIN)
|
2010-11-18 17:20:44 -08:00
|
|
|
if (rv) {
|
|
|
|
LOG(("backup_discard: unable to remove: " LOG_S "\n", backup));
|
|
|
|
NS_tchar path[MAXPATHLEN];
|
|
|
|
GetTempFileNameW(DELETE_DIR, L"moz", 0, path);
|
|
|
|
if (rename_file(backup, path)) {
|
|
|
|
LOG(("backup_discard: failed to rename file:" LOG_S ", dst:" LOG_S "\n",
|
|
|
|
backup, path));
|
|
|
|
return WRITE_ERROR;
|
|
|
|
}
|
|
|
|
// The MoveFileEx call to remove the file on OS reboot will fail if the
|
|
|
|
// process doesn't have write access to the HKEY_LOCAL_MACHINE registry key
|
|
|
|
// but this is ok since the installer / uninstaller will delete the
|
|
|
|
// directory containing the file along with its contents after an update is
|
|
|
|
// applied, on reinstall, and on uninstall.
|
|
|
|
if (MoveFileEx(path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT)) {
|
|
|
|
LOG(("backup_discard: file renamed and will be removed on OS " \
|
|
|
|
"reboot: " LOG_S "\n", path));
|
|
|
|
} else {
|
|
|
|
LOG(("backup_discard: failed to schedule OS reboot removal of " \
|
|
|
|
"file: " LOG_S "\n", path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2007-03-22 10:30:00 -07:00
|
|
|
if (rv)
|
|
|
|
return WRITE_ERROR;
|
2010-11-18 17:20:44 -08:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function for post-processing a temporary backup.
|
2009-07-29 23:01:36 -07:00
|
|
|
static void backup_finish(const NS_tchar *path, int status)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (status == OK)
|
|
|
|
backup_discard(path);
|
|
|
|
else
|
|
|
|
backup_restore(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static int DoUpdate();
|
|
|
|
|
|
|
|
class Action
|
|
|
|
{
|
|
|
|
public:
|
2009-10-06 16:39:51 -07:00
|
|
|
Action() : mProgressCost(1), mNext(NULL) { }
|
2007-03-22 10:30:00 -07:00
|
|
|
virtual ~Action() { }
|
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
virtual int Parse(NS_tchar *line) = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Do any preprocessing to ensure that the action can be performed. Execute
|
|
|
|
// will be called if this Action and all others return OK from this method.
|
|
|
|
virtual int Prepare() = 0;
|
|
|
|
|
|
|
|
// Perform the operation. Return OK to indicate success. After all actions
|
|
|
|
// have been executed, Finish will be called. A requirement of Execute is
|
2008-10-01 23:49:45 -07:00
|
|
|
// that its operation be reversable from Finish.
|
2007-03-22 10:30:00 -07:00
|
|
|
virtual int Execute() = 0;
|
2011-04-11 21:23:27 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Finish is called after execution of all actions. If status is OK, then
|
|
|
|
// all actions were successfully executed. Otherwise, some action failed.
|
|
|
|
virtual void Finish(int status) = 0;
|
|
|
|
|
2009-10-06 16:39:51 -07:00
|
|
|
int mProgressCost;
|
2007-03-22 10:30:00 -07:00
|
|
|
private:
|
|
|
|
Action* mNext;
|
|
|
|
|
|
|
|
friend class ActionList;
|
|
|
|
};
|
|
|
|
|
|
|
|
class RemoveFile : public Action
|
|
|
|
{
|
|
|
|
public:
|
2011-04-11 21:23:27 -07:00
|
|
|
RemoveFile() : mFile(NULL), mSkip(0) { }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
int Parse(NS_tchar *line);
|
2007-03-22 10:30:00 -07:00
|
|
|
int Prepare();
|
|
|
|
int Execute();
|
|
|
|
void Finish(int status);
|
|
|
|
|
|
|
|
private:
|
2011-04-11 21:23:27 -07:00
|
|
|
const NS_tchar *mFile;
|
2007-03-22 10:30:00 -07:00
|
|
|
int mSkip;
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
2011-04-11 21:23:27 -07:00
|
|
|
RemoveFile::Parse(NS_tchar *line)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// format "<deadfile>"
|
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
mFile = get_valid_path(&line);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!mFile)
|
|
|
|
return PARSE_ERROR;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
RemoveFile::Prepare()
|
|
|
|
{
|
2011-04-11 21:23:27 -07:00
|
|
|
// Skip the file if it already doesn't exist.
|
|
|
|
int rv = NS_taccess(mFile, F_OK);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (rv) {
|
|
|
|
mSkip = 1;
|
2009-10-06 16:39:51 -07:00
|
|
|
mProgressCost = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
LOG(("PREPARE REMOVEFILE " LOG_S "\n", mFile));
|
|
|
|
|
|
|
|
// Make sure that we're actually a file...
|
|
|
|
struct stat fileInfo;
|
|
|
|
rv = NS_tstat(mFile, &fileInfo);
|
|
|
|
if (rv) {
|
|
|
|
LOG(("failed to read file status info: " LOG_S ", err: %d\n", mFile,
|
|
|
|
errno));
|
|
|
|
return READ_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!S_ISREG(fileInfo.st_mode)) {
|
|
|
|
LOG(("path present, but not a file: " LOG_S "\n", mFile));
|
|
|
|
return UNEXPECTED_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_tchar *slash = (NS_tchar *) NS_tstrrchr(mFile, NS_T('/'));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (slash) {
|
2009-07-29 23:01:36 -07:00
|
|
|
*slash = NS_T('\0');
|
2011-04-11 21:23:27 -07:00
|
|
|
rv = NS_taccess(mFile, W_OK);
|
2009-07-29 23:01:36 -07:00
|
|
|
*slash = NS_T('/');
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2009-07-29 23:01:36 -07:00
|
|
|
rv = NS_taccess(NS_T("."), W_OK);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rv) {
|
|
|
|
LOG(("access failed: %d\n", errno));
|
|
|
|
return WRITE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
RemoveFile::Execute()
|
|
|
|
{
|
|
|
|
if (mSkip)
|
|
|
|
return OK;
|
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
LOG(("EXECUTE REMOVEFILE " LOG_S "\n", mFile));
|
2009-10-06 16:39:51 -07:00
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
// The file is checked for existence here and in Prepare since it might have
|
|
|
|
// been removed by a separate instruction: bug 311099.
|
|
|
|
int rv = NS_taccess(mFile, F_OK);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (rv) {
|
|
|
|
LOG(("file cannot be removed because it does not exist; skipping\n"));
|
|
|
|
mSkip = 1;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
// Rename the old file. It will be removed in Finish.
|
|
|
|
rv = backup_create(mFile);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (rv) {
|
|
|
|
LOG(("backup_create failed: %d\n", rv));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RemoveFile::Finish(int status)
|
|
|
|
{
|
|
|
|
if (mSkip)
|
|
|
|
return;
|
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
LOG(("FINISH REMOVEFILE " LOG_S "\n", mFile));
|
|
|
|
|
|
|
|
backup_finish(mFile, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
class RemoveDir : public Action
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RemoveDir() : mDir(NULL), mSkip(0) { }
|
2009-10-06 16:39:51 -07:00
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
virtual int Parse(NS_tchar *line);
|
|
|
|
virtual int Prepare(); // check that the source dir exists
|
|
|
|
virtual int Execute();
|
|
|
|
virtual void Finish(int status);
|
|
|
|
|
|
|
|
private:
|
|
|
|
const NS_tchar *mDir;
|
|
|
|
int mSkip;
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
RemoveDir::Parse(NS_tchar *line)
|
|
|
|
{
|
|
|
|
// format "<deaddir>/"
|
|
|
|
|
|
|
|
mDir = get_valid_path(&line, true);
|
|
|
|
if (!mDir)
|
|
|
|
return PARSE_ERROR;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
RemoveDir::Prepare()
|
|
|
|
{
|
|
|
|
// We expect the directory to exist if we are to remove it.
|
|
|
|
int rv = NS_taccess(mDir, F_OK);
|
|
|
|
if (rv) {
|
|
|
|
mSkip = 1;
|
|
|
|
mProgressCost = 0;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(("PREPARE REMOVEDIR " LOG_S "/\n", mDir));
|
|
|
|
|
|
|
|
// Make sure that we're actually a dir.
|
|
|
|
struct stat dirInfo;
|
|
|
|
rv = NS_tstat(mDir, &dirInfo);
|
|
|
|
if (rv) {
|
|
|
|
LOG(("failed to read directory status info: " LOG_S ", err: %d\n", mDir,
|
|
|
|
errno));
|
|
|
|
return READ_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!S_ISDIR(dirInfo.st_mode)) {
|
|
|
|
LOG(("path present, but not a directory: " LOG_S "\n", mDir));
|
|
|
|
return UNEXPECTED_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = NS_taccess(mDir, W_OK);
|
|
|
|
if (rv) {
|
|
|
|
LOG(("access failed: %d, %d\n", rv, errno));
|
|
|
|
return WRITE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
RemoveDir::Execute()
|
|
|
|
{
|
|
|
|
if (mSkip)
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
LOG(("EXECUTE REMOVEDIR " LOG_S "/\n", mDir));
|
|
|
|
|
|
|
|
// The directory is checked for existence at every step since it might have
|
|
|
|
// been removed by a separate instruction: bug 311099.
|
|
|
|
int rv = NS_taccess(mDir, F_OK);
|
|
|
|
if (rv) {
|
|
|
|
LOG(("directory no longer exists; skipping\n"));
|
|
|
|
mSkip = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RemoveDir::Finish(int status)
|
|
|
|
{
|
|
|
|
if (mSkip || status != OK)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LOG(("FINISH REMOVEDIR " LOG_S "/\n", mDir));
|
|
|
|
|
|
|
|
// The directory is checked for existence at every step since it might have
|
|
|
|
// been removed by a separate instruction: bug 311099.
|
|
|
|
int rv = NS_taccess(mDir, F_OK);
|
|
|
|
if (rv) {
|
|
|
|
LOG(("directory no longer exists; skipping\n"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (status == OK) {
|
|
|
|
if (NS_trmdir(mDir)) {
|
|
|
|
LOG(("non-fatal error removing directory: " LOG_S "/, rv: %d, err: %d\n",
|
|
|
|
mDir, rv, errno));
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class AddFile : public Action
|
|
|
|
{
|
|
|
|
public:
|
2011-04-11 21:23:27 -07:00
|
|
|
AddFile() : mFile(NULL)
|
|
|
|
, mAdded(false)
|
|
|
|
{ }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
virtual int Parse(NS_tchar *line);
|
|
|
|
virtual int Prepare();
|
2007-03-22 10:30:00 -07:00
|
|
|
virtual int Execute();
|
|
|
|
virtual void Finish(int status);
|
|
|
|
|
|
|
|
private:
|
2011-04-11 21:23:27 -07:00
|
|
|
const NS_tchar *mFile;
|
|
|
|
bool mAdded;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
int
|
2011-04-11 21:23:27 -07:00
|
|
|
AddFile::Parse(NS_tchar *line)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// format "<newfile>"
|
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
mFile = get_valid_path(&line);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!mFile)
|
|
|
|
return PARSE_ERROR;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
AddFile::Prepare()
|
|
|
|
{
|
2011-04-11 21:23:27 -07:00
|
|
|
LOG(("PREPARE ADD " LOG_S "\n", mFile));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
AddFile::Execute()
|
|
|
|
{
|
2011-04-11 21:23:27 -07:00
|
|
|
LOG(("EXECUTE ADD " LOG_S "\n", mFile));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
// First make sure that we can actually get rid of any existing file.
|
2011-04-11 21:23:27 -07:00
|
|
|
rv = NS_taccess(mFile, F_OK);
|
2009-07-29 23:01:36 -07:00
|
|
|
if (rv == 0) {
|
2011-04-11 21:23:27 -07:00
|
|
|
rv = backup_create(mFile);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (rv)
|
|
|
|
return rv;
|
2009-07-29 23:01:36 -07:00
|
|
|
} else {
|
2011-04-11 21:23:27 -07:00
|
|
|
rv = ensure_parent_dir(mFile);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (rv)
|
|
|
|
return rv;
|
|
|
|
}
|
2009-07-29 23:01:36 -07:00
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
#ifdef XP_WIN
|
|
|
|
char sourcefile[MAXPATHLEN];
|
|
|
|
if (!WideCharToMultiByte(CP_UTF8, 0, mFile, -1, sourcefile, MAXPATHLEN,
|
|
|
|
NULL, NULL)) {
|
|
|
|
LOG(("error converting wchar to utf8: %d\n", GetLastError()));
|
2012-01-18 10:43:21 -08:00
|
|
|
return STRING_CONVERSION_ERROR;
|
2011-04-11 21:23:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
rv = gArchiveReader.ExtractFile(sourcefile, mFile);
|
|
|
|
#else
|
|
|
|
rv = gArchiveReader.ExtractFile(mFile, mFile);
|
|
|
|
#endif
|
|
|
|
if (!rv) {
|
|
|
|
mAdded = true;
|
|
|
|
}
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AddFile::Finish(int status)
|
|
|
|
{
|
2011-04-11 21:23:27 -07:00
|
|
|
LOG(("FINISH ADD " LOG_S "\n", mFile));
|
|
|
|
// When there is an update failure and a file has been added it is removed
|
|
|
|
// here since there might not be a backup to replace it.
|
|
|
|
if (status && mAdded)
|
|
|
|
NS_tremove(mFile);
|
|
|
|
backup_finish(mFile, status);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class PatchFile : public Action
|
|
|
|
{
|
|
|
|
public:
|
2009-11-18 21:58:16 -08:00
|
|
|
PatchFile() : mPatchIndex(-1), buf(NULL) { }
|
2011-04-11 21:23:27 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
virtual ~PatchFile();
|
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
virtual int Parse(NS_tchar *line);
|
|
|
|
virtual int Prepare(); // should check for patch file and for checksum here
|
2007-03-22 10:30:00 -07:00
|
|
|
virtual int Execute();
|
|
|
|
virtual void Finish(int status);
|
|
|
|
|
|
|
|
private:
|
2009-07-16 15:21:09 -07:00
|
|
|
int LoadSourceFile(FILE* ofile);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static int sPatchIndex;
|
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
const NS_tchar *mPatchFile;
|
|
|
|
const NS_tchar *mFile;
|
2007-03-22 10:30:00 -07:00
|
|
|
int mPatchIndex;
|
|
|
|
MBSPatchHeader header;
|
|
|
|
unsigned char *buf;
|
2009-11-18 21:58:16 -08:00
|
|
|
NS_tchar spath[MAXPATHLEN];
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
int PatchFile::sPatchIndex = 0;
|
|
|
|
|
|
|
|
PatchFile::~PatchFile()
|
|
|
|
{
|
|
|
|
// delete the temporary patch file
|
2009-11-18 21:58:16 -08:00
|
|
|
if (spath[0])
|
|
|
|
NS_tremove(spath);
|
2007-12-31 07:15:43 -08:00
|
|
|
|
2009-11-18 21:58:16 -08:00
|
|
|
if (buf)
|
|
|
|
free(buf);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-07-16 15:21:09 -07:00
|
|
|
PatchFile::LoadSourceFile(FILE* ofile)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
struct stat os;
|
2011-04-11 21:23:27 -07:00
|
|
|
int rv = fstat(fileno((FILE *)ofile), &os);
|
2010-09-09 12:22:02 -07:00
|
|
|
if (rv) {
|
2010-11-18 17:20:44 -08:00
|
|
|
LOG(("LoadSourceFile: unable to stat destination file: " LOG_S ", " \
|
2011-04-11 21:23:27 -07:00
|
|
|
"err: %d\n", mFile, errno));
|
2007-03-22 10:30:00 -07:00
|
|
|
return READ_ERROR;
|
2010-09-09 12:22:02 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-09-09 12:22:02 -07:00
|
|
|
if (PRUint32(os.st_size) != header.slen) {
|
|
|
|
LOG(("LoadSourceFile: destination file size %d does not match expected size %d\n",
|
|
|
|
PRUint32(os.st_size), header.slen));
|
2007-03-22 10:30:00 -07:00
|
|
|
return UNEXPECTED_ERROR;
|
2010-09-09 12:22:02 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
buf = (unsigned char *) malloc(header.slen);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!buf)
|
2012-01-18 10:43:21 -08:00
|
|
|
return UPDATER_MEM_ERROR;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-09-20 21:41:38 -07:00
|
|
|
size_t r = header.slen;
|
2007-03-22 10:30:00 -07:00
|
|
|
unsigned char *rb = buf;
|
|
|
|
while (r) {
|
2012-01-12 16:38:20 -08:00
|
|
|
const size_t count = mmin(SSIZE_MAX, r);
|
|
|
|
size_t c = fread(rb, 1, count, ofile);
|
|
|
|
if (c != count) {
|
2010-09-09 12:22:02 -07:00
|
|
|
LOG(("LoadSourceFile: error reading destination file: " LOG_S "\n",
|
2011-04-11 21:23:27 -07:00
|
|
|
mFile));
|
2007-03-22 10:30:00 -07:00
|
|
|
return READ_ERROR;
|
2010-09-09 12:22:02 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
r -= c;
|
|
|
|
rb += c;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the contents of the source file correspond to what we expect.
|
|
|
|
|
|
|
|
unsigned int crc = crc32(buf, header.slen);
|
|
|
|
|
|
|
|
if (crc != header.scrc32) {
|
2010-12-21 21:46:32 -08:00
|
|
|
LOG(("LoadSourceFile: destination file crc %d does not match expected " \
|
|
|
|
"crc %d\n", crc, header.scrc32));
|
2007-03-22 10:30:00 -07:00
|
|
|
return CRC_ERROR;
|
|
|
|
}
|
2011-04-11 21:23:27 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-04-11 21:23:27 -07:00
|
|
|
PatchFile::Parse(NS_tchar *line)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// format "<patchfile>" "<filetopatch>"
|
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
// Get the path to the patch file inside of the mar
|
2007-03-22 10:30:00 -07:00
|
|
|
mPatchFile = mstrtok(kQuote, &line);
|
|
|
|
if (!mPatchFile)
|
|
|
|
return PARSE_ERROR;
|
|
|
|
|
|
|
|
// consume whitespace between args
|
2011-04-11 21:23:27 -07:00
|
|
|
NS_tchar *q = mstrtok(kQuote, &line);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!q)
|
|
|
|
return PARSE_ERROR;
|
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
mFile = get_valid_path(&line);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!mFile)
|
|
|
|
return PARSE_ERROR;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PatchFile::Prepare()
|
|
|
|
{
|
2011-04-11 21:23:27 -07:00
|
|
|
LOG(("PREPARE PATCH " LOG_S "\n", mFile));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// extract the patch to a temporary file
|
|
|
|
mPatchIndex = sPatchIndex++;
|
|
|
|
|
2010-09-20 21:41:38 -07:00
|
|
|
NS_tsnprintf(spath, sizeof(spath)/sizeof(spath[0]),
|
|
|
|
NS_T("%s/%d.patch"), gSourcePath, mPatchIndex);
|
2007-12-31 07:15:43 -08:00
|
|
|
|
|
|
|
NS_tremove(spath);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-12-31 07:15:43 -08:00
|
|
|
FILE *fp = NS_tfopen(spath, NS_T("wb"));
|
|
|
|
if (!fp)
|
|
|
|
return WRITE_ERROR;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
#ifdef XP_WIN
|
|
|
|
char sourcefile[MAXPATHLEN];
|
|
|
|
if (!WideCharToMultiByte(CP_UTF8, 0, mPatchFile, -1, sourcefile, MAXPATHLEN,
|
|
|
|
NULL, NULL)) {
|
|
|
|
LOG(("error converting wchar to utf8: %d\n", GetLastError()));
|
2012-01-18 10:43:21 -08:00
|
|
|
return STRING_CONVERSION_ERROR;
|
2011-04-11 21:23:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int rv = gArchiveReader.ExtractFileToStream(sourcefile, fp);
|
|
|
|
#else
|
2007-12-31 07:15:43 -08:00
|
|
|
int rv = gArchiveReader.ExtractFileToStream(mPatchFile, fp);
|
2011-04-11 21:23:27 -07:00
|
|
|
#endif
|
2007-12-31 07:15:43 -08:00
|
|
|
fclose(fp);
|
2009-11-18 21:58:16 -08:00
|
|
|
return rv;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-11-18 21:58:16 -08:00
|
|
|
int
|
|
|
|
PatchFile::Execute()
|
|
|
|
{
|
2011-04-11 21:23:27 -07:00
|
|
|
LOG(("EXECUTE PATCH " LOG_S "\n", mFile));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-11-18 21:58:16 -08:00
|
|
|
AutoFile pfile = NS_tfopen(spath, NS_T("rb"));
|
2009-07-16 15:21:09 -07:00
|
|
|
if (pfile == NULL)
|
2007-03-22 10:30:00 -07:00
|
|
|
return READ_ERROR;
|
|
|
|
|
2009-11-18 21:58:16 -08:00
|
|
|
int rv = MBS_ReadHeader(pfile, &header);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (rv)
|
|
|
|
return rv;
|
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
FILE *origfile = NULL;
|
|
|
|
#ifdef XP_WIN
|
|
|
|
if (NS_tstrcmp(mFile, gCallbackRelPath) == 0) {
|
|
|
|
// Read from the copy of the callback when patching since the callback can't
|
|
|
|
// be opened for reading to prevent the application from being launched.
|
|
|
|
origfile = NS_tfopen(gCallbackBackupPath, NS_T("rb"));
|
|
|
|
} else {
|
|
|
|
origfile = NS_tfopen(mFile, NS_T("rb"));
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
origfile = NS_tfopen(mFile, NS_T("rb"));
|
|
|
|
#endif
|
|
|
|
|
2010-09-09 12:22:02 -07:00
|
|
|
if (!origfile) {
|
2011-04-11 21:23:27 -07:00
|
|
|
LOG(("unable to open destination file: " LOG_S ", err: %d\n", mFile,
|
2010-11-18 17:20:44 -08:00
|
|
|
errno));
|
2007-03-22 10:30:00 -07:00
|
|
|
return READ_ERROR;
|
2010-09-09 12:22:02 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-11-18 21:58:16 -08:00
|
|
|
rv = LoadSourceFile(origfile);
|
|
|
|
fclose(origfile);
|
|
|
|
if (rv) {
|
2007-03-22 10:30:00 -07:00
|
|
|
LOG(("LoadSourceFile failed\n"));
|
2009-11-18 21:58:16 -08:00
|
|
|
return rv;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-11-18 17:20:44 -08:00
|
|
|
// Rename the destination file if it exists before proceeding so it can be
|
|
|
|
// used to restore the file to its original state if there is an error.
|
2007-03-22 10:30:00 -07:00
|
|
|
struct stat ss;
|
2011-04-11 21:23:27 -07:00
|
|
|
rv = NS_tstat(mFile, &ss);
|
|
|
|
if (rv) {
|
|
|
|
LOG(("failed to read file status info: " LOG_S ", err: %d\n", mFile,
|
|
|
|
errno));
|
2007-03-22 10:30:00 -07:00
|
|
|
return READ_ERROR;
|
2011-04-11 21:23:27 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
rv = backup_create(mFile);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (rv)
|
|
|
|
return rv;
|
|
|
|
|
2010-12-02 19:57:05 -08:00
|
|
|
#if defined(HAVE_POSIX_FALLOCATE)
|
2011-04-11 21:23:27 -07:00
|
|
|
AutoFile ofile = ensure_open(mFile, NS_T("wb+"), ss.st_mode);
|
|
|
|
posix_fallocate(fileno((FILE *)ofile), 0, header.dlen);
|
2010-12-02 19:57:05 -08:00
|
|
|
#elif defined(XP_WIN)
|
2011-04-11 21:23:27 -07:00
|
|
|
bool shouldTruncate = true;
|
2010-12-02 19:57:05 -08:00
|
|
|
// Creating the file, setting the size, and then closing the file handle
|
|
|
|
// lessens fragmentation more than any other method tested. Other methods that
|
|
|
|
// have been tested are:
|
|
|
|
// 1. _chsize / _chsize_s reduced fragmentation but though not completely.
|
|
|
|
// 2. _get_osfhandle and then setting the size reduced fragmentation though
|
|
|
|
// not completely. There are also reports of _get_osfhandle failing on
|
|
|
|
// mingw.
|
2011-04-11 21:23:27 -07:00
|
|
|
HANDLE hfile = CreateFileW(mFile,
|
2010-12-02 19:57:05 -08:00
|
|
|
GENERIC_WRITE,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
CREATE_ALWAYS,
|
|
|
|
FILE_ATTRIBUTE_NORMAL,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if (hfile != INVALID_HANDLE_VALUE) {
|
|
|
|
if (SetFilePointer(hfile, header.dlen, NULL, FILE_BEGIN) != INVALID_SET_FILE_POINTER &&
|
|
|
|
SetEndOfFile(hfile) != 0) {
|
2011-04-11 21:23:27 -07:00
|
|
|
shouldTruncate = false;
|
2010-12-02 19:57:05 -08:00
|
|
|
}
|
|
|
|
CloseHandle(hfile);
|
|
|
|
}
|
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
AutoFile ofile = ensure_open(mFile, shouldTruncate ? NS_T("wb+") : NS_T("rb+"), ss.st_mode);
|
2010-12-02 19:57:05 -08:00
|
|
|
#elif defined(XP_MACOSX)
|
2011-04-11 21:23:27 -07:00
|
|
|
AutoFile ofile = ensure_open(mFile, NS_T("wb+"), ss.st_mode);
|
2010-12-02 19:57:05 -08:00
|
|
|
// Modified code from FileUtils.cpp
|
|
|
|
fstore_t store = {F_ALLOCATECONTIG, F_PEOFPOSMODE, 0, header.dlen};
|
|
|
|
// Try to get a continous chunk of disk space
|
2011-04-11 21:23:27 -07:00
|
|
|
rv = fcntl(fileno((FILE *)ofile), F_PREALLOCATE, &store);
|
2010-12-02 19:57:05 -08:00
|
|
|
if (rv == -1) {
|
|
|
|
// OK, perhaps we are too fragmented, allocate non-continuous
|
|
|
|
store.fst_flags = F_ALLOCATEALL;
|
2011-04-11 21:23:27 -07:00
|
|
|
rv = fcntl(fileno((FILE *)ofile), F_PREALLOCATE, &store);
|
2010-12-02 19:57:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rv != -1) {
|
2011-04-11 21:23:27 -07:00
|
|
|
ftruncate(fileno((FILE *)ofile), header.dlen);
|
2010-12-02 19:57:05 -08:00
|
|
|
}
|
|
|
|
#else
|
2011-04-11 21:23:27 -07:00
|
|
|
AutoFile ofile = ensure_open(mFile, NS_T("wb+"), ss.st_mode);
|
2010-12-02 19:57:05 -08:00
|
|
|
#endif
|
|
|
|
|
2010-09-09 12:22:02 -07:00
|
|
|
if (ofile == NULL) {
|
2011-04-11 21:23:27 -07:00
|
|
|
LOG(("unable to create new file: " LOG_S ", err: %d\n", mFile, errno));
|
2007-03-22 10:30:00 -07:00
|
|
|
return WRITE_ERROR;
|
2010-09-09 12:22:02 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-12-02 19:57:05 -08:00
|
|
|
#ifdef XP_WIN
|
|
|
|
if (!shouldTruncate) {
|
|
|
|
fseek(ofile, 0, SEEK_SET);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-11-18 21:58:16 -08:00
|
|
|
rv = MBS_ApplyPatch(&header, pfile, buf, ofile);
|
|
|
|
|
|
|
|
// Go ahead and do a bit of cleanup now to minimize runtime overhead.
|
2010-09-09 12:22:02 -07:00
|
|
|
// Set pfile to NULL to make AutoFile close the file so it can be deleted on
|
|
|
|
// Windows.
|
|
|
|
pfile = NULL;
|
2009-11-18 21:58:16 -08:00
|
|
|
NS_tremove(spath);
|
2011-04-11 21:23:27 -07:00
|
|
|
spath[0] = NS_T('\0');
|
2009-11-18 21:58:16 -08:00
|
|
|
free(buf);
|
|
|
|
buf = NULL;
|
|
|
|
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PatchFile::Finish(int status)
|
|
|
|
{
|
2011-04-11 21:23:27 -07:00
|
|
|
LOG(("FINISH PATCH " LOG_S "\n", mFile));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
backup_finish(mFile, status);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class AddIfFile : public AddFile
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AddIfFile() : mTestFile(NULL) { }
|
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
virtual int Parse(NS_tchar *line);
|
|
|
|
virtual int Prepare();
|
2007-03-22 10:30:00 -07:00
|
|
|
virtual int Execute();
|
|
|
|
virtual void Finish(int status);
|
|
|
|
|
|
|
|
protected:
|
2011-04-11 21:23:27 -07:00
|
|
|
const NS_tchar *mTestFile;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
int
|
2011-04-11 21:23:27 -07:00
|
|
|
AddIfFile::Parse(NS_tchar *line)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// format "<testfile>" "<newfile>"
|
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
mTestFile = get_valid_path(&line);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!mTestFile)
|
|
|
|
return PARSE_ERROR;
|
|
|
|
|
|
|
|
// consume whitespace between args
|
2011-04-11 21:23:27 -07:00
|
|
|
NS_tchar *q = mstrtok(kQuote, &line);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!q)
|
|
|
|
return PARSE_ERROR;
|
|
|
|
|
|
|
|
return AddFile::Parse(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
AddIfFile::Prepare()
|
|
|
|
{
|
2011-04-11 21:23:27 -07:00
|
|
|
// If the test file does not exist, then skip this action.
|
|
|
|
if (NS_taccess(mTestFile, F_OK)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
mTestFile = NULL;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AddFile::Prepare();
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
AddIfFile::Execute()
|
|
|
|
{
|
|
|
|
if (!mTestFile)
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
return AddFile::Execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AddIfFile::Finish(int status)
|
|
|
|
{
|
|
|
|
if (!mTestFile)
|
|
|
|
return;
|
|
|
|
|
|
|
|
AddFile::Finish(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
class PatchIfFile : public PatchFile
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PatchIfFile() : mTestFile(NULL) { }
|
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
virtual int Parse(NS_tchar *line);
|
|
|
|
virtual int Prepare(); // should check for patch file and for checksum here
|
2007-03-22 10:30:00 -07:00
|
|
|
virtual int Execute();
|
|
|
|
virtual void Finish(int status);
|
|
|
|
|
|
|
|
private:
|
2011-04-11 21:23:27 -07:00
|
|
|
const NS_tchar *mTestFile;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
int
|
2011-04-11 21:23:27 -07:00
|
|
|
PatchIfFile::Parse(NS_tchar *line)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// format "<testfile>" "<patchfile>" "<filetopatch>"
|
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
mTestFile = get_valid_path(&line);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!mTestFile)
|
|
|
|
return PARSE_ERROR;
|
|
|
|
|
|
|
|
// consume whitespace between args
|
2011-04-11 21:23:27 -07:00
|
|
|
NS_tchar *q = mstrtok(kQuote, &line);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!q)
|
|
|
|
return PARSE_ERROR;
|
|
|
|
|
|
|
|
return PatchFile::Parse(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PatchIfFile::Prepare()
|
|
|
|
{
|
2011-04-11 21:23:27 -07:00
|
|
|
// If the test file does not exist, then skip this action.
|
|
|
|
if (NS_taccess(mTestFile, F_OK)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
mTestFile = NULL;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PatchFile::Prepare();
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PatchIfFile::Execute()
|
|
|
|
{
|
|
|
|
if (!mTestFile)
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
return PatchFile::Execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PatchIfFile::Finish(int status)
|
|
|
|
{
|
|
|
|
if (!mTestFile)
|
|
|
|
return;
|
|
|
|
|
|
|
|
PatchFile::Finish(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
#include "nsWindowsRestart.cpp"
|
2012-01-04 20:19:14 -08:00
|
|
|
#include "uachelper.h"
|
2012-01-04 20:19:16 -08:00
|
|
|
#include "pathhash.h"
|
2009-11-13 13:11:20 -08:00
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static void
|
2012-01-04 20:19:18 -08:00
|
|
|
LaunchCallbackApp(const NS_tchar *workingDir,
|
|
|
|
int argc,
|
|
|
|
NS_tchar **argv,
|
|
|
|
bool usingService)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-07-21 00:25:37 -07:00
|
|
|
putenv(const_cast<char*>("NO_EM_RESTART="));
|
|
|
|
putenv(const_cast<char*>("MOZ_LAUNCHED_CHILD=1"));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-08-25 18:41:18 -07:00
|
|
|
// Run from the specified working directory (see bug 312360). This is not
|
|
|
|
// necessary on Windows CE since the application that launches the updater
|
|
|
|
// passes the working directory as an --environ: command line argument.
|
2012-01-04 20:19:14 -08:00
|
|
|
if (NS_tchdir(workingDir) != 0) {
|
2009-07-29 23:01:36 -07:00
|
|
|
LOG(("Warning: chdir failed\n"));
|
2012-01-04 20:19:14 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#if defined(USE_EXECV)
|
|
|
|
execv(argv[0], argv);
|
|
|
|
#elif defined(XP_MACOSX)
|
|
|
|
LaunchChild(argc, argv);
|
2011-04-11 21:23:27 -07:00
|
|
|
#elif defined(XP_WIN)
|
2012-01-04 20:19:16 -08:00
|
|
|
// Do not allow the callback to run when running an update through the
|
|
|
|
// service as session 0. The unelevated updater.exe will do the launching.
|
|
|
|
if (!usingService) {
|
|
|
|
WinLaunchChild(argv[0], argc, argv, NULL);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
#else
|
|
|
|
# warning "Need implementaton of LaunchCallbackApp"
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
WriteStatusFile(int status)
|
|
|
|
{
|
|
|
|
// This is how we communicate our completion status to the main application.
|
|
|
|
|
2008-01-02 10:05:01 -08:00
|
|
|
NS_tchar filename[MAXPATHLEN];
|
2010-09-20 21:41:38 -07:00
|
|
|
NS_tsnprintf(filename, sizeof(filename)/sizeof(filename[0]),
|
|
|
|
NS_T("%s/update.status"), gSourcePath);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-07-16 15:21:09 -07:00
|
|
|
AutoFile file = NS_tfopen(filename, NS_T("wb+"));
|
|
|
|
if (file == NULL)
|
2007-03-22 10:30:00 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
const char *text;
|
|
|
|
|
|
|
|
char buf[32];
|
|
|
|
if (status == OK) {
|
|
|
|
text = "succeeded\n";
|
|
|
|
} else {
|
2010-09-20 21:41:38 -07:00
|
|
|
snprintf(buf, sizeof(buf)/sizeof(buf[0]), "failed: %d\n", status);
|
2007-03-22 10:30:00 -07:00
|
|
|
text = buf;
|
|
|
|
}
|
2009-07-16 15:21:09 -07:00
|
|
|
fwrite(text, strlen(text), 1, file);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-01-04 20:19:15 -08:00
|
|
|
static bool
|
|
|
|
WriteStatusApplying()
|
|
|
|
{
|
|
|
|
NS_tchar filename[MAXPATHLEN];
|
|
|
|
NS_tsnprintf(filename, sizeof(filename)/sizeof(filename[0]),
|
|
|
|
NS_T("%s/update.status"), gSourcePath);
|
|
|
|
|
|
|
|
AutoFile file = NS_tfopen(filename, NS_T("wb+"));
|
|
|
|
if (file == NULL)
|
|
|
|
return false;
|
|
|
|
|
2012-01-04 20:19:16 -08:00
|
|
|
static const char kApplying[] = "applying";
|
2012-01-04 20:19:15 -08:00
|
|
|
if (fwrite(kApplying, strlen(kApplying), 1, file) != 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the update.status file and sets isPendingService to true if
|
|
|
|
* the status is set to pending-service.
|
|
|
|
*
|
|
|
|
* @param isPendingService Out parameter for specifying if the status
|
|
|
|
* is set to pending-service or not.
|
|
|
|
* @return true if the information was retrieved and it is pending
|
|
|
|
* or pending-service.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
IsUpdateStatusPending(bool &isPendingService)
|
|
|
|
{
|
|
|
|
bool isPending = false;
|
|
|
|
isPendingService = false;
|
|
|
|
NS_tchar filename[MAXPATHLEN];
|
|
|
|
NS_tsnprintf(filename, sizeof(filename)/sizeof(filename[0]),
|
|
|
|
NS_T("%s/update.status"), gSourcePath);
|
|
|
|
|
|
|
|
AutoFile file = NS_tfopen(filename, NS_T("rb"));
|
|
|
|
if (file == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
char buf[32] = { 0 };
|
|
|
|
fread(buf, sizeof(buf), 1, file);
|
|
|
|
|
|
|
|
const char kPending[] = "pending";
|
|
|
|
const char kPendingService[] = "pending-service";
|
|
|
|
isPending = strncmp(buf, kPending,
|
|
|
|
sizeof(kPending) - 1) == 0;
|
|
|
|
|
|
|
|
isPendingService = strncmp(buf, kPendingService,
|
|
|
|
sizeof(kPendingService) - 1) == 0;
|
|
|
|
return isPending;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the update.status file and sets isSuccess to true if
|
|
|
|
* the status is set to succeeded.
|
|
|
|
*
|
|
|
|
* @param isSucceeded Out parameter for specifying if the status
|
|
|
|
* is set to succeeded or not.
|
|
|
|
* @return true if the information was retrieved and it is succeeded.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
IsUpdateStatusSucceeded(bool &isSucceeded)
|
|
|
|
{
|
|
|
|
isSucceeded = false;
|
|
|
|
NS_tchar filename[MAXPATHLEN];
|
|
|
|
NS_tsnprintf(filename, sizeof(filename)/sizeof(filename[0]),
|
|
|
|
NS_T("%s/update.status"), gSourcePath);
|
|
|
|
|
|
|
|
AutoFile file = NS_tfopen(filename, NS_T("rb"));
|
|
|
|
if (file == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
char buf[32] = { 0 };
|
|
|
|
fread(buf, sizeof(buf), 1, file);
|
|
|
|
|
|
|
|
const char kSucceeded[] = "succeeded";
|
|
|
|
isSucceeded = strncmp(buf, kSucceeded,
|
|
|
|
sizeof(kSucceeded) - 1) == 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static void
|
|
|
|
UpdateThreadFunc(void *param)
|
|
|
|
{
|
|
|
|
// open ZIP archive and process...
|
|
|
|
|
2008-01-02 10:05:01 -08:00
|
|
|
NS_tchar dataFile[MAXPATHLEN];
|
2010-09-20 21:41:38 -07:00
|
|
|
NS_tsnprintf(dataFile, sizeof(dataFile)/sizeof(dataFile[0]),
|
|
|
|
NS_T("%s/update.mar"), gSourcePath);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
int rv = gArchiveReader.Open(dataFile);
|
|
|
|
if (rv == OK) {
|
|
|
|
rv = DoUpdate();
|
|
|
|
gArchiveReader.Close();
|
|
|
|
}
|
|
|
|
|
2010-09-29 19:55:47 -07:00
|
|
|
if (rv) {
|
2007-03-22 10:30:00 -07:00
|
|
|
LOG(("failed: %d\n", rv));
|
2010-09-29 19:55:47 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
// If the update was successful we need to update the timestamp
|
|
|
|
// on the top-level Mac OS X bundle directory so that Mac OS X's
|
|
|
|
// Launch Services picks up any major changes. Here we assume that
|
|
|
|
// the current working directory is the top-level bundle directory.
|
|
|
|
char* cwd = getcwd(NULL, 0);
|
|
|
|
if (cwd) {
|
|
|
|
if (utimes(cwd, NULL) != 0) {
|
|
|
|
LOG(("Couldn't set access/modification time on application bundle.\n"));
|
|
|
|
}
|
|
|
|
free(cwd);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG(("Couldn't get current working directory for setting "
|
|
|
|
"access/modification time on application bundle.\n"));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
LOG(("succeeded\n"));
|
2010-09-29 19:55:47 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
WriteStatusFile(rv);
|
|
|
|
|
|
|
|
LOG(("calling QuitProgressUI\n"));
|
|
|
|
QuitProgressUI();
|
|
|
|
}
|
|
|
|
|
2007-12-31 07:15:43 -08:00
|
|
|
int NS_main(int argc, NS_tchar **argv)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
InitProgressUI(&argc, &argv);
|
|
|
|
|
2010-10-01 21:11:24 -07:00
|
|
|
// To process an update the updater command line must at a minimum have the
|
|
|
|
// directory path containing the updater.mar file to process as the first argument
|
|
|
|
// and the directory to apply the update to as the second argument. When the
|
|
|
|
// updater is launched by another process the PID of the parent process should be
|
|
|
|
// provided in the optional third argument and the updater will wait on the parent
|
|
|
|
// process to exit if the value is non-zero and the process is present. This is
|
|
|
|
// necessary due to not being able to update files that are in use on Windows. The
|
|
|
|
// optional fourth argument is the callback's working directory and the optional
|
|
|
|
// fifth argument is the callback path. The callback is the application to launch
|
|
|
|
// after updating and it will be launched when these arguments are provided
|
|
|
|
// whether the update was successful or not. All remaining arguments are optional
|
|
|
|
// and are passed to the callback when it is launched.
|
|
|
|
if (argc < 3) {
|
|
|
|
fprintf(stderr, "Usage: updater update-dir apply-to-dir [wait-pid [callback-working-dir callback-path args...]]\n");
|
2007-03-22 10:30:00 -07:00
|
|
|
return 1;
|
|
|
|
}
|
2010-10-01 21:11:24 -07:00
|
|
|
|
|
|
|
// Change current directory to the directory where we need to apply the update.
|
|
|
|
if (NS_tchdir(argv[2]) != 0) {
|
2009-07-29 23:01:36 -07:00
|
|
|
return 1;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-01-04 20:19:15 -08:00
|
|
|
// The directory containing the update information.
|
|
|
|
gSourcePath = argv[1];
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
2012-01-04 20:19:16 -08:00
|
|
|
// Disable every privilege we don't need. Processes started using
|
|
|
|
// CreateProcess will use the same token as this process.
|
|
|
|
UACHelper::DisablePrivileges(NULL);
|
|
|
|
|
2012-01-04 20:19:15 -08:00
|
|
|
bool useService = false;
|
2012-01-04 20:19:16 -08:00
|
|
|
bool testOnlyFallbackKeyExists = false;
|
2011-12-28 18:08:37 -08:00
|
|
|
bool noServiceFallback = getenv("MOZ_NO_SERVICE_FALLBACK") != NULL;
|
|
|
|
putenv(const_cast<char*>("MOZ_NO_SERVICE_FALLBACK="));
|
2012-01-04 20:19:16 -08:00
|
|
|
|
2012-01-04 20:19:15 -08:00
|
|
|
// We never want the service to be used unless we build with
|
|
|
|
// the maintenance service.
|
|
|
|
#ifdef MOZ_MAINTENANCE_SERVICE
|
|
|
|
IsUpdateStatusPending(useService);
|
2012-01-04 20:19:16 -08:00
|
|
|
// Our tests run with a different apply directory for each test.
|
|
|
|
// We use this registry key on our test slaves to store the
|
|
|
|
// allowed name/issuers.
|
|
|
|
HKEY testOnlyFallbackKey;
|
|
|
|
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
|
|
|
|
TEST_ONLY_FALLBACK_KEY_PATH, 0,
|
|
|
|
KEY_READ | KEY_WOW64_64KEY,
|
|
|
|
&testOnlyFallbackKey) == ERROR_SUCCESS) {
|
|
|
|
testOnlyFallbackKeyExists = true;
|
|
|
|
RegCloseKey(testOnlyFallbackKey);
|
|
|
|
}
|
|
|
|
|
2012-01-04 20:19:15 -08:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!WriteStatusApplying()) {
|
|
|
|
LOG(("failed setting status to 'applying'\n"));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-02-07 18:41:21 -08:00
|
|
|
#ifdef XP_WIN
|
|
|
|
// Remove everything except close window from the context menu
|
|
|
|
{
|
|
|
|
HKEY hkApp;
|
2011-06-09 15:14:19 -07:00
|
|
|
RegCreateKeyExW(HKEY_CURRENT_USER, L"Software\\Classes\\Applications",
|
|
|
|
0, NULL, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL,
|
|
|
|
&hkApp, NULL);
|
|
|
|
RegCloseKey(hkApp);
|
2011-02-07 18:41:21 -08:00
|
|
|
if (RegCreateKeyExW(HKEY_CURRENT_USER,
|
|
|
|
L"Software\\Classes\\Applications\\updater.exe",
|
|
|
|
0, NULL, REG_OPTION_VOLATILE, KEY_SET_VALUE, NULL,
|
|
|
|
&hkApp, NULL) == ERROR_SUCCESS) {
|
|
|
|
RegSetValueExW(hkApp, L"IsHostApp", 0, REG_NONE, 0, 0);
|
|
|
|
RegSetValueExW(hkApp, L"NoOpenWith", 0, REG_NONE, 0, 0);
|
|
|
|
RegSetValueExW(hkApp, L"NoStartPage", 0, REG_NONE, 0, 0);
|
|
|
|
RegCloseKey(hkApp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-10-01 21:11:24 -07:00
|
|
|
// If there is a PID specified and it is not '0' then wait for the process to exit.
|
|
|
|
if (argc > 3) {
|
2010-09-20 21:41:38 -07:00
|
|
|
#ifdef XP_WIN
|
2010-10-01 21:11:24 -07:00
|
|
|
__int64 pid = _wtoi64(argv[3]);
|
|
|
|
if (pid != 0) {
|
2011-04-11 21:23:27 -07:00
|
|
|
HANDLE parent = OpenProcess(SYNCHRONIZE, false, (DWORD) pid);
|
2008-08-26 14:06:45 -07:00
|
|
|
// May return NULL if the parent process has already gone away.
|
|
|
|
// Otherwise, wait for the parent process to exit before starting the
|
|
|
|
// update.
|
|
|
|
if (parent) {
|
|
|
|
DWORD result = WaitForSingleObject(parent, 5000);
|
|
|
|
CloseHandle(parent);
|
|
|
|
if (result != WAIT_OBJECT_0)
|
|
|
|
return 1;
|
|
|
|
}
|
2011-04-11 21:23:27 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
#else
|
2011-04-11 21:23:27 -07:00
|
|
|
int pid = atoi(argv[3]);
|
|
|
|
if (pid != 0)
|
2010-10-01 21:11:24 -07:00
|
|
|
waitpid(pid, NULL, 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-10-01 21:11:24 -07:00
|
|
|
// The callback is the remaining arguments starting at callbackIndex.
|
|
|
|
// The argument specified by callbackIndex is the callback executable and the
|
|
|
|
// argument prior to callbackIndex is the working directory.
|
|
|
|
const int callbackIndex = 5;
|
|
|
|
|
2011-12-28 18:08:37 -08:00
|
|
|
bool usingService = false;
|
2011-04-11 21:23:27 -07:00
|
|
|
#if defined(XP_WIN)
|
2011-12-28 18:08:37 -08:00
|
|
|
usingService = getenv("MOZ_USING_SERVICE") != NULL;
|
|
|
|
putenv(const_cast<char*>("MOZ_USING_SERVICE="));
|
2012-01-04 20:19:17 -08:00
|
|
|
// lastFallbackError keeps track of the last error for the service not being
|
|
|
|
// used, in case of an error when fallback is not enabled we write the
|
|
|
|
// error to the update.status file.
|
|
|
|
// When fallback is disabled (MOZ_NO_SERVICE_FALLBACK does not exist) then
|
|
|
|
// we will instead fallback to not using the service and display a UAC prompt.
|
|
|
|
int lastFallbackError = FALLBACKKEY_UNKNOWN_ERROR;
|
|
|
|
|
2008-06-23 12:06:37 -07:00
|
|
|
// Launch a second instance of the updater with the runas verb on Windows
|
|
|
|
// when write access is denied to the installation directory.
|
|
|
|
HANDLE updateLockFileHandle;
|
|
|
|
NS_tchar elevatedLockFilePath[MAXPATHLEN];
|
2012-01-04 20:19:16 -08:00
|
|
|
if (argc > callbackIndex && !usingService) {
|
2008-09-01 13:09:57 -07:00
|
|
|
NS_tchar updateLockFilePath[MAXPATHLEN];
|
2010-09-20 21:41:38 -07:00
|
|
|
NS_tsnprintf(updateLockFilePath,
|
|
|
|
sizeof(updateLockFilePath)/sizeof(updateLockFilePath[0]),
|
2010-10-01 21:11:24 -07:00
|
|
|
NS_T("%s.update_in_progress.lock"), argv[callbackIndex]);
|
2008-09-01 13:09:57 -07:00
|
|
|
|
|
|
|
// The update_in_progress.lock file should only exist during an update. In
|
|
|
|
// case it exists attempt to remove it and exit if that fails to prevent
|
|
|
|
// simultaneous updates occurring.
|
|
|
|
if (!_waccess(updateLockFilePath, F_OK) &&
|
|
|
|
NS_tremove(updateLockFilePath) != 0) {
|
|
|
|
fprintf(stderr, "Update already in progress! Exiting\n");
|
2008-06-23 12:06:37 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-09-01 13:09:57 -07:00
|
|
|
updateLockFileHandle = CreateFileW(updateLockFilePath,
|
|
|
|
GENERIC_READ | GENERIC_WRITE,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
OPEN_ALWAYS,
|
|
|
|
FILE_FLAG_DELETE_ON_CLOSE,
|
|
|
|
NULL);
|
|
|
|
|
2010-09-20 21:41:38 -07:00
|
|
|
NS_tsnprintf(elevatedLockFilePath,
|
|
|
|
sizeof(elevatedLockFilePath)/sizeof(elevatedLockFilePath[0]),
|
2008-09-01 13:09:57 -07:00
|
|
|
NS_T("%s/update_elevated.lock"), argv[1]);
|
|
|
|
|
2012-01-04 20:19:16 -08:00
|
|
|
if (updateLockFileHandle == INVALID_HANDLE_VALUE ||
|
2012-01-04 20:19:18 -08:00
|
|
|
(useService && testOnlyFallbackKeyExists && noServiceFallback)) {
|
2008-09-01 13:09:57 -07:00
|
|
|
if (!_waccess(elevatedLockFilePath, F_OK) &&
|
|
|
|
NS_tremove(elevatedLockFilePath) != 0) {
|
|
|
|
fprintf(stderr, "Update already elevated! Exiting\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2008-06-23 12:06:37 -07:00
|
|
|
|
2008-09-01 13:09:57 -07:00
|
|
|
HANDLE elevatedFileHandle;
|
|
|
|
elevatedFileHandle = CreateFileW(elevatedLockFilePath,
|
|
|
|
GENERIC_READ | GENERIC_WRITE,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
OPEN_ALWAYS,
|
|
|
|
FILE_FLAG_DELETE_ON_CLOSE,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if (elevatedFileHandle == INVALID_HANDLE_VALUE) {
|
|
|
|
fprintf(stderr, "Unable to create elevated lock file! Exiting\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2008-06-23 12:06:37 -07:00
|
|
|
|
2008-09-01 13:09:57 -07:00
|
|
|
PRUnichar *cmdLine = MakeCommandLine(argc - 1, argv + 1);
|
|
|
|
if (!cmdLine) {
|
|
|
|
CloseHandle(elevatedFileHandle);
|
|
|
|
return 1;
|
|
|
|
}
|
2008-06-23 12:06:37 -07:00
|
|
|
|
2012-01-04 20:19:16 -08:00
|
|
|
// Make sure the service registry entries for the instsallation path
|
|
|
|
// are available. If not don't use the service.
|
|
|
|
if (useService) {
|
|
|
|
WCHAR maintenanceServiceKey[MAX_PATH + 1];
|
|
|
|
if (CalculateRegistryPathFromFilePath(argv[2], maintenanceServiceKey)) {
|
|
|
|
HKEY baseKey;
|
2012-01-04 20:19:16 -08:00
|
|
|
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
|
|
|
|
maintenanceServiceKey, 0,
|
|
|
|
KEY_READ | KEY_WOW64_64KEY,
|
|
|
|
&baseKey) == ERROR_SUCCESS) {
|
|
|
|
RegCloseKey(baseKey);
|
|
|
|
} else {
|
|
|
|
useService = testOnlyFallbackKeyExists;
|
2012-01-04 20:19:17 -08:00
|
|
|
if (!useService) {
|
|
|
|
lastFallbackError = FALLBACKKEY_NOKEY_ERROR;
|
|
|
|
}
|
2012-01-04 20:19:16 -08:00
|
|
|
}
|
|
|
|
} else {
|
2012-01-04 20:19:17 -08:00
|
|
|
useService = false;
|
|
|
|
lastFallbackError = FALLBACKKEY_REGPATH_ERROR;
|
2012-01-04 20:19:16 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-04 20:19:15 -08:00
|
|
|
// Originally we used to write "pending" to update.status before
|
|
|
|
// launching the service command. This is no longer needed now
|
|
|
|
// since the service command is launched from updater.exe. If anything
|
|
|
|
// fails in between, we can fall back to using the normal update process
|
|
|
|
// on our own.
|
|
|
|
|
|
|
|
// If we still want to use the service try to launch the service
|
|
|
|
// comamnd for the update.
|
|
|
|
if (useService) {
|
|
|
|
// If the update couldn't be started, then set useService to false so
|
|
|
|
// we do the update the old way.
|
2011-12-28 18:08:37 -08:00
|
|
|
DWORD ret = LaunchServiceSoftwareUpdateCommand(argc, (LPCWSTR *)argv);
|
|
|
|
useService = (ret == ERROR_SUCCESS);
|
2012-01-04 20:19:19 -08:00
|
|
|
// If the command was launched then wait for the service to be done.
|
2012-01-04 20:19:15 -08:00
|
|
|
if (useService) {
|
2011-12-28 18:08:37 -08:00
|
|
|
DWORD lastState = WaitForServiceStop(SVC_NAME, 600);
|
|
|
|
if (lastState != SERVICE_STOPPED) {
|
2012-01-04 20:19:19 -08:00
|
|
|
// If the service doesn't stop after 10 minutes there is
|
|
|
|
// something seriously wrong.
|
|
|
|
lastFallbackError = FALLBACKKEY_SERVICE_NO_STOP_ERROR;
|
|
|
|
useService = false;
|
|
|
|
}
|
2011-12-28 18:08:37 -08:00
|
|
|
} else {
|
|
|
|
lastFallbackError = FALLBACKKEY_LAUNCH_ERROR;
|
2012-01-04 20:19:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we started the service command, and it finished, check the
|
|
|
|
// update.status file to make sure it succeeded, and if it did
|
|
|
|
// we need to manually start the PostUpdate process from the
|
|
|
|
// current user's session of this unelevated updater.exe the
|
|
|
|
// current process is running as.
|
|
|
|
if (useService) {
|
|
|
|
bool updateStatusSucceeded = false;
|
|
|
|
if (IsUpdateStatusSucceeded(updateStatusSucceeded) &&
|
|
|
|
updateStatusSucceeded) {
|
2012-01-04 20:19:16 -08:00
|
|
|
if (!LaunchWinPostProcess(argv[2], gSourcePath, false, NULL)) {
|
|
|
|
fprintf(stderr, "The post update process which runs as the user"
|
|
|
|
" for service update could not be launched.");
|
|
|
|
}
|
2012-01-04 20:19:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we didn't want to use the service at all, or if an update was
|
|
|
|
// already happening, or launching the service command failed, then
|
2012-01-04 20:19:17 -08:00
|
|
|
// launch the elevated updater.exe as we do without the service.
|
|
|
|
// We don't launch the elevated updater in the case that we did have
|
|
|
|
// write access all along because in that case the only reason we're
|
|
|
|
// using the service is because we are testing.
|
2012-01-04 20:19:18 -08:00
|
|
|
if (!useService && !noServiceFallback &&
|
|
|
|
updateLockFileHandle == INVALID_HANDLE_VALUE) {
|
2012-01-04 20:19:15 -08:00
|
|
|
SHELLEXECUTEINFO sinfo;
|
|
|
|
memset(&sinfo, 0, sizeof(SHELLEXECUTEINFO));
|
|
|
|
sinfo.cbSize = sizeof(SHELLEXECUTEINFO);
|
|
|
|
sinfo.fMask = SEE_MASK_FLAG_NO_UI |
|
|
|
|
SEE_MASK_FLAG_DDEWAIT |
|
|
|
|
SEE_MASK_NOCLOSEPROCESS;
|
|
|
|
sinfo.hwnd = NULL;
|
|
|
|
sinfo.lpFile = argv[0];
|
|
|
|
sinfo.lpParameters = cmdLine;
|
|
|
|
sinfo.lpVerb = L"runas";
|
|
|
|
sinfo.nShow = SW_SHOWNORMAL;
|
|
|
|
|
|
|
|
bool result = ShellExecuteEx(&sinfo);
|
|
|
|
free(cmdLine);
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
WaitForSingleObject(sinfo.hProcess, INFINITE);
|
|
|
|
CloseHandle(sinfo.hProcess);
|
|
|
|
} else {
|
|
|
|
WriteStatusFile(ELEVATION_CANCELED);
|
|
|
|
}
|
2008-09-01 13:09:57 -07:00
|
|
|
}
|
2008-06-23 12:06:37 -07:00
|
|
|
|
2012-01-04 20:19:18 -08:00
|
|
|
// We are already contained in a condition where argc > callbackIndex
|
|
|
|
LaunchCallbackApp(argv[4], argc - callbackIndex,
|
|
|
|
argv + callbackIndex, usingService);
|
2008-09-01 13:09:57 -07:00
|
|
|
|
|
|
|
CloseHandle(elevatedFileHandle);
|
2012-01-04 20:19:17 -08:00
|
|
|
|
2012-01-04 20:19:18 -08:00
|
|
|
if (!useService && !noServiceFallback &&
|
|
|
|
INVALID_HANDLE_VALUE == updateLockFileHandle) {
|
2012-01-04 20:19:17 -08:00
|
|
|
// We didn't use the service and we did run the elevated updater.exe.
|
|
|
|
// The elevated updater.exe is responsible for writing out the
|
|
|
|
// update.status file.
|
|
|
|
return 0;
|
|
|
|
} else if(useService) {
|
|
|
|
// The service command was launched. The service is responsible for
|
|
|
|
// writing out the update.status file.
|
|
|
|
if (updateLockFileHandle != INVALID_HANDLE_VALUE) {
|
|
|
|
CloseHandle(updateLockFileHandle);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
// Otherwise the service command was not launched at all.
|
|
|
|
// We are only reaching this code path because we had write access
|
|
|
|
// all along to the directory and a fallback key existed, and we
|
|
|
|
// have fallback disabled (MOZ_NO_SERVICE_FALLBACK env var exists).
|
|
|
|
// We only currently use this env var from XPCShell tests.
|
|
|
|
CloseHandle(updateLockFileHandle);
|
|
|
|
WriteStatusFile(lastFallbackError);
|
|
|
|
return 0;
|
|
|
|
}
|
2008-09-01 13:09:57 -07:00
|
|
|
}
|
2008-06-23 12:06:37 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-01-04 20:19:14 -08:00
|
|
|
LogInit(gSourcePath, NS_T("update.log"));
|
2010-10-01 21:11:24 -07:00
|
|
|
LOG(("SOURCE DIRECTORY " LOG_S "\n", argv[1]));
|
|
|
|
LOG(("DESTINATION DIRECTORY " LOG_S "\n", argv[2]));
|
2010-09-09 12:22:02 -07:00
|
|
|
|
2009-11-13 13:11:20 -08:00
|
|
|
#ifdef XP_WIN
|
2011-04-11 21:23:27 -07:00
|
|
|
// Allocate enough space for the length of the path an optional additional
|
|
|
|
// trailing slash and null termination.
|
|
|
|
NS_tchar *destpath = (NS_tchar *) malloc((NS_tstrlen(argv[2]) + 2) * sizeof(NS_tchar));
|
|
|
|
if (!destpath)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
NS_tchar *c = destpath;
|
|
|
|
NS_tstrcpy(c, argv[2]);
|
|
|
|
c += NS_tstrlen(argv[2]);
|
|
|
|
if (argv[2][NS_tstrlen(argv[2]) - 1] != NS_T('/') &&
|
|
|
|
argv[2][NS_tstrlen(argv[2]) - 1] != NS_T('\\')) {
|
|
|
|
NS_tstrcat(c, NS_T("/"));
|
|
|
|
c += NS_tstrlen(NS_T("/"));
|
|
|
|
}
|
|
|
|
*c = NS_T('\0');
|
|
|
|
c++;
|
|
|
|
|
|
|
|
gDestPath = destpath;
|
|
|
|
|
2009-11-13 13:11:20 -08:00
|
|
|
HANDLE callbackFile = INVALID_HANDLE_VALUE;
|
2010-10-01 21:11:24 -07:00
|
|
|
if (argc > callbackIndex) {
|
2011-04-11 21:23:27 -07:00
|
|
|
// If the callback executable is specified it must exist for a successful
|
2011-12-28 18:08:37 -08:00
|
|
|
// update. It is important we null out the whole buffer here because later
|
|
|
|
// we make the assumption that the callback application is inside the
|
|
|
|
// apply-to dir. If we don't have a fully null'ed out buffer it can lead
|
|
|
|
// to stack corruption which causes crashes and other problems.
|
2011-04-11 21:23:27 -07:00
|
|
|
NS_tchar callbackLongPath[MAXPATHLEN];
|
2011-12-28 18:08:37 -08:00
|
|
|
ZeroMemory(callbackLongPath, sizeof(callbackLongPath));
|
2011-04-11 21:23:27 -07:00
|
|
|
if (!GetLongPathNameW(argv[callbackIndex], callbackLongPath,
|
|
|
|
sizeof(callbackLongPath)/sizeof(callbackLongPath[0]))) {
|
2010-10-01 21:11:24 -07:00
|
|
|
LOG(("NS_main: unable to find callback file: " LOG_S "\n", argv[callbackIndex]));
|
2009-11-13 13:11:20 -08:00
|
|
|
LogFinish();
|
|
|
|
WriteStatusFile(WRITE_ERROR);
|
|
|
|
EXIT_WHEN_ELEVATED(elevatedLockFilePath, updateLockFileHandle, 1);
|
2012-01-04 20:19:18 -08:00
|
|
|
LaunchCallbackApp(argv[4],
|
|
|
|
argc - callbackIndex,
|
|
|
|
argv + callbackIndex,
|
|
|
|
usingService);
|
2009-11-13 13:11:20 -08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
NS_tchar applyDirLongPath[MAXPATHLEN];
|
|
|
|
if (!GetLongPathNameW(argv[2], applyDirLongPath,
|
|
|
|
sizeof(applyDirLongPath)/sizeof(applyDirLongPath[0]))) {
|
|
|
|
LOG(("NS_main: unable to find apply to dir: " LOG_S "\n", argv[2]));
|
|
|
|
LogFinish();
|
|
|
|
WriteStatusFile(WRITE_ERROR);
|
|
|
|
EXIT_WHEN_ELEVATED(elevatedLockFilePath, updateLockFileHandle, 1);
|
2012-01-04 20:19:18 -08:00
|
|
|
LaunchCallbackApp(argv[4],
|
|
|
|
argc - callbackIndex,
|
|
|
|
argv + callbackIndex,
|
|
|
|
usingService);
|
2011-04-11 21:23:27 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int len = NS_tstrlen(applyDirLongPath);
|
|
|
|
NS_tchar *s = callbackLongPath;
|
|
|
|
NS_tchar *d = gCallbackRelPath;
|
|
|
|
// advance to the apply to directory and advance past the trailing backslash
|
|
|
|
// if present.
|
|
|
|
s += len;
|
|
|
|
if (*s == NS_T('\\'))
|
|
|
|
++s;
|
|
|
|
|
|
|
|
// Copy the string and replace backslashes with forward slashes along the
|
|
|
|
// way.
|
|
|
|
do {
|
|
|
|
if (*s == NS_T('\\'))
|
|
|
|
*d = NS_T('/');
|
|
|
|
else
|
|
|
|
*d = *s;
|
|
|
|
++s;
|
|
|
|
++d;
|
|
|
|
} while (*s);
|
|
|
|
*d = NS_T('\0');
|
|
|
|
++d;
|
|
|
|
|
|
|
|
// Make a copy of the callback executable so it can be read when patching.
|
|
|
|
NS_tsnprintf(gCallbackBackupPath,
|
|
|
|
sizeof(gCallbackBackupPath)/sizeof(gCallbackBackupPath[0]),
|
2010-10-01 21:11:24 -07:00
|
|
|
NS_T("%s" CALLBACK_BACKUP_EXT), argv[callbackIndex]);
|
2011-04-11 21:23:27 -07:00
|
|
|
NS_tremove(gCallbackBackupPath);
|
|
|
|
CopyFileW(argv[callbackIndex], gCallbackBackupPath, false);
|
2009-11-13 13:11:20 -08:00
|
|
|
|
2010-11-18 17:21:20 -08:00
|
|
|
// Since the process may be signaled as exited by WaitForSingleObject before
|
|
|
|
// the release of the executable image try to lock the main executable file
|
|
|
|
// multiple times before giving up.
|
2012-01-12 06:54:40 -08:00
|
|
|
const int max_retries = 10;
|
|
|
|
int retries = 1;
|
2010-11-18 17:21:20 -08:00
|
|
|
do {
|
2011-04-11 21:23:27 -07:00
|
|
|
// By opening a file handle wihout FILE_SHARE_READ to the callback
|
|
|
|
// executable, the OS will prevent launching the process while it is
|
|
|
|
// being updated.
|
2010-11-18 17:21:20 -08:00
|
|
|
callbackFile = CreateFileW(argv[callbackIndex],
|
|
|
|
DELETE | GENERIC_WRITE,
|
2011-04-11 21:23:27 -07:00
|
|
|
// allow delete, rename, and write
|
|
|
|
FILE_SHARE_DELETE | FILE_SHARE_WRITE,
|
2010-11-18 17:21:20 -08:00
|
|
|
NULL, OPEN_EXISTING, 0, NULL);
|
|
|
|
if (callbackFile != INVALID_HANDLE_VALUE)
|
|
|
|
break;
|
|
|
|
|
2012-01-12 06:54:40 -08:00
|
|
|
DWORD lastError = GetLastError();
|
|
|
|
LOG(("NS_main: callback app open attempt %d failed. " \
|
|
|
|
"File: " LOG_S ". Last error: %d\n", retries,
|
|
|
|
argv[callbackIndex], lastError));
|
|
|
|
|
|
|
|
Sleep(100);
|
|
|
|
} while (++retries <= max_retries);
|
2010-11-18 17:21:20 -08:00
|
|
|
|
2009-11-13 13:11:20 -08:00
|
|
|
// CreateFileW will fail if the callback executable is already in use. Since
|
|
|
|
// it isn't possible to update write the status file and return.
|
|
|
|
if (callbackFile == INVALID_HANDLE_VALUE) {
|
|
|
|
LOG(("NS_main: file in use - failed to exclusively open executable " \
|
2010-10-01 21:11:24 -07:00
|
|
|
"file: " LOG_S "\n", argv[callbackIndex]));
|
2009-11-13 13:11:20 -08:00
|
|
|
LogFinish();
|
|
|
|
WriteStatusFile(WRITE_ERROR);
|
2011-04-11 21:23:27 -07:00
|
|
|
NS_tremove(gCallbackBackupPath);
|
2009-11-13 13:11:20 -08:00
|
|
|
EXIT_WHEN_ELEVATED(elevatedLockFilePath, updateLockFileHandle, 1);
|
2012-01-04 20:19:18 -08:00
|
|
|
LaunchCallbackApp(argv[4],
|
|
|
|
argc - callbackIndex,
|
|
|
|
argv + callbackIndex,
|
|
|
|
usingService);
|
2009-11-13 13:11:20 -08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-11-18 17:20:44 -08:00
|
|
|
// The directory to move files that are in use to on Windows. This directory
|
2011-04-11 21:23:27 -07:00
|
|
|
// will be deleted after the update is finished or on OS reboot using
|
2010-11-18 17:20:44 -08:00
|
|
|
// MoveFileEx if it contains files that are in use.
|
|
|
|
if (NS_taccess(DELETE_DIR, F_OK)) {
|
|
|
|
NS_tmkdir(DELETE_DIR, 0755);
|
2009-10-06 16:39:51 -07:00
|
|
|
}
|
2011-04-11 21:23:27 -07:00
|
|
|
#endif /* XP_WIN */
|
2009-10-06 16:39:51 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Run update process on a background thread. ShowProgressUI may return
|
|
|
|
// before QuitProgressUI has been called, so wait for UpdateThreadFunc to
|
|
|
|
// terminate.
|
|
|
|
Thread t;
|
2009-11-13 13:11:20 -08:00
|
|
|
if (t.Run(UpdateThreadFunc, NULL) == 0) {
|
2007-03-22 10:30:00 -07:00
|
|
|
ShowProgressUI();
|
2009-11-13 13:11:20 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
t.Join();
|
|
|
|
|
2009-11-13 13:11:20 -08:00
|
|
|
#ifdef XP_WIN
|
2010-10-01 21:11:24 -07:00
|
|
|
if (argc > callbackIndex) {
|
2009-11-13 13:11:20 -08:00
|
|
|
CloseHandle(callbackFile);
|
2011-04-11 21:23:27 -07:00
|
|
|
// Remove the copy of the callback executable.
|
|
|
|
NS_tremove(gCallbackBackupPath);
|
2009-11-13 13:11:20 -08:00
|
|
|
}
|
2010-11-18 17:20:44 -08:00
|
|
|
|
|
|
|
if (_wrmdir(DELETE_DIR)) {
|
|
|
|
LOG(("NS_main: unable to remove directory: " LOG_S ", err: %d\n",
|
|
|
|
DELETE_DIR, errno));
|
|
|
|
// The directory probably couldn't be removed due to it containing files
|
|
|
|
// that are in use and will be removed on OS reboot. The call to remove the
|
|
|
|
// directory on OS reboot is done after the calls to remove the files so the
|
|
|
|
// files are removed first on OS reboot since the directory must be empty
|
|
|
|
// for the directory removal to be successful. The MoveFileEx call to remove
|
|
|
|
// the directory on OS reboot will fail if the process doesn't have write
|
|
|
|
// access to the HKEY_LOCAL_MACHINE registry key but this is ok since the
|
|
|
|
// installer / uninstaller will delete the directory along with its contents
|
|
|
|
// after an update is applied, on reinstall, and on uninstall.
|
|
|
|
if (MoveFileEx(DELETE_DIR, NULL, MOVEFILE_DELAY_UNTIL_REBOOT)) {
|
|
|
|
LOG(("NS_main: directory will be removed on OS reboot: " LOG_S "\n",
|
|
|
|
DELETE_DIR));
|
|
|
|
} else {
|
|
|
|
LOG(("NS_main: failed to schedule OS reboot removal of " \
|
|
|
|
"directory: " LOG_S "\n", DELETE_DIR));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* XP_WIN */
|
2009-11-13 13:11:20 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
LogFinish();
|
|
|
|
|
2010-10-01 21:11:24 -07:00
|
|
|
if (argc > callbackIndex) {
|
2011-04-11 21:23:27 -07:00
|
|
|
#if defined(XP_WIN)
|
2009-11-13 13:11:20 -08:00
|
|
|
if (gSucceeded) {
|
2012-01-04 20:19:14 -08:00
|
|
|
// The service update will only be executed if it is already installed.
|
|
|
|
// For first time installs of the service, the install will happen from
|
|
|
|
// the PostUpdate process. We do the service update process here
|
|
|
|
// because it's possible we are updating with updater.exe without the
|
|
|
|
// service if the service failed to apply the update. We want to update
|
|
|
|
// the service to a newer version in that case. If we are not running
|
2012-01-04 20:19:15 -08:00
|
|
|
// through the service, then MOZ_USING_SERVICE will not exist.
|
|
|
|
if (!usingService) {
|
2012-01-04 20:19:14 -08:00
|
|
|
if (!LaunchWinPostProcess(argv[2], gSourcePath, false, NULL)) {
|
|
|
|
LOG(("NS_main: The post update process could not be launched.\n"));
|
|
|
|
}
|
|
|
|
StartServiceUpdate(argc, argv);
|
|
|
|
}
|
2009-08-25 18:41:18 -07:00
|
|
|
}
|
2009-11-13 13:11:20 -08:00
|
|
|
EXIT_WHEN_ELEVATED(elevatedLockFilePath, updateLockFileHandle, 0);
|
2011-04-11 21:23:27 -07:00
|
|
|
#endif /* XP_WIN */
|
2010-05-22 14:10:31 -07:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
if (gSucceeded) {
|
2010-10-01 21:11:24 -07:00
|
|
|
LaunchMacPostProcess(argv[callbackIndex]);
|
2010-05-22 14:10:31 -07:00
|
|
|
}
|
|
|
|
#endif /* XP_MACOSX */
|
2012-01-04 20:19:18 -08:00
|
|
|
LaunchCallbackApp(argv[4],
|
|
|
|
argc - callbackIndex,
|
|
|
|
argv + callbackIndex,
|
|
|
|
usingService);
|
2009-08-25 18:41:18 -07:00
|
|
|
}
|
2009-11-13 13:11:20 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
class ActionList
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ActionList() : mFirst(NULL), mLast(NULL), mCount(0) { }
|
|
|
|
~ActionList();
|
|
|
|
|
|
|
|
void Append(Action* action);
|
|
|
|
int Prepare();
|
|
|
|
int Execute();
|
|
|
|
void Finish(int status);
|
|
|
|
|
|
|
|
private:
|
|
|
|
Action *mFirst;
|
|
|
|
Action *mLast;
|
|
|
|
int mCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
ActionList::~ActionList()
|
|
|
|
{
|
|
|
|
Action* a = mFirst;
|
|
|
|
while (a) {
|
|
|
|
Action *b = a;
|
|
|
|
a = a->mNext;
|
|
|
|
delete b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ActionList::Append(Action *action)
|
|
|
|
{
|
|
|
|
if (mLast)
|
|
|
|
mLast->mNext = action;
|
|
|
|
else
|
|
|
|
mFirst = action;
|
|
|
|
|
|
|
|
mLast = action;
|
|
|
|
mCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ActionList::Prepare()
|
|
|
|
{
|
|
|
|
// If the action list is empty then we should fail in order to signal that
|
|
|
|
// something has gone wrong. Otherwise we report success when nothing is
|
|
|
|
// actually done. See bug 327140.
|
|
|
|
if (mCount == 0) {
|
|
|
|
LOG(("empty action list\n"));
|
|
|
|
return UNEXPECTED_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
Action *a = mFirst;
|
2009-10-06 16:39:51 -07:00
|
|
|
int i = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
while (a) {
|
|
|
|
int rv = a->Prepare();
|
|
|
|
if (rv)
|
|
|
|
return rv;
|
|
|
|
|
2009-10-06 16:39:51 -07:00
|
|
|
float percent = float(++i) / float(mCount);
|
|
|
|
UpdateProgressUI(PROGRESS_PREPARE_SIZE * percent);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
a = a->mNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ActionList::Execute()
|
|
|
|
{
|
2009-10-06 16:39:51 -07:00
|
|
|
int currentProgress = 0, maxProgress = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
Action *a = mFirst;
|
|
|
|
while (a) {
|
2009-10-06 16:39:51 -07:00
|
|
|
maxProgress += a->mProgressCost;
|
|
|
|
a = a->mNext;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-06 16:39:51 -07:00
|
|
|
a = mFirst;
|
|
|
|
while (a) {
|
2007-03-22 10:30:00 -07:00
|
|
|
int rv = a->Execute();
|
2009-07-29 23:01:36 -07:00
|
|
|
if (rv) {
|
2007-03-22 10:30:00 -07:00
|
|
|
LOG(("### execution failed\n"));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2009-10-06 16:39:51 -07:00
|
|
|
currentProgress += a->mProgressCost;
|
|
|
|
float percent = float(currentProgress) / float(maxProgress);
|
2009-10-06 16:39:51 -07:00
|
|
|
UpdateProgressUI(PROGRESS_PREPARE_SIZE +
|
|
|
|
PROGRESS_EXECUTE_SIZE * percent);
|
2009-10-06 16:39:51 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
a = a->mNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ActionList::Finish(int status)
|
|
|
|
{
|
|
|
|
Action *a = mFirst;
|
2009-10-06 16:39:51 -07:00
|
|
|
int i = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
while (a) {
|
|
|
|
a->Finish(status);
|
2009-10-06 16:39:51 -07:00
|
|
|
|
|
|
|
float percent = float(++i) / float(mCount);
|
|
|
|
UpdateProgressUI(PROGRESS_PREPARE_SIZE +
|
|
|
|
PROGRESS_EXECUTE_SIZE +
|
|
|
|
PROGRESS_FINISH_SIZE * percent);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
a = a->mNext;
|
|
|
|
}
|
|
|
|
|
2007-07-30 13:16:56 -07:00
|
|
|
if (status == OK)
|
2010-05-22 14:10:31 -07:00
|
|
|
gSucceeded = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
int add_dir_entries(const NS_tchar *dirpath, ActionList *list)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-04-11 21:23:27 -07:00
|
|
|
int rv = OK;
|
|
|
|
WIN32_FIND_DATAW finddata;
|
|
|
|
HANDLE hFindFile;
|
|
|
|
NS_tchar searchspec[MAXPATHLEN];
|
|
|
|
NS_tchar foundpath[MAXPATHLEN];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
NS_tsnprintf(searchspec, sizeof(searchspec)/sizeof(searchspec[0]),
|
|
|
|
NS_T("%s*"), dirpath);
|
|
|
|
const NS_tchar *pszSpec = get_full_path(searchspec);
|
|
|
|
|
|
|
|
hFindFile = FindFirstFileW(pszSpec, &finddata);
|
|
|
|
if (hFindFile != INVALID_HANDLE_VALUE) {
|
|
|
|
do {
|
|
|
|
// Don't process the current or parent directory.
|
|
|
|
if (NS_tstrcmp(finddata.cFileName, NS_T(".")) == 0 ||
|
|
|
|
NS_tstrcmp(finddata.cFileName, NS_T("..")) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
NS_tsnprintf(foundpath, sizeof(foundpath)/sizeof(foundpath[0]),
|
|
|
|
NS_T("%s%s"), dirpath, finddata.cFileName);
|
|
|
|
if (finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
|
|
|
NS_tsnprintf(foundpath, sizeof(foundpath)/sizeof(foundpath[0]),
|
|
|
|
NS_T("%s/"), foundpath);
|
|
|
|
// Recurse into the directory.
|
|
|
|
rv = add_dir_entries(foundpath, list);
|
|
|
|
if (rv) {
|
|
|
|
LOG(("add_dir_entries error: " LOG_S ", err: %d\n", foundpath, rv));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Add the file to be removed to the ActionList.
|
|
|
|
NS_tchar *quotedpath = get_quoted_path(foundpath);
|
|
|
|
if (!quotedpath)
|
|
|
|
return PARSE_ERROR;
|
|
|
|
|
|
|
|
Action *action = new RemoveFile();
|
|
|
|
rv = action->Parse(quotedpath);
|
|
|
|
if (rv) {
|
|
|
|
LOG(("add_dir_entries Parse error on recurse: " LOG_S ", err: %d\n", quotedpath, rv));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
list->Append(action);
|
|
|
|
}
|
|
|
|
} while (FindNextFileW(hFindFile, &finddata) != 0);
|
|
|
|
|
|
|
|
FindClose(hFindFile);
|
|
|
|
{
|
|
|
|
// Add the directory to be removed to the ActionList.
|
|
|
|
NS_tchar *quotedpath = get_quoted_path(dirpath);
|
|
|
|
if (!quotedpath)
|
|
|
|
return PARSE_ERROR;
|
|
|
|
|
|
|
|
Action *action = new RemoveDir();
|
|
|
|
rv = action->Parse(quotedpath);
|
|
|
|
if (rv)
|
|
|
|
LOG(("add_dir_entries Parse error on close: " LOG_S ", err: %d\n", quotedpath, rv));
|
|
|
|
else
|
|
|
|
list->Append(action);
|
|
|
|
}
|
2010-09-09 12:22:02 -07:00
|
|
|
}
|
2007-12-31 07:15:43 -08:00
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
int add_dir_entries(const NS_tchar *dirpath, ActionList *list)
|
|
|
|
{
|
|
|
|
int rv = OK;
|
|
|
|
FTS *ftsdir;
|
|
|
|
FTSENT *ftsdirEntry;
|
|
|
|
NS_tchar searchpath[MAXPATHLEN];
|
|
|
|
|
|
|
|
NS_tsnprintf(searchpath, sizeof(searchpath)/sizeof(searchpath[0]), NS_T("%s"),
|
|
|
|
dirpath);
|
|
|
|
// Remove the trailing slash so the paths don't contain double slashes. The
|
|
|
|
// existence of the slash has already been checked in DoUpdate.
|
|
|
|
searchpath[NS_tstrlen(searchpath) - 1] = NS_T('\0');
|
|
|
|
char* const pathargv[] = {searchpath, NULL};
|
|
|
|
|
|
|
|
// FTS_NOCHDIR is used so relative paths from the destination directory are
|
|
|
|
// returned.
|
|
|
|
if (!(ftsdir = fts_open(pathargv,
|
|
|
|
FTS_PHYSICAL | FTS_NOSTAT | FTS_XDEV | FTS_NOCHDIR,
|
|
|
|
NULL)))
|
|
|
|
return UNEXPECTED_ERROR;
|
|
|
|
|
|
|
|
while ((ftsdirEntry = fts_read(ftsdir)) != NULL) {
|
|
|
|
NS_tchar foundpath[MAXPATHLEN];
|
|
|
|
NS_tchar *quotedpath;
|
|
|
|
Action *action = NULL;
|
|
|
|
|
|
|
|
switch (ftsdirEntry->fts_info) {
|
|
|
|
// Filesystem objects that shouldn't be in the application's directories
|
|
|
|
case FTS_SL:
|
|
|
|
case FTS_SLNONE:
|
|
|
|
case FTS_DEFAULT:
|
|
|
|
LOG(("add_dir_entries: found a non-standard file: " LOG_S "\n",
|
|
|
|
ftsdirEntry->fts_path));
|
|
|
|
// Fall through and try to remove as a file
|
|
|
|
|
|
|
|
// Files
|
|
|
|
case FTS_F:
|
|
|
|
case FTS_NSOK:
|
|
|
|
// Add the file to be removed to the ActionList.
|
|
|
|
NS_tsnprintf(foundpath, sizeof(foundpath)/sizeof(foundpath[0]),
|
|
|
|
NS_T("%s"), ftsdirEntry->fts_accpath);
|
|
|
|
quotedpath = get_quoted_path(foundpath);
|
|
|
|
if (!quotedpath) {
|
2012-01-18 10:43:21 -08:00
|
|
|
rv = UPDATER_QUOTED_PATH_MEM_ERROR;
|
2011-04-11 21:23:27 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
action = new RemoveFile();
|
|
|
|
rv = action->Parse(quotedpath);
|
|
|
|
if (!rv)
|
|
|
|
list->Append(action);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Directories
|
|
|
|
case FTS_DP:
|
|
|
|
rv = OK;
|
|
|
|
// Add the directory to be removed to the ActionList.
|
|
|
|
NS_tsnprintf(foundpath, sizeof(foundpath)/sizeof(foundpath[0]),
|
|
|
|
NS_T("%s/"), ftsdirEntry->fts_accpath);
|
|
|
|
quotedpath = get_quoted_path(foundpath);
|
|
|
|
if (!quotedpath) {
|
2012-01-18 10:43:21 -08:00
|
|
|
rv = UPDATER_QUOTED_PATH_MEM_ERROR;
|
2011-04-11 21:23:27 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
action = new RemoveDir();
|
|
|
|
rv = action->Parse(quotedpath);
|
|
|
|
if (!rv)
|
|
|
|
list->Append(action);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Errors
|
|
|
|
case FTS_DNR:
|
|
|
|
case FTS_NS:
|
|
|
|
// ENOENT is an acceptable error for FTS_DNR and FTS_NS and means that
|
|
|
|
// we're racing with ourselves. Though strange, the entry will be
|
|
|
|
// removed anyway.
|
|
|
|
if (ENOENT == ftsdirEntry->fts_errno) {
|
|
|
|
rv = OK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Fall through
|
|
|
|
|
|
|
|
case FTS_ERR:
|
|
|
|
rv = UNEXPECTED_ERROR;
|
|
|
|
LOG(("add_dir_entries: fts_read() error: " LOG_S ", err: %d\n",
|
|
|
|
ftsdirEntry->fts_path, ftsdirEntry->fts_errno));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FTS_DC:
|
|
|
|
rv = UNEXPECTED_ERROR;
|
|
|
|
LOG(("add_dir_entries: fts_read() returned FT_DC: " LOG_S "\n",
|
|
|
|
ftsdirEntry->fts_path));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// FTS_D is ignored and FTS_DP is used instead (post-order).
|
|
|
|
rv = OK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rv != OK)
|
|
|
|
break;
|
2010-09-09 12:22:02 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
fts_close(ftsdir);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static NS_tchar*
|
|
|
|
GetManifestContents(const NS_tchar *manifest)
|
|
|
|
{
|
2009-07-16 15:21:09 -07:00
|
|
|
AutoFile mfile = NS_tfopen(manifest, NS_T("rb"));
|
2010-09-09 12:22:02 -07:00
|
|
|
if (mfile == NULL) {
|
2011-04-11 21:23:27 -07:00
|
|
|
LOG(("GetManifestContents: error opening manifest file: " LOG_S "\n", manifest));
|
|
|
|
return NULL;
|
2010-09-09 12:22:02 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
struct stat ms;
|
2011-04-11 21:23:27 -07:00
|
|
|
int rv = fstat(fileno((FILE *)mfile), &ms);
|
2010-09-09 12:22:02 -07:00
|
|
|
if (rv) {
|
2011-04-11 21:23:27 -07:00
|
|
|
LOG(("GetManifestContents: error stating manifest file: " LOG_S "\n", manifest));
|
|
|
|
return NULL;
|
2010-09-09 12:22:02 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
char *mbuf = (char *) malloc(ms.st_size + 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!mbuf)
|
2011-04-11 21:23:27 -07:00
|
|
|
return NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-09-20 21:41:38 -07:00
|
|
|
size_t r = ms.st_size;
|
2007-03-22 10:30:00 -07:00
|
|
|
char *rb = mbuf;
|
|
|
|
while (r) {
|
2012-01-12 16:38:20 -08:00
|
|
|
const size_t count = mmin(SSIZE_MAX, r);
|
|
|
|
size_t c = fread(rb, 1, count, mfile);
|
|
|
|
if (c != count) {
|
2011-04-11 21:23:27 -07:00
|
|
|
LOG(("GetManifestContents: error reading manifest file: " LOG_S "\n", manifest));
|
|
|
|
return NULL;
|
2010-09-09 12:22:02 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
r -= c;
|
|
|
|
rb += c;
|
|
|
|
}
|
|
|
|
mbuf[ms.st_size] = '\0';
|
2011-04-11 21:23:27 -07:00
|
|
|
rb = mbuf;
|
|
|
|
|
|
|
|
#ifndef XP_WIN
|
|
|
|
return rb;
|
|
|
|
#else
|
|
|
|
NS_tchar *wrb = (NS_tchar *) malloc((ms.st_size + 1) * sizeof(NS_tchar));
|
|
|
|
if (!wrb)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, rb, -1, wrb,
|
|
|
|
ms.st_size + 1)) {
|
|
|
|
LOG(("GetManifestContents: error converting utf8 to utf16le: %d\n", GetLastError()));
|
|
|
|
free(mbuf);
|
|
|
|
free(wrb);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
free(mbuf);
|
|
|
|
|
|
|
|
return wrb;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-01-18 14:10:38 -08:00
|
|
|
int AddPreCompleteActions(ActionList *list)
|
2011-04-11 21:23:27 -07:00
|
|
|
{
|
|
|
|
NS_tchar *rb = GetManifestContents(NS_T("precomplete"));
|
|
|
|
if (rb == NULL) {
|
|
|
|
LOG(("AddPreCompleteActions: error getting contents of precomplete " \
|
|
|
|
"manifest\n"));
|
|
|
|
// Applications aren't required to have a precomplete manifest yet.
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rv;
|
|
|
|
NS_tchar *line;
|
|
|
|
while((line = mstrtok(kNL, &rb)) != 0) {
|
|
|
|
// skip comments
|
|
|
|
if (*line == NS_T('#'))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
NS_tchar *token = mstrtok(kWhitespace, &line);
|
|
|
|
if (!token) {
|
|
|
|
LOG(("AddPreCompleteActions: token not found in manifest\n"));
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
Action *action = NULL;
|
|
|
|
if (NS_tstrcmp(token, NS_T("remove")) == 0) { // rm file
|
|
|
|
action = new RemoveFile();
|
|
|
|
}
|
2012-01-18 14:10:38 -08:00
|
|
|
else if (NS_tstrcmp(token, NS_T("remove-cc")) == 0) { // no longer supported
|
|
|
|
continue;
|
2011-04-11 21:23:27 -07:00
|
|
|
}
|
|
|
|
else if (NS_tstrcmp(token, NS_T("rmdir")) == 0) { // rmdir if empty
|
|
|
|
action = new RemoveDir();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG(("AddPreCompleteActions: unknown token: " LOG_S "\n", token));
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!action)
|
2012-01-18 10:43:21 -08:00
|
|
|
return BAD_ACTION_ERROR;
|
2011-04-11 21:23:27 -07:00
|
|
|
|
|
|
|
rv = action->Parse(line);
|
|
|
|
if (rv)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
list->Append(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DoUpdate()
|
|
|
|
{
|
|
|
|
NS_tchar manifest[MAXPATHLEN];
|
|
|
|
NS_tsnprintf(manifest, sizeof(manifest)/sizeof(manifest[0]),
|
|
|
|
NS_T("%s/update.manifest"), gSourcePath);
|
|
|
|
|
|
|
|
// extract the manifest
|
|
|
|
int rv = gArchiveReader.ExtractFile("updatev2.manifest", manifest);
|
|
|
|
if (rv) {
|
|
|
|
rv = gArchiveReader.ExtractFile("update.manifest", manifest);
|
|
|
|
if (rv) {
|
|
|
|
LOG(("DoUpdate: error extracting manifest file\n"));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_tchar *rb = GetManifestContents(manifest);
|
|
|
|
if (rb == NULL) {
|
|
|
|
LOG(("DoUpdate: error opening manifest file: " LOG_S "\n", manifest));
|
|
|
|
return READ_ERROR;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
ActionList list;
|
2011-04-11 21:23:27 -07:00
|
|
|
NS_tchar *line;
|
|
|
|
bool isFirstAction = true;
|
|
|
|
bool isComplete = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
while((line = mstrtok(kNL, &rb)) != 0) {
|
|
|
|
// skip comments
|
2011-04-11 21:23:27 -07:00
|
|
|
if (*line == NS_T('#'))
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
NS_tchar *token = mstrtok(kWhitespace, &line);
|
2010-09-09 12:22:02 -07:00
|
|
|
if (!token) {
|
|
|
|
LOG(("DoUpdate: token not found in manifest\n"));
|
2007-03-22 10:30:00 -07:00
|
|
|
return PARSE_ERROR;
|
2010-09-09 12:22:02 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-04-11 21:23:27 -07:00
|
|
|
if (isFirstAction && NS_tstrcmp(token, NS_T("type")) == 0) {
|
|
|
|
const NS_tchar *type = mstrtok(kQuote, &line);
|
|
|
|
LOG(("UPDATE TYPE " LOG_S "\n", type));
|
|
|
|
if (NS_tstrcmp(type, NS_T("complete")) == 0) {
|
|
|
|
isComplete = true;
|
2012-01-18 14:10:38 -08:00
|
|
|
rv = AddPreCompleteActions(&list);
|
2011-04-11 21:23:27 -07:00
|
|
|
if (rv)
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
isFirstAction = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
isFirstAction = false;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
Action *action = NULL;
|
2011-04-11 21:23:27 -07:00
|
|
|
if (NS_tstrcmp(token, NS_T("remove")) == 0) { // rm file
|
2007-03-22 10:30:00 -07:00
|
|
|
action = new RemoveFile();
|
|
|
|
}
|
2011-04-11 21:23:27 -07:00
|
|
|
else if (NS_tstrcmp(token, NS_T("rmdir")) == 0) { // rmdir if empty
|
|
|
|
action = new RemoveDir();
|
|
|
|
}
|
|
|
|
else if (NS_tstrcmp(token, NS_T("rmrfdir")) == 0) { // rmdir recursive
|
|
|
|
const NS_tchar *reldirpath = mstrtok(kQuote, &line);
|
|
|
|
if (!reldirpath)
|
|
|
|
return PARSE_ERROR;
|
|
|
|
|
|
|
|
if (reldirpath[NS_tstrlen(reldirpath) - 1] != NS_T('/'))
|
|
|
|
return PARSE_ERROR;
|
|
|
|
|
|
|
|
rv = add_dir_entries(reldirpath, &list);
|
|
|
|
if (rv)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (NS_tstrcmp(token, NS_T("add")) == 0) {
|
2007-03-22 10:30:00 -07:00
|
|
|
action = new AddFile();
|
|
|
|
}
|
2011-04-11 21:23:27 -07:00
|
|
|
else if (NS_tstrcmp(token, NS_T("patch")) == 0) {
|
2007-03-22 10:30:00 -07:00
|
|
|
action = new PatchFile();
|
|
|
|
}
|
2011-04-11 21:23:27 -07:00
|
|
|
else if (NS_tstrcmp(token, NS_T("add-if")) == 0) { // Add if exists
|
2007-03-22 10:30:00 -07:00
|
|
|
action = new AddIfFile();
|
|
|
|
}
|
2011-04-11 21:23:27 -07:00
|
|
|
else if (NS_tstrcmp(token, NS_T("patch-if")) == 0) { // Patch if exists
|
2007-03-22 10:30:00 -07:00
|
|
|
action = new PatchIfFile();
|
|
|
|
}
|
2012-01-18 14:10:38 -08:00
|
|
|
else if (NS_tstrcmp(token, NS_T("add-cc")) == 0) { // no longer supported
|
|
|
|
continue;
|
2011-04-11 21:23:27 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
else {
|
2011-04-11 21:23:27 -07:00
|
|
|
LOG(("DoUpdate: unknown token: " LOG_S "\n", token));
|
2007-03-22 10:30:00 -07:00
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!action)
|
2012-01-18 10:43:21 -08:00
|
|
|
return BAD_ACTION_ERROR;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
rv = action->Parse(line);
|
|
|
|
if (rv)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
list.Append(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = list.Prepare();
|
|
|
|
if (rv)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
rv = list.Execute();
|
|
|
|
|
|
|
|
list.Finish(rv);
|
|
|
|
return rv;
|
|
|
|
}
|