Skip to content

Commit c230d51

Browse files
committed
[*.h,*.c,CMakeLists.txt] Implement async and sync APIs for more libraries ; add tests for this
1 parent 10ec345 commit c230d51

15 files changed

+613
-1053
lines changed

acquire/acquire_checksums.h

Lines changed: 44 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,70 @@
11
#ifndef LIBACQUIRE_ACQUIRE_CHECKSUMS_H
22
#define LIBACQUIRE_ACQUIRE_CHECKSUMS_H
33

4-
/**
5-
* @file acquire_checksums.h
6-
* @brief Collection of checksum function declarations and interface.
7-
*
8-
* This header declares typedefs and functions for computing and verifying
9-
* various checksum algorithms like crc32c, md5, sha1, etc.
10-
* Also provides functionality to get checksum function by name.
11-
*
12-
* NOTE: Always `#include` this when adding new checksum implementations,
13-
* to ensure the implementation matches the prototype.
14-
*/
15-
164
#ifdef __cplusplus
175
extern "C" {
186
#endif /* __cplusplus */
197

8+
#include "acquire_handle.h"
209
#include "libacquire_export.h"
21-
#if defined(HAS_STDBOOL) && !defined(bool)
22-
#include <stdbool.h>
23-
#else
24-
#include "acquire_stdbool.h"
25-
#endif
26-
#include "acquire_config.h"
27-
#include "acquire_string_extras.h"
2810

2911
/**
30-
* @brief Boolean result type alias for checksum validation functions.
12+
* @brief Enumeration of supported checksum algorithms.
3113
*/
14+
enum Checksum {
15+
LIBACQUIRE_CRC32C,
16+
LIBACQUIRE_SHA256,
17+
LIBACQUIRE_SHA512,
18+
LIBACQUIRE_UNSUPPORTED_CHECKSUM
19+
};
3220

3321
/**
34-
* @brief Verify file using CRC32C checksum.
35-
*
36-
* @param filename Path to file.
37-
* @param hash Expected CRC32C checksum string.
38-
*
39-
* @return true if valid.
22+
* @brief Converts a string name (e.g., "sha256") to a Checksum enum.
23+
* @return The corresponding enum, or LIBACQUIRE_UNSUPPORTED_CHECKSUM.
4024
*/
41-
extern LIBACQUIRE_EXPORT bool crc32c(const char *filename, const char *hash);
25+
extern LIBACQUIRE_EXPORT enum Checksum string2checksum(const char *);
26+
27+
/* --- Asynchronous Verification API --- */
4228

4329
/**
44-
* @brief Verify file using SHA256 checksum.
45-
*
46-
* @param filename Path to file.
47-
* @param hash Expected checksum string.
48-
*
49-
* @return true if valid.
30+
* @brief Begins an asynchronous checksum verification (non-blocking).
31+
* @param handle An initialized acquire handle.
32+
* @param filepath The path to the file to verify.
33+
* @param algorithm The checksum algorithm to use.
34+
* @param expected_hash The expected hexadecimal checksum string.
35+
* @return 0 on success, non-zero on failure.
5036
*/
51-
extern LIBACQUIRE_EXPORT bool sha256(const char *filename, const char *hash);
37+
extern LIBACQUIRE_EXPORT int
38+
acquire_verify_async_start(struct acquire_handle *handle, const char *filepath,
39+
enum Checksum algorithm, const char *expected_hash);
5240

5341
/**
54-
* @brief Verify file using SHA256 checksum.
55-
*
56-
* @param filename Path to file.
57-
* @param hash Expected checksum string.
58-
*
59-
* @return true if valid.
42+
* @brief Polls the status of an asynchronous verification, processing a chunk.
43+
* @param handle The handle for the in-progress operation.
44+
* @return The current status of the operation.
6045
*/
61-
extern LIBACQUIRE_EXPORT bool sha512(const char *filename, const char *hash);
46+
extern LIBACQUIRE_EXPORT enum acquire_status
47+
acquire_verify_async_poll(struct acquire_handle *handle);
6248

63-
enum Checksum {
64-
LIBACQUIRE_CRC32C,
65-
LIBACQUIRE_SHA256,
66-
LIBACQUIRE_SHA512,
67-
LIBACQUIRE_UNSUPPORTED_CHECKSUM
68-
};
49+
/**
50+
* @brief Requests cancellation of an asynchronous verification.
51+
* @param handle The handle for the operation to be cancelled.
52+
*/
53+
extern LIBACQUIRE_EXPORT void
54+
acquire_verify_async_cancel(struct acquire_handle *handle);
6955

70-
extern LIBACQUIRE_EXPORT enum Checksum string2checksum(const char *);
56+
/* --- Synchronous Verification API --- */
7157

7258
/**
73-
* @brief Obtain a pointer to a checksum function by name.
74-
*
75-
* Returns a pointer to a function matching the
76-
* signature of checksum_func_t, or NULL if not found.
77-
*
78-
* @param checksum Discriminant from `enum Checksum` representing checksum
79-
* algorithm
80-
*
81-
* @return Function pointer on success; NULL on failure.
59+
* @brief Verifies a file's checksum synchronously (blocking).
60+
* @return 0 if the checksum matches, -1 on failure or mismatch.
8261
*/
83-
extern LIBACQUIRE_EXPORT bool (*get_checksum_function(enum Checksum checksum))(
84-
const char *, const char *);
85-
86-
#if defined(LIBACQUIRE_IMPLEMENTATION) && defined(CHECKSUM_IMPL)
62+
extern LIBACQUIRE_EXPORT int acquire_verify_sync(struct acquire_handle *handle,
63+
const char *filepath,
64+
enum Checksum algorithm,
65+
const char *expected_hash);
8766

67+
#if defined(LIBACQUIRE_IMPLEMENTATION) && defined(LIBACQUIRE_CHECKSUMS_IMPL)
8868
enum Checksum string2checksum(const char *const s) {
8969
if (s == NULL)
9070
return LIBACQUIRE_UNSUPPORTED_CHECKSUM;
@@ -97,26 +77,10 @@ enum Checksum string2checksum(const char *const s) {
9777
else
9878
return LIBACQUIRE_UNSUPPORTED_CHECKSUM;
9979
}
100-
101-
bool (*get_checksum_function(enum Checksum checksum))(const char *,
102-
const char *) {
103-
switch (checksum) {
104-
case LIBACQUIRE_CRC32C:
105-
return crc32c;
106-
case LIBACQUIRE_SHA256:
107-
return sha256;
108-
case LIBACQUIRE_SHA512:
109-
return sha512;
110-
case LIBACQUIRE_UNSUPPORTED_CHECKSUM:
111-
default:
112-
return NULL;
113-
}
114-
}
115-
116-
#endif /* defined(LIBACQUIRE_IMPLEMENTATION) && defined(CHECKSUM_IMPL) */
80+
#endif /* defined(LIBACQUIRE_IMPLEMENTATION) && \
81+
defined(LIBACQUIRE_CHECKSUMS_IMPL) */
11782

11883
#ifdef __cplusplus
11984
}
12085
#endif /* __cplusplus */
121-
122-
#endif /* ! LIBACQUIRE_ACQUIRE_CHECKSUMS_H */
86+
#endif /* !LIBACQUIRE_ACQUIRE_CHECKSUMS_H */

acquire/acquire_extract.h

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
1-
/* acquire/acquire_extract.h */
21
#ifndef LIBACQUIRE_ACQUIRE_EXTRACT_H
32
#define LIBACQUIRE_ACQUIRE_EXTRACT_H
43

5-
/**
6-
* @file acquire_extract.h
7-
* @brief Public API for synchronous and asynchronous archive extraction.
8-
*
9-
* This module provides a handle-based API for both blocking and non-blocking
10-
* archive extraction. The archive format is detected automatically by the
11-
* backend (e.g., libarchive).
12-
*/
13-
144
#include "acquire_handle.h"
155
#include "libacquire_export.h"
166

@@ -22,40 +12,19 @@ extern "C" {
2212

2313
/**
2414
* @brief Begins an asynchronous extraction (non-blocking).
25-
*
26-
* This function initializes the extraction process. The actual work happens
27-
* during calls to `acquire_extract_async_poll`.
28-
*
29-
* @param handle An initialized acquire handle.
30-
* @param archive_path The path to the source archive file (e.g., .zip,
31-
* .tar.gz).
32-
* @param dest_path The directory to extract files into.
33-
* @return 0 on success, non-zero on failure.
3415
*/
3516
extern LIBACQUIRE_EXPORT int
3617
acquire_extract_async_start(struct acquire_handle *handle,
3718
const char *archive_path, const char *dest_path);
3819

3920
/**
40-
* @brief Polls the status of an asynchronous extraction, processing one entry.
41-
*
42-
* In a non-blocking application, this should be called repeatedly until it
43-
* no longer returns `ACQUIRE_IN_PROGRESS`. Each call processes one file or
44-
* directory entry from the archive.
45-
*
46-
* @param handle The handle for the in-progress operation.
47-
* @return The current status of the operation.
21+
* @brief Polls the status of an asynchronous extraction.
4822
*/
4923
extern LIBACQUIRE_EXPORT enum acquire_status
5024
acquire_extract_async_poll(struct acquire_handle *handle);
5125

5226
/**
5327
* @brief Requests cancellation of an asynchronous extraction.
54-
*
55-
* The cancellation will be processed on the next call to
56-
* `acquire_extract_async_poll`.
57-
*
58-
* @param handle The handle for the operation to be cancelled.
5928
*/
6029
extern LIBACQUIRE_EXPORT void
6130
acquire_extract_async_cancel(struct acquire_handle *handle);
@@ -64,48 +33,12 @@ acquire_extract_async_cancel(struct acquire_handle *handle);
6433

6534
/**
6635
* @brief Extracts an archive synchronously (blocking).
67-
*
68-
* This is a helper function that wraps the asynchronous API, calling `poll`
69-
* in a loop until the operation is complete.
70-
*
71-
* @param handle An initialized acquire handle.
72-
* @param archive_path The path to the source archive file.
73-
* @param dest_path The directory to extract files into.
74-
* @return 0 on success, non-zero on failure. Details can be retrieved from the
75-
* handle.
7636
*/
7737
extern LIBACQUIRE_EXPORT int acquire_extract_sync(struct acquire_handle *handle,
7838
const char *archive_path,
7939
const char *dest_path);
8040

81-
/* --- Deprecated Symbols --- */
82-
83-
/**
84-
* @deprecated This enum is no longer used in the public API and will be
85-
* removed in a future version. Archive format is now auto-detected.
86-
*/
87-
enum Archive {
88-
LIBACQUIRE_ZIP,
89-
LIBACQUIRE_INFER,
90-
LIBACQUIRE_UNSUPPORTED_ARCHIVE
91-
};
92-
93-
/**
94-
* @deprecated This function is no longer needed for the public API and will be
95-
* removed in a future version.
96-
*/
97-
extern LIBACQUIRE_EXPORT enum Archive extension2archive(const char *extension);
98-
99-
/**
100-
* @deprecated This function is deprecated in favor of `acquire_extract_sync`.
101-
* It will be removed in a future version.
102-
*/
103-
extern LIBACQUIRE_EXPORT int extract_archive(enum Archive archive,
104-
const char *archive_filepath,
105-
const char *output_folder);
106-
10741
#ifdef __cplusplus
10842
}
10943
#endif /* __cplusplus */
110-
11144
#endif /* !LIBACQUIRE_ACQUIRE_EXTRACT_H */

0 commit comments

Comments
 (0)