int do_open(char * filename, int flags, int mode) { iNode * posible_file = search_directory(filename, current); int fd; if (posible_file != NULL) { if (posible_file->gid < currentUsr.group) return -2; if (posible_file->identifier == LINK) { posible_file = fs_get_inode(posible_file->link); } if ((fd = search_for_inode(posible_file->iNode_number)) != -1) { return fd; } else { return insert_fd(posible_file->iNode_number); } } else { if (flags == 0) { do_creat(filename, mode); } else { return -1; } } }
int do_creat(char * filename, int mode) { int i; iNode * ret; if ((ret = insert_file(filename, mode, current)) == NULL) { return -1; } int fd = insert_fd(ret->iNode_number); return fd; }
void cp_file(char * filename, iNode * origin, iNode * path_inode) { iNode * ret, * aux; char * buff; int fd; aux = current; if ( (ret = insert_file(filename, 777, path_inode)) == NULL) return ; insert_fd(ret->iNode_number); current = origin; fd = do_open(filename, 1, 2); buff = malloc(getsize(fd)); read(fd, buff, -1); //printf("\nhasta ahora todo va bien. filename:%s. buffer:%s", filename, buff); //while(1); current = path_inode; fd = do_open(filename, 1, 777); write(fd, buff, str_len(buff)); current = aux; }
int storage_file_open(struct storage_msg *msg, const void *r, size_t req_len) { char *path = NULL; const struct storage_file_open_req *req = r; struct storage_file_open_resp resp = {0}; if (req_len < sizeof(*req)) { ALOGE("%s: invalid request length (%zd < %zd)\n", __func__, req_len, sizeof(*req)); msg->result = STORAGE_ERR_NOT_VALID; goto err_response; } size_t fname_len = strlen(req->name); if (fname_len != req_len - sizeof(*req)) { ALOGE("%s: invalid filename length (%zd != %zd)\n", __func__, fname_len, req_len - sizeof(*req)); msg->result = STORAGE_ERR_NOT_VALID; goto err_response; } int rc = asprintf(&path, "%s/%s", ssdir_name, req->name); if (rc < 0) { ALOGE("%s: asprintf failed\n", __func__); msg->result = STORAGE_ERR_GENERIC; goto err_response; } int open_flags = O_RDWR; if (req->flags & STORAGE_FILE_OPEN_TRUNCATE) open_flags |= O_TRUNC; if (req->flags & STORAGE_FILE_OPEN_CREATE) { /* open or create */ if (req->flags & STORAGE_FILE_OPEN_CREATE_EXCLUSIVE) { /* create exclusive */ open_flags |= O_CREAT | O_EXCL; rc = TEMP_FAILURE_RETRY(open(path, open_flags, S_IRUSR | S_IWUSR)); } else { /* try open first */ rc = TEMP_FAILURE_RETRY(open(path, open_flags, S_IRUSR | S_IWUSR)); if (rc == -1 && errno == ENOENT) { /* then try open with O_CREATE */ open_flags |= O_CREAT; rc = TEMP_FAILURE_RETRY(open(path, open_flags, S_IRUSR | S_IWUSR)); } } } else { /* open an existing file */ rc = TEMP_FAILURE_RETRY(open(path, open_flags, S_IRUSR | S_IWUSR)); } if (rc < 0) { rc = errno; if (errno == EEXIST || errno == ENOENT) { ALOGV("%s: failed to open file \"%s\": %s\n", __func__, path, strerror(errno)); } else { ALOGE("%s: failed to open file \"%s\": %s\n", __func__, path, strerror(errno)); } msg->result = translate_errno(rc); goto err_response; } free(path); /* at this point rc contains storage file fd */ msg->result = STORAGE_NO_ERROR; resp.handle = insert_fd(open_flags, rc); ALOGV("%s: \"%s\": fd = %u: handle = %d\n", __func__, path, rc, resp.handle); return ipc_respond(msg, &resp, sizeof(resp)); err_response: if (path) free(path); return ipc_respond(msg, NULL, 0); }