Two fixes
[tfsprogs.git] / shell.c
blob748361d4ae8392a156c8c49d7b81720b920d479a
1 #include <stdio.h>
2 #include <string.h>
3 #include <malloc.h>
4 #include <unistd.h>
5 #include <fcntl.h>
6 #include<sys/stat.h>
7 #include "tfs.h"
8 #include "file.h"
9 #include "dirent.h"
11 static struct file * tfs_file_open(struct tfs_sb_info *sbi, char *filename, uint32_t flags)
13 struct file *fobj;
14 char *file = filename;
16 fobj = tfs_open(sbi, file, flags);
17 if (!fobj) {
18 printf("tfs_open: open file %s error!\n", filename);
19 return NULL;
22 return fobj;
25 static void cd(struct tfs_sb_info *sbi, char *dst_dir)
27 DIR *old = this_dir;
29 if (*dst_dir) {
30 this_dir = tfs_opendir(sbi, dst_dir);
31 if (!this_dir) {
32 printf("cd: %s: no such directory\n", dst_dir);
33 this_dir = old;
34 return;
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);
40 this_dir = old;
41 } else {
42 tfs_closedir(old);
48 static void cat(struct tfs_sb_info *sbi, char *filename)
50 struct file *file = tfs_file_open(sbi, filename, 0);
51 char buf[1024];
52 int bytes_read;
54 if (!file)
55 return;
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);
68 struct dirent *de;
70 if (!dir) {
71 printf("open file or dir %s failed!\n", filename);
72 return;
75 if (dir->dd_dir->inode->i_mode == TFS_FILE) {
76 printf("%d\t %s\n", dir->dd_dir->inode->i_ino, filename);
77 tfs_closedir(dir);
78 return;
81 while ((de = tfs_readdir(dir))) {
82 printf("%6d\t %s\n", de->d_ino, de->d_name);
83 free(de);
86 tfs_closedir(dir);
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)
101 char buf[1024];
102 int count;
103 struct file *from_file;
104 struct file *to_file;
107 from_file = tfs_file_open(sbi, from, 0);
108 if (!from_file)
109 return;
110 to_file = tfs_file_open(sbi, to, LOOKUP_CREATE);
111 if (!to_file) {
112 tfs_close(from_file);
113 return;
116 while ((count = tfs_read(from_file, buf, sizeof(buf))) > 0) {
117 count = tfs_write(to_file, buf, count);
118 if (count == -1)
119 printf("write error!\n");
120 else
121 printf(" == %d bytes written!\n", count);
124 tfs_close(from_file);
125 tfs_close(to_file);
128 void cp_in(struct tfs_sb_info *sbi, char *from)
130 char buf[1024];
131 int count;
132 int fd;
133 int bytes_written;
134 struct file *file;
136 file = tfs_file_open(sbi, from, LOOKUP_CREATE);
137 if (!file)
138 return;
140 fd = open(from, O_RDONLY);
141 if (fd < 0) {
142 printf("open file %s error!\n", from);
143 return;
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");
149 else
150 printf(" == %d bytes written!\n", bytes_written);
153 tfs_close(file);
156 void cp_out(struct tfs_sb_info *sbi, char *to)
158 char buf[1024];
159 int count;
160 int fd;
161 int bytes_written;
162 struct file *file;
164 file = tfs_file_open(sbi, to, 0);
165 if (!file)
166 return;
168 fd = open(to, O_RDWR);
169 if (fd < 0) {
170 printf("open file %s error!\n", to);
171 return;
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");
177 else
178 printf(" == %d bytes written!\n", bytes_written);
181 tfs_close(file);
184 int main(int argc, char *argv[])
186 struct tfs_sb_info *sbi;
187 char *fs;
188 char cmd[128];
190 if (argc != 2) {
191 printf("Usage: tfsh fs!\n");
192 return -1;
196 fs = argv[1];
197 sbi = tfs_mount_by_fsname(fs);
198 if (!sbi) {
199 printf("tfs mount failed!\n");
200 return 1;
203 cache_init(sbi);
205 /* init the this_dir */
206 this_dir = tfs_opendir(sbi, "/");
207 if (!this_dir) {
208 printf("reading '/' as this_dir error!\n");
209 return -1;
212 while (1) {
213 char c;
214 char *p = cmd;
215 char *opt;
217 printf("> ");
219 while((c = getchar()) != '\n')
220 *p++ = c;
221 *p = 0;
223 p = cmd;
224 while (*p != ' ' && *p)
225 p++;
226 if (*p) {
227 *p++ = 0;
228 while (*p == ' ')
229 p++;
230 opt = p;
231 } else
232 opt = NULL;
235 if (strcmp(cmd, "cp_in") == 0) {
236 printf("copying in '%s' ... \n", opt);
237 cp_in(sbi, opt);
238 } else if (strcmp(cmd, "cp_out") == 0) {
239 printf("copying out file '%s' ...\n", opt);
240 cp_out(sbi, opt);
241 } else if (strcmp(cmd, "cp") == 0) {
242 printf("copying file '%s' to '%s' ...\n", opt, opt);
243 cp(sbi, opt, opt);
244 } else if (strcmp(cmd, "cat") == 0) {
245 printf("cating file '%s' ... \n", opt);
246 cat(sbi, opt);
247 } else if (strcmp(cmd, "cd") == 0) {
248 printf("cd in '%s' ... \n", opt);
249 cd(sbi, opt);
250 } else if (strcmp(cmd, "echo") == 0) {
251 printf("%s\n", opt);
252 } else if (strcmp(cmd, "ls") == 0) {
253 if (!opt)
254 opt = ".";
255 printf("listing file '%s' ...\n", opt);
256 ls(sbi, opt);
257 } else if (strcmp(cmd, "mkdir") == 0) {
258 printf("making dir '%s' ... \n", opt);
259 cmd_mkdir(sbi, opt);
260 } else if (strcmp(cmd, "rmdir") == 0) {
261 printf("remoing dir '%s' ... \n", opt);
262 cmd_rmdir(sbi, 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);
268 touch(sbi, opt);
269 } else if (strcmp(cmd, "quit") == 0 ||
270 strcmp(cmd, "exit") == 0) {
271 break;
272 } else {
273 printf("unknow command!\n");
277 free(sbi);