Skip to content

Commit efd8c48

Browse files
committed
complete upgrade nffs to 1.0.0
1 parent 868395e commit efd8c48

File tree

23 files changed

+612
-44
lines changed

23 files changed

+612
-44
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#ifndef __DISK_H__
21+
#define __DISK_H__
22+
23+
#include <stddef.h>
24+
#include <inttypes.h>
25+
#include <os/queue.h>
26+
27+
#ifdef __cplusplus
28+
extern "C" {
29+
#endif
30+
31+
#define DISK_EOK 0 /* Success */
32+
#define DISK_EHW 1 /* Error accessing storage medium */
33+
#define DISK_ENOMEM 2 /* Insufficient memory */
34+
#define DISK_ENOENT 3 /* No such file or directory */
35+
#define DISK_EOS 4 /* OS error */
36+
#define DISK_EUNINIT 5 /* File system not initialized */
37+
38+
struct disk_ops {
39+
int (*read)(uint8_t, uint32_t, void *, uint32_t);
40+
int (*write)(uint8_t, uint32_t, const void *, uint32_t);
41+
int (*ioctl)(uint8_t, uint32_t, void *);
42+
43+
SLIST_ENTRY(disk_ops) sc_next;
44+
};
45+
46+
int disk_register(const char *disk_name, const char *fs_name, struct disk_ops *dops);
47+
struct disk_ops *disk_ops_for(const char *disk_name);
48+
char *disk_fs_for(const char *disk_name);
49+
char *disk_name_from_path(const char *path);
50+
char *disk_filepath_from_path(const char *path);
51+
52+
#ifdef __cplusplus
53+
}
54+
#endif
55+
56+
#endif

libraries/nffs/src/fs/disk/pkg.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
20+
pkg.name: fs/disk
21+
pkg.description: Disk management layer to glue filesystems to disk devices.
22+
pkg.author: "Apache Mynewt <[email protected]>"
23+
pkg.homepage: "http://mynewt.apache.org/"
24+
pkg.keywords:
25+
26+
pkg.deps:
27+
- kernel/os

libraries/nffs/src/fs/disk/src/disk.c

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#include <syscfg/syscfg.h>
21+
#include <disk/disk.h>
22+
#include <stdlib.h>
23+
#include <string.h>
24+
25+
struct disk_info {
26+
const char *disk_name;
27+
const char *fs_name;
28+
struct disk_ops *dops;
29+
30+
SLIST_ENTRY(disk_info) sc_next;
31+
};
32+
33+
static SLIST_HEAD(, disk_info) disks = SLIST_HEAD_INITIALIZER();
34+
35+
/**
36+
*
37+
*/
38+
int disk_register(const char *disk_name, const char *fs_name, struct disk_ops *dops)
39+
{
40+
struct disk_info *info = NULL;
41+
struct disk_info *sc;
42+
43+
SLIST_FOREACH(sc, &disks, sc_next) {
44+
if (strcmp(sc->disk_name, disk_name) == 0) {
45+
return DISK_ENOENT;
46+
}
47+
}
48+
49+
info = malloc(sizeof(struct disk_info));
50+
if (!info) {
51+
return DISK_ENOMEM;
52+
}
53+
54+
info->disk_name = disk_name;
55+
info->fs_name = fs_name;
56+
info->dops = dops;
57+
58+
SLIST_INSERT_HEAD(&disks, info, sc_next);
59+
60+
return 0;
61+
}
62+
63+
struct disk_ops *
64+
disk_ops_for(const char *disk_name)
65+
{
66+
struct disk_info *sc;
67+
68+
if (disk_name) {
69+
SLIST_FOREACH(sc, &disks, sc_next) {
70+
if (strcmp(sc->disk_name, disk_name) == 0) {
71+
return sc->dops;
72+
}
73+
}
74+
}
75+
76+
return NULL;
77+
}
78+
79+
char *
80+
disk_fs_for(const char *disk_name)
81+
{
82+
struct disk_info *sc;
83+
84+
if (disk_name) {
85+
SLIST_FOREACH(sc, &disks, sc_next) {
86+
if (strcmp(sc->disk_name, disk_name) == 0) {
87+
return ((char *) sc->fs_name);
88+
}
89+
}
90+
}
91+
92+
return NULL;
93+
}
94+
95+
char *
96+
disk_name_from_path(const char *path)
97+
{
98+
char *colon;
99+
uint8_t len;
100+
char *disk;
101+
102+
colon = (char *) path;
103+
while (*colon && *colon != ':') {
104+
colon++;
105+
}
106+
107+
if (*colon != ':') {
108+
return NULL;
109+
}
110+
111+
len = colon - path;
112+
disk = malloc(len + 1);
113+
if (!disk) {
114+
return NULL;
115+
}
116+
memcpy(disk, path, len);
117+
disk[len] = '\0';
118+
119+
return disk;
120+
}
121+
122+
/**
123+
* @brief Returns the path with the disk prefix removed (if found)
124+
*
125+
* Paths should be given in the form disk<number>:/path. This routine
126+
* will parse and return only the path, removing the disk information.
127+
*/
128+
char *
129+
disk_filepath_from_path(const char *path)
130+
{
131+
char *colon;
132+
char *filepath;
133+
size_t len;
134+
135+
colon = (char *) path;
136+
while (*colon && *colon != ':') {
137+
colon++;
138+
}
139+
140+
if (*colon != ':') {
141+
filepath = strdup(path);
142+
} else {
143+
colon++;
144+
len = strlen(colon);
145+
filepath = malloc(len);
146+
memcpy(filepath, colon, len);
147+
filepath[len] = '\0';
148+
}
149+
150+
return filepath;
151+
}

