OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4Update.cpp File Reference
#include "C4Include.h"
#include "c4group/C4Update.h"
#include "C4Version.h"
#include "c4group/C4Components.h"
#include "c4group/C4Group.h"
#include "lib/C4Log.h"
Include dependency graph for C4Update.cpp:

Go to the source code of this file.

Classes

class  C4GroupEx
 

Functions

C4ConfigGetCfg ()
 
bool C4Group_CopyEntry (C4Group *pFrom, C4Group *pTo, const char *strItemName)
 
bool C4Group_ApplyUpdate (C4Group &hGroup, unsigned long ParentProcessID)
 
void MemScramble (BYTE *, int)
 

Variables

char C4Group_TempPath [_MAX_PATH+1]
 

Function Documentation

bool C4Group_ApplyUpdate ( C4Group hGroup,
unsigned long  ParentProcessID 
)

Definition at line 42 of file C4Update.cpp.

References _MAX_FNAME, C4CFN_UpdateCore, C4CFN_UpdateEntries, C4Group_ApplyUpdate(), C4Group_ExplodeDirectory(), C4Group_IsGroup(), C4UPD_CHK_ALREADY_UPDATED, C4UPD_CHK_BAD_SOURCE, C4UPD_CHK_BAD_VERSION, C4UPD_CHK_NO_SOURCE, C4UPD_CHK_OK, C4UpdatePackage::Check(), C4Group::Close(), C4UpdatePackageCore::DestPath, C4Group::EntryCount(), C4UpdatePackage::Execute(), C4Group::Extract(), C4Group::FindEntry(), C4Group::FindNextEntry(), StdStrBuf::getData(), C4UpdatePackage::Load(), C4UpdatePackageCore::Name, C4Group::OpenAsChild(), C4UpdatePackageCore::RequireVersion, C4Group::ResetSearch(), SGetModule(), and WildcardMatch().

Referenced by C4Group_ApplyUpdate(), and ProcessGroup().

