我通常使用wget来下载Makefile中的文件,例如:
my_file: wget http://myserver/$@ -O $@.tmp && mv $@.tmp $@
我首先在tmp文件中下载:如果传输中断,重新启动Makefile时文件将正确重新下载。
问题是,使用-O选项时,仅当远程文件比本地文件更新时,才能使用-N选项来获取文件。
我的问题是:在Makefile中,只有当远程文件更新时才能下载文件,并且能够传输中断的最佳方法是什么?
I usually use wget to download files in a Makefile, e.g.:
my_file: wget http://myserver/$@ -O $@.tmp && mv $@.tmp $@
I first download in a tmp file: in case the transfer is interrupted, the file will be correctly re-download when re-launching the Makefile.
The problem is, with the -O option, it is not possible to use the -N option to fetch the file only if the remote file is more recent than the local file.
My question is: what is the best way, in a Makefile, to download a file only if the remote file is more recent, and to be robust to transfer interruptions?
最满意答案
我通过使用cURL而不是wget解决了我的问题。 我的Makefile目标变为:
my_file: curl -s -S -L -f http://myserver/$@ -z $@ -o $@.tmp && mv -f $@.tmp $@ 2>/dev/null || rm -f $@.tmp $@说明:
-s: silent, no progress bar displayed -S: if silent, shows error message on fail -L: in case of redirection, follow it and redo the request; this is necessary to correctly get modification date -f: in case of error do not display the document returned -z my_local_file: download remote file only if last modification date more recent that modification date of 'my_local_file' -o filename: store downloaded file into 'filename' 成功时,将临时文件移动到最终位置( && mv -f $@.tmp $@ ) 如果文件未重载,则mv stderr重定向到/dev/null ,因此临时文件不存在( && mv -f $@.tmp $@ 2> /dev/null ) 如果出现错误, rm临时文件和最终文件( || rm -f $@.tmp $@ )I solved my problem by using cURL instead of wget. My Makefile target becomes:
my_file: curl -s -S -L -f http://myserver/$@ -z $@ -o $@.tmp && mv -f $@.tmp $@ 2>/dev/null || rm -f $@.tmp $@Explanation:
-s: silent, no progress bar displayed -S: if silent, shows error message on fail -L: in case of redirection, follow it and redo the request; this is necessary to correctly get modification date -f: in case of error do not display the document returned -z my_local_file: download remote file only if last modification date more recent that modification date of 'my_local_file' -o filename: store downloaded file into 'filename' on success, move temp file to final location (&& mv -f $@.tmp $@) stderr of mv redirected to /dev/null in case the file was not redownloaded, so that the temp file does not exist (&& mv -f $@.tmp $@ 2> /dev/null) in case of error, rm temp file and final file (|| rm -f $@.tmp $@)更多推荐
发布评论