hack のためのネタ帳, etc,,,

問題点

環境

環境

症状

-N -c を両方指定すると、-c が優先されてしまい -N が機能しない。
結果、以下のように、
  • 古いファイルとファイルサイズに差異がある場合、download を resume しようとしてファイルを破損する
  • 古いファイルとファイルサイズに差異がない場合、download が complete 扱いされて更新されない
$ ls -l setup.bz2*; md5sum setup.bz2*
-rw-r--r--+ 1 kou None 2066818 Feb 23 08:40 setup.bz2
-rw-r--r--+ 1 kou None      72 Feb 23 12:10 setup.bz2.sig
1104e25d6b0cfc643febf39d689ca362 *setup.bz2
9a1eabe88e3f8d86b8724883d55381ab *setup.bz2.sig

$ gpg --keyring pubring.gpg setup.bz2.sig
gpg: assuming signed data in `setup.bz2'
gpg: Signature made Tue Feb 23 12:10:32 2016 JST using DSA key ID 676041BA
gpg: Good signature from "Cygwin <cygwin@cygwin.com>"
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: 1169 DF9F 2273 4F74 3AA5  9232 A9A2 62FF 6760 41BA

$ wget -N -c http://ftp.jaist.ac.jp/pub/cygwin/x86_64/setup.bz2 http://ftp.jaist.ac.jp/pub/cygwin/x86_64/setup.bz2.sig
--2016-02-25 18:27:17--  http://ftp.jaist.ac.jp/pub/cygwin/x86_64/setup.bz2
Resolving ftp.jaist.ac.jp (ftp.jaist.ac.jp)... 150.65.7.130, 2001:df0:2ed:feed::feed
Connecting to ftp.jaist.ac.jp (ftp.jaist.ac.jp)|150.65.7.130|:80... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 2073038 (2.0M), 6220 (6.1K) remaining [application/x-bzip2]
Saving to: ‘setup.bz2’

setup.bz2           100%[+++++++++++++++++++>]   1.98M  --.-KB/s    in 0s

2016-02-25 18:27:17 (237 MB/s) - ‘setup.bz2’ saved [2073038/2073038]

--2016-02-25 18:27:17--  http://ftp.jaist.ac.jp/pub/cygwin/x86_64/setup.bz2.sig
Reusing existing connection to ftp.jaist.ac.jp:80.
HTTP request sent, awaiting response... 416 Requested Range Not Satisfiable

    The file is already fully retrieved; nothing to do.

FINISHED --2016-02-25 18:27:17--
Total wall clock time: 0.1s
Downloaded: 1 files, 6.1K in 0s (237 MB/s)

$ ls -l setup.bz2*; md5sum setup.bz2*
-rw-r--r--+ 1 kou None 2073038 Feb 25 15:10 setup.bz2
-rw-r--r--+ 1 kou None      72 Feb 23 12:10 setup.bz2.sig
ff5c8d90234240671eac9cb47f09cb30 *setup.bz2
9a1eabe88e3f8d86b8724883d55381ab *setup.bz2.sig

$ gpg --keyring pubring.gpg setup.bz2.sig                                       gpg: assuming signed data in `setup.bz2'
gpg: Signature made Tue Feb 23 12:10:32 2016 JST using DSA key ID 676041BA
gpg: BAD signature from "Cygwin <cygwin@cygwin.com>"
-N オプションのみにすれば以下のように更新されるが、この場合、-c がないため download が未完了だった場合 resume されない。
$ ls -l setup.bz2*; md5sum setup.bz2*
-rw-r--r--+ 1 kou None 2066818 Feb 23 08:40 setup.bz2
-rw-r--r--+ 1 kou None      72 Feb 23 12:10 setup.bz2.sig
1104e25d6b0cfc643febf39d689ca362 *setup.bz2
9a1eabe88e3f8d86b8724883d55381ab *setup.bz2.sig

$ wget -N http://ftp.jaist.ac.jp/pub/cygwin/x86_64/setup.bz2 http://ftp.jaist.ac.jp/pub/cygwin/x86_64/setup.bz2.sig
--2016-02-25 18:30:11--  http://ftp.jaist.ac.jp/pub/cygwin/x86_64/setup.bz2
Resolving ftp.jaist.ac.jp (ftp.jaist.ac.jp)... 150.65.7.130, 2001:df0:2ed:feed::feed
Connecting to ftp.jaist.ac.jp (ftp.jaist.ac.jp)|150.65.7.130|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2073038 (2.0M) [application/x-bzip2]
Saving to: ‘setup.bz2’

setup.bz2           100%[===================>]   1.98M  4.87MB/s    in 0.4s

2016-02-25 18:30:12 (4.87 MB/s) - ‘setup.bz2’ saved [2073038/2073038]

--2016-02-25 18:30:12--  http://ftp.jaist.ac.jp/pub/cygwin/x86_64/setup.bz2.sig
Reusing existing connection to ftp.jaist.ac.jp:80.
HTTP request sent, awaiting response... 200 OK
Length: 72 [application/pgp-signature]
Saving to: ‘setup.bz2.sig’

setup.bz2.sig       100%[===================>]      72  --.-KB/s    in 0s

2016-02-25 18:30:12 (3.13 MB/s) - ‘setup.bz2.sig’ saved [72/72]

FINISHED --2016-02-25 18:30:12--
Total wall clock time: 0.6s
Downloaded: 2 files, 2.0M in 0.4s (4.87 MB/s)

$ ls -l setup.bz2*; md5sum setup.bz2*
-rw-r--r--+ 1 kou None 2073038 Feb 25 15:10 setup.bz2
-rw-r--r--+ 1 kou None      72 Feb 25 18:10 setup.bz2.sig
2974e4ff8f13a019ed48cf25100a8d4b *setup.bz2
4704b624aa98d624f2da1762810059b7 *setup.bz2.sig

$ gpg --keyring pubring.gpg setup.bz2.sig                                       gpg: assuming signed data in `setup.bz2'
gpg: Signature made Thu Feb 25 18:10:39 2016 JST using DSA key ID 676041BA
gpg: Good signature from "Cygwin <cygwin@cygwin.com>"
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: 1169 DF9F 2273 4F74 3AA5  9232 A9A2 62FF 6760 41BA
-N -c を併用した場合の正しい動作は、
  • Last-Modified ヘッダとローカルのタイムスタンプを比較し、ローカルが古い場合はファイル全体を download (現状だと -N オプションのみの動作と等価)
  • Last-Modified ヘッダとローカルのタイムスタンプをが等しい場合、resume downalod (現状だと -c オプションのみの動作と等価)
じゃないかと思う。

解析

v1.17.1 を読解してみたところ、意外と面倒臭そうなことが分かった。
とりあえず、ソースと言うか、関数が地味に長くて読み辛いのが地味に厳しい。

まず、上記の setup.bz2.sig を見るとリモートとローカルでファイルサイズが同じだと
http response で 416 Requested Range Not Satisfiable が返って来るが、
src/http.c # L3621-3622 では statcode == HTTP_STATUS_RANGE_NOT_SATISFIABLE だと
-N オプションで true になっている opt.timestamping を無視して
無条件に The file is already fully retrieved と判断してしまう。
-N オプションがある時は、「ローカルのタイムスタンプ < リモートのタイムスタンプ」の場合のみそう判断すべきだろう。

次に、src/http.c # Ln3944-3951 では、
-c で true になっている opt.always_rest を見て
hstat.restval = st.st_size (ダウンロード開始位置 = ローカルのファイルサイズファイルサイズ)
としているのだが、opt.timestamping を考慮してない。
更に、src/http.c # L4168-4191 でリモートとローカルの新旧判定しているが、
hstat.orig_file_tstamp >= tmr だと問答無用に Server file no newer than local file としているので、
hstat.orig_file_tstamp == tmr なら resume download、
hstat.orig_file_tstamp < tmr (ローカルのタイムスタンプ < リモートのタイムスタンプ)なら hstat.restval = 0 (hstat.len の方が適切かも?)
としてやる必要がある。

また、例えば Ctrl-C で downalod を中断すると、中断した時点の時刻がファイルのタイムスタンプになってしまう。
このため -Nc としていると、ローカルの方が新しいので retrieve 不要と判断されてしまう。
src/http.c # L4272 で touch() しているので、
Ctrl-C された時は、trap してこれを処理しておいてやらないと、-N -c を併用した際に resume download が出来ない。

だいたいこんなところか?

関連

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

Wiki内検索

フリーエリア

管理人/副管理人のみ編集できます