cleanup for tfs
[thunix.git] / fs / tfs / tfs_cmd.c
blob24d26399469734aa8f7efca48c1790b6920e0fa0
1 #include <stdio.h>
2 #include <string.h>
3 #include <tfs.h>
4 #include <file.h>
5 #include <dirent.h>
8 static struct file * tfs_file_open(struct tfs_sb_info *sbi, char *filename, uint32_t flags)
10 struct file *file;
12 file = tfs_open(sbi, filename, flags);
13 if (!file) {
14 printk("tfs_open: open file %s error!\n", filename);
15 return NULL;
18 return file;
21 void cd(struct tfs_sb_info *sbi, char *dst_dir)
23 DIR *old = this_dir;
25 if (*dst_dir) {
26 this_dir = tfs_opendir(sbi, dst_dir);
27 if (!this_dir) {
28 printk("cd: %s: no such directory\n", dst_dir);
29 this_dir = old;
30 return;
33 if (this_dir->dd_dir->inode->i_mode != TFS_DIR) {
34 printk("cd: %s: is not a directory\n", dst_dir);
35 tfs_closedir(this_dir);
36 this_dir = old;
37 } else {
38 tfs_closedir(old);
42 TFS_DEBUG("CDed in '%s' with inode '%d'\n", dst_dir, this_dir->dd_dir->inode->i_ino);
46 void cat(struct tfs_sb_info *sbi, char *filename)
48 struct file *file = tfs_file_open(sbi, filename, 0);
49 char buf[1024];
50 int bytes_read;
52 if (!file)
53 return;
54 while ((bytes_read = tfs_read(file, buf, sizeof(buf))) > 0)
55 printk("%s", buf);
56 printk("\n");
59 void ls(struct tfs_sb_info *sbi, char *filename)
61 DIR *dir = tfs_opendir(sbi, filename);
62 struct dirent *de;
64 if (!dir) {
65 printk("open dir %s failed!\n", filename);
66 return;
69 if (dir->dd_dir->inode->i_mode == TFS_FILE) {
70 printk("%d\t %s\n", dir->dd_dir->inode->i_ino, filename);
71 tfs_closedir(dir);
72 return;
75 while (de = tfs_readdir(dir)) {
76 printk("%6d\t %s\n", de->d_ino, de->d_name);
77 free(de);
80 tfs_closedir(dir);
83 void mkdir(struct tfs_sb_info *sbi, char *filename)
85 tfs_mkdir(sbi, filename);
88 void rmdir(struct tfs_sb_info *sbi, char *filename)
90 tfs_rmdir(sbi, filename);
93 void rm(struct tfs_sb_info *sbi, char *filename)
95 tfs_unlink(sbi, filename);
98 void touch (struct tfs_sb_info *sbi, char *filename)
100 struct file *file = tfs_file_open(sbi, filename, LOOKUP_CREATE);
104 void cp(struct tfs_sb_info *sbi, char *from, char *to)
106 char buf[1024];
107 int count;
108 struct file *from_file;
109 struct file *to_file;
112 from_file = tfs_file_open(sbi, from, 0);
113 if (!from_file)
114 return;
115 to_file = tfs_file_open(sbi, to, LOOKUP_CREATE);
116 if (!to_file) {
117 tfs_close(from_file);
118 return;
121 while ((count = tfs_read(from_file, buf, sizeof(buf))) > 0) {
122 count = tfs_write(to_file, buf, count);
123 if (count == -1)
124 printk("write error!\n");
125 else
126 printk(" == %d bytes written!\n", count);
129 tfs_close(from_file);
130 tfs_close(to_file);