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