-
Notifications
You must be signed in to change notification settings - Fork 224
/
Copy pathos-impl-filesys.c
341 lines (302 loc) · 11.4 KB
/
os-impl-filesys.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
* NASA Docket No. GSC-18,370-1, and identified as "Operating System Abstraction Layer"
*
* Copyright (c) 2019 United States Government as represented by
* the Administrator of the National Aeronautics and Space Administration.
* All Rights Reserved.
*
* Licensed 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
/**
* \file os-impl-filesys.c
* \ingroup posix
* \author joseph.p.hickey@nasa.gov
*
*/
/****************************************************************************************
INCLUDE FILES
***************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <dirent.h>
#include <sys/statvfs.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <sys/vfs.h>
#include "os-posix.h"
#include "os-shared-filesys.h"
#include "os-shared-idmap.h"
/****************************************************************************************
DEFINES
***************************************************************************************/
/****************************************************************************************
GLOBAL DATA
***************************************************************************************/
const char OS_POSIX_DEVICEFILE_PREFIX[] = "/dev/";
/****************************************************************************************
Filesys API
***************************************************************************************/
/* --------------------------------------------------------------------------------------
Name: OS_Posix_FileSysAPI_Impl_Init
Purpose: Filesystem API global initialization
Returns: OS_SUCCESS if success
---------------------------------------------------------------------------------------*/
int32 OS_Posix_FileSysAPI_Impl_Init(void)
{
return OS_SUCCESS;
} /* end OS_Posix_FileSysAPI_Impl_Init */
/*
* System Level API
*/
/*----------------------------------------------------------------
*
* Function: OS_FileSysStartVolume_Impl
*
* Purpose: Implemented per internal OSAL API
* See prototype for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_FileSysStartVolume_Impl(const OS_object_token_t *token)
{
OS_filesys_internal_record_t *local;
struct stat stat_buf;
const char * tmpdir;
uint32 i;
enum
{
VOLATILE_DISK_LOC_DEV_SHM,
VOLATILE_DISK_LOC_ENV,
VOLATILE_DISK_LOC_VARTMP,
VOLATILE_DISK_LOC_TMP,
VOLATILE_DISK_LOC_MAX
};
local = OS_OBJECT_TABLE_GET(OS_filesys_table, *token);
/*
* Determine basic type of filesystem, if not already known
*/
if (local->fstype == OS_FILESYS_TYPE_UNKNOWN &&
strncmp(local->device_name, OS_POSIX_DEVICEFILE_PREFIX, sizeof(OS_POSIX_DEVICEFILE_PREFIX) - 1) == 0)
{
/*
* If referring to a real device in the /dev filesystem,
* then assume it is a normal disk.
*/
local->fstype = OS_FILESYS_TYPE_NORMAL_DISK;
}
/*
* For VOLATILE volumes, there are two options:
* - The /dev/shm filesystem, if it exists
* - The /tmp filesystem
*
* The /dev/shm is preferable because it should actually be a ramdisk, but
* it is system-specific - should exist on Linux if it is mounted.
* The /tmp file system might be a regular persistent disk, but should always exist
* on any POSIX-compliant OS.
*/
tmpdir = NULL;
if (local->fstype == OS_FILESYS_TYPE_VOLATILE_DISK)
{
/* find a suitable location to keep the volatile disk */
for (i = 0; i <= VOLATILE_DISK_LOC_MAX; ++i)
{
switch (i)
{
case VOLATILE_DISK_LOC_DEV_SHM:
/* This is most preferable because it should actually be a ramdisk */
tmpdir = "/dev/shm";
break;
case VOLATILE_DISK_LOC_ENV:
/* try the TMPDIR environment variable, if set */
tmpdir = getenv("TMPDIR");
break;
case VOLATILE_DISK_LOC_VARTMP:
/* try /var/tmp directory */
tmpdir = "/var/tmp";
break;
case VOLATILE_DISK_LOC_TMP:
/* use /tmp directory as a last resort */
tmpdir = "/tmp";
break;
default:
tmpdir = NULL;
break;
}
if (tmpdir != NULL && stat(tmpdir, &stat_buf) == 0)
{
/* check if the user has write permission to the directory */
if ((stat_buf.st_mode & S_IWOTH) != 0 ||
((stat_buf.st_mode & S_IWGRP) != 0 && stat_buf.st_gid == getegid()) ||
((stat_buf.st_mode & S_IWUSR) != 0 && stat_buf.st_uid == geteuid()))
{
break;
}
}
}
if (tmpdir == NULL)
{
/* OS provides no place to put the volume */
OS_DEBUG("No storage location for volatile volumes");
return OS_FS_ERR_DRIVE_NOT_CREATED;
}
snprintf(local->system_mountpt, sizeof(local->system_mountpt), "%s/osal:%s", tmpdir, local->volume_name);
}
return OS_SUCCESS;
} /* end OS_FileSysStartVolume_Impl */
/*----------------------------------------------------------------
*
* Function: OS_FileSysStopVolume_Impl
*
* Purpose: Implemented per internal OSAL API
* See prototype for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_FileSysStopVolume_Impl(const OS_object_token_t *token)
{
/*
* This is a no-op.
*
* Volatile volumes are just directories created in the temp dir,
* and this will not remove the directories just in case something
* went wrong.
*
* If the volume is started again, the directory will be re-used.
*/
return OS_SUCCESS;
} /* end OS_FileSysStopVolume_Impl */
/*----------------------------------------------------------------
*
* Function: OS_FileSysFormatVolume_Impl
*
* Purpose: Implemented per internal OSAL API
* See prototype for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_FileSysFormatVolume_Impl(const OS_object_token_t *token)
{
/*
* In theory, this should wipe any existing files in the ramdisk,
* but since ramdisks here are implemented using a directory within a tmpfs,
* removal of such files could be risky if something goes wrong,
* because it might remove files that were important.
*
* So the safest option is just a no-op.
* (this is also backward compatible since POSIX mkfs was always a no-op)
*/
return OS_SUCCESS;
} /* end OS_FileSysFormatVolume_Impl */
/*----------------------------------------------------------------
*
* Function: OS_FileSysMountVolume_Impl
*
* Purpose: Implemented per internal OSAL API
* See prototype for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_FileSysMountVolume_Impl(const OS_object_token_t *token)
{
OS_filesys_internal_record_t *local;
struct stat stat_buf;
local = OS_OBJECT_TABLE_GET(OS_filesys_table, *token);
/*
* This will do a mkdir() for the mount point if it does
* not already exist.
*/
if (stat(local->system_mountpt, &stat_buf) != 0)
{
if (mkdir(local->system_mountpt, 0700) < 0)
{
OS_DEBUG("ERROR: Cannot create mount point %s: %s", local->system_mountpt, strerror(errno));
return OS_FS_ERR_DRIVE_NOT_CREATED;
}
}
else if (!S_ISDIR(stat_buf.st_mode))
{
OS_DEBUG("ERROR: Volume %s exists and is not a directory", local->system_mountpt);
return OS_FS_ERR_DRIVE_NOT_CREATED;
}
/*
* NOTE: The mount() system call could be used here to actually
* mount a disk, if warranted. For all current POSIX-based PSPs,
* this is not needed, because the volumes are all pre-mounted
* through the system init before OSAL starts.
*
* For volatile filesystems (ramdisk) these were created within
* a temp filesystem, so all that is needed is to ensure the
* mount point exists. For any other FS type, trigger an
* error to indicate that it is not implemented in this OSAL.
*/
if (local->fstype != OS_FILESYS_TYPE_VOLATILE_DISK && local->fstype != OS_FILESYS_TYPE_FS_BASED)
{
/* the mount command is not implemented for this FS type */
return OS_ERR_NOT_IMPLEMENTED;
}
return OS_SUCCESS;
} /* end OS_FileSysMountVolume_Impl */
/*----------------------------------------------------------------
*
* Function: OS_FileSysUnmountVolume_Impl
*
* Purpose: Implemented per internal OSAL API
* See prototype for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_FileSysUnmountVolume_Impl(const OS_object_token_t *token)
{
/*
* NOTE: Mounting/Unmounting on POSIX is not implemented.
* For backward compatibility this call must return success.
*
* This is a no-op. The mount point that was created during
* the mount process can stay for the next mount.
*/
return OS_SUCCESS;
} /* end OS_FileSysUnmountVolume_Impl */
/*----------------------------------------------------------------
*
* Function: OS_FileSysStatVolume_Impl
*
* Purpose: Implemented per internal OSAL API
* See prototype for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_FileSysStatVolume_Impl(const OS_object_token_t *token, OS_statvfs_t *result)
{
OS_filesys_internal_record_t *local;
struct statvfs stat_buf;
local = OS_OBJECT_TABLE_GET(OS_filesys_table, *token);
if (statvfs(local->system_mountpt, &stat_buf) != 0)
{
return OS_ERROR;
}
result->block_size = OSAL_SIZE_C(stat_buf.f_bsize);
result->blocks_free = OSAL_BLOCKCOUNT_C(stat_buf.f_bfree);
result->total_blocks = OSAL_BLOCKCOUNT_C(stat_buf.f_blocks);
return (OS_SUCCESS);
} /* end OS_FileSysStatVolume_Impl */
/*----------------------------------------------------------------
*
* Function: OS_FileSysCheckVolume_Impl
*
* Purpose: Implemented per internal OSAL API
* See prototype for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_FileSysCheckVolume_Impl(const OS_object_token_t *token, bool repair)
{
return OS_ERR_NOT_IMPLEMENTED;
} /* end OS_FileSysCheckVolume_Impl */