1.1 --- a/dstest_block_client.cc Wed Mar 17 00:10:47 2021 +0100
1.2 +++ b/dstest_block_client.cc Thu Mar 18 00:59:55 2021 +0100
1.3 @@ -33,6 +33,24 @@
1.4
1.5
1.6
1.7 +static void show(file_t *file, unsigned long step, unsigned long sample)
1.8 +{
1.9 + /* Allocate a buffer for sampling from the file. */
1.10 +
1.11 + char buf[sample + 1];
1.12 +
1.13 + for (unsigned long offset = 0; offset < file_populated_span(file); offset += step)
1.14 + {
1.15 + unsigned long remaining = file_populated_span(file) - offset;
1.16 + unsigned long sample_remaining = remaining < sample ? remaining : sample;
1.17 +
1.18 + printf("%ld bytes from %p...\n", sample_remaining, (file->memory + offset));
1.19 + strncpy(buf, (file->memory + offset), sample_remaining);
1.20 + buf[sample_remaining] = '\0';
1.21 + printf("%s\n", buf);
1.22 + }
1.23 +}
1.24 +
1.25 int main(int argc, char *argv[])
1.26 {
1.27 if (argc < 4)
1.28 @@ -47,10 +65,6 @@
1.29 unsigned long step = atoi(argv[2]);
1.30 unsigned long sample = atoi(argv[3]);
1.31
1.32 - /* Allocate a buffer for sampling from the file. */
1.33 -
1.34 - char buf[sample + 1];
1.35 -
1.36 /* Obtain access to the filesystem. */
1.37
1.38 l4_cap_idx_t server = l4re_env_get_cap("server");
1.39 @@ -76,16 +90,39 @@
1.40 return 1;
1.41 }
1.42
1.43 + show(&file, step, sample);
1.44 +
1.45 + /* Resizing must occur before writing beyond the end of file. Otherwise, the
1.46 + data may get discarded if the supporting flexpage needs to be flushed. */
1.47 +
1.48 + offset_t new_region = round(file_populated_span(&file), page(1));
1.49 +
1.50 + printf("Resize to %ld...\n", new_region + file_populated_span(&file));
1.51 +
1.52 + err = file_resize(&file, new_region + file_populated_span(&file));
1.53 +
1.54 + if (err)
1.55 + {
1.56 + printf("Could not resize file: %s\n", l4sys_errtostr(err));
1.57 + return 1;
1.58 + }
1.59 +
1.60 + printf("Resized file...\n");
1.61 +
1.62 + /* Copy the sampled data to another file region. */
1.63 +
1.64 + printf("Copy data to %ld...\n", new_region);
1.65 +
1.66 for (unsigned long offset = 0; offset < file_populated_span(&file); offset += step)
1.67 {
1.68 - unsigned long remaining = file_populated_span(&file) - offset;
1.69 - unsigned long sample_remaining = remaining < sample ? remaining : sample;
1.70 + memcpy(file.memory + new_region + offset, file.memory + offset, sample);
1.71 + if (step > sample)
1.72 + memset(file.memory + new_region + offset + sample, 0, step - sample);
1.73 + }
1.74
1.75 - printf("%ld bytes from %p...\n", sample_remaining, (file.memory + offset));
1.76 - strncpy(buf, (file.memory + offset), sample_remaining);
1.77 - buf[sample_remaining] = '\0';
1.78 - printf("%s\n", buf);
1.79 - }
1.80 + show(&file, step, sample);
1.81 +
1.82 + printf("File shown.\n");
1.83
1.84 return 0;
1.85 }