static int my_read(int fd, void *buffer, unsigned size) { lock_acquire(&filesys_lock); if(!address_valid(buffer)){ lock_release(&filesys_lock); my_exit(-1); } if(fd == STDIN_FILENO){ unsigned i = 0; uint8_t* local_buffer = (uint8_t *)buffer; for(;i< size; i++) local_buffer[i] = input_getc(); lock_release(&filesys_lock); return size; } struct file * fp = get_file_by_fd(fd); if(!fp){ lock_release(&filesys_lock); return -1; } int byte = file_read(fp, buffer, size); lock_release(&filesys_lock); return byte; }
static void read_file(t_server *serv, int s) { t_file *file; char buf[512]; char ret[1024]; int fd; int r; if ((r = read(s, buf, 511)) > 0) { buf[r] = 0; if ((file = get_file_by_fd(serv->files, s)) != NULL) { sprintf(ret, ".downloads/%s", file->name); if ((fd = open(ret, \ O_CREAT | O_APPEND | O_RDWR, 0666)) != -1) { swrite(fd, buf); close(fd); } } } else { close(s); serv->fd_type[s] = FD_FREE; } }
static int my_filesize(int fd) { lock_acquire(&filesys_lock); struct file * fp = get_file_by_fd(fd); if(fp == NULL){ lock_release(&filesys_lock); return -1; } lock_release(&filesys_lock); return file_length(fp); }
static void my_seek(int fd, unsigned position) { lock_acquire(&filesys_lock); struct file *fp = get_file_by_fd(fd); if(fp == NULL){ lock_release(&filesys_lock); return; } file_seek(fp, position); lock_release(&filesys_lock); return; }
static unsigned my_tell(int fd) { lock_acquire(&filesys_lock); struct file * fp = get_file_by_fd(fd); if(fd == NULL){ lock_release(&filesys_lock); return -1; } off_t off = file_tell(fp); lock_release(&filesys_lock); return off; }
// khg : syscalls ------------------------------- static int my_write (int fd, const void *buffer, unsigned length) { lock_acquire(&filesys_lock); if(!address_valid(buffer)){ lock_release(&filesys_lock); my_exit(-1); } if (fd == STDOUT_FILENO){/* stdout */ putbuf (buffer, length); lock_release(&filesys_lock); return length; } struct file *fp = get_file_by_fd(fd); if(fp == NULL){ lock_release(&filesys_lock); return -1; } char *temp = malloc(sizeof(char) * (length+1)); int i, check = 0, c; c = file_read(fp, temp, length); for(i=0; i < c ; ++i) { if(temp[i]!=((char *)buffer)[i]) check = 1; } if(check == 0) // same { lock_release(&filesys_lock); return c; } else file_seek(fp, file_tell(fp)-c); int byte = file_write(fp, buffer, length); lock_release(&filesys_lock); return byte; }
void file_read(t_server *serv, int s) { t_file *file; struct sockaddr_in csin; socklen_t len; int cs; len = sizeof(csin); cs = xaccept(s, (struct sockaddr *)&csin, &len); if ((file = get_file_by_fd(serv->files, s)) != NULL) file->fd = cs; serv->fd_type[cs] = FD_WFILE_READ; serv->fct_read[cs] = read_file; serv->fct_write[cs] = NULL; close(s); serv->fd_type[s] = FD_FREE; }
static void write_file(t_server *serv, int s) { t_file *file; char buf[512]; char ret[1024]; int fd; int r; if ((file = get_file_by_fd(serv->files, s)) != NULL) { sprintf(ret, ".downloads/%s", file->name); if ((fd = open(ret, O_RDONLY)) != -1) { if ((r = read(fd, buf, 511)) > 0) { buf[r] = 0; swrite(s, buf); close(fd); } else shutting_down(serv, file, ret, s); } } }