f0c044adb1
C file changes: examples/bastest/bastest_main.c examples/elf/elf_main.c examples/module/module_main.c examples/posix_spawn/spawn_main.c examples/romfs/romfs_main.c examples/sotest/sotest_main.c examples/unionfs/unionfs_main.c Update examples/elf/elf_main.c Co-authored-by: Xiang Xiao <xiaoxiang781216@gmail.com> Update examples/unionfs/unionfs_main.c Co-authored-by: Xiang Xiao <xiaoxiang781216@gmail.com> Update examples/unionfs/unionfs_main.c Co-authored-by: Xiang Xiao <xiaoxiang781216@gmail.com> Update examples/posix_spawn/spawn_main.c Co-authored-by: Xiang Xiao <xiaoxiang781216@gmail.com> Update examples/elf/elf_main.c Co-authored-by: Xiang Xiao <xiaoxiang781216@gmail.com> Update examples/elf/elf_main.c Co-authored-by: Xiang Xiao <xiaoxiang781216@gmail.com>
204 lines
6.7 KiB
C
204 lines
6.7 KiB
C
/****************************************************************************
|
|
* apps/examples/unionfs/unionfs_main.c
|
|
*
|
|
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
*
|
|
* 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 <nuttx/config.h>
|
|
|
|
#include <sys/types.h>
|
|
#include <sys/mount.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/mman.h>
|
|
#include <sys/boardctl.h>
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
#include <fcntl.h>
|
|
#include <dirent.h>
|
|
#include <errno.h>
|
|
|
|
#include <nuttx/drivers/ramdisk.h>
|
|
#include <nuttx/fs/unionfs.h>
|
|
|
|
#include "romfs_atestdir.h"
|
|
#include "romfs_btestdir.h"
|
|
|
|
/****************************************************************************
|
|
* Pre-processor Definitions
|
|
****************************************************************************/
|
|
|
|
/* Configuration settings */
|
|
|
|
#ifdef CONFIG_DISABLE_MOUNTPOINT
|
|
# error "Mountpoint support is disabled"
|
|
#endif
|
|
|
|
#ifndef CONFIG_FS_ROMFS
|
|
# error "ROMFS support not enabled"
|
|
#endif
|
|
|
|
#ifndef CONFIG_FS_UNIONFS
|
|
# error "Union File System support not enabled"
|
|
#endif
|
|
|
|
#ifndef CONFIG_EXAMPLES_UNIONFS_RAMDEVNO_A
|
|
# define CONFIG_EXAMPLES_UNIONFS_RAMDEVNO_A 1
|
|
#endif
|
|
|
|
#ifndef CONFIG_EXAMPLES_UNIONFS_RAMDEVNO_B
|
|
# define CONFIG_EXAMPLES_UNIONFS_RAMDEVNO_B 2
|
|
#endif
|
|
|
|
#ifndef CONFIG_EXAMPLES_UNIONFS_SECTORSIZE
|
|
# define CONFIG_EXAMPLES_UNIONFS_SECTORSIZE 64
|
|
#endif
|
|
|
|
#ifndef CONFIG_EXAMPLES_UNIONFS_MOUNTPT
|
|
# define "/mnt/unionfs"
|
|
#endif
|
|
|
|
#ifndef CONFIG_EXAMPLES_UNIONFS_TMPA
|
|
# define "/mnt/a"
|
|
#endif
|
|
|
|
#ifndef CONFIG_EXAMPLES_UNIONFS_TMPB
|
|
# define "/mnt/b"
|
|
#endif
|
|
|
|
#define NSECTORS(b) (((b)+CONFIG_EXAMPLES_UNIONFS_SECTORSIZE-1)/CONFIG_EXAMPLES_UNIONFS_SECTORSIZE)
|
|
#define STR_RAMDEVNO(m) #m
|
|
#define MKMOUNT_DEVNAME(m) "/dev/ram" STR_RAMDEVNO(m)
|
|
#define MOUNT_DEVNAME_A MKMOUNT_DEVNAME(CONFIG_EXAMPLES_UNIONFS_RAMDEVNO_A)
|
|
#define MOUNT_DEVNAME_B MKMOUNT_DEVNAME(CONFIG_EXAMPLES_UNIONFS_RAMDEVNO_B)
|
|
|
|
#define SCRATCHBUFFER_SIZE 1024
|
|
|
|
/* Test directory stuff */
|
|
|
|
#define WRITABLE_MODE (S_IWOTH|S_IWGRP|S_IWUSR)
|
|
#define READABLE_MODE (S_IROTH|S_IRGRP|S_IRUSR)
|
|
#define EXECUTABLE_MODE (S_IXOTH|S_IXGRP|S_IXUSR)
|
|
|
|
#define DIRECTORY_MODE (S_IFDIR|READABLE_MODE|EXECUTABLE_MODE)
|
|
#define FILE_MODE (S_IFREG|READABLE_MODE)
|
|
|
|
/****************************************************************************
|
|
* Public Functions
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
* Name: unionfs_main
|
|
****************************************************************************/
|
|
|
|
int main(int argc, FAR char *argv[])
|
|
{
|
|
int ret;
|
|
struct boardioc_romdisk_s desc;
|
|
|
|
/* Create a RAM disk for file system 1 */
|
|
|
|
desc.minor = CONFIG_EXAMPLES_UNIONFS_RAMDEVNO_A; /* Minor device number of the ROM disk. */
|
|
desc.nsectors = NSECTORS(atestdir_img_len); /* The number of sectors in the ROM disk */
|
|
desc.sectsize = CONFIG_EXAMPLES_UNIONFS_SECTORSIZE; /* The size of one sector in bytes */
|
|
desc.image = (FAR uint8_t *)atestdir_img; /* File system image */
|
|
|
|
ret = boardctl(BOARDIOC_ROMDISK, (uintptr_t)&desc);
|
|
|
|
if (ret < 0)
|
|
{
|
|
printf("ERROR: Failed to create file system 1 RAM disk: %s\n",
|
|
strerror(errno));
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
/* Mount test file system 1 */
|
|
|
|
printf("Mounting ROMFS file system 1 at target=%s with source=%s\n",
|
|
CONFIG_EXAMPLES_UNIONFS_TMPA, MOUNT_DEVNAME_A);
|
|
|
|
ret = mount(MOUNT_DEVNAME_A, CONFIG_EXAMPLES_UNIONFS_TMPA, "romfs",
|
|
MS_RDONLY, NULL);
|
|
if (ret < 0)
|
|
{
|
|
printf("ERROR: File system 1 mount failed: %s\n", strerror(errno));
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
/* Create a RAM disk for file system 2 */
|
|
|
|
desc.minor = CONFIG_EXAMPLES_UNIONFS_RAMDEVNO_B; /* Minor device number of the ROM disk. */
|
|
desc.nsectors = NSECTORS(btestdir_img_len); /* The number of sectors in the ROM disk */
|
|
desc.image = (FAR uint8_t *)btestdir_img; /* File system image */
|
|
|
|
ret = boardctl(BOARDIOC_ROMDISK, (uintptr_t)&desc);
|
|
|
|
if (ret < 0)
|
|
{
|
|
printf("ERROR: Failed to create file system 2 RAM disk: %s\n",
|
|
strerror(errno));
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
/* Mount test file system 2 */
|
|
|
|
printf("Mounting ROMFS file system 2 at target=%s with source=%s\n",
|
|
CONFIG_EXAMPLES_UNIONFS_TMPB, MOUNT_DEVNAME_B);
|
|
|
|
ret = mount(MOUNT_DEVNAME_B, CONFIG_EXAMPLES_UNIONFS_TMPB, "romfs",
|
|
MS_RDONLY, NULL);
|
|
if (ret < 0)
|
|
{
|
|
printf("ERROR: File system 2 mount failed: %s\n", strerror(errno));
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
/* Now create and mount the union file system */
|
|
|
|
ret = unionfs_mount(CONFIG_EXAMPLES_UNIONFS_TMPA, NULL,
|
|
CONFIG_EXAMPLES_UNIONFS_TMPB, "offset",
|
|
CONFIG_EXAMPLES_UNIONFS_MOUNTPT);
|
|
if (ret < 0)
|
|
{
|
|
printf("ERROR: Failed to create the union file system: %d\n", ret);
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|