Skip to content

Commit 8d33691

Browse files
committed
测试断点续传
1 parent 743f820 commit 8d33691

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

examples/wget.cpp

+18-5
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,17 @@ static int wget(const char* url, const char* filepath, wget_progress_cb progress
4646
}
4747

4848
// open file
49+
std::string filepath_download(filepath);
50+
filepath_download += ".download";
4951
HFile file;
50-
ret = file.open(filepath, "wb");
52+
if (use_range) {
53+
ret = file.open(filepath_download.c_str(), "ab");
54+
from = file.size();
55+
} else {
56+
ret = file.open(filepath_download.c_str(), "wb");
57+
}
5158
if (ret != 0) {
52-
fprintf(stderr, "Failed to open file %s\n", filepath);
59+
fprintf(stderr, "Failed to open file %s\n", filepath_download.c_str());
5360
return ret;
5461
}
5562
printf("Save file to %s ...\n", filepath);
@@ -80,7 +87,7 @@ static int wget(const char* url, const char* filepath, wget_progress_cb progress
8087
fprintf(stderr, "request error: %d\n", ret);
8188
goto error;
8289
}
83-
return 0;
90+
goto success;
8491
}
8592

8693
// Range: bytes=from-to
@@ -103,10 +110,16 @@ static int wget(const char* url, const char* filepath, wget_progress_cb progress
103110
}
104111
}
105112

106-
return 0;
113+
success:
114+
file.close();
115+
ret = rename(filepath_download.c_str(), filepath);
116+
if (ret != 0) {
117+
fprintf(stderr, "mv %s => %s failed: %s:%d\n", filepath_download.c_str(), filepath, strerror(ret), ret);
118+
}
119+
return ret;
107120
error:
108121
file.close();
109-
// remove(filepath);
122+
// remove(filepath_download.c_str());
110123
return ret;
111124
}
112125

0 commit comments

Comments
 (0)