2011-03-20 19:18:19 +01:00
|
|
|
/****************************************************************************
|
2021-06-16 09:22:16 +02:00
|
|
|
* apps/examples/mount/mount_main.c
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/statfs.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "mount.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-02 22:06:11 +02:00
|
|
|
* Pre-processor Definitions
|
2011-03-20 19:18:19 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define TEST_USE_STAT 1
|
|
|
|
#define TEST_SHOW_DIRECTORIES 1
|
|
|
|
#define TEST_USE_STATFS 1
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const char g_mntdir[] = "/mnt";
|
|
|
|
static const char g_target[] = "/mnt/fs";
|
|
|
|
static const char g_filesystemtype[] = "vfat";
|
|
|
|
|
|
|
|
static const char g_testdir1[] = "/mnt/fs/TestDir";
|
|
|
|
static const char g_testdir2[] = "/mnt/fs/NewDir1";
|
|
|
|
static const char g_testdir3[] = "/mnt/fs/NewDir2";
|
|
|
|
static const char g_testdir4[] = "/mnt/fs/NewDir3";
|
|
|
|
#ifdef CONFIG_EXAMPLES_MOUNT_DEVNAME
|
|
|
|
static const char g_testfile1[] = "/mnt/fs/TestDir/TestFile.txt";
|
|
|
|
#endif
|
|
|
|
static const char g_testfile2[] = "/mnt/fs/TestDir/WrTest1.txt";
|
|
|
|
static const char g_testfile3[] = "/mnt/fs/NewDir1/WrTest2.txt";
|
|
|
|
static const char g_testfile4[] = "/mnt/fs/NewDir3/Renamed.txt";
|
|
|
|
static const char g_testmsg[] = "This is a write test";
|
|
|
|
|
|
|
|
static int g_nerrors = 0;
|
|
|
|
|
|
|
|
static char g_namebuffer[256];
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
const char g_source[] = MOUNT_DEVNAME;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef TEST_USE_STAT
|
|
|
|
static void show_stat(const char *path, struct stat *ps)
|
|
|
|
{
|
|
|
|
printf("%s stat:\n", path);
|
|
|
|
printf("\tmode : %08x\n", ps->st_mode);
|
2018-09-22 21:24:24 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
if (S_ISREG(ps->st_mode))
|
|
|
|
{
|
|
|
|
printf("\ttype : File\n");
|
|
|
|
}
|
|
|
|
else if (S_ISDIR(ps->st_mode))
|
|
|
|
{
|
|
|
|
printf("\ttype : Directory\n");
|
|
|
|
}
|
|
|
|
else if (S_ISCHR(ps->st_mode))
|
|
|
|
{
|
|
|
|
printf("\ttype : Character driver\n");
|
|
|
|
}
|
|
|
|
else if (S_ISBLK(ps->st_mode))
|
|
|
|
{
|
|
|
|
printf("\ttype : Block driver\n");
|
|
|
|
}
|
2018-09-22 21:24:24 +02:00
|
|
|
else if (S_ISMQ(ps->st_mode))
|
|
|
|
{
|
|
|
|
printf("\ttype : Message queue\n");
|
|
|
|
}
|
|
|
|
else if (S_ISSEM(ps->st_mode))
|
|
|
|
{
|
|
|
|
printf("\ttype : Named semaphore\n");
|
|
|
|
}
|
|
|
|
else if (S_ISSHM(ps->st_mode))
|
|
|
|
{
|
|
|
|
printf("\ttype : Shared memory\n");
|
|
|
|
}
|
|
|
|
else if (S_ISSOCK(ps->st_mode))
|
|
|
|
{
|
|
|
|
printf("\ttype : Socket\n");
|
|
|
|
}
|
|
|
|
else if (S_ISMTD(ps->st_mode))
|
|
|
|
{
|
|
|
|
printf("\ttype : Named MTD driver\n");
|
|
|
|
}
|
|
|
|
else if (S_ISLNK(ps->st_mode))
|
|
|
|
{
|
|
|
|
printf("\ttype : Symbolic link\n");
|
|
|
|
}
|
2011-03-20 19:18:19 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("\ttype : Unknown\n");
|
|
|
|
}
|
|
|
|
|
2020-11-13 10:26:00 +01:00
|
|
|
printf("\tsize : %jd (bytes)\n", (intmax_t)ps->st_size);
|
2011-03-20 19:18:19 +01:00
|
|
|
printf("\tblock size : %d (bytes)\n", ps->st_blksize);
|
2020-11-13 10:26:00 +01:00
|
|
|
printf("\tsize : %ju (blocks)\n", (uintmax_t)ps->st_blocks);
|
|
|
|
printf("\taccess time : %ju\n", (uintmax_t)ps->st_atime);
|
|
|
|
printf("\tmodify time : %ju\n", (uintmax_t)ps->st_mtime);
|
|
|
|
printf("\tchange time : %ju\n", (uintmax_t)ps->st_ctime);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: show_statfs
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef TEST_USE_STATFS
|
|
|
|
static void show_statfs(const char *path)
|
|
|
|
{
|
|
|
|
struct statfs buf;
|
|
|
|
int ret;
|
|
|
|
|
2020-07-02 16:28:09 +02:00
|
|
|
/* Try stat() against a file or directory. It should fail with
|
|
|
|
* expectederror
|
|
|
|
*/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
printf("show_statfs: Try statfs(%s)\n", path);
|
|
|
|
ret = statfs(path, &buf);
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
printf("show_statfs: statfs(%s) succeeded\n", path);
|
2020-11-13 10:26:00 +01:00
|
|
|
printf("\tFS Type : %0" PRIx32 "\n", buf.f_type);
|
2020-11-10 23:31:33 +01:00
|
|
|
printf("\tBlock size : %zd\n", buf.f_bsize);
|
2020-11-13 10:26:00 +01:00
|
|
|
printf("\tNumber of blocks : %jd\n", (intmax_t)buf.f_blocks);
|
|
|
|
printf("\tFree blocks : %jd\n", (intmax_t)buf.f_bfree);
|
|
|
|
printf("\tFree user blocks : %jd\n", (intmax_t)buf.f_bavail);
|
|
|
|
printf("\tNumber file nodes : %jd\n", (intmax_t)buf.f_files);
|
|
|
|
printf("\tFree file nodes : %jd\n", (intmax_t)buf.f_ffree);
|
2020-11-10 23:31:33 +01:00
|
|
|
printf("\tFile name length : %zd\n", buf.f_namelen);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("show_statfs: ERROR statfs(%s) failed with errno=%d\n",
|
|
|
|
path, errno);
|
|
|
|
g_nerrors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define show_statfs(p)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: show_directories
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef TEST_SHOW_DIRECTORIES
|
|
|
|
static void show_directories(const char *path, int indent)
|
|
|
|
{
|
|
|
|
DIR *dirp;
|
|
|
|
struct dirent *direntry;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
dirp = opendir(path);
|
2020-07-02 16:28:09 +02:00
|
|
|
if (!dirp)
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
2020-07-02 16:28:09 +02:00
|
|
|
printf("show_directories: ERROR opendir(\"%s\") with errno=%d\n",
|
2011-03-20 19:18:19 +01:00
|
|
|
path, errno);
|
|
|
|
g_nerrors++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (direntry = readdir(dirp); direntry; direntry = readdir(dirp))
|
|
|
|
{
|
|
|
|
for (i = 0; i < 2*indent; i++)
|
|
|
|
{
|
|
|
|
putchar(' ');
|
|
|
|
}
|
2020-07-02 16:28:09 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
if (DIRENT_ISDIRECTORY(direntry->d_type))
|
|
|
|
{
|
|
|
|
char *subdir;
|
|
|
|
printf("%s/\n", direntry->d_name);
|
|
|
|
sprintf(g_namebuffer, "%s/%s", path, direntry->d_name);
|
|
|
|
subdir = strdup(g_namebuffer);
|
2020-07-02 16:28:09 +02:00
|
|
|
show_directories(subdir, indent + 1);
|
2011-03-20 19:18:19 +01:00
|
|
|
free(subdir);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("%s\n", direntry->d_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dirp);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define show_directories(p,i)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fail_read_open
|
|
|
|
****************************************************************************/
|
2014-01-01 15:52:58 +01:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
#ifdef CONFIG_EXAMPLES_MOUNT_DEVNAME
|
|
|
|
static void fail_read_open(const char *path, int expectederror)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
printf("fail_read_open: Try open(%s) for reading\n", path);
|
|
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd >= 0)
|
|
|
|
{
|
|
|
|
printf("fail_read_open: ERROR open(%s) succeeded\n", path);
|
|
|
|
g_nerrors++;
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
else if (errno != expectederror)
|
|
|
|
{
|
2020-07-02 16:28:09 +02:00
|
|
|
printf("fail_read_open: ERROR open(%s) with errno=%d(expect %d)\n",
|
2011-03-20 19:18:19 +01:00
|
|
|
path, errno, expectederror);
|
|
|
|
g_nerrors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: read_test_file
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void read_test_file(const char *path)
|
|
|
|
{
|
|
|
|
char buffer[128];
|
|
|
|
int nbytes;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/* Read a test file that is already on the test file system image */
|
|
|
|
|
|
|
|
printf("read_test_file: opening %s for reading\n", path);
|
|
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
printf("read_test_file: ERROR failed to open %s, errno=%d\n",
|
|
|
|
path, errno);
|
|
|
|
g_nerrors++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memset(buffer, 0, 128);
|
|
|
|
nbytes = read(fd, buffer, 128);
|
|
|
|
if (nbytes < 0)
|
|
|
|
{
|
|
|
|
printf("read_test_file: ERROR failed to read from %s, errno=%d\n",
|
|
|
|
path, errno);
|
|
|
|
g_nerrors++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-02 16:28:09 +02:00
|
|
|
buffer[127] = '\0';
|
2011-03-20 19:18:19 +01:00
|
|
|
printf("read_test_file: Read \"%s\" from %s\n", buffer, path);
|
|
|
|
}
|
2020-07-02 16:28:09 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: write_test_file
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void write_test_file(const char *path)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/* Write a test file into a pre-existing file on the test file system */
|
|
|
|
|
|
|
|
printf("write_test_file: opening %s for writing\n", path);
|
|
|
|
|
2020-07-02 16:28:09 +02:00
|
|
|
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
2011-03-20 19:18:19 +01:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
2020-07-02 16:28:09 +02:00
|
|
|
printf("write_test_file: ERROR to open %s for writing, errno=%d\n",
|
2011-03-20 19:18:19 +01:00
|
|
|
path, errno);
|
|
|
|
g_nerrors++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int nbytes = write(fd, g_testmsg, strlen(g_testmsg));
|
|
|
|
if (nbytes < 0)
|
|
|
|
{
|
|
|
|
printf("write_test_file: ERROR failed to write to %s, errno=%d\n",
|
|
|
|
path, errno);
|
|
|
|
g_nerrors++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("write_test_file: wrote %d bytes to %s\n", nbytes, path);
|
|
|
|
}
|
2020-07-02 16:28:09 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fail_mkdir
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void fail_mkdir(const char *path, int expectederror)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2020-07-02 16:28:09 +02:00
|
|
|
/* Try mkdir() against a file or directory. It should fail with
|
|
|
|
* expectederror
|
|
|
|
*/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
printf("fail_mkdir: Try mkdir(%s)\n", path);
|
|
|
|
|
|
|
|
ret = mkdir(path, 0666);
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
printf("fail_mkdir: ERROR mkdir(%s) succeeded\n", path);
|
|
|
|
g_nerrors++;
|
|
|
|
}
|
|
|
|
else if (errno != expectederror)
|
|
|
|
{
|
2020-07-02 16:28:09 +02:00
|
|
|
printf("fail_mkdir: ERROR mkdir(%s) with errno=%d(expect %d)\n",
|
2011-03-20 19:18:19 +01:00
|
|
|
path, errno, expectederror);
|
|
|
|
g_nerrors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: succeed_mkdir
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void succeed_mkdir(const char *path)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
printf("succeed_mkdir: Try mkdir(%s)\n", path);
|
|
|
|
|
|
|
|
ret = mkdir(path, 0666);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
printf("succeed_mkdir: ERROR mkdir(%s) failed with errno=%d\n",
|
|
|
|
path, errno);
|
|
|
|
g_nerrors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fail_rmdir
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void fail_rmdir(const char *path, int expectederror)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2020-07-02 16:28:09 +02:00
|
|
|
/* Try rmdir() against a file or directory. It should fail with
|
|
|
|
* expectederror
|
|
|
|
*/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
printf("fail_rmdir: Try rmdir(%s)\n", path);
|
|
|
|
|
|
|
|
ret = rmdir(path);
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
printf("fail_rmdir: ERROR rmdir(%s) succeeded\n", path);
|
|
|
|
g_nerrors++;
|
|
|
|
}
|
|
|
|
else if (errno != expectederror)
|
|
|
|
{
|
2020-07-02 16:28:09 +02:00
|
|
|
printf("fail_rmdir: ERROR rmdir(%s) with errno=%d(expect %d)\n",
|
2011-03-20 19:18:19 +01:00
|
|
|
path, errno, expectederror);
|
|
|
|
g_nerrors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: succeed_rmdir
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void succeed_rmdir(const char *path)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
printf("succeed_rmdir: Try rmdir(%s)\n", path);
|
|
|
|
|
|
|
|
ret = rmdir(path);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
printf("succeed_rmdir: ERROR rmdir(%s) failed with errno=%d\n",
|
|
|
|
path, errno);
|
|
|
|
g_nerrors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fail_unlink
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void fail_unlink(const char *path, int expectederror)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2020-07-02 16:28:09 +02:00
|
|
|
/* Try unlink() against a file or directory. It should fail with
|
|
|
|
* expectederror
|
|
|
|
*/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
printf("fail_unlink: Try unlink(%s)\n", path);
|
|
|
|
|
|
|
|
ret = unlink(path);
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
printf("fail_unlink: ERROR unlink(%s) succeeded\n", path);
|
|
|
|
g_nerrors++;
|
|
|
|
}
|
|
|
|
else if (errno != expectederror)
|
|
|
|
{
|
2020-07-02 16:28:09 +02:00
|
|
|
printf("fail_unlink: ERROR unlink(%s) with errno=%d(expect %d)\n",
|
2011-03-20 19:18:19 +01:00
|
|
|
path, errno, expectederror);
|
|
|
|
g_nerrors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: succeed_unlink
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void succeed_unlink(const char *path)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Try unlink() against the test file. It should succeed. */
|
|
|
|
|
|
|
|
printf("succeed_unlink: Try unlink(%s)\n", path);
|
|
|
|
|
|
|
|
ret = unlink(path);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
printf("succeed_unlink: ERROR unlink(%s) failed with errno=%d\n",
|
|
|
|
path, errno);
|
|
|
|
g_nerrors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fail_rename
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-07-02 16:28:09 +02:00
|
|
|
static void fail_rename(const char *oldpath, const char *newpath,
|
|
|
|
int expectederror)
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2020-07-02 16:28:09 +02:00
|
|
|
/* Try rename() against a file or directory. It should fail with
|
|
|
|
* expectederror
|
|
|
|
*/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
printf("fail_rename: Try rename(%s->%s)\n", oldpath, newpath);
|
|
|
|
|
|
|
|
ret = rename(oldpath, newpath);
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
printf("fail_rename: ERROR rename(%s->%s) succeeded\n",
|
|
|
|
oldpath, newpath);
|
|
|
|
g_nerrors++;
|
|
|
|
}
|
|
|
|
else if (errno != expectederror)
|
|
|
|
{
|
2020-07-02 16:28:09 +02:00
|
|
|
printf("fail_rename: ERROR rename(%s->%s) with errno=%d(expect %d)\n",
|
2011-03-20 19:18:19 +01:00
|
|
|
oldpath, newpath, errno, expectederror);
|
|
|
|
g_nerrors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: succeed_rename
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void succeed_rename(const char *oldpath, const char *newpath)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
printf("succeed_rename: Try rename(%s->%s)\n", oldpath, newpath);
|
|
|
|
|
|
|
|
ret = rename(oldpath, newpath);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
printf("succeed_rename: ERROR rename(%s->%s) failed with errno=%d\n",
|
|
|
|
oldpath, newpath, errno);
|
|
|
|
g_nerrors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fail_stat
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef TEST_USE_STAT
|
|
|
|
static void fail_stat(const char *path, int expectederror)
|
|
|
|
{
|
|
|
|
struct stat buf;
|
|
|
|
int ret;
|
|
|
|
|
2020-07-02 16:28:09 +02:00
|
|
|
/* Try stat() against a file or directory. It should fail with
|
|
|
|
* expectederror
|
|
|
|
*/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
printf("fail_stat: Try stat(%s)\n", path);
|
|
|
|
|
|
|
|
ret = stat(path, &buf);
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
printf("fail_stat: ERROR stat(%s) succeeded\n", path);
|
|
|
|
show_stat(path, &buf);
|
|
|
|
g_nerrors++;
|
|
|
|
}
|
|
|
|
else if (errno != expectederror)
|
|
|
|
{
|
2020-07-02 16:28:09 +02:00
|
|
|
printf("fail_stat: ERROR stat(%s) failed with errno=%d(expected %d)\n",
|
2011-03-20 19:18:19 +01:00
|
|
|
path, errno, expectederror);
|
|
|
|
g_nerrors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define fail_stat(p,e);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: succeed_stat
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef TEST_USE_STAT
|
|
|
|
static void succeed_stat(const char *path)
|
|
|
|
{
|
|
|
|
struct stat buf;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
printf("succeed_stat: Try stat(%s)\n", path);
|
|
|
|
|
|
|
|
ret = stat(path, &buf);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
printf("succeed_stat: ERROR stat(%s) failed with errno=%d\n",
|
|
|
|
path, errno);
|
|
|
|
g_nerrors++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("succeed_stat: stat(%s) succeeded\n", path);
|
|
|
|
show_stat(path, &buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define succeed_stat(p)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-08-30 22:13:50 +02:00
|
|
|
* Name: mount_main
|
2011-03-20 19:18:19 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-06 17:23:23 +02:00
|
|
|
int main(int argc, FAR char *argv[])
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
#ifndef CONFIG_EXAMPLES_MOUNT_DEVNAME
|
|
|
|
/* Create a RAM disk for the test */
|
|
|
|
|
|
|
|
ret = create_ramdisk();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2012-08-30 22:13:50 +02:00
|
|
|
printf("mount_main: ERROR failed to create RAM disk\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Mount the test file system (see arch/sim/src/up_deviceimage.c */
|
|
|
|
|
2012-08-30 22:13:50 +02:00
|
|
|
printf("mount_main: mounting %s filesystem at target=%s with source=%s\n",
|
2011-03-20 19:18:19 +01:00
|
|
|
g_filesystemtype, g_target, g_source);
|
|
|
|
|
|
|
|
ret = mount(g_source, g_target, g_filesystemtype, 0, NULL);
|
2012-08-30 22:13:50 +02:00
|
|
|
printf("mount_main: mount() returned %d\n", ret);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
show_statfs(g_mntdir);
|
|
|
|
show_statfs(g_target);
|
|
|
|
|
|
|
|
#ifdef CONFIG_EXAMPLES_MOUNT_DEVNAME
|
|
|
|
/* Read a test file that is already on the test file system image */
|
|
|
|
|
|
|
|
show_directories("", 0);
|
|
|
|
succeed_stat(g_testfile1);
|
|
|
|
show_statfs(g_testfile1);
|
|
|
|
read_test_file(g_testfile1);
|
|
|
|
#else
|
2020-07-02 16:28:09 +02:00
|
|
|
/* Create the test directory that would have been on the canned
|
|
|
|
* filesystem
|
|
|
|
*/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
succeed_mkdir(g_testdir1);
|
|
|
|
show_directories("", 0);
|
|
|
|
succeed_stat(g_testdir1);
|
|
|
|
show_statfs(g_testdir1);
|
|
|
|
#endif
|
|
|
|
|
2020-07-02 16:28:09 +02:00
|
|
|
/* Write a test file into a pre-existing directory on the test file
|
|
|
|
* system
|
|
|
|
*/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
fail_stat(g_testfile2, ENOENT);
|
|
|
|
write_test_file(g_testfile2);
|
|
|
|
show_directories("", 0);
|
|
|
|
succeed_stat(g_testfile2);
|
|
|
|
show_statfs(g_testfile2);
|
|
|
|
|
|
|
|
/* Read the file that we just wrote */
|
|
|
|
|
|
|
|
read_test_file(g_testfile2);
|
|
|
|
|
2020-07-02 16:28:09 +02:00
|
|
|
/* Try rmdir() against a file on the directory. It should fail with
|
|
|
|
* ENOTDIR
|
|
|
|
*/
|
2011-03-20 19:18:19 +01:00
|
|
|
#ifdef CONFIG_EXAMPLES_MOUNT_DEVNAME
|
|
|
|
fail_rmdir(g_testfile1, ENOTDIR);
|
|
|
|
#endif
|
|
|
|
|
2020-07-02 16:28:09 +02:00
|
|
|
/* Try rmdir() against the test directory. It should fail with
|
|
|
|
* ENOTEMPTY
|
|
|
|
*/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
fail_rmdir(g_testdir1, ENOTEMPTY);
|
|
|
|
|
2020-07-02 16:28:09 +02:00
|
|
|
/* Try unlink() against the test directory. It should fail with
|
|
|
|
* EISDIR
|
|
|
|
*/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
fail_unlink(g_testdir1, EISDIR);
|
|
|
|
|
|
|
|
/* Try unlink() against the test file1. It should succeed. */
|
|
|
|
#ifdef CONFIG_EXAMPLES_MOUNT_DEVNAME
|
|
|
|
succeed_unlink(g_testfile1);
|
|
|
|
fail_stat(g_testfile1, ENOENT);
|
|
|
|
show_directories("", 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Attempt to open testfile1 should fail with ENOENT */
|
|
|
|
#ifdef CONFIG_EXAMPLES_MOUNT_DEVNAME
|
|
|
|
fail_read_open(g_testfile1, ENOENT);
|
|
|
|
#endif
|
2020-07-02 16:28:09 +02:00
|
|
|
/* Try rmdir() against the test directory. It should still fail with
|
|
|
|
* ENOTEMPTY
|
|
|
|
*/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
fail_rmdir(g_testdir1, ENOTEMPTY);
|
|
|
|
|
|
|
|
/* Try mkdir() against the test file2. It should fail with EEXIST. */
|
|
|
|
|
|
|
|
fail_mkdir(g_testfile2, EEXIST);
|
|
|
|
|
|
|
|
/* Try unlink() against the test file2. It should succeed. */
|
|
|
|
|
|
|
|
succeed_unlink(g_testfile2);
|
|
|
|
show_directories("", 0);
|
|
|
|
fail_stat(g_testfile2, ENOENT);
|
|
|
|
|
|
|
|
/* Try mkdir() against the test dir1. It should fail with EEXIST. */
|
|
|
|
|
|
|
|
fail_mkdir(g_testdir1, EEXIST);
|
|
|
|
|
|
|
|
/* Try rmdir() against the test directory. mkdir should now succeed. */
|
|
|
|
|
|
|
|
succeed_rmdir(g_testdir1);
|
|
|
|
show_directories("", 0);
|
|
|
|
fail_stat(g_testdir1, ENOENT);
|
|
|
|
|
|
|
|
/* Try mkdir() against the test dir2. It should succeed */
|
|
|
|
|
|
|
|
succeed_mkdir(g_testdir2);
|
|
|
|
show_directories("", 0);
|
|
|
|
succeed_stat(g_testdir2);
|
|
|
|
show_statfs(g_testdir2);
|
|
|
|
|
|
|
|
/* Try mkdir() against the test dir2. It should fail with EXIST */
|
|
|
|
|
|
|
|
fail_mkdir(g_testdir2, EEXIST);
|
|
|
|
|
|
|
|
/* Write a test file into a new directory on the test file system */
|
|
|
|
|
|
|
|
fail_stat(g_testfile3, ENOENT);
|
|
|
|
write_test_file(g_testfile3);
|
|
|
|
show_directories("", 0);
|
|
|
|
succeed_stat(g_testfile3);
|
|
|
|
show_statfs(g_testfile3);
|
|
|
|
|
|
|
|
/* Read the file that we just wrote */
|
|
|
|
|
|
|
|
read_test_file(g_testfile3);
|
|
|
|
|
|
|
|
/* Use mkdir() to create test dir3. It should succeed */
|
|
|
|
|
|
|
|
fail_stat(g_testdir3, ENOENT);
|
|
|
|
succeed_mkdir(g_testdir3);
|
|
|
|
show_directories("", 0);
|
|
|
|
succeed_stat(g_testdir3);
|
|
|
|
show_statfs(g_testdir3);
|
|
|
|
|
2020-07-02 16:28:09 +02:00
|
|
|
/* Try rename() on the root directory. Should fail with EXDEV */
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2020-11-05 15:44:05 +01:00
|
|
|
fail_rename(g_mntdir, g_testdir4, EXDEV);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2020-11-05 15:44:05 +01:00
|
|
|
/* Try rename() to an existing directory. Should fail with ENOENT */
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2020-11-05 15:44:05 +01:00
|
|
|
fail_rename(g_testdir4, g_testdir3, ENOENT);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/* Try rename() to a non-existing directory. Should succeed */
|
|
|
|
|
|
|
|
fail_stat(g_testdir4, ENOENT);
|
|
|
|
succeed_rename(g_testdir3, g_testdir4);
|
|
|
|
show_directories("", 0);
|
|
|
|
fail_stat(g_testdir3, ENOENT);
|
|
|
|
succeed_stat(g_testdir4);
|
|
|
|
show_statfs(g_testdir4);
|
|
|
|
|
|
|
|
/* Try rename() of file. Should work. */
|
|
|
|
|
|
|
|
fail_stat(g_testfile4, ENOENT);
|
|
|
|
succeed_rename(g_testfile3, g_testfile4);
|
|
|
|
show_directories("", 0);
|
|
|
|
fail_stat(g_testfile3, ENOENT);
|
|
|
|
succeed_stat(g_testfile4);
|
|
|
|
show_statfs(g_testfile4);
|
|
|
|
|
|
|
|
/* Make sure that we can still read the renamed file */
|
|
|
|
|
|
|
|
read_test_file(g_testfile4);
|
|
|
|
|
|
|
|
/* Unmount the file system */
|
|
|
|
|
2012-08-30 22:13:50 +02:00
|
|
|
printf("mount_main: Try unmount(%s)\n", g_target);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
ret = umount(g_target);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2012-08-30 22:13:50 +02:00
|
|
|
printf("mount_main: ERROR umount() failed, errno %d\n", errno);
|
2011-03-20 19:18:19 +01:00
|
|
|
g_nerrors++;
|
|
|
|
}
|
|
|
|
|
2012-08-30 22:13:50 +02:00
|
|
|
printf("mount_main: %d errors reported\n", g_nerrors);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
return 0;
|
|
|
|
}
|