📄 mount_main.c
字号:
/**************************************************************************** * mount_main.c * * Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved. * Author: Gregory Nutt <spudmonkey@racsa.co.cr> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. Neither the name NuttX nor the names of its contributors may be * used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************//**************************************************************************** * Included Files ****************************************************************************/#include <sys/mount.h>#include <sys/types.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"/**************************************************************************** * Definitions ****************************************************************************/#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_DEVNAMEstatic const char g_testfile1[] = "/mnt/fs/TestDir/TestFile.txt";#endifstatic 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_STATstatic void show_stat(const char *path, struct stat *ps){ printf("%s stat:\n", path); printf("\tmode : %08x\n", ps->st_mode); 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"); } else { printf("\ttype : Unknown\n"); } printf("\tsize : %d (bytes)\n", ps->st_size); printf("\tblock size : %d (bytes)\n", ps->st_blksize); printf("\tsize : %d (blocks)\n", ps->st_blocks); printf("\taccess time : %d\n", ps->st_atime); printf("\tmodify time : %d\n", ps->st_mtime); printf("\tchange time : %d\n", ps->st_ctime);}#endif/**************************************************************************** * Name: show_statfs ****************************************************************************/#ifdef TEST_USE_STATFSstatic void show_statfs(const char *path){ struct statfs buf; int ret; /* Try stat() against a file or directory. It should fail with expectederror */ printf("show_statfs: Try statfs(%s)\n", path); ret = statfs(path, &buf); if (ret == 0) { printf("show_statfs: statfs(%s) succeeded\n", path); printf("\tFS Type : %0x\n", buf.f_type); printf("\tBlock size : %d\n", buf.f_bsize); printf("\tNumber of blocks : %d\n", buf.f_blocks); printf("\tFree blocks : %d\n", buf.f_bfree); printf("\tFree user blocks : %d\n", buf.f_bavail); printf("\tNumber file nodes : %d\n", buf.f_files); printf("\tFree file nodes : %d\n", buf.f_ffree); printf("\tFile name length : %d\n", buf.f_namelen); } 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_DIRECTORIESstatic void show_directories(const char *path, int indent){ DIR *dirp; struct dirent *direntry; int i; dirp = opendir(path); if ( !dirp ) { printf("show_directories: ERROR opendir(\"%s\") failed with errno=%d\n", path, errno); g_nerrors++; return; } for (direntry = readdir(dirp); direntry; direntry = readdir(dirp)) { for (i = 0; i < 2*indent; i++) { putchar(' '); } 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); show_directories( subdir, indent + 1); free(subdir); } else { printf("%s\n", direntry->d_name); } } closedir(dirp);}#else# define show_directories(p,i)#endif/**************************************************************************** * Name: fail_read_open ****************************************************************************/#ifdef CONFIG_EXAMPLES_MOUNT_DEVNAMEstatic 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) { printf("fail_read_open: ERROR open(%s) failed with errno=%d (expected %d)\n", 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 { buffer[127]='\0'; printf("read_test_file: Read \"%s\" from %s\n", buffer, path); } 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); fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644); if (fd < 0) { printf("write_test_file: ERROR failed to open %s for writing, errno=%d\n", 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); } close(fd); }}/**************************************************************************** * Name: fail_mkdir ****************************************************************************/static void fail_mkdir(const char *path, int expectederror){ int ret; /* Try mkdir() against a file or directory. It should fail with expectederror */ 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) { printf("fail_mkdir: ERROR mkdir(%s) failed with errno=%d (expected %d)\n", 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; /* Try rmdir() against a file or directory. It should fail with expectederror */ printf("fail_rmdir: Try rmdir(%s)\n", path); ret = rmdir(path); if (ret == 0) { printf("fail_rmdir: ERROR rmdir(%s) succeeded\n", path);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -