Download data
Back
case C_FILE_WGET:
{ // Get a http/ftp file with wget
// Execute wget, outputing to a named pipe
// Not very clean but it works...
if(numP < 3) {
QWrite("ERROR: Not enough parameters", out);
break;
}
if((GetTickCount() - lastWget) < WGET_MINWAIT) {
// Return -1 if user is calling wget too often
QWrite("-1", out);
break;
}
// wget should always return an empty file if an error occurs so no handling should be required for it
// TODO: maybe should return some sort of error message for OFP...
HANDLE pipe;
if(nomap)
// Create a named pipe for wget to write into
pipe = CreateNamedPipe(file.filename, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES, PIPEBUFSIZE, PIPEBUFSIZE, NMPWAIT_USE_DEFAULT_WAIT, NULL);
char *cline = new char[256+strlen(file.filename)+strlen(par[2])];
sprintf(cline, "-q --tries=1 --output-document=\"%s\" --timeout=3 --user-agent=fwatch/%.2f %s", file.filename, SCRIPT_VERSION, par[2]);
// Win23 API bloat
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
ZeroMemory(&pi, sizeof(pi));
// Execute wget
CreateProcess("fwatch/data/wget.exe", cline, NULL, NULL, TRUE, HIGH_PRIORITY_CLASS, NULL, NULL, &si, &pi);
delete[] cline;
lastWget = GetTickCount();
if(nomap) {
// Wait for wget to do its thing
char *buf = new char[PIPEBUFSIZE+1];
if(!buf)
break;
DWORD st, foo = 1, foo2;
do {
// Check exit code
GetExitCodeProcess(pi.hProcess, &st);
Sleep(5); // Lets not eat all cpu cycles
// Read named pipe and output to the out pipe for OFP
ReadFile(pipe, buf, PIPEBUFSIZE, &foo, NULL);
//buf[foo] = 0x00;
if(foo)
WriteFile(out, buf, foo, &foo2, NULL);
// Wait until the process has exited and no more data in pipe
} while(st == STILL_ACTIVE || foo);
WriteFile(out, "\0", 1, &foo2, NULL);
CloseHandle(pipe);
delete[] buf;
} else {
DWORD st;
do {
// Check exit code
GetExitCodeProcess(pi.hProcess, &st);
Sleep(10); // Lets not eat all cpu cycles
// Wait until the process has exited
} while(st == STILL_ACTIVE);
}
}
break;
Back