Contiki 3.x
cfs-clean.c
Go to the documentation of this file.
1 /**
2  * \file
3  * Clean the Coffee file system up.
4  * Removes every file except for:
5  * sampleconfig
6  * rNUMBER
7  * \author
8  * Arthur Fabre
9  */
10 
11 #include "contiki.h"
12 #include "cfs/cfs.h"
13 #include <stdio.h>
14 #include <stdbool.h>
15 #include <string.h>
16 
17 #define CONFIG "sampleconfig"
18 
19 #define FILENAME_PREFIX "r"
20 
21 PROCESS(listcoffee_process, "CFS/Coffee process");
22 AUTOSTART_PROCESSES(&listcoffee_process);
23 
24 bool is_num(char c) {
25  return c > 47 && c < 58;
26 }
27 
28 void coffee_clean() {
29  struct cfs_dirent dirent;
30  struct cfs_dir dir;
31 
32  printf("Cleaning filesystem\n");
33 
34  if (cfs_opendir(&dir, "/") == 0) {
35  printf("\tOpened folder\n");
36 
37  while (cfs_readdir(&dir, &dirent) != -1) {
38  printf("Found file %s\n", dirent.name);
39 
40  // If it's a config file, ignore it
41  if (strncmp(dirent.name, CONFIG, strlen(CONFIG)) == 0) {
42  printf("It's a config file, skipping\n");
43  continue;
44  }
45 
46  // If it's a reading ignore it
47  // Check the name is at least two long, starts with r, and that the next char is a digit
48  if (strlen(dirent.name) > 1 && strncmp(dirent.name, FILENAME_PREFIX, strlen(FILENAME_PREFIX)) == 0 && is_num(dirent.name[1])) {
49  printf("It's a sample file, skipping\n");
50  continue;
51  }
52 
53  // Delete everything else
54  if (cfs_remove(dirent.name) == -1) {
55  printf("Failed to delete file %s\n", dirent.name);
56  } else {
57  printf("Removed file %s\n", dirent.name);
58  }
59  }
60  }
61 
62  printf("Done cleaning file system\n");
63 }
64 
65 PROCESS_THREAD(listcoffee_process, ev, data) {
66  PROCESS_BEGIN();
67 
68  coffee_clean();
69 
70  PROCESS_END();
71 }
int cfs_opendir(struct cfs_dir *dir, const char *name)
Open a directory for reading directory entries.
Definition: cfs-coffee.c:1283
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
int cfs_readdir(struct cfs_dir *dir, struct cfs_dirent *record)
Read a directory entry.
Definition: cfs-coffee.c:1294
int cfs_remove(const char *name)
Remove a file.
Definition: cfs-coffee.c:1094
CFS header file.
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120