[Krafton Jungle] PintOS 2.0.0
크래프톤 정글 PintOS
 
Loading...
Searching...
No Matches
fsutil.c File Reference
#include "filesys/fsutil.h"
#include <debug.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "filesys/directory.h"
#include "filesys/file.h"
#include "filesys/filesys.h"
#include "devices/disk.h"
#include "threads/malloc.h"
#include "threads/palloc.h"
#include "threads/vaddr.h"
Include dependency graph for fsutil.c:

Functions

void fsutil_ls (char **argv UNUSED)
 
void fsutil_cat (char **argv)
 
void fsutil_rm (char **argv)
 
void fsutil_put (char **argv)
 
void fsutil_get (char **argv)
 

Function Documentation

◆ fsutil_cat()

void fsutil_cat ( char **  argv)
32 {
33 const char *file_name = argv[1];
34
35 struct file *file;
36 char *buffer;
37
38 printf ("Printing '%s' to the console...\n", file_name);
39 file = filesys_open (file_name);
40 if (file == NULL)
41 PANIC ("%s: open failed", file_name);
43 for (;;) {
46 if (n == 0)
47 break;
48
49 hex_dump (pos, buffer, n, true);
50 }
53}
#define PANIC(...)
Definition: debug.h:14
void file_close(struct file *file)
Definition: file.c:53
off_t file_read(struct file *file, void *buffer, off_t size)
Definition: file.c:73
off_t file_tell(struct file *file)
Definition: file.c:158
struct file * filesys_open(const char *name)
Definition: filesys.c:81
static struct intq buffer
Definition: input.c:7
int32_t off_t
Definition: off_t.h:9
@ PAL_ASSERT
Definition: palloc.h:9
void * palloc_get_page(enum palloc_flags)
Definition: palloc.c:301
void palloc_free_page(void *)
Definition: palloc.c:333
#define NULL
Definition: stddef.h:4
void hex_dump(uintptr_t ofs, const void *, size_t size, bool ascii)
Definition: stdio.c:553
int printf(const char *,...) PRINTF_FORMAT(1
Definition: file.c:7
off_t pos
Definition: file.c:9
#define PGSIZE
Definition: vaddr.h:20
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fsutil_get()

void fsutil_get ( char **  argv)
141 {
142 static disk_sector_t sector = 0;
143
144 const char *file_name = argv[1];
145 void *buffer;
146 struct file *src;
147 struct disk *dst;
148 off_t size;
149
150 printf ("Getting '%s' from the file system...\n", file_name);
151
152 /* Allocate buffer. */
154 if (buffer == NULL)
155 PANIC ("couldn't allocate buffer");
156
157 /* Open source file. */
158 src = filesys_open (file_name);
159 if (src == NULL)
160 PANIC ("%s: open failed", file_name);
161 size = file_length (src);
162
163 /* Open target disk. */
164 dst = disk_get (1, 0);
165 if (dst == NULL)
166 PANIC ("couldn't open target disk (hdc or hd1:0)");
167
168 /* Write size to sector 0. */
170 memcpy (buffer, "GET", 4);
171 ((int32_t *) buffer)[1] = size;
172 disk_write (dst, sector++, buffer);
173
174 /* Do copy. */
175 while (size > 0) {
176 int chunk_size = size > DISK_SECTOR_SIZE ? DISK_SECTOR_SIZE : size;
177 if (sector >= disk_size (dst))
178 PANIC ("%s: out of space on scratch disk", file_name);
179 if (file_read (src, buffer, chunk_size) != chunk_size)
180 PANIC ("%s: read failed with %"PROTd" bytes unread", file_name, size);
181 memset (buffer + chunk_size, 0, DISK_SECTOR_SIZE - chunk_size);
182 disk_write (dst, sector++, buffer);
183 size -= chunk_size;
184 }
185
186 /* Finish up. */
187 file_close (src);
188 free (buffer);
189}
disk_sector_t disk_size(struct disk *d)
Definition: disk.c:200
void disk_write(struct disk *d, disk_sector_t sec_no, const void *buffer)
Definition: disk.c:235
struct disk * disk_get(int chan_no, int dev_no)
Definition: disk.c:186
#define DISK_SECTOR_SIZE
Definition: disk.h:8
uint32_t disk_sector_t
Definition: disk.h:12
off_t file_length(struct file *file)
Definition: file.c:141
void * malloc(size_t) __attribute__((malloc))
Definition: malloc.c:85
void free(void *)
Definition: malloc.c:202
uint16_t size
Definition: mmu.h:0
#define PROTd
Definition: off_t.h:13
signed int int32_t
Definition: stdint.h:12
void * memcpy(void *, const void *, size_t)
Definition: string.c:7
void * memset(void *, int, size_t)
Definition: string.c:258
Definition: disk.c:52
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fsutil_ls()

void fsutil_ls ( char **argv  UNUSED)
16 {
17 struct dir *dir;
18 char name[NAME_MAX + 1];
19
20 printf ("Files in the root directory:\n");
21 dir = dir_open_root ();
22 if (dir == NULL)
23 PANIC ("root dir open failed");
24 while (dir_readdir (dir, name))
25 printf ("%s\n", name);
26 printf ("End of listing.\n");
27}
bool dir_readdir(struct dir *dir, char name[NAME_MAX+1])
Definition: directory.c:205
struct dir * dir_open_root(void)
Definition: directory.c:48
#define NAME_MAX
Definition: directory.h:12
Definition: directory.c:10
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fsutil_put()

void fsutil_put ( char **  argv)
78 {
79 static disk_sector_t sector = 0;
80
81 const char *file_name = argv[1];
82 struct disk *src;
83 struct file *dst;
84 off_t size;
85 void *buffer;
86
87 printf ("Putting '%s' into the file system...\n", file_name);
88
89 /* Allocate buffer. */
91 if (buffer == NULL)
92 PANIC ("couldn't allocate buffer");
93
94 /* Open source disk and read file size. */
95 src = disk_get (1, 0);
96 if (src == NULL)
97 PANIC ("couldn't open source disk (hdc or hd1:0)");
98
99 /* Read file size. */
100 disk_read (src, sector++, buffer);
101 if (memcmp (buffer, "PUT", 4))
102 PANIC ("%s: missing PUT signature on scratch disk", file_name);
103 size = ((int32_t *) buffer)[1];
104 if (size < 0)
105 PANIC ("%s: invalid file size %d", file_name, size);
106
107 /* Create destination file. */
108 if (!filesys_create (file_name, size))
109 PANIC ("%s: create failed", file_name);
110 dst = filesys_open (file_name);
111 if (dst == NULL)
112 PANIC ("%s: open failed", file_name);
113
114 /* Do copy. */
115 while (size > 0) {
116 int chunk_size = size > DISK_SECTOR_SIZE ? DISK_SECTOR_SIZE : size;
117 disk_read (src, sector++, buffer);
118 if (file_write (dst, buffer, chunk_size) != chunk_size)
119 PANIC ("%s: write failed with %"PROTd" bytes unwritten",
120 file_name, size);
121 size -= chunk_size;
122 }
123
124 /* Finish up. */
125 file_close (dst);
126 free (buffer);
127}
void disk_read(struct disk *d, disk_sector_t sec_no, void *buffer)
Definition: disk.c:211
off_t file_write(struct file *file, const void *buffer, off_t size)
Definition: file.c:97
bool filesys_create(const char *name, off_t initial_size)
Definition: filesys.c:61
int memcmp(const void *, const void *, size_t)
Definition: string.c:48
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fsutil_rm()

void fsutil_rm ( char **  argv)
57 {
58 const char *file_name = argv[1];
59
60 printf ("Deleting '%s'...\n", file_name);
61 if (!filesys_remove (file_name))
62 PANIC ("%s: delete failed\n", file_name);
63}
bool filesys_remove(const char *name)
Definition: filesys.c:97
Here is the call graph for this function:
Here is the caller graph for this function: