Tips & Tricks
Ripresa download con curl
Leggendo il man di cURL ho notato che ha, fra le tante, l'opzione di poter effettuare il resume del download, ma qualora la connessione cadesse sarebbe necessario l'intervento dell'utente per riavviare il down. Allora ho pensato di automatizzare il tutto. In *nix è elementare, creare un piccolo script per gestire il tutto:
while ! curl -C - -O 'http://server.com/path/to/file/file.tar.gz'; do sleep 5; done
Credo che lo stesso si possa fare in Windows con uno script batch, o usando la nuova shell di Microsoft: Powershell (Monad). Ma non volendo installare nulla, ho seguito la via, per me, più semplice: farmi un programmino in C, che mi gestisca il tutto. Ed è venuto fuori questo:
/***************************************************************************
downmgr.c
Copyright (c) 2007 Giuseppe Sammarco
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
***************************************************************************/
#include <time.h>
#include <process.h>
#include <stdio.h>
void sleep(unsigned int);
void sleep(unsigned int msec){
clock_t fine = msec + clock();
while (fine > clock());
}
int main(int argc, char **argv){
const char *args[10];
int ret;
if(argc<2){
printf("Uso: %s [link]", argv[0]);
printf("\n\nEsempio: %s http://server.com/path/to/file/file.tar.gz", argv[0]);
exit(-1);
}
args[0] = "curl"; // eseguibile
args[1] = "-C -"; // opzione di curl per il resume del download --> vedi manuale
args[2] = "-O"; // genera il nome del file di output dal link per il download --> vedi manuale
args[3] = argv[1]; //gli passo il link per il download
args[4] = NULL; /* terminare sempre con NULL */
do{
ret = _spawnv(_P_WAIT, args[0], args); //esegue la chiamate di curl.exe con le opzioni e ne
//attende il completamento e ritorna l'exit code di curl
if(ret != 0) //se il down fallisce aspetta 10 secondi e riavvia curl
sleep(10000);
}while(ret != 0);
if(!ret){
printf("\nDownload terminato con successo!");
exit (0);
}
else{
printf("\nErrore nel download!");
exit (-1);
}
return 0;
}
Non ha la pretesa di essere completo, né tantomeno ottimizzato! E' frutto di appena qualche minuto di lavoro alla tastiera! E lascia amplissimo spazio ai miglioramenti. Magari in futuro!






