L4Re/departure

Annotated tests/dstest_file_readdir_concurrent.cc

260:ad13c46ff610
2022-02-18 Paul Boddie Improved test diagnostic output.
paul@215 1
/*
paul@215 2
 * Test directory reading operations, exploring concurrency issues.
paul@215 3
 *
paul@237 4
 * Copyright (C) 2020, 2021, 2022 Paul Boddie <paul@boddie.org.uk>
paul@215 5
 *
paul@215 6
 * This program is free software; you can redistribute it and/or
paul@215 7
 * modify it under the terms of the GNU General Public License as
paul@215 8
 * published by the Free Software Foundation; either version 2 of
paul@215 9
 * the License, or (at your option) any later version.
paul@215 10
 *
paul@215 11
 * This program is distributed in the hope that it will be useful,
paul@215 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
paul@215 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
paul@215 14
 * GNU General Public License for more details.
paul@215 15
 *
paul@215 16
 * You should have received a copy of the GNU General Public License
paul@215 17
 * along with this program; if not, write to the Free Software
paul@215 18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
paul@215 19
 * Boston, MA  02110-1301, USA
paul@215 20
 */
paul@215 21
paul@215 22
#include <l4/re/env.h>
paul@215 23
#include <l4/sys/err.h>
paul@215 24
paul@215 25
#include <stdio.h>
paul@215 26
#include <string.h>
paul@215 27
#include <stdlib.h>
paul@215 28
paul@215 29
#include <fsclient/client.h>
paul@215 30
#include <systypes/fcntl.h>
paul@215 31
paul@215 32
paul@215 33
paul@260 34
static int count_files(const char *filename, int *found)
paul@260 35
{
paul@260 36
  file_t *reader = client_opendir(filename);
paul@260 37
  struct dirent *dirent;
paul@260 38
paul@260 39
  if (reader == NULL)
paul@260 40
    return 1;
paul@260 41
paul@260 42
  *found = 0;
paul@260 43
paul@260 44
  while ((dirent = client_readdir(reader)) != NULL)
paul@260 45
  {
paul@260 46
    free(dirent);
paul@260 47
    (*found)++;
paul@260 48
  }
paul@260 49
paul@260 50
  client_close(reader);
paul@260 51
  return 0;
paul@260 52
}
paul@260 53
paul@215 54
int main(int argc, char *argv[])
paul@215 55
{
paul@215 56
  if (argc < 2)
paul@215 57
  {
paul@237 58
    printf("Need a directory name.\n");
paul@215 59
    return 1;
paul@215 60
  }
paul@215 61
paul@215 62
  char *filename = argv[1];
paul@260 63
  int original;
paul@260 64
paul@260 65
  if (count_files(filename, &original))
paul@260 66
  {
paul@260 67
    printf("Could not read from directory.\n");
paul@260 68
    return 1;
paul@260 69
  }
paul@260 70
paul@260 71
  printf("Files found: %d\n", original);
paul@260 72
paul@260 73
  struct dirent *dirent;
paul@260 74
  int filenum_base = 1000, filenum = filenum_base;
paul@260 75
  int to_create = 100, remaining = to_create;
paul@260 76
  char buffer[strlen(filename) + strlen("/file-XXXX.txt") + 10];
paul@260 77
  char data[256];
paul@260 78
  int found = 0;
paul@260 79
  file_t *files[to_create];
paul@215 80
paul@215 81
  printf("Opening %s...\n", filename);
paul@215 82
paul@237 83
  file_t *reader = client_opendir(filename);
paul@215 84
paul@215 85
  if (reader == NULL)
paul@215 86
  {
paul@215 87
    printf("Could not read from directory.\n");
paul@215 88
    return 1;
paul@215 89
  }
paul@215 90
paul@215 91
  printf("Reading...\n");
paul@215 92
paul@215 93
  while ((dirent = client_readdir(reader)) != NULL)
paul@215 94
  {
paul@215 95
    free(dirent);
paul@215 96
    found++;
paul@215 97
paul@215 98
    /* Create files while getting the listing. */
paul@215 99
paul@215 100
    if (remaining)
paul@215 101
    {
paul@260 102
      sprintf(buffer, "%s/file-%d.txt", filename, filenum);
paul@215 103
paul@237 104
      file_t *file = client_open(buffer, O_RDWR | O_CREAT);
paul@215 105
paul@215 106
      if (file == NULL)
paul@215 107
      {
paul@215 108
        printf("Could not open file: %s\n", buffer);
paul@215 109
        return 1;
paul@215 110
      }
paul@215 111
paul@232 112
      /* Write something to each file. */
paul@232 113
paul@260 114
      sprintf(data, "Data in file-%d.txt", filenum);
paul@232 115
      client_write(file, data, strlen(data));
paul@232 116
paul@232 117
      /* Remember the file for later. */
paul@232 118
paul@260 119
      files[filenum - filenum_base] = file;
paul@232 120
paul@215 121
      remaining--;
paul@260 122
      filenum++;
paul@215 123
    }
paul@215 124
  }
paul@215 125
paul@215 126
  printf("Files found: %d\n", found);
paul@215 127
paul@215 128
  /* Re-read, counting files. */
paul@215 129
paul@260 130
  if (count_files(filename, &found))
paul@215 131
  {
paul@215 132
    printf("Could not read from directory.\n");
paul@215 133
    return 1;
paul@215 134
  }
paul@215 135
paul@215 136
  printf("Files found: %d\n", found);
paul@215 137
paul@232 138
  /* Remove some files. */
paul@232 139
paul@260 140
  for (filenum = 1000, remaining = to_create; remaining > 0; remaining--, filenum++)
paul@232 141
  {
paul@260 142
    sprintf(buffer, "%s/file-%d.txt", filename, filenum);
paul@232 143
paul@237 144
    long err = client_remove(buffer);
paul@232 145
paul@232 146
    if (err)
paul@232 147
    {
paul@232 148
      printf("Could not remove file: %s\n", buffer);
paul@232 149
      return 1;
paul@232 150
    }
paul@232 151
  }
paul@232 152
paul@232 153
  /* Re-read, counting files. */
paul@232 154
paul@260 155
  if (count_files(filename, &found))
paul@232 156
  {
paul@232 157
    printf("Could not read from directory.\n");
paul@232 158
    return 1;
paul@232 159
  }
paul@232 160
paul@232 161
  printf("Files found: %d\n", found);
paul@232 162
paul@232 163
  /* Read from the still-open but now removed files. */
paul@232 164
paul@260 165
  found = 0;
paul@260 166
paul@260 167
  for (filenum = 1000, remaining = 0; remaining < to_create; remaining++, filenum++)
paul@232 168
  {
paul@260 169
    file_t *file = files[filenum - filenum_base];
paul@260 170
    char read_data[256];
paul@232 171
paul@232 172
    client_seek(file, SEEK_SET, 0);
paul@260 173
    offset_t nread = client_read(file, read_data, 256);
paul@260 174
paul@260 175
    if (nread)
paul@260 176
    {
paul@260 177
      read_data[nread] = '\0';
paul@260 178
      sprintf(data, "Data in file-%d.txt", filenum);
paul@260 179
paul@260 180
      if (!strcmp(data, read_data))
paul@260 181
        found++;
paul@260 182
    }
paul@260 183
  }
paul@260 184
paul@260 185
  printf("Files still open: %d (%d)\n", found, to_create);
paul@232 186
paul@260 187
  /* Close the files. */
paul@260 188
paul@260 189
  for (remaining = 0; remaining < to_create; remaining++)
paul@260 190
    client_close(files[remaining]);
paul@260 191
paul@260 192
  printf("Closed files.\n");
paul@260 193
paul@260 194
  /* Re-read, counting files. */
paul@260 195
paul@260 196
  if (count_files(filename, &found))
paul@260 197
  {
paul@260 198
    printf("Could not read from directory.\n");
paul@260 199
    return 1;
paul@232 200
  }
paul@232 201
paul@260 202
  printf("Files found: %d (%d)\n", found, original);
paul@260 203
paul@215 204
  return 0;
paul@215 205
}
paul@215 206
paul@215 207
// vim: tabstop=2 expandtab shiftwidth=2