|
| 1 | +/* acquire/acquire_download.h */ |
1 | 2 | #ifndef LIBACQUIRE_ACQUIRE_DOWNLOAD_H |
2 | 3 | #define LIBACQUIRE_ACQUIRE_DOWNLOAD_H |
3 | 4 |
|
4 | 5 | /** |
5 | 6 | * @file acquire_download.h |
6 | | - * @brief Functionality to download files from network locations. |
| 7 | + * @brief Public API for synchronous and asynchronous file downloads. |
7 | 8 | * |
8 | | - * This module handles downloading files via protocols like HTTP(S), |
9 | | - * providing progress monitoring and retry logic support. |
10 | | - * |
11 | | - * NOTE: Always `#include` this when adding new download implementations, |
12 | | - * to ensure the implementation matches the prototype. |
| 9 | + * This module provides both synchronous (blocking) and asynchronous |
| 10 | + * (non-blocking) APIs for downloading files. Operations are managed |
| 11 | + * through a stateful `acquire_handle`. |
13 | 12 | */ |
14 | 13 |
|
15 | | -#include <stdlib.h> |
16 | | - |
17 | 14 | #ifdef __cplusplus |
18 | 15 | extern "C" { |
19 | | -#elif defined(HAS_STDBOOL) && !defined(bool) |
20 | | -#include <stdbool.h> |
21 | | -#else |
22 | | -#include "acquire_stdbool.h" |
23 | 16 | #endif /* __cplusplus */ |
| 17 | + |
| 18 | +#include "acquire_checksums.h" |
24 | 19 | #include "acquire_config.h" |
25 | 20 | #include "acquire_fileutils.h" |
26 | 21 | #include "acquire_url_utils.h" |
27 | | - |
28 | | -#include "acquire_checksums.h" |
29 | 22 | #include "libacquire_export.h" |
30 | 23 |
|
31 | | -#if (defined(LIBACQUIRE_USE_COMMON_CRYPTO) && LIBACQUIRE_USE_COMMON_CRYPTO) || \ |
32 | | - (defined(LIBACQUIRE_USE_OPENSSL) && LIBACQUIRE_USE_OPENSSL) |
33 | | -#include "acquire_openssl.h" |
34 | | -#elif defined(LIBACQUIRE_USE_WINCRYPT) && LIBACQUIRE_USE_WINCRYPT |
35 | | -#include "acquire_wincrypt.h" |
36 | | -#endif /* (defined(LIBACQUIRE_USE_COMMON_CRYPTO) && \ |
37 | | - LIBACQUIRE_USE_COMMON_CRYPTO) || (defined(LIBACQUIRE_USE_OPENSSL) && \ |
38 | | - LIBACQUIRE_USE_OPENSSL) */ |
| 24 | +#include <stdio.h> /* For FILE* */ |
| 25 | +#include <sys/types.h> /* For off_t */ |
| 26 | + |
| 27 | +/* --- Asynchronous Operation Status --- */ |
| 28 | + |
| 29 | +/** |
| 30 | + * @brief Describes the current state of an asynchronous operation. |
| 31 | + */ |
| 32 | +enum acquire_status { |
| 33 | + ACQUIRE_IDLE, /* The handle is ready but no operation is active. */ |
| 34 | + ACQUIRE_IN_PROGRESS, /* The operation is running. */ |
| 35 | + ACQUIRE_COMPLETE, /* The operation finished successfully. */ |
| 36 | + ACQUIRE_ERROR_CANCELLED, /* The operation was cancelled by the user. */ |
| 37 | + ACQUIRE_ERROR /* The operation failed. */ |
| 38 | +}; |
| 39 | + |
| 40 | +/* --- Download Operation Handle --- */ |
| 41 | + |
| 42 | +/** |
| 43 | + * @brief A handle managing the state of a single download operation. |
| 44 | + * The definition is public to avoid a typedef, per project constraints. |
| 45 | + * Fields marked "Internal use" should not be modified by the user. |
| 46 | + */ |
| 47 | +struct acquire_handle { |
| 48 | + /* --- Publicly readable state --- */ |
| 49 | + off_t bytes_downloaded; |
| 50 | + off_t total_size; |
| 51 | + enum acquire_status status; |
| 52 | + char error_message[256]; |
| 53 | + |
| 54 | + /* --- Internal use only --- */ |
| 55 | + int cancel_flag; |
| 56 | + void *backend_handle; /* Opaque handle for backend-specific data. */ |
| 57 | + FILE *output_file; |
| 58 | +}; |
| 59 | + |
| 60 | +/* --- Handle Management --- */ |
| 61 | + |
| 62 | +/** |
| 63 | + * @brief Creates and initializes a new acquire handle. |
| 64 | + * @return A new handle, or NULL on failure. Caller must free with |
| 65 | + * acquire_handle_free. |
| 66 | + */ |
| 67 | +extern LIBACQUIRE_EXPORT struct acquire_handle *acquire_handle_init(void); |
| 68 | + |
| 69 | +/** |
| 70 | + * @brief Frees all resources associated with a handle. |
| 71 | + * @param handle The handle to free. |
| 72 | + */ |
| 73 | +extern LIBACQUIRE_EXPORT void |
| 74 | +acquire_handle_free(struct acquire_handle *handle); |
| 75 | + |
| 76 | +/** |
| 77 | + * @brief Gets a human-readable error string for the last error on the handle. |
| 78 | + * @param handle The handle. |
| 79 | + * @return A constant string describing the last error. |
| 80 | + */ |
| 81 | +extern LIBACQUIRE_EXPORT const char * |
| 82 | +acquire_handle_get_error(struct acquire_handle *handle); |
| 83 | + |
| 84 | +/* --- Synchronous API --- */ |
| 85 | + |
| 86 | +/** |
| 87 | + * @brief Downloads a file synchronously (blocking). |
| 88 | + * @param handle A configured acquire handle. |
| 89 | + * @param url The URL to download. |
| 90 | + * @param dest_path The local path to save the file to. |
| 91 | + * @return 0 on success, non-zero on failure. Get details from the handle. |
| 92 | + */ |
| 93 | +extern LIBACQUIRE_EXPORT int |
| 94 | +acquire_download_sync(struct acquire_handle *handle, const char *url, |
| 95 | + const char *dest_path); |
| 96 | + |
| 97 | +/* --- Asynchronous API --- */ |
| 98 | + |
| 99 | +/** |
| 100 | + * @brief Begins an asynchronous download (non-blocking). |
| 101 | + * @param handle A configured acquire handle. |
| 102 | + * @param url The URL to download. |
| 103 | + * @param dest_path The local path to save the file to. |
| 104 | + * @return 0 on success, non-zero on failure. |
| 105 | + */ |
| 106 | +extern LIBACQUIRE_EXPORT int |
| 107 | +acquire_download_async_start(struct acquire_handle *handle, const char *url, |
| 108 | + const char *dest_path); |
| 109 | + |
| 110 | +/** |
| 111 | + * @brief Polls the status of an asynchronous operation, performing work. |
| 112 | + * @param handle The handle for the operation. |
| 113 | + * @return The current status of the operation (e.g., ACQUIRE_IN_PROGRESS). |
| 114 | + */ |
| 115 | +extern LIBACQUIRE_EXPORT enum acquire_status |
| 116 | +acquire_download_async_poll(struct acquire_handle *handle); |
| 117 | + |
| 118 | +/** |
| 119 | + * @brief Requests cancellation of an asynchronous operation. |
| 120 | + * @param handle The handle for the operation. |
| 121 | + */ |
| 122 | +extern LIBACQUIRE_EXPORT void |
| 123 | +acquire_download_async_cancel(struct acquire_handle *handle); |
| 124 | + |
| 125 | +/* --- Other Helpers --- */ |
39 | 126 |
|
40 | 127 | extern LIBACQUIRE_EXPORT const char *get_download_dir(void); |
41 | 128 |
|
42 | 129 | extern LIBACQUIRE_EXPORT bool is_downloaded(const char *, enum Checksum, |
43 | 130 | const char *, const char *); |
44 | 131 |
|
45 | | -extern LIBACQUIRE_EXPORT int download(const char *, enum Checksum, const char *, |
46 | | - const char *, bool, size_t, size_t); |
47 | | - |
48 | | -extern LIBACQUIRE_EXPORT int download_many(const char *[], const char *[], |
49 | | - enum Checksum[], const char *, bool, |
50 | | - size_t, size_t); |
51 | 132 | #ifdef __cplusplus |
52 | 133 | } |
53 | 134 | #endif /* __cplusplus */ |
|
0 commit comments