libraries/nffs/src/fs/fs/include/fs/fs.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ int fs_dirent_is_dir(const struct fs_dirent *);
7979
#define FS_EACCESS 12 /* Operation prohibited by file open mode */
8080
#define FS_EUNINIT 13 /* File system not initialized */
8181

82+
#define FS_NMGR_ID_FILE 0
83+
84+
#define FS_NMGR_MAX_MSG 400
85+
#define FS_NMGR_MAX_NAME 64
86+
8287
#ifdef __cplusplus
8388
}
8489
#endif

libraries/nffs/src/fs/fs/include/fs/fs_if.h

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
extern "C" {
2525
#endif
2626

27+
#include <os/queue.h>
28+
2729
/*
2830
* Common interface filesystem(s) provide.
2931
*/
@@ -52,12 +54,41 @@ struct fs_ops {
5254
int (*f_dirent_is_dir)(const struct fs_dirent *dirent);
5355

5456
const char *f_name;
57+
58+
SLIST_ENTRY(fs_ops) sc_next;
5559
};
5660

57-
/*
58-
* Currently allow only one type of FS, starts at root.
61+
struct fops_container {
62+
struct fs_ops *fops;
63+
};
64+
65+
/**
66+
* Registers a new filesystem interface
67+
*
68+
* @param fops filesystem operations table
69+
*
70+
* @return 0 on success, non-zero on failure
5971
*/
60-
int fs_register(const struct fs_ops *);
72+
int fs_register(struct fs_ops *fops);
73+
74+
/**
75+
* Will look for the number of registered filesystems and will return
76+
* the fops if there is only one.
77+
*
78+
* @return fops if there's only one registered filesystem, NULL otherwise.
79+
*/
80+
struct fs_ops *fs_ops_try_unique(void);
81+
82+
/**
83+
* Retrieve a filesystem's operations table
84+
*
85+
* @param name Name of the filesystem to retrieve fs_ops for
86+
*
87+
* @return valid pointer on success, NULL on failure
88+
*/
89+
struct fs_ops *fs_ops_for(const char *name);
90+
91+
struct fs_ops *fs_ops_from_container(struct fops_container *container);
6192

6293
#ifdef __cplusplus
6394
}

libraries/nffs/src/fs/fs/pkg.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@ pkg.keywords:
2626
- filesystem
2727
- ffs
2828

29+
pkg.deps:
30+
- fs/disk
31+
2932
pkg.deps.FS_CLI:
3033
- sys/shell

libraries/nffs/src/fs/fs/src/fs_dirent.c

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,52 @@
2020
#include <fs/fs_if.h>
2121
#include "fs_priv.h"
2222

23+
struct fs_ops *fops_from_filename(const char *);
24+
25+
static struct fs_ops *
26+
fops_from_dir(const struct fs_dir *dir)
27+
{
28+
return fs_ops_from_container((struct fops_container *) dir);
29+
}
30+
31+
static inline struct fs_ops *
32+
fops_from_dirent(const struct fs_dirent *dirent)
33+
{
34+
return fs_ops_from_container((struct fops_container *) dirent);
35+
}
36+
2337
int
2438
fs_opendir(const char *path, struct fs_dir **out_dir)
2539
{
26-
return fs_root_ops->f_opendir(path, out_dir);
40+
struct fs_ops *fops = fops_from_filename(path);
41+
return fops->f_opendir(path, out_dir);
2742
}
2843

2944
int
3045
fs_readdir(struct fs_dir *dir, struct fs_dirent **out_dirent)
3146
{
32-
return fs_root_ops->f_readdir(dir, out_dirent);
47+
struct fs_ops *fops = fops_from_dir(dir);
48+
return fops->f_readdir(dir, out_dirent);
3349
}
3450

3551
int
3652
fs_closedir(struct fs_dir *dir)
3753
{
38-
return fs_root_ops->f_closedir(dir);
54+
struct fs_ops *fops = fops_from_dir(dir);
55+
return fops->f_closedir(dir);
3956
}
4057

4158
int
4259
fs_dirent_name(const struct fs_dirent *dirent, size_t max_len,
4360
char *out_name, uint8_t *out_name_len)
4461
{
45-
return fs_root_ops->f_dirent_name(dirent, max_len, out_name, out_name_len);
62+
struct fs_ops *fops = fops_from_dirent(dirent);
63+
return fops->f_dirent_name(dirent, max_len, out_name, out_name_len);
4664
}
4765

4866
int
4967
fs_dirent_is_dir(const struct fs_dirent *dirent)
5068
{
51-
return fs_root_ops->f_dirent_is_dir(dirent);
69+
struct fs_ops *fops = fops_from_dirent(dirent);
70+
return fops->f_dirent_is_dir(dirent);
5271
}

0 commit comments

Comments
 (0)