L4Re/departure

Annotated tests/dstest_host_client.cc

85:13e188dd5a93
2021-04-04 Paul Boddie Introduced file opening flags to various operations.
paul@0 1
/*
paul@0 2
 * Test dataspace operations.
paul@0 3
 *
paul@0 4
 * Copyright (C) 2020, 2021 Paul Boddie <paul@boddie.org.uk>
paul@0 5
 *
paul@0 6
 * This program is free software; you can redistribute it and/or
paul@0 7
 * modify it under the terms of the GNU General Public License as
paul@0 8
 * published by the Free Software Foundation; either version 2 of
paul@0 9
 * the License, or (at your option) any later version.
paul@0 10
 *
paul@0 11
 * This program is distributed in the hope that it will be useful,
paul@0 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
paul@0 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
paul@0 14
 * GNU General Public License for more details.
paul@0 15
 *
paul@0 16
 * You should have received a copy of the GNU General Public License
paul@0 17
 * along with this program; if not, write to the Free Software
paul@0 18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
paul@0 19
 * Boston, MA  02110-1301, USA
paul@0 20
 */
paul@0 21
paul@0 22
#include <l4/re/env.h>
paul@0 23
#include <l4/sys/err.h>
paul@0 24
paul@85 25
#include <systypes/fcntl.h>
paul@85 26
paul@0 27
#include <stdio.h>
paul@0 28
#include <string.h>
paul@0 29
#include <stdlib.h>
paul@0 30
paul@34 31
#include "file.h"
paul@7 32
#include "memory_utils.h"
paul@7 33
paul@0 34
paul@0 35
paul@12 36
int main(int argc, char *argv[])
paul@0 37
{
paul@12 38
  if (argc < 4)
paul@12 39
  {
paul@12 40
    printf("Need filename, step and sample size.\n");
paul@12 41
    return 1;
paul@12 42
  }
paul@12 43
paul@12 44
  /* Obtain filename and access parameters. */
paul@12 45
paul@12 46
  char *filename = argv[1];
paul@12 47
  unsigned long step = atoi(argv[2]);
paul@12 48
  unsigned long sample = atoi(argv[3]);
paul@12 49
paul@12 50
  /* Allocate a buffer for sampling from the file. */
paul@12 51
paul@12 52
  char buf[sample + 1];
paul@12 53
paul@12 54
  /* Obtain access to the filesystem. */
paul@12 55
paul@0 56
  l4_cap_idx_t server = l4re_env_get_cap("server");
paul@10 57
paul@10 58
  /* Invoke the open method to receive the file reference. */
paul@10 59
paul@34 60
  file_t file;
paul@85 61
  long err = file_open(&file, filename, O_RDWR, server);
paul@10 62
paul@10 63
  if (err)
paul@10 64
  {
paul@10 65
    printf("Could not obtain file: %s\n", l4sys_errtostr(err));
paul@10 66
    return 1;
paul@10 67
  }
paul@10 68
paul@34 69
  /* A region of the file is mapped. */
paul@0 70
paul@34 71
  err = file_mmap(&file, 0, page(10));
paul@7 72
paul@7 73
  if (err)
paul@7 74
  {
paul@7 75
    printf("Could not map file region: %s\n", l4sys_errtostr(err));
paul@7 76
    return 1;
paul@7 77
  }
paul@7 78
paul@45 79
  for (unsigned long offset = 0; offset < file_populated_span(&file); offset += step)
paul@0 80
  {
paul@45 81
    unsigned long remaining = file_populated_span(&file) - offset;
paul@12 82
    unsigned long sample_remaining = remaining < sample ? remaining : sample;
paul@12 83
paul@34 84
    printf("%ld bytes from %p...\n", sample_remaining, (file.memory + offset));
paul@34 85
    strncpy(buf, (file.memory + offset), sample_remaining);
paul@12 86
    buf[sample_remaining] = '\0';
paul@7 87
    printf("%s\n", buf);
paul@0 88
  }
paul@0 89
paul@68 90
  printf("File shown.\n");
paul@68 91
paul@0 92
  return 0;
paul@0 93
}
paul@34 94
paul@34 95
// vim: tabstop=2 expandtab shiftwidth=2