1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
// fgfilegrab.cc
#include "fgfilegrab.h"
// Possible protocol interface derived classes
#ifndef _FGFTPCON_H
#include "fgftpcon.h"
#endif
// Possible picker interface derived classes
#ifndef _FGPICKALL_H
#include "fgpickall.h"
#endif
#ifndef _FGPICKALLSYNC_H
#include "fgpickallsync.h"
#endif
#ifndef _FGPICKBEST_H
#include "fgpickbest.h"
#endif
#ifndef _FGPICKREGEXP_H
#include "fgpickregexp.h"
#endif
#ifndef _FGPICKREGEXPSYNC_H
#include "fgpickregexpsync.h"
#endif
// Directory listings
#ifndef _FGDLIST_H
#include "fgdlist.h"
#endif
// Local filesystem helper
#ifndef _FGFSHELP_H
#include "fgfshelp.h"
#endif
// Action lists
#ifndef _FGALIST_H
#include "fgalist.h"
#endif
#ifndef _FGLOGGER_H
#include "fglogger.h"
#endif
#ifndef _FGEXC_H
#include "fgexc.h"
#endif
#include <assert.h>
#include <stdlib.h>
FGFileGrab::FGFileGrab(const FGString& ruleName,
const FGString& host, const FGString& remDir,
const FGString& localDir, const FGString& file)
: mpConInterface(0), mpPickInterface(0)
{
mRemoteHost = host;
mRemoteDir = remDir;
mLocalDir = localDir;
mRuleName = ruleName;
// XXX :-)
mpConInterface = new FGFTPCon;
FGString cmp("*");
FGString cmp2("*!");
int nameLen = file.GetLength();
if (file == cmp) {
mpPickInterface = new FGPickAll(mpConInterface);
} else if (file == cmp2) {
mpPickInterface = new FGPickAllSync(mpConInterface);
} else if (nameLen > 1 && file[0] == '/' && file[1] == '!') {
// /! sequence means match a regular expression, and delete local
// files matching if not found remotely, too.
FGString regExp(file.Right(nameLen - 2));
// Alert alert leak
mpPickInterface = new FGPickRegexpSync(mpConInterface, regExp);
} else if (nameLen > 0 && file[0] == '/') {
// / character means regular expression follows
FGString regExp(file.Right(nameLen - 1));
// Alert throws exception - leaks mpConInterface
mpPickInterface = new FGPickRegexp(mpConInterface, regExp);
} else {
// Extract number of revisions if neccessary
int nrevs = 1;
FGString newFile(file);
if (file.GetLength() > 1 && file[0] == '[') {
unsigned int index = 1;
FGString strNum;
while (index < file.GetLength() && file[index] != ']') {
strNum += file[index];
index++;
}
// If we ended by hitting "]" then extract revisions count
// and lop [] enclosed bit off string
if (index < file.GetLength()) {
int newRevs = atoi(strNum);
if (newRevs > 1) {
nrevs = newRevs;
}
int charsLeft = file.GetLength() - (index + 1);
newFile = file.Right(charsLeft);
}
}
// Alert alert this might throw an exception
// If this class (FGFileGrab) allocates any resources in the
// constructor they will need to be taken care of
mpPickInterface = new FGPickBest(mpConInterface, newFile, nrevs);
}
}
const FGString&
FGFileGrab::GetRuleName(void) const
{
return mRuleName;
}
void
FGFileGrab::FreeResources(void)
{
if (mpConInterface) {
delete mpConInterface;
}
if (mpPickInterface) {
delete mpPickInterface;
}
}
void
FGFileGrab::GrabIt(void) const
{
// Pre-conditions
assert(mpConInterface != 0);
assert(mpPickInterface != 0);
FGLogger& theLog = FGLogger::GetLogger();
// Connect to remote
FGString logMsg;
logMsg = "Trying to connect to host: ";
logMsg += mRemoteHost;
theLog.LogMsg(logMsg, FGLogger::kFGLLVerbose);
mpConInterface->Connect(mRemoteHost);
theLog.LogMsg("Connect successful", FGLogger::kFGLLVerbose);
// Once we are connected we must guarantee to call Disconnect() at
// some time
try {
// Change to required directory
logMsg = "Changing to remote directory: ";
logMsg += mRemoteDir;
theLog.LogMsg(logMsg, FGLogger::kFGLLVerbose);
mpConInterface->ChangeDir(mRemoteDir);
// Get list of files in said dir
theLog.LogMsg("Getting local and remote file lists",
FGLogger::kFGLLVerbose);
FGDirListing remoteList = mpConInterface->GetDirListing();
// Get list of files in local dir of interest
FGDirListing localList = FGFileSystemHelper::GetDirListing(mLocalDir);
// Ask the picker interface what we propose to do given the two file lists
FGActionList actions = mpPickInterface->DecideActions(localList, remoteList);
// Let it rip...
try {
actions.DoActions();
}
catch (FGException&) {
actions.FreeResources();
throw;
}
// Free some resources
actions.FreeResources();
}
catch (FGException&) {
theLog.LogMsg("Disconnecting (error condition)",
FGLogger::kFGLLVerbose);
mpConInterface->Disconnect();
throw;
}
// Clean up
theLog.LogMsg("Disconnecting", FGLogger::kFGLLVerbose);
mpConInterface->Disconnect();
}
|