L4Re/departure

Annotated tests/dstest_file_rename.cc

391:bc65615a8fed
2022-06-30 Paul Boddie Added missing structure members. mmap-region-flags
paul@236 1
/*
paul@236 2
 * Test renaming operations.
paul@236 3
 *
paul@236 4
 * Copyright (C) 2020, 2021, 2022 Paul Boddie <paul@boddie.org.uk>
paul@236 5
 *
paul@236 6
 * This program is free software; you can redistribute it and/or
paul@236 7
 * modify it under the terms of the GNU General Public License as
paul@236 8
 * published by the Free Software Foundation; either version 2 of
paul@236 9
 * the License, or (at your option) any later version.
paul@236 10
 *
paul@236 11
 * This program is distributed in the hope that it will be useful,
paul@236 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
paul@236 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
paul@236 14
 * GNU General Public License for more details.
paul@236 15
 *
paul@236 16
 * You should have received a copy of the GNU General Public License
paul@236 17
 * along with this program; if not, write to the Free Software
paul@236 18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
paul@236 19
 * Boston, MA  02110-1301, USA
paul@236 20
 */
paul@236 21
paul@236 22
#include <l4/re/env.h>
paul@236 23
#include <l4/sys/err.h>
paul@236 24
paul@236 25
#include <stdio.h>
paul@236 26
#include <string.h>
paul@236 27
#include <stdlib.h>
paul@236 28
paul@236 29
#include <fsclient/client.h>
paul@236 30
#include <systypes/fcntl.h>
paul@236 31
paul@236 32
paul@236 33
paul@271 34
/* Show a directory's contents and determine if all expected files are
paul@271 35
   present. */
paul@271 36
paul@271 37
static int show_directory(const char *filename, int expected)
paul@236 38
{
paul@271 39
  file_t *reader = client_opendir(filename);
paul@271 40
paul@271 41
  if (reader == NULL)
paul@236 42
  {
paul@271 43
    printf("Could not read from directory.\n");
paul@236 44
    return 1;
paul@236 45
  }
paul@236 46
paul@271 47
  printf("Reading...\n");
paul@271 48
paul@271 49
  struct dirent *dirent;
paul@271 50
  int renamed = 0;
paul@271 51
paul@271 52
  while ((dirent = client_readdir(reader)) != NULL)
paul@271 53
  {
paul@271 54
    if (!strncmp(dirent->d_name, "renamed-", 8))
paul@271 55
      renamed++;
paul@271 56
paul@271 57
    printf("> %s\n", dirent->d_name);
paul@271 58
    free(dirent);
paul@271 59
  }
paul@271 60
paul@271 61
  printf("Directory shown.\n");
paul@271 62
paul@271 63
  printf("Renamed files: %d (%d)\n", renamed, expected);
paul@271 64
paul@271 65
  if (renamed != expected)
paul@271 66
    return 1;
paul@271 67
paul@271 68
  return 0;
paul@271 69
}
paul@271 70
paul@271 71
int main(int argc, char *argv[])
paul@271 72
{
paul@271 73
  if (argc < 3)
paul@271 74
  {
paul@271 75
    printf("Need a directory containing files and a new directory name.\n");
paul@271 76
    return 1;
paul@271 77
  }
paul@271 78
paul@271 79
  char *filename = argv[1], *newdir = argv[2];
paul@236 80
paul@236 81
  printf("Opening %s...\n", filename);
paul@236 82
paul@237 83
  file_t *reader = client_opendir(filename);
paul@236 84
paul@236 85
  if (reader == NULL)
paul@236 86
  {
paul@236 87
    printf("Could not read from directory.\n");
paul@236 88
    return 1;
paul@236 89
  }
paul@236 90
paul@236 91
  printf("Reading...\n");
paul@236 92
paul@236 93
  struct dirent *dirent;
paul@236 94
paul@236 95
  while ((dirent = client_readdir(reader)) != NULL)
paul@236 96
  {
paul@236 97
    printf("> %s\n", dirent->d_name);
paul@236 98
    free(dirent);
paul@236 99
  }
paul@236 100
paul@236 101
  printf("Directory shown.\n");
paul@236 102
paul@236 103
  /* Rename some files. */
paul@236 104
paul@236 105
  char source[strlen(filename) + strlen("/file-XXXX.txt") + 10];
paul@236 106
  char target[strlen(filename) + strlen("/file-XXXX.txt") + 10];
paul@260 107
  int filenum, to_rename = 100;
paul@271 108
  long err;
paul@236 109
paul@260 110
  for (filenum = 1; filenum <= to_rename; filenum++)
paul@236 111
  {
paul@236 112
    sprintf(source, "%s/file-%d.txt", filename, filenum);
paul@236 113
    sprintf(target, "%s/renamed-%d.txt", filename, filenum);
paul@236 114
paul@271 115
    err = client_rename(source, target);
paul@236 116
paul@236 117
    if (err)
paul@236 118
    {
paul@236 119
      printf("Could not rename file: %s\n", source);
paul@236 120
      return 1;
paul@236 121
    }
paul@236 122
  }
paul@236 123
paul@236 124
  /* Show the new listing. */
paul@236 125
paul@271 126
  if (show_directory(filename, to_rename))
paul@271 127
    return 1;
paul@271 128
paul@271 129
  /* Create a directory and move files into it. */
paul@271 130
paul@271 131
  err = client_mkdir(newdir, 0755);
paul@271 132
paul@271 133
  if (err)
paul@271 134
  {
paul@271 135
    printf("Could not make directory: %s\n", newdir);
paul@271 136
    return 1;
paul@271 137
  }
paul@271 138
paul@237 139
  reader = client_opendir(filename);
paul@236 140
paul@236 141
  if (reader == NULL)
paul@236 142
  {
paul@236 143
    printf("Could not read from directory.\n");
paul@236 144
    return 1;
paul@236 145
  }
paul@236 146
paul@271 147
  printf("Renaming...\n");
paul@260 148
paul@236 149
  while ((dirent = client_readdir(reader)) != NULL)
paul@236 150
  {
paul@260 151
    if (!strncmp(dirent->d_name, "renamed-", 8))
paul@271 152
    {
paul@271 153
      sprintf(source, "%s/%s", filename, dirent->d_name);
paul@271 154
      sprintf(target, "%s/%s", newdir, dirent->d_name);
paul@271 155
paul@271 156
      err = client_rename(source, target);
paul@260 157
paul@271 158
      if (err)
paul@271 159
      {
paul@271 160
        printf("Could not rename file: %s\n", source);
paul@271 161
        return 1;
paul@271 162
      }
paul@271 163
    }
paul@271 164
paul@236 165
    free(dirent);
paul@236 166
  }
paul@236 167
paul@271 168
  /* Show the new directory's listing. */
paul@236 169
paul@271 170
  if (show_directory(newdir, to_rename))
paul@271 171
    return 1;
paul@271 172
paul@271 173
  printf("End of tests.\n");
paul@260 174
paul@236 175
  return 0;
paul@236 176
}
paul@236 177
paul@236 178
// vim: tabstop=2 expandtab shiftwidth=2