Пример #1
0
int stat(const char *path, struct stat *buf)
{
    FileSystemMessage msg;
    FileStat st;
    ProcessID mnt = findMount(path);

    /* Fill message. */
    msg.action = StatFile;
    msg.path   = (char *) path;
    msg.stat   = &st;
    msg.type   = IPCType;
    
    /* Ask the FileSystem for the information. */
    if (mnt)
    {
	IPCMessage(mnt, API::SendReceive, &msg, sizeof(msg));

        /* Copy information into buf. */
	if (msg.result == ESUCCESS)
	{
	    buf->fromFileStat(&st);
	}
	/* Set errno. */
	errno = msg.result;
    }
    else
	errno = ENOENT;
    
    /* Success. */
    return msg.result == ESUCCESS ? 0 : -1;
}
Пример #2
0
int mkdir(const char *path, mode_t mode)
{
    FileSystemMessage msg;
    ProcessID mnt = findMount(path);

    /* Fill message. */
    msg.action = CreateFile;
    msg.buffer = (char *) path;
    msg.mode   = mode;
    msg.filetype = DirectoryFile;
    
    /* Ask the FileSystem to create it. */
    if (mnt)
    {
	IPCMessage(mnt, SendReceive, &msg, sizeof(msg));

	/* Set errno. */
	errno = msg.result;
    }
    else
	errno = ENOENT;
    
    /* Success. */
    return msg.result == ESUCCESS ? 0 : -1;
}
Пример #3
0
return_type sWrite(int nparams, arg_type* a){
    if(debug)printf("got into sWrite\n");
 //    if (nparams != 5) {
	// 	r.return_val = NULL;
	// 	r.return_size = 0;
	// 	return r;
	// }

    char *user_ip = (char *)a->arg_val;
	int fd = *(int *)a->next->arg_val;
	void *buff = a->next->next->arg_val;
	int count = *(unsigned int *)a->next->next->next->arg_val;
    char *alias = a->next->next->next->next->arg_val;

    MountedUser *mounted = findMount(user_ip, alias);
    if (mounted == NULL){
        return createReturn(1, -1);
    }
	
    int bytesWritten = -1;
    bytesWritten = write(fd, buff, count);

    if(bytesWritten > 0){
        return createReturn(0, bytesWritten);
    }else{
        return createReturn(errno, -1);
    }
}
Пример #4
0
int touch(const char *path, mode_t mode) /* Creamos touch */
{
    FileSystemMessage msg;
    ProcessID mnt = findMount(path);

    /* ¿Como es el archivo?. */
    msg.action = CreateFile;    /* ¿Que hacemos con el archivo? */
    msg.buffer = (char *) path;
    msg.mode   = mode;
    msg.filetype = RegularFile; /* Tipo de archivo : txt, directorio, etc */
    
    /* Le preguntamos al OS si permite crearlo. */
    if (mnt)
    {
	IPCMessage(mnt, SendReceive, &msg, sizeof(msg));

	/* ¿Error?. */
	errno = msg.result;
    }
    else
	errno = ENOENT;
    
    /* Finalizando..... */
    return msg.result == ESUCCESS ? 0 : -1;
}
Пример #5
0
int mkdir(const char *path, mode_t mode)
{
    FileSystemMessage msg;
    ProcessID mnt = findMount(path);

    /* Fill message. */
    msg.type   = ChannelMessage::Request;
    msg.action = CreateFile;
    msg.path   = (char *) path;
    msg.mode   = mode;
    msg.filetype = DirectoryFile;

    /* Ask the FileSystem to create it. */
    if (mnt)
    {
        ChannelClient::instance->syncSendReceive(&msg, mnt);

        // Set errno
        errno = msg.result;
    }
    else
        errno = ENOENT;

    /* Success. */
    return msg.result == ESUCCESS ? 0 : -1;
}
Пример #6
0
return_type sReadDir(const int nparams, arg_type* a){
    // if(nparams != 3){
    //     r.return_val = NULL;
    //     r.return_size = 0;
    //     return r;
    // }
    
    char *user_ip = (char *)a->arg_val;
    char *alias = (char *)a->next->arg_val;
	char *filepath = (char *)a->next->next->arg_val;

    char *rootname = findRootName(filepath);
    MountedUser *mounted = findMount(user_ip, alias);

    if(mounted == NULL){
        return createReturn(1, -1);
    }

    OpenedFolder *openfolder = findOpenFolder(mounted, prependRootName(filepath));

    struct dirent *readDirectory = readdir(openfolder->storedDir);


    if(readDirectory == NULL){
        if(debug)printf("in sReadDir: reading the directory is NULL\n");
        return createReturn(1, -1);
    }   

    struct fsDirent *fsdir = (struct fsDirent*)malloc(sizeof(struct fsDirent));

    if(readDirectory->d_type == DT_DIR){
        if(debug)printf("type: folder\n");
         fsdir->entType = 1;
    }else if(readDirectory->d_type == DT_REG){
        if(debug)printf("type: file\n");
        fsdir->entType = 0;
    }else{
        if(debug)printf("type: unknown\n");
        fsdir->entType = -1;
    }

    strcpy(fsdir->entName, readDirectory->d_name);
    if(debug)printf("fsdir->entName = %s\n", fsdir->entName);
    if(debug)printf("fsdir->entType = %d\n", fsdir->entType);
    
    void *returnBuff = malloc(sizeof(struct fsDirent)+sizeof(int));
    void *ptr = returnBuff;

    int val = 0;
    void *buff = malloc(sizeof(int));
    memcpy(buff, &val, sizeof(int));
    
    memcpy(ptr, buff, sizeof(int));
    ptr += sizeof(int);
    memcpy(ptr, fsdir, sizeof(struct fsDirent));
    r.return_val = returnBuff;
    r.return_size = sizeof(struct fsDirent) + sizeof(int);

    return r;
}
Пример #7
0
//nparams: user_ip
return_type sUnmount(const int nparams, arg_type* a) {

    if(debug)printf("recevied %d nparams in sUnmount\n", nparams);
	// if (nparams != 2) {
	// 	r.return_val = NULL;
	// 	r.return_size = 0;
	// 	return r;
	// }

	char *user_ip = (char *)a->arg_val;
    char *alias = (char *)a->next->arg_val;

    MountedUser *found = findMount(user_ip, alias);

    if (found == NULL) {
        if(debug)printf("found is null in sUnmount\n");
        return createReturn(1, -1);
    }

    if (removeMount(user_ip, alias) != 0) {
        if(debug)printf("problem with removeMount\n");
        return createReturn(1, -1);
    }
    if(debug)printf("returning a good value from sUnmount\n");
    return createReturn(0, 0);
}
Пример #8
0
//params: user_ip -> filepath -> mode
return_type sOpen(const int nparams, arg_type* a){
	// if (nparams != 4) {
	// 	r.return_val = NULL;
	// 	r.return_size = 0;
	// 	return r;
	// }

	char *user_ip = (char *)a->arg_val;
    char *alias = (char *)a->next->arg_val;
    char *filepath = (char *)a->next->next->arg_val;
    char *fullpath = prependRootName(filepath);
	int mode = *(int *)a->next->next->next->arg_val;
    if(debug)printf("fullpath on server: %s\n", fullpath);

    if (checkFileInUse(fullpath) == 1){
        if(debug)printf("file in use");
        return createReturn(26, -1);
    }

    // Check if user is mounted
    MountedUser *mounted = findMount(user_ip, alias);
    if (mounted == NULL){
        return createReturn(1, -1);
    }

    // If the file does not exist or is a folder return error
    struct stat buffer;
	int err = stat(fullpath, &buffer);
    if(debug)printf("errval: %d\n", err);
	if (err == -1 && mode == 0){
        if(debug)printf("cannot open in read mode\n");
        return createReturn(err, 0);
    }

	if (S_ISDIR(buffer.st_mode)){
        if(debug)printf("wtf its a folder\n");
        return createReturn(21, 0);
    }

	int fd;
	if (mode == 1) {
		fd = open(fullpath, O_CREAT | O_RDWR | O_APPEND | O_TRUNC, S_IRUSR | S_IWUSR);
	}else {
		fd = open(fullpath, O_RDONLY | S_IRUSR);
	}

    if(debug)printf("the fd is %d\n", fd);
    // Can just add struct because we know it doesn't exist from checkFileInUse
	if (addOpenFile(user_ip, alias, fd, fullpath) != 0){
        if(debug)printf("closing the file descriptor %d because of error\n", fd);
        close(fd);
        return createReturn(1, -1);
    }
	return createReturn(0, fd);
}
Пример #9
0
//nparams: user_ip -> filepath
return_type sOpenDir(int nparams, arg_type* a) {
 //    if (nparams != 3) {
	// 	r.return_val = NULL;
	// 	r.return_size = 0;
	// 	return r;
	// }

    char *user_ip = (char *)(a->arg_val);
    char *alias = (char *)(a->next->arg_val);
    char *path = (char *)(a->next->next->arg_val);

    char new_path[256];
    strcpy(new_path, prependRootName(path));
    MountedUser *mounted = findMount(user_ip, alias);

    if (mounted == NULL){
        printf("exit 5\n");
        return createReturn(1, -1);
    }

    if (findOpenFolder(mounted, new_path) != NULL){
        printf("exit 4\n");
        return createReturn(2, -1);
    }

    DIR *d = NULL;
    d = opendir(new_path);
    
    if(d == NULL){
        printf("exit 1\n");
        return createReturn(errno, -1);
    }

    // if(debug)printf("trying to add the open folder\n");
    if (addOpenFolder(mounted, new_path, d) != 0) {
        closedir(d);
        printf("exit 2\n");
        return createReturn(1, -1);
    }

    // if(debug)printf("weird stuff\n");
    OpenedFolder *new_open = findOpenFolder(mounted, new_path);
    new_open->storedDir = d;

    if(debug)printf("return from sOpenDir\n");
    printf("exit 3\n");
    return createReturn(0, 0);
}
Пример #10
0
//nparams: user_ip -> fd -> buf -> count -> alias
return_type sRead(int nparams, arg_type* a) {
 //    if (nparams != 4) {
	// 	r.return_val = NULL;
	// 	r.return_size = 0;
	// 	return r;
	// }

    char *user_ip = (char *)a->arg_val;
	int fd = *(int *)a->next->arg_val;
	int count = *(int *)a->next->next->arg_val;
    char *alias = (char *)a->next->next->next->arg_val;

    MountedUser *mounted = findMount(user_ip, alias);
    if (mounted == NULL){
        return createReturn(1, -1);
    }

    int bytesRead = -1;

    char *buff = malloc(count);
    memset(buff, 0, count);
    bytesRead = read(fd, buff, count);
    if(debug)printf("buffer contained: %s\n", buff);
    if(debug)printf("bytesread is %d, count is %d\n", bytesRead, count);
    
    if(debug)printf("sending back the buffer %s\n", buff);

    if(bytesRead > 0){
        void *returnBuff = malloc(bytesRead+sizeof(int));

        int val = 0;
        void *temp = malloc(sizeof(int));
        memcpy(temp, &val, sizeof(int));

        void *ptr = returnBuff;
        memcpy(ptr, temp, sizeof(int));
        ptr += sizeof(int);
        memcpy(ptr, buff, bytesRead);
        r.return_val = returnBuff;
        r.return_size = bytesRead + sizeof(int);
        return r;
    }else{
        r.return_val = &errno;
        r.return_size = sizeof(int);
        return r;
    }
}
Пример #11
0
//params: filepath
return_type sCloseDir(int nparams, arg_type* a){
    
 //    if (nparams != 3) {
	// 	r.return_val = NULL;
	// 	r.return_size = 0;
	// 	return r;
	// }

	char *user_ip = (char *)a->arg_val;
    char *alias = (char *)a->next->arg_val;
    char *path = (char *)a->next->next->arg_val;

    char *fullpath = malloc(sizeof(char) *256);

    strcpy(fullpath, prependRootName(path));

    if(debug)printf("fullpath is %s\n", fullpath);

    MountedUser *mounted = findMount(user_ip, alias);

    if (mounted == NULL) {
        if(debug)printf("Mounted is NULL\n");
        return createReturn(2, -1);
    }

    OpenedFolder *openfolder = findOpenFolder(mounted, fullpath);

    
    if (openfolder == NULL){
        if(debug)printf("open folder is not open\n");
        return createReturn(2, -1);
    }

    if (removeOpenFolder(mounted, fullpath) != 0) {
        if(debug)printf("remove folder not working\n");
        return createReturn(2, -1);
    }

    return createReturn(0, 0);

}
Пример #12
0
// Look up and return the inode for a path name.
// If parent != 0, return the inode for the parent and copy the final
// path element into name, which must have room for DIRSIZ bytes.
// Must be called inside a transaction since it calls iput().
static struct inode*
namex(char *path, int nameiparent, char *name)
{
  struct inode *ip, *next;
  if(*path == '/')
    ip = iget(ROOTDEV, ROOTINO);
  else{
    ip = (findMount(path));
    if(ip!=0){
      //memset(path, 0, strlen(path));
      //memmove(path, "/", 1);
        return ip;
    }
    ip = idup(proc->cwd);
  }

  while((path = skipelem(path, name)) != 0){
    ilock(ip);
    if(ip->type != T_DIR){
      iunlockput(ip);
      return 0;
    }
    if(nameiparent && *path == '\0'){
      // Stop one level early.
      iunlock(ip);
      return ip;
    }
    if((next = dirlookup(ip, name, 0)) == 0){
      iunlockput(ip);
      return 0;
    }
    iunlockput(ip);
    ip = next;
  }
  if(nameiparent){
    iput(ip);
    return 0;
  }
  return ip;
}
Пример #13
0
int open(const char *path, int oflag, ...)
{
    FileSystemMessage msg;
    ProcessID mnt = findMount(path);
    
    /* Fill message. */
    msg.action = OpenFile;
    msg.buffer = (char *) path;
    
    /* Ask the FileSystem. */
    if (mnt)
    {
	IPCMessage(mnt, SendReceive, &msg, sizeof(msg));
    
	/* Set errno. */
	errno = msg.result;
    }
    else
	errno = ENOENT;
    
    /* Success. */
    return msg.result == ESUCCESS ? msg.fd : -1;
}
Пример #14
0
//nparams: user_ip -> fd
return_type sClose(int nparams, arg_type* a) {
	// if (nparams != 3) {
	// 	r.return_val = NULL;
	// 	r.return_size = 0;
	// 	return r;
	// }

    char *user_ip = (char *)a->arg_val;
	char *alias = (char *)a->next->arg_val;
    int fd = *(int *)a->next->next->arg_val;

    // Check if user is mounted
    MountedUser *mounted = findMount(user_ip, alias);
    if (mounted == NULL){
        return createReturn(1, -1);
    }

    if (removeOpenFile(user_ip, alias, fd) != 0){
        return createReturn(1, -1);
    }

    return createReturn(0, 0);
}
Пример #15
0
//nparams: user_ip
return_type sMount(const int nparams, arg_type* a) {	
 //    if (nparams != 2) {
	// 	r.return_val = NULL;
	// 	r.return_size = 0;
	// 	return r;
	// }

    char *user_ip = (char *)a->arg_val;
    char *alias  = (char *)a->next->arg_val;

    MountedUser *mounted = findMount(user_ip, alias);
    if(debug)printf("Mounted address: %p\n", mounted);

    if(debug)printMount();
    if (mounted != NULL) {
        if(debug) printf("Mounted is not NULL\n");
        return createReturn(13, -1);
    }

    addMount(user_ip, alias);

    return createReturn(0, 0);
}
Пример #16
0
ssize_t read(int fildes, void *buf, size_t nbyte)
{
    FileSystemMessage msg;
    ProcessID mnt = findMount(fildes);

    /* Read the file. */
    if (mnt)
    {
	msg.action = ReadFile;
        msg.fd     = fildes;
        msg.buffer = (char *) buf;
        msg.size   = nbyte;
        msg.offset = ZERO;
        IPCMessage(mnt, SendReceive, &msg, sizeof(msg));

        /* Set error number. */
	errno = msg.result;
    }
    else
	errno = ENOENT;
    
    /* Success. */
    return errno >= 0 ? errno : (ssize_t) -1;
}