L4Re/departure

Annotated tests/dstest_block_client_simple.cc

144:9c62ddc654a7
2021-07-24 Paul Boddie Introduced filesystem objects, separated file paging functionality from openers, and made the ext2 file opener configurable for specific users, each opener being created via the ext2 filesystem object. Changed the file opening mechanism so that openers are called from the file paging functionality where new file accessors and mappers need to be created. A file opening interface has been defined to establish the functionality provided by each opener to implement its part of the mechanism. Introduced filesystem-related functions to the client and file libraries, also changing functions with overridable capability details to accept the actual capability index instead of the name of the capability in the environment. Changed the libext2fs interfacing to work with the updated client library.
paul@90 1
/*
paul@90 2
 * Test dataspace operations.
paul@90 3
 *
paul@90 4
 * Copyright (C) 2020, 2021 Paul Boddie <paul@boddie.org.uk>
paul@90 5
 *
paul@90 6
 * This program is free software; you can redistribute it and/or
paul@90 7
 * modify it under the terms of the GNU General Public License as
paul@90 8
 * published by the Free Software Foundation; either version 2 of
paul@90 9
 * the License, or (at your option) any later version.
paul@90 10
 *
paul@90 11
 * This program is distributed in the hope that it will be useful,
paul@90 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
paul@90 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
paul@90 14
 * GNU General Public License for more details.
paul@90 15
 *
paul@90 16
 * You should have received a copy of the GNU General Public License
paul@90 17
 * along with this program; if not, write to the Free Software
paul@90 18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
paul@90 19
 * Boston, MA  02110-1301, USA
paul@90 20
 */
paul@90 21
paul@90 22
#include <systypes/fcntl.h>
paul@90 23
paul@90 24
#include <stdio.h>
paul@106 25
#include <stdlib.h>
paul@90 26
paul@94 27
#include <fsclient/client.h>
paul@90 28
paul@90 29
paul@90 30
paul@90 31
int main(int argc, char *argv[])
paul@90 32
{
paul@90 33
  if (argc < 2)
paul@90 34
  {
paul@144 35
    printf("Need a filename, optional user identifier, and optional repetition.\n");
paul@90 36
    return 1;
paul@90 37
  }
paul@90 38
paul@90 39
  /* Obtain filename and access parameters. */
paul@90 40
paul@90 41
  char *filename = argv[1];
paul@144 42
  sys_uid_t uid = argc > 2 ? atoi(argv[2]) : 0;
paul@144 43
  int repetition = argc > 3 ? atoi(argv[3]) : 10;
paul@144 44
  file_t *file;
paul@144 45
paul@144 46
  /* With a user, open a user-specific file opener. */
paul@144 47
paul@144 48
  if (uid)
paul@144 49
  {
paul@144 50
    l4_cap_idx_t opener = client_open_for_user(uid, uid, 0022);
paul@90 51
paul@144 52
    if (l4_is_invalid_cap(opener))
paul@144 53
    {
paul@144 54
      printf("Could not obtain opener for file.\n");
paul@144 55
      return 1;
paul@144 56
    }
paul@90 57
paul@144 58
    /* Invoke the open method to receive the file reference. */
paul@144 59
paul@144 60
    file = client_open_using(filename, O_RDWR, opener);
paul@144 61
  }
paul@144 62
  else
paul@144 63
    file = client_open(filename, O_RDWR);
paul@90 64
paul@90 65
  if (file == NULL)
paul@90 66
  {
paul@90 67
    printf("Could not obtain file.\n");
paul@90 68
    return 1;
paul@90 69
  }
paul@90 70
paul@90 71
  /* Copy the sampled data to another file region. */
paul@90 72
paul@90 73
  offset_t size = file->size;
paul@90 74
  char buffer[size];
paul@90 75
  offset_t nread = client_read(file, buffer, size);
paul@90 76
paul@90 77
  if (nread != size)
paul@90 78
  {
paul@90 79
    printf("Could not read entire file: %ld out of %ld bytes.\n", nread, size);
paul@90 80
    return 1;
paul@90 81
  }
paul@90 82
paul@90 83
  printf("File contents...\n");
paul@90 84
paul@90 85
  fwrite(buffer, sizeof(char), nread, stdout);
paul@90 86
paul@90 87
  printf("Copy %ld bytes to end...\n", nread);
paul@90 88
paul@106 89
  for (int times = 0; times < repetition; times++)
paul@90 90
  {
paul@90 91
    printf("Copy #%d...\n", times);
paul@90 92
paul@90 93
    offset_t nwritten = client_write(file, buffer, nread);
paul@90 94
paul@90 95
    if (nwritten != nread)
paul@90 96
    {
paul@90 97
      printf("Could not write file section: %ld instead of %ld bytes.\n",
paul@90 98
             nwritten, nread);
paul@90 99
      return 1;
paul@90 100
    }
paul@90 101
  }
paul@90 102
paul@90 103
  printf("File is now %ld in size.\n", file->size);
paul@90 104
paul@90 105
  printf("Seek to start...\n");
paul@90 106
paul@90 107
  client_seek(file, 0, SEEK_SET);
paul@90 108
paul@90 109
  printf("At mapped region from %ld to %ld with data at %ld to %ld.\n",
paul@90 110
         file->start_pos, file->end_pos, file->data_current, file->data_end);
paul@90 111
paul@90 112
  printf("File contents...\n");
paul@90 113
paul@90 114
  offset_t position = 0;
paul@90 115
paul@90 116
  while (position < file->size)
paul@90 117
  {
paul@90 118
    offset_t nread = client_read(file, buffer, size);
paul@90 119
paul@90 120
    if ((nread != size) && (nread != file->size - position))
paul@90 121
    {
paul@90 122
      printf("Could not read file section: %ld instead of %ld or remaining %ld bytes.\n",
paul@90 123
             nread, size, file->size - position);
paul@90 124
      return 1;
paul@90 125
    }
paul@90 126
paul@90 127
    fwrite(buffer, sizeof(char), nread, stdout);
paul@90 128
    position += nread;
paul@90 129
  }
paul@90 130
paul@90 131
  printf("File shown.\n");
paul@90 132
paul@90 133
  return 0;
paul@90 134
}
paul@90 135
paul@90 136
// vim: tabstop=2 expandtab shiftwidth=2