仅在Makefile中最近更改时才下载文件(How to download a file only if more recently changed in Makefile)

编程入门 行业动态 更新时间:2024-10-24 11:21:31
仅在Makefile中最近更改时才下载文件(How to download a file only if more recently changed in Makefile)

我通常使用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 $@)

更多推荐

本文发布于:2023-07-31 00:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340461.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时才   文件   Makefile   changed   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!