uplinkc: add download_range function (#3704)
This commit is contained in:
parent
011bb5d551
commit
f56107fc61
@ -278,6 +278,31 @@ func download(bucketRef C.BucketRef, path *C.char, cErr **C.char) C.DownloaderRe
|
|||||||
})}
|
})}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//export download_range
|
||||||
|
// download_range returns an Object's data from specified range
|
||||||
|
func download_range(bucketRef C.BucketRef, path *C.char, start, limit int64, cErr **C.char) C.DownloaderRef {
|
||||||
|
bucket, ok := universe.Get(bucketRef._handle).(*Bucket)
|
||||||
|
if !ok {
|
||||||
|
*cErr = C.CString("invalid bucket")
|
||||||
|
return C.DownloaderRef{}
|
||||||
|
}
|
||||||
|
|
||||||
|
scope := bucket.scope.child()
|
||||||
|
|
||||||
|
rc, err := bucket.DownloadRange(scope.ctx, C.GoString(path), start, limit)
|
||||||
|
if err != nil {
|
||||||
|
if !errs2.IsCanceled(err) {
|
||||||
|
*cErr = C.CString(fmt.Sprintf("%+v", err))
|
||||||
|
}
|
||||||
|
return C.DownloaderRef{}
|
||||||
|
}
|
||||||
|
|
||||||
|
return C.DownloaderRef{universe.Add(&Download{
|
||||||
|
scope: scope,
|
||||||
|
rc: rc,
|
||||||
|
})}
|
||||||
|
}
|
||||||
|
|
||||||
//export download_read
|
//export download_read
|
||||||
// download_read reads data upto `length` bytes into `bytes` buffer and returns
|
// download_read reads data upto `length` bytes into `bytes` buffer and returns
|
||||||
// the count of bytes read. The exact number of bytes returned depends on different
|
// the count of bytes read. The exact number of bytes returned depends on different
|
||||||
|
31
lib/uplinkc/testdata/object_test.c
vendored
31
lib/uplinkc/testdata/object_test.c
vendored
@ -187,6 +187,37 @@ void handle_project(ProjectRef project) {
|
|||||||
free_downloader(downloader);
|
free_downloader(downloader);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{ // download range
|
||||||
|
long start = 100 + (i+1);
|
||||||
|
long limit = 1024 * (i+1);
|
||||||
|
DownloaderRef downloader = download_range(bucket, object_paths[i], start, limit, err);
|
||||||
|
require_noerror(*err);
|
||||||
|
|
||||||
|
uint8_t *downloaded_data = malloc(limit);
|
||||||
|
memset(downloaded_data, '\0', limit);
|
||||||
|
size_t downloaded_total = 0;
|
||||||
|
|
||||||
|
size_t size_to_read = 256 + i;
|
||||||
|
while (downloaded_total < limit) {
|
||||||
|
size_t read_size = download_read(downloader, &downloaded_data[downloaded_total], size_to_read, err);
|
||||||
|
require_noerror(*err);
|
||||||
|
|
||||||
|
if (read_size == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
downloaded_total += read_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
download_close(downloader, err);
|
||||||
|
require_noerror(*err);
|
||||||
|
require(limit == downloaded_total);
|
||||||
|
require(memcmp(&data[start], downloaded_data, limit) == 0);
|
||||||
|
|
||||||
|
free(downloaded_data);
|
||||||
|
free_downloader(downloader);
|
||||||
|
}
|
||||||
|
|
||||||
if (data != NULL) {
|
if (data != NULL) {
|
||||||
free(data);
|
free(data);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user