43 {
44  // Wait for parent process to terminate (so we can safely replace the executable)
45 #ifdef _WIN32
46  if(ParentProcessID)
47  {
48  HANDLE ParentProcess = OpenProcess(SYNCHRONIZE, FALSE, ParentProcessID);
49  if(ParentProcess)
50  {
51  // If we couldn't find a handle then either
52  // a) the process terminated already, which is great.
53  // b) OpenProcess() failed, which is not so great. But let's still try to do
54  // the update.
55  printf("Waiting for parent process to terminate...");
56  DWORD res = WaitForSingleObject(ParentProcess, 10000);
57  if(res == WAIT_TIMEOUT)
58  fprintf(stderr, "Parent process did not terminate after 10 seconds. Continuing...");
59  }
60  }
61 #else
62  // We could use waitpid on Unix, but we don't need that functionality there anyway...
63 #endif
64 
65  // Process object update group (GRPUP_Entries.txt found)
66  C4UpdatePackage Upd;
67  if (hGroup.FindEntry(C4CFN_UpdateEntries))
68  if (Upd.Load(&hGroup))
69  {
70  // Do update check first (ensure packet has everything it needs in order to perfom the update)
71  int iRes = Upd.Check(&hGroup);
72  switch (iRes)
73  {
74  // Bad version - checks against version of the applying executable (major version must match, minor version must be equal or higher)
76  fprintf(stderr, "This update %s can only be applied using version %d.%d.%d.%d or higher.\n", Upd.Name, Upd.RequireVersion[0], Upd.RequireVersion[1], Upd.RequireVersion[2], Upd.RequireVersion[3]);
77  return false;
78  // Target not found: keep going
80  fprintf(stderr, "Target %s for update %s not found. Ignoring.\n", Upd.DestPath, Upd.Name);
81  return true;
82  // Target mismatch: abort updating
84  fprintf(stderr, "Target %s incorrect version for update %s. Ignoring.\n", Upd.DestPath, Upd.Name);
85  return true;
86  // Target already updated: keep going
88  fprintf(stderr,"Target %s already up-to-date at %s.\n", Upd.DestPath, Upd.Name);
89  return true;
90  // Ok to perform update
91  case C4UPD_CHK_OK:
92  printf("Updating %s to %s... ", Upd.DestPath, Upd.Name);
93  // Make sure the user sees the message while the work is in progress
94  fflush(stdout);
95  // Execute update
96  if (Upd.Execute(&hGroup))
97  {
98  printf("Ok\n");
99  return true;
100  }
101  else
102  {
103  printf("Failed\n");
104  return false;
105  }
106  // Unknown return value from update
107  default:
108  fprintf(stderr,"Unknown error while updating.\n");
109  return false;
110  }
111  }
112 
113  // Process binary update group (AutoUpdate.txt found, additional binary files found)
114  if (hGroup.EntryCount(C4CFN_UpdateCore))
115  if (hGroup.EntryCount() - hGroup.EntryCount(C4CFN_UpdateCore) - hGroup.EntryCount("*.ocu") > 0)
116  {
117  // Notice: AutoUpdate.txt is currently not processed...
118  char strEntry[_MAX_FNAME + 1] = "";
119  StdStrBuf strList;
120  printf("Updating binaries...\n");
121  hGroup.ResetSearch();
122  // Look for binaries
123  while (hGroup.FindNextEntry("*", strEntry))
124  // Accept everything except *.ocu, AutoUpdate.txt, and c4group.exe (which is assumed not to work under Windows)
125  if (!WildcardMatch("*.ocu", strEntry) && !WildcardMatch(C4CFN_UpdateCore, strEntry) && !WildcardMatch("c4group.exe", strEntry))
126  { strList += strEntry; strList += ";"; }
127  // Extract binaries to current working directory
128  if (!hGroup.Extract(strList.getData()))
129  return false;
130  // If extracted file is a group, explode it (this is meant for Clonk.app on Mac)
131  for (int i = 0; SGetModule(strList.getData(), i, strEntry); i++)
132  if (C4Group_IsGroup(strEntry))
133  {
134  printf("Exploding: %s\n", strEntry);
135  if (!C4Group_ExplodeDirectory(strEntry))
136  return false;
137  }
138  }
139 
140  // Process any child updates (*.ocu)
141  if (hGroup.FindEntry("*.ocu"))
142  {
143  // Process all children
144  char strEntry[_MAX_FNAME + 1] = "";
145  C4Group hChild;
146  hGroup.ResetSearch();
147  while (hGroup.FindNextEntry("*.ocu", strEntry))
148  if (hChild.OpenAsChild(&hGroup, strEntry))
149  {
150  bool ok = C4Group_ApplyUpdate(hChild, 0);
151  hChild.Close();
152  // Failure on child update
153  if (!ok) return false;
154  }
155  }
156 
157  // Success
158  return true;
159 }
const char * getData() const
Definition: StdBuf.h:450
bool FindEntry(const char *szWildCard, StdStrBuf *sFileName=nullptr, size_t *iSize=nullptr)
Definition: C4Group.cpp:1774
bool C4Group_ApplyUpdate(C4Group &hGroup, unsigned long ParentProcessID)
Definition: C4Update.cpp:42
bool C4Group_ExplodeDirectory(const char *szFilename)
Definition: C4Group.cpp:351
bool SGetModule(const char *szList, int iIndex, char *sTarget, int iSize)
Definition: Standard.cpp:510
#define C4UPD_CHK_BAD_SOURCE
Definition: C4Update.h:43
int EntryCount(const char *szWildCard=nullptr)
Definition: C4Group.cpp:1850
char DestPath[_MAX_PATH+1]
Definition: C4Update.h:31
int Check(C4Group *pGroup)
Definition: C4Update.cpp:431
#define C4CFN_UpdateCore
Definition: C4Components.h:50
#define C4UPD_CHK_NO_SOURCE
Definition: C4Update.h:42
#define C4CFN_UpdateEntries
Definition: C4Components.h:51
int32_t RequireVersion[4]
Definition: C4Update.h:29
bool Execute(C4Group *pGroup)
Definition: C4Update.cpp:303
bool C4Group_IsGroup(const char *szFilename)
Definition: C4Group.cpp:94
bool Close()
Definition: C4Group.cpp:755
#define _MAX_FNAME
#define C4UPD_CHK_ALREADY_UPDATED
Definition: C4Update.h:44
char Name[C4MaxName+1]
Definition: C4Update.h:30
#define C4UPD_CHK_BAD_VERSION
Definition: C4Update.h:45
bool FindNextEntry(const char *szWildCard, StdStrBuf *sFileName=nullptr, size_t *iSize=nullptr, bool fStartAtFilename=false)
Definition: C4Group.cpp:1780
bool WildcardMatch(const char *szWildcard, const char *szString)
Definition: StdFile.cpp:384
bool Load(C4Group *pGroup)
Definition: C4Update.cpp:280
void ResetSearch(bool reload_contents=false)
Definition: C4Group.cpp:1013
#define C4UPD_CHK_OK
Definition: C4Update.h:41
bool Extract(const char *szFiles, const char *szExtractTo=nullptr, const char *szExclude=nullptr)
Definition: C4Group.cpp:1471
uint32_t DWORD
bool OpenAsChild(C4Group *pMother, const char *szEntryName, bool fExclusive=false, bool fCreate=false)
Definition: C4Group.cpp:1585

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Group_CopyEntry ( C4Group pFrom,
C4Group pTo,
const char *  strItemName 
)

Definition at line 31 of file C4Update.cpp.

References C4Group::Add(), iSize, and C4Group::LoadEntry().

Referenced by C4UpdatePackage::DoUpdate(), and C4UpdatePackage::MkUp().

32 {
33  // read entry
34  char *pData; size_t iSize;
35  if (!pFrom->LoadEntry(strItemName, &pData, &iSize))
36  return false;
37  if (!pTo->Add(strItemName, pData, iSize, false, true))
38  return false;
39  return true;
40 }
bool LoadEntry(const char *szEntryName, char **lpbpBuf, size_t *ipSize=nullptr, int iAppendZeros=0)
Definition: C4Group.cpp:1893
bool Add(const char *szFile, const char *szAddAs)
Definition: C4Group.cpp:1316
int iSize
Definition: TstC4NetIO.cpp:35

Here is the call graph for this function:

Here is the caller graph for this function:

C4Config* GetCfg ( )
void MemScramble ( BYTE ,
int   
)

Definition at line 390 of file C4Group.cpp.

References iSize.

Referenced by C4UpdatePackage::MakeUpdate(), C4Group::OpenAsChild(), and C4Group::Save().

391 {
392  int cnt; BYTE temp;
393  // XOR deface
394  for (cnt=0; cnt<iSize; cnt++)
395  bypBuffer[cnt] ^= 237;
396  // BYTE swap
397  for (cnt=0; cnt+2<iSize; cnt+=3)
398  {
399  temp = bypBuffer[cnt];
400  bypBuffer[cnt] = bypBuffer[cnt+2];
401  bypBuffer[cnt+2] = temp;
402  }
403 }
uint8_t BYTE
int iSize
Definition: TstC4NetIO.cpp:35

Here is the caller graph for this function:

Variable Documentation