11 static struct file
* tfs_file_open(struct tfs_sb_info
*sbi
, char *filename
, uint32_t flags
)
14 char *file
= filename
;
16 fobj
= tfs_open(sbi
, file
, flags
);
18 printf("tfs_open: open file %s error!\n", filename
);
25 static void cd(struct tfs_sb_info
*sbi
, char *dst_dir
)
30 this_dir
= tfs_opendir(sbi
, dst_dir
);
32 printf("cd: %s: no such directory\n", dst_dir
);
37 if (this_dir
->dd_dir
->inode
->i_mode
!= TFS_DIR
) {
38 printf("cd: %s: is not a directory\n", dst_dir
);
39 tfs_closedir(this_dir
);
48 static void cat(struct tfs_sb_info
*sbi
, char *filename
)
50 struct file
*file
= tfs_file_open(sbi
, filename
, 0);
56 while ((bytes_read
= tfs_read(file
, buf
, sizeof(buf
))) > 0)
57 write(1, buf
, bytes_read
);
60 static void touch(struct tfs_sb_info
*sbi
, char *filename
)
62 struct file
*file
= tfs_file_open(sbi
, filename
, LOOKUP_CREATE
);
65 static void ls(struct tfs_sb_info
*sbi
, char *filename
)
67 DIR *dir
= tfs_opendir(sbi
, filename
);
71 printf("open file or dir %s failed!\n", filename
);
75 if (dir
->dd_dir
->inode
->i_mode
== TFS_FILE
) {
76 printf("%d\t %s\n", dir
->dd_dir
->inode
->i_ino
, filename
);
81 while ((de
= tfs_readdir(dir
))) {
82 printf("%6d\t %s\n", de
->d_ino
, de
->d_name
);
89 static void cmd_mkdir(struct tfs_sb_info
*sbi
, char *filename
)
91 tfs_mkdir(sbi
, filename
);
94 static void cmd_rmdir(struct tfs_sb_info
*sbi
, char *filename
)
96 tfs_rmdir(sbi
, filename
);
99 void cp(struct tfs_sb_info
*sbi
, char *from
, char *to
)
103 struct file
*from_file
;
104 struct file
*to_file
;
107 from_file
= tfs_file_open(sbi
, from
, 0);
110 to_file
= tfs_file_open(sbi
, to
, LOOKUP_CREATE
);
112 tfs_close(from_file
);
116 while ((count
= tfs_read(from_file
, buf
, sizeof(buf
))) > 0) {
117 count
= tfs_write(to_file
, buf
, count
);
119 printf("write error!\n");
121 printf(" == %d bytes written!\n", count
);
124 tfs_close(from_file
);
128 void cp_in(struct tfs_sb_info
*sbi
, char *from
)
136 file
= tfs_file_open(sbi
, from
, LOOKUP_CREATE
);
140 fd
= open(from
, O_RDONLY
);
142 printf("open file %s error!\n", from
);
145 while ((count
= read(fd
, buf
, sizeof(buf
))) > 0) {
146 bytes_written
= tfs_write(file
, buf
, count
);
147 if (bytes_written
== -1)
148 printf("write error!\n");
150 printf(" == %d bytes written!\n", bytes_written
);
156 void cp_out(struct tfs_sb_info
*sbi
, char *to
)
164 file
= tfs_file_open(sbi
, to
, 0);
168 fd
= open(to
, O_RDWR
);
170 printf("open file %s error!\n", to
);
173 while ((count
= tfs_read(file
, buf
, sizeof(buf
))) > 0) {
174 bytes_written
= write(fd
, buf
, count
);
175 if (bytes_written
== -1)
176 printf("write error!\n");
178 printf(" == %d bytes written!\n", bytes_written
);
184 int main(int argc
, char *argv
[])
186 struct tfs_sb_info
*sbi
;
191 printf("Usage: tfsh fs!\n");
197 sbi
= tfs_mount_by_fsname(fs
);
199 printf("tfs mount failed!\n");
205 /* init the this_dir */
206 this_dir
= tfs_opendir(sbi
, "/");
208 printf("reading '/' as this_dir error!\n");
219 while((c
= getchar()) != '\n')
224 while (*p
!= ' ' && *p
)
235 if (strcmp(cmd
, "cp_in") == 0) {
236 printf("copying in '%s' ... \n", opt
);
238 } else if (strcmp(cmd
, "cp_out") == 0) {
239 printf("copying out file '%s' ...\n", opt
);
241 } else if (strcmp(cmd
, "cp") == 0) {
242 printf("copying file '%s' to '%s' ...\n", opt
, opt
);
244 } else if (strcmp(cmd
, "cat") == 0) {
245 printf("cating file '%s' ... \n", opt
);
247 } else if (strcmp(cmd
, "cd") == 0) {
248 printf("cd in '%s' ... \n", opt
);
250 } else if (strcmp(cmd
, "echo") == 0) {
252 } else if (strcmp(cmd
, "ls") == 0) {
255 printf("listing file '%s' ...\n", opt
);
257 } else if (strcmp(cmd
, "mkdir") == 0) {
258 printf("making dir '%s' ... \n", opt
);
260 } else if (strcmp(cmd
, "rmdir") == 0) {
261 printf("remoing dir '%s' ... \n", opt
);
263 } else if (strcmp(cmd
, "rm") == 0) {
264 printf("removing file '%s' ...\n", opt
);
265 tfs_unlink(sbi
, opt
);
266 } else if (strcmp(cmd
, "touch") == 0) {
267 printf("touching file '%s' ...\n", opt
);
269 } else if (strcmp(cmd
, "quit") == 0 ||
270 strcmp(cmd
, "exit") == 0) {
273 printf("unknow command!\n");