|
25 | 25 | #include "gcs.h" |
26 | 26 | #include "gcs_store.h" |
27 | 27 |
|
| 28 | +static void normalize_stream_suffix(char *out, size_t out_size, const char *in) |
| 29 | +{ |
| 30 | + size_t i; |
| 31 | + char ch; |
| 32 | + |
| 33 | + if (!out || out_size == 0) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + if (!in) { |
| 38 | + out[0] = '\0'; |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + for (i = 0; i < out_size - 1 && in[i] != '\0'; i++) { |
| 43 | + ch = in[i]; |
| 44 | + if ((ch >= 'a' && ch <= 'z') || |
| 45 | + (ch >= 'A' && ch <= 'Z') || |
| 46 | + (ch >= '0' && ch <= '9') || |
| 47 | + ch == '_' || ch == '-' || ch == '.') { |
| 48 | + out[i] = ch; |
| 49 | + } |
| 50 | + else { |
| 51 | + out[i] = '_'; |
| 52 | + } |
| 53 | + } |
| 54 | + out[i] = '\0'; |
| 55 | +} |
| 56 | + |
28 | 57 | static flb_sds_t gen_store_filename(void) |
29 | 58 | { |
30 | 59 | unsigned long hash; |
@@ -54,22 +83,55 @@ static flb_sds_t gen_store_filename(void) |
54 | 83 |
|
55 | 84 | int gcs_store_init(struct flb_gcs *ctx) |
56 | 85 | { |
| 86 | + const char *instance_name; |
| 87 | + char stream_suffix[96]; |
| 88 | + flb_sds_t stream_name; |
| 89 | + |
| 90 | + stream_name = flb_sds_create_size(64); |
| 91 | + if (!stream_name) { |
| 92 | + flb_errno(); |
| 93 | + return -1; |
| 94 | + } |
| 95 | + |
57 | 96 | ctx->fs = flb_fstore_create(ctx->store_dir, FLB_FSTORE_FS); |
58 | 97 | if (!ctx->fs) { |
59 | 98 | return -1; |
60 | 99 | } |
61 | 100 |
|
62 | | - ctx->fs_stream = flb_fstore_stream_create(ctx->fs, "gcs_upload_buffer"); |
| 101 | + instance_name = ctx->ins->alias ? ctx->ins->alias : ctx->ins->name; |
| 102 | + normalize_stream_suffix(stream_suffix, sizeof(stream_suffix), instance_name); |
| 103 | + |
| 104 | + flb_sds_printf(&stream_name, "gcs_upload_buffer_%s", stream_suffix); |
| 105 | + if (!stream_name) { |
| 106 | + flb_fstore_destroy(ctx->fs); |
| 107 | + ctx->fs = NULL; |
| 108 | + |
| 109 | + return -1; |
| 110 | + } |
| 111 | + |
| 112 | + ctx->fs_stream_name = stream_name; |
| 113 | + ctx->fs_stream = flb_fstore_stream_create(ctx->fs, ctx->fs_stream_name); |
63 | 114 | if (!ctx->fs_stream) { |
| 115 | + flb_sds_destroy(ctx->fs_stream_name); |
| 116 | + ctx->fs_stream_name = NULL; |
| 117 | + flb_sds_destroy(stream_name); |
64 | 118 | flb_fstore_destroy(ctx->fs); |
65 | 119 | ctx->fs = NULL; |
| 120 | + |
66 | 121 | return -1; |
67 | 122 | } |
| 123 | + flb_sds_destroy(stream_name); |
| 124 | + |
68 | 125 | return 0; |
69 | 126 | } |
70 | 127 |
|
71 | 128 | int gcs_store_exit(struct flb_gcs *ctx) |
72 | 129 | { |
| 130 | + if (ctx->fs_stream_name) { |
| 131 | + flb_sds_destroy(ctx->fs_stream_name); |
| 132 | + ctx->fs_stream_name = NULL; |
| 133 | + } |
| 134 | + |
73 | 135 | if (ctx->fs) { |
74 | 136 | flb_fstore_destroy(ctx->fs); |
75 | 137 | } |
|
0 commit comments