[a97ee41] | 1 | /*===============================================================*\ |
---|
| 2 | | Project: RTEMS fsmount | |
---|
| 3 | +-----------------------------------------------------------------+ |
---|
| 4 | | File: fsmount.c | |
---|
| 5 | +-----------------------------------------------------------------+ |
---|
| 6 | | Copyright (c) 2003 IMD | |
---|
| 7 | | Ingenieurbuero fuer Microcomputertechnik Th. Doerfler | |
---|
| 8 | | <Thomas.Doerfler@imd-systems.de> | |
---|
| 9 | | all rights reserved | |
---|
| 10 | +-----------------------------------------------------------------+ |
---|
| 11 | | this file contains the fsmount functions. These functions | |
---|
| 12 | | are used to mount a list of filesystems (and create their mount | |
---|
| 13 | | points before) | |
---|
| 14 | | | |
---|
| 15 | | The license and distribution terms for this file may be | |
---|
| 16 | | found in the file LICENSE in this distribution or at | |
---|
| 17 | | http://www.OARcorp.com/rtems/license.html. | |
---|
| 18 | | | |
---|
| 19 | +-----------------------------------------------------------------+ |
---|
| 20 | | date history ID | |
---|
| 21 | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
---|
| 22 | | 02.07.03 creation doe | |
---|
| 23 | \*===============================================================*/ |
---|
| 24 | #include <rtems.h> |
---|
| 25 | #include <rtems/fsmount.h> |
---|
| 26 | #include <stdio.h> |
---|
| 27 | #include <stdlib.h> |
---|
| 28 | #include <string.h> |
---|
| 29 | #include <errno.h> |
---|
| 30 | #include <imfs.h> |
---|
| 31 | #include <sys/stat.h> |
---|
| 32 | |
---|
| 33 | /*=========================================================================*\ |
---|
| 34 | | Function: | |
---|
| 35 | \*-------------------------------------------------------------------------*/ |
---|
| 36 | int rtems_fsmount_create_mountpoint |
---|
| 37 | ( |
---|
| 38 | /*-------------------------------------------------------------------------*\ |
---|
| 39 | | Purpose: | |
---|
| 40 | | This function will create the mount point given | |
---|
| 41 | +---------------------------------------------------------------------------+ |
---|
| 42 | | Input Parameters: | |
---|
| 43 | \*-------------------------------------------------------------------------*/ |
---|
| 44 | const char *mount_point |
---|
| 45 | ) |
---|
| 46 | /*-------------------------------------------------------------------------*\ |
---|
| 47 | | Return Value: | |
---|
| 48 | | 0, if success, -1 and errno if failed | |
---|
| 49 | \*=========================================================================*/ |
---|
| 50 | { |
---|
| 51 | int rc = 0; |
---|
| 52 | char *tok_buffer = NULL; |
---|
| 53 | char *token = NULL; |
---|
| 54 | int token_len; |
---|
| 55 | size_t total_len; |
---|
| 56 | IMFS_token_types token_type; |
---|
| 57 | struct stat file_info; |
---|
| 58 | /* |
---|
| 59 | * allocate temp memory to rebuild path name |
---|
| 60 | */ |
---|
| 61 | tok_buffer = calloc(strlen(mount_point)+1,sizeof(char)); |
---|
| 62 | token = tok_buffer; |
---|
| 63 | total_len = 0; |
---|
| 64 | do { |
---|
| 65 | /* |
---|
| 66 | * scan through given string, one segment at a time |
---|
| 67 | */ |
---|
| 68 | token_type = IMFS_get_token(mount_point+total_len,token,&token_len); |
---|
| 69 | total_len += token_len; |
---|
| 70 | strncpy(tok_buffer,mount_point,total_len); |
---|
| 71 | tok_buffer[total_len] = '\0'; |
---|
| 72 | |
---|
| 73 | if ((token_type != IMFS_NO_MORE_PATH) && |
---|
| 74 | (token_type != IMFS_CURRENT_DIR) && |
---|
| 75 | (token_type != IMFS_INVALID_TOKEN)) { |
---|
| 76 | /* |
---|
| 77 | * check, whether segment exists |
---|
| 78 | */ |
---|
| 79 | if (0 != stat(tok_buffer,&file_info)) { |
---|
| 80 | /* |
---|
| 81 | * if not, create directory |
---|
| 82 | */ |
---|
| 83 | rc = mknod(tok_buffer,S_IRWXU | S_IRWXG | S_IRWXO | S_IFDIR,0); |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | } while ((rc == 0) && |
---|
| 87 | (token_type != IMFS_NO_MORE_PATH) && |
---|
| 88 | (token_type != IMFS_INVALID_TOKEN)); |
---|
| 89 | |
---|
| 90 | /* |
---|
| 91 | * return token buffer to heap |
---|
| 92 | */ |
---|
| 93 | if (tok_buffer != NULL) { |
---|
| 94 | free(tok_buffer); |
---|
| 95 | } |
---|
| 96 | return rc; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /*=========================================================================*\ |
---|
| 100 | | Function: | |
---|
| 101 | \*-------------------------------------------------------------------------*/ |
---|
| 102 | int rtems_fsmount |
---|
| 103 | ( |
---|
| 104 | /*-------------------------------------------------------------------------*\ |
---|
| 105 | | Purpose: | |
---|
| 106 | | This function will create the mount points listed and mount the file | |
---|
| 107 | | systems listed in the calling parameters | |
---|
| 108 | +---------------------------------------------------------------------------+ |
---|
| 109 | | Input Parameters: | |
---|
| 110 | \*-------------------------------------------------------------------------*/ |
---|
| 111 | const fstab_t *fstab_ptr, |
---|
| 112 | int fstab_count, |
---|
| 113 | int *fail_idx |
---|
| 114 | ) |
---|
| 115 | /*-------------------------------------------------------------------------*\ |
---|
| 116 | | Return Value: | |
---|
| 117 | | 0, if success, -1 and errno if failed | |
---|
| 118 | \*=========================================================================*/ |
---|
| 119 | { |
---|
| 120 | int rc = 0; |
---|
| 121 | int tmp_rc; |
---|
| 122 | int fstab_idx = 0; |
---|
| 123 | rtems_filesystem_mount_table_entry_t *tmp_mt_entry; |
---|
| 124 | boolean terminate = FALSE; |
---|
| 125 | |
---|
| 126 | /* |
---|
| 127 | * scan through all fstab entries; |
---|
| 128 | */ |
---|
| 129 | while (!terminate && |
---|
| 130 | (fstab_idx < fstab_count)) { |
---|
| 131 | tmp_rc = 0; |
---|
| 132 | /* |
---|
| 133 | * create mount point |
---|
| 134 | */ |
---|
| 135 | if (tmp_rc == 0) { |
---|
| 136 | tmp_rc = rtems_fsmount_create_mountpoint(fstab_ptr->mount_point); |
---|
| 137 | if (tmp_rc != 0) { |
---|
| 138 | if (0 != (fstab_ptr->report_reasons & FSMOUNT_MNTPNT_CRTERR)) { |
---|
| 139 | printf("fsmount: creation of mount point \"%s\" failed: %s\n", |
---|
| 140 | fstab_ptr->mount_point, |
---|
| 141 | strerror(errno)); |
---|
| 142 | } |
---|
| 143 | if (0 != (fstab_ptr->abort_reasons & FSMOUNT_MNTPNT_CRTERR)) { |
---|
| 144 | terminate = TRUE; |
---|
| 145 | rc = tmp_rc; |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | /* |
---|
| 150 | * mount device to given mount point |
---|
| 151 | */ |
---|
| 152 | if (tmp_rc == RTEMS_SUCCESSFUL) { |
---|
| 153 | tmp_rc = mount(&tmp_mt_entry, |
---|
| 154 | fstab_ptr->fs_ops, |
---|
| 155 | fstab_ptr->mount_options, |
---|
| 156 | fstab_ptr->dev, |
---|
| 157 | fstab_ptr->mount_point); |
---|
| 158 | if (tmp_rc != RTEMS_SUCCESSFUL) { |
---|
| 159 | if (0 != (fstab_ptr->report_reasons & FSMOUNT_MNT_FAILED)) { |
---|
| 160 | printf("fsmount: mounting of \"%s\" to" |
---|
| 161 | " \"%s\" failed: %s\n", |
---|
| 162 | fstab_ptr->dev, |
---|
| 163 | fstab_ptr->mount_point, |
---|
| 164 | strerror(errno)); |
---|
| 165 | } |
---|
| 166 | if (0 != (fstab_ptr->abort_reasons & FSMOUNT_MNT_FAILED)) { |
---|
| 167 | terminate = TRUE; |
---|
| 168 | rc = tmp_rc; |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | else { |
---|
| 172 | if (0 != (fstab_ptr->report_reasons & FSMOUNT_MNT_OK)) { |
---|
| 173 | printf("fsmount: mounting of \"%s\" to" |
---|
| 174 | " \"%s\" succeeded\n", |
---|
| 175 | fstab_ptr->dev, |
---|
| 176 | fstab_ptr->mount_point); |
---|
| 177 | } |
---|
| 178 | if (0 != (fstab_ptr->abort_reasons & FSMOUNT_MNT_OK)) { |
---|
| 179 | terminate = TRUE; |
---|
| 180 | } |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | /* |
---|
| 184 | * proceed to next entry |
---|
| 185 | */ |
---|
| 186 | if (!terminate) { |
---|
| 187 | fstab_ptr++; |
---|
| 188 | fstab_idx++; |
---|
| 189 | } |
---|
| 190 | } |
---|
| 191 | if (fail_idx != NULL) { |
---|
| 192 | *fail_idx = fstab_idx; |
---|
| 193 | } |
---|
| 194 | return rc; |
---|
| 195 | } |
---|
| 196 | |
---|