fs.h
Go to the documentation of this file.
1 /*
2  * KubOS Core Flight Services
3  * Copyright (C) 2016 Kubos Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifdef YOTTA_CFG_FS
19 
20 #ifndef FS_H
21 #define FS_H
22 
23 #include <stdbool.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <sys/stat.h>
28 #include <sys/unistd.h>
29 
30 #ifdef HAVE_FS_CONF_H
31 #include "fs_conf.h"
32 #endif
33 
34 #ifndef FS_MAX_MOUNTS
35 #define FS_MAX_MOUNTS 2
36 #endif
37 
38 #ifndef FS_MAX_FDS
39 #define FS_MAX_FDS 10
40 #endif
41 
42 #ifndef FS_MAX_FILENAME
43 #define FS_MAX_FILENAME 32
44 #endif
45 
46 #define FS_RETURN_ERR(r__, e__) do { \
47  r__->_errno = e__; \
48  return -1; \
49 } while (0)
50 
51 struct fs_mount_s;
52 struct fs_fd_s;
53 struct fs_dir_s;
54 struct fs_info_s;
55 
56 typedef struct {
57  const char *name;
58  void *priv_data;
59 
60  int (*mount)(struct fs_mount_s *mnt);
61  int (*unmount)(struct fs_mount_s *mnt);
62  int (*open)(struct _reent *r, const char *path, int flags, int mode, struct fs_fd_s *fd_out);
63  int (*close)(struct _reent *r, struct fs_fd_s *fd);
64  long (*write)(struct _reent *r, struct fs_fd_s *fd, const char *ptr, int len);
65  long (*read)(struct _reent *r, struct fs_fd_s *fd, char *ptr, int len);
66  _off_t (*lseek)(struct _reent *r, struct fs_fd_s *fd, _off_t pos, int dir);
67  int (*sync)(struct _reent *r, struct fs_fd_s *fd);
68  int (*fstat)(struct _reent *r, struct fs_fd_s *fd, struct stat *st);
69  int (*stat)(struct _reent *r, char *path, struct stat *st);
70  int (*unlink)(struct _reent *r, char *path);
71  int (*opendir)(struct fs_dir_s *out, const char *path);
72  int (*readdir)(struct fs_dir_s *dir, struct fs_info_s *info);
73  int (*closedir)(struct fs_dir_s *dir);
74 } fs_dev_t;
75 
76 typedef struct fs_mount_s {
77  const char *mount;
78  bool in_use;
79  fs_dev_t *dev;
80 } fs_mount_t;
81 
82 typedef struct fs_fd_s {
83  bool active;
84  int8_t fd;
85  fs_dev_t *dev;
86  void *priv_data;
87 } fs_fd_t;
88 
89 typedef struct fs_dir_s {
90  fs_dev_t *dev;
91  void *priv_data;
92 } fs_dir_t;
93 
94 #define FS_ATTR_DIR 0x01
95 #define FS_ATTR_READONLY 0x02
96 #define FS_ATTR_HIDDEN 0x04
97 
98 typedef struct fs_info_s {
99  fs_dev_t *dev;
100  char name[FS_MAX_FILENAME];
101  uint16_t name_len;
102  struct stat st;
103 } fs_info_t;
104 
105 void fs_init(void);
106 int fs_mount(const char *mount, fs_dev_t *dev);
107 int fs_open(struct _reent *r, const char *path, int flags, int mode);
108 long fs_read(struct _reent *r, int fd, void *buffer, unsigned int count);
109 long fs_write(struct _reent *r, int fd, const void *data, unsigned int count);
110 int fs_close(struct _reent *r, int fd);
111 _off_t fs_lseek(struct _reent *r, int fd, _off_t pos, int dir);
112 int fs_sync(struct _reent *r, int fd);
113 int fs_fstat(struct _reent *r, int fd, struct stat *st);
114 int fs_stat(struct _reent *r, char *path, struct stat *st);
115 int fs_unlink(struct _reent *r, char *path);
116 int fs_unmount(const char *mount);
117 int fs_next_fd(void);
118 
119 int fs_opendir(fs_dir_t *out, const char *path);
120 int fs_readdir(fs_dir_t *dir, fs_info_t *info);
121 int fs_closedir(fs_dir_t *dir);
122 #endif
123 
124 #endif