1 /*
2  * Copyright (c) 2025 Endress+Hauser AG
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <ff.h>
8 #include <zephyr/device.h>
9 #include <zephyr/fs/fs.h>
10 #include <zephyr/kernel.h>
11 #include <zephyr/logging/log.h>
12 #include <zephyr/storage/disk_access.h>
13 
14 LOG_MODULE_REGISTER(main, LOG_LEVEL_INF);
15 
16 #define AUTOMOUNT_NODE DT_NODELABEL(ffs1)
17 FS_FSTAB_DECLARE_ENTRY(AUTOMOUNT_NODE);
18 
19 #define FILE_PATH "/RAM:/hello.txt"
20 
main(void)21 int main(void)
22 {
23 	struct fs_file_t file;
24 	int rc;
25 	static const char data[] = "Hello";
26 	/* You can get direct mount point to automounted node too */
27 	struct fs_mount_t *auto_mount_point = &FS_FSTAB_ENTRY(AUTOMOUNT_NODE);
28 	struct fs_dirent stat;
29 
30 	fs_file_t_init(&file);
31 
32 	rc = fs_open(&file, FILE_PATH, FS_O_CREATE | FS_O_WRITE);
33 	if (rc != 0) {
34 		LOG_ERR("Accessing filesystem failed");
35 		return rc;
36 	}
37 
38 	rc = fs_write(&file, data, strlen(data));
39 	if (rc != strlen(data)) {
40 		LOG_ERR("Writing filesystem failed");
41 		return rc;
42 	}
43 
44 	rc = fs_close(&file);
45 	if (rc != 0) {
46 		LOG_ERR("Closing file failed");
47 		return rc;
48 	}
49 
50 	/* You can unmount the automount node */
51 	rc = fs_unmount(auto_mount_point);
52 	if (rc != 0) {
53 		LOG_ERR("Failed to do unmount");
54 		return rc;
55 	};
56 
57 	/* And mount it back */
58 	rc = fs_mount(auto_mount_point);
59 	if (rc != 0) {
60 		LOG_ERR("Failed to remount the auto-mount node");
61 		return rc;
62 	}
63 
64 	/* Is the file still there? */
65 	rc = fs_stat(FILE_PATH, &stat);
66 	if (rc != 0) {
67 		LOG_ERR("File status check failed %d", rc);
68 		return rc;
69 	}
70 
71 	LOG_INF("Filesystem access successful");
72 
73 	return 0;
74 }
75