L4Re/departure

Annotated tests/dstest_file_access.cc

391:bc65615a8fed
2022-06-30 Paul Boddie Added missing structure members. mmap-region-flags
paul@239 1
/*
paul@239 2
 * Test file access.
paul@239 3
 *
paul@239 4
 * Copyright (C) 2020, 2021, 2022 Paul Boddie <paul@boddie.org.uk>
paul@239 5
 *
paul@239 6
 * This program is free software; you can redistribute it and/or
paul@239 7
 * modify it under the terms of the GNU General Public License as
paul@239 8
 * published by the Free Software Foundation; either version 2 of
paul@239 9
 * the License, or (at your option) any later version.
paul@239 10
 *
paul@239 11
 * This program is distributed in the hope that it will be useful,
paul@239 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
paul@239 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
paul@239 14
 * GNU General Public License for more details.
paul@239 15
 *
paul@239 16
 * You should have received a copy of the GNU General Public License
paul@239 17
 * along with this program; if not, write to the Free Software
paul@239 18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
paul@239 19
 * Boston, MA  02110-1301, USA
paul@239 20
 */
paul@239 21
paul@239 22
#include <l4/re/env.h>
paul@239 23
#include <l4/sys/err.h>
paul@239 24
paul@239 25
#include <stdio.h>
paul@239 26
#include <string.h>
paul@239 27
#include <stdlib.h>
paul@239 28
paul@239 29
#include <fsclient/client.h>
paul@239 30
#include <systypes/fcntl.h>
paul@239 31
paul@239 32
paul@239 33
paul@239 34
int main(int argc, char *argv[])
paul@239 35
{
paul@239 36
  if (argc < 2)
paul@239 37
  {
paul@239 38
    printf("Need directory names.\n");
paul@239 39
    return 1;
paul@239 40
  }
paul@239 41
paul@239 42
  for (int i = 1; i < argc; i++)
paul@239 43
  {
paul@239 44
    char *dirname = argv[i];
paul@239 45
paul@239 46
    printf("Opening %s...\n", dirname);
paul@239 47
paul@239 48
    file_t *reader = client_opendir(dirname);
paul@239 49
paul@239 50
    if (reader == NULL)
paul@239 51
    {
paul@239 52
      printf("Could not read from directory: %s\n", dirname);
paul@239 53
      continue;
paul@239 54
    }
paul@239 55
paul@239 56
    printf("Reading...\n");
paul@239 57
paul@239 58
    struct dirent *dirent;
paul@239 59
paul@239 60
    while ((dirent = client_readdir(reader)) != NULL)
paul@239 61
    {
paul@239 62
      char *filename = dirent->d_name;
paul@239 63
      if (!strlen(filename) || (filename[0] == '.'))
paul@239 64
        continue;
paul@239 65
paul@239 66
      char path[strlen(filename) + strlen(dirname) + 2];
paul@239 67
paul@239 68
      sprintf(path, "%s/%s", dirname, filename);
paul@239 69
paul@239 70
      printf("Attempt to read %s...\n", filename);
paul@239 71
paul@239 72
      file_t *file = client_open(path, O_RDONLY);
paul@239 73
paul@239 74
      if (file == NULL)
paul@239 75
        printf("Could not open file: %s\n", filename);
paul@239 76
      else
paul@239 77
      {
paul@239 78
        char buffer[32];
paul@239 79
        offset_t nread = client_read(file, buffer, 32);
paul@239 80
paul@239 81
        fputs("Start of file: ", stdout);
paul@239 82
        fwrite(buffer, sizeof(char), nread, stdout);
paul@239 83
        fputs("\n", stdout);
paul@239 84
paul@239 85
        client_close(file);
paul@239 86
      }
paul@239 87
paul@239 88
      free(dirent);
paul@239 89
    }
paul@239 90
paul@239 91
    printf("Directory shown.\n");
paul@239 92
paul@239 93
  }
paul@239 94
paul@239 95
  return 0;
paul@239 96
}
paul@239 97
paul@239 98
// vim: tabstop=2 expandtab shiftwidth=2