client/src/unifycr.c
int unifycr_fid_open(const char* path, int flags, mode_t mode, int* outfid, off_t* outpos)
{
    /* check that path is short enough */
    size_t pathlen = strlen(path) + 1;
    if (pathlen > UNIFYCR_MAX_FILENAME) {
        return UNIFYCR_ERR_NAMETOOLONG;
    }

    /* assume that we'll place the file pointer at the start of the file */
    off_t pos = 0;

    /* check whether this file already exists */
    int fid = unifycr_get_fid_from_path(path);
    debug("unifycr_get_fid_from_path() gave %d\n", fid);

    int gfid = -1, rc = 0;
    if (fs_type == UNIFYCR_LOG) {
		if (fid < 0) {
			rc = unifycr_get_global_fid(path, &gfid);
			if (rc != UNIFYCR_SUCCESS) {
				debug("Failed to generate fid for file %s\n", path);
				return UNIFYCR_ERR_IO;
			}

			gfid = abs(gfid);

			unifycr_fattr_t *ptr_meta = NULL;
			rc = get_global_file_meta(gfid, &ptr_meta);
			if (ptr_meta == NULL) {
				fid = -1;
			} else {
				/* other process has created this file, but its
				 * attribute is not cached locally,
				 * allocate a file id slot for this existing file */
				fid = unifycr_fid_create_file(path);
				if (fid < 0){
				   debug("Failed to create new file %s\n", path);
				   return UNIFYCR_ERR_NFILE;
				}

				/* initialize the storage for the file */
				int store_rc = unifycr_fid_store_alloc(fid);
				if (store_rc != UNIFYCR_SUCCESS) {
					debug("Failed to create storage for file %s\n", path);
					return UNIFYCR_ERR_IO;
				}
				/* initialize the global metadata
				 * */
				unifycr_filemeta_t* meta = unifycr_get_meta_from_fid(fid);
				meta->real_size = ptr_meta->file_attr.st_size;
				ptr_meta->fid = fid;
				ptr_meta->gfid = gfid;

				ins_file_meta(&unifycr_fattrs,\
						ptr_meta);
				free(ptr_meta);
				}
			} else {

			}
		}

    if (fid < 0) {
        /* file does not exist */
        /* create file if O_CREAT is set */
        if (flags & O_CREAT) {
            debug("Couldn't find entry for %s in UNIFYCR\n", path);
            debug("unifycr_superblock = %p; free_fid_stack = %p; free_chunk_stack = %p; unifycr_filelist = %p; chunks = %p\n",
                  unifycr_superblock, free_fid_stack, free_chunk_stack, unifycr_filelist, unifycr_chunks
            );

            /* allocate a file id slot for this new file */
            fid = unifycr_fid_create_file(path);
            if (fid < 0){
               debug("Failed to create new file %s\n", path);
               return UNIFYCR_ERR_NFILE;
            }

            /* initialize the storage for the file */
            int store_rc = unifycr_fid_store_alloc(fid);
            if (store_rc != UNIFYCR_SUCCESS) {
                debug("Failed to create storage for file %s\n", path);
                return UNIFYCR_ERR_IO;
            }

            if (fs_type == UNIFYCR_LOG) {
            	/*create a file and send its attribute to key-value store*/
 				unifycr_fattr_t *new_fmeta =\
 						(unifycr_fattr_t *)malloc(sizeof(unifycr_fattr_t));
 				strcpy(new_fmeta->filename, path);
 				new_fmeta->fid = fid;
 				new_fmeta->gfid = gfid;

 				set_global_file_meta(new_fmeta);
 				ins_file_meta(&unifycr_fattrs,\
 						new_fmeta);
 				free(new_fmeta);

             }
        } else {
            /* ERROR: trying to open a file that does not exist without O_CREATE */
            debug("Couldn't find entry for %s in UNIFYCR\n", path);
            return UNIFYCR_ERR_NOENT;
        }
    } else {
        /* file already exists */

        /* if O_CREAT and O_EXCL are set, this is an error */
        if ((flags & O_CREAT) && (flags & O_EXCL)) {
            /* ERROR: trying to open a file that exists with O_CREATE and O_EXCL */
            return UNIFYCR_ERR_EXIST;
        }

        /* if O_DIRECTORY is set and fid is not a directory, error */
        if ((flags & O_DIRECTORY) && !unifycr_fid_is_dir(fid)) {
            return UNIFYCR_ERR_NOTDIR;
        }

        /* if O_DIRECTORY is not set and fid is a directory, error */ 
        if (!(flags & O_DIRECTORY) && unifycr_fid_is_dir(fid)) {
            return UNIFYCR_ERR_NOTDIR;
        }

        /* if O_TRUNC is set with RDWR or WRONLY, need to truncate file */
        if ((flags & O_TRUNC) && (flags & (O_RDWR | O_WRONLY))) {
            unifycr_fid_truncate(fid, 0);
        }

        /* if O_APPEND is set, we need to place file pointer at end of file */
        if (flags & O_APPEND) {
            unifycr_filemeta_t* meta = unifycr_get_meta_from_fid(fid);
            pos = meta->size;
        }
    }

    /* TODO: allocate a free file descriptor and associate it with fid */
    /* set in_use flag and file pointer */
    *outfid = fid;
    *outpos = pos;
    debug("UNIFYCR_open generated fd %d for file %s\n", fid, path);    

    /* don't conflict with active system fds that range from 0 - (fd_limit) */
    return UNIFYCR_SUCCESS;
}
/* opens a new file id with specified path, access flags, and permissions,
 * fills outfid with file id and outpos with position for current file pointer,
 * returns UNIFYCR error code */
opens a new file id with specified path, access flags, and permissions,
fills outfid with file id and outpos with position for current file pointer,
returns UNIFYCR error code
opens a new file id with specified path access flags and permissions fills outfid with file id and outpos with position for current file pointer returns UNIFYCR error code
int unifycr_fid_open(const char* path, int flags, mode_t mode, int* outfid, off_t* outpos)
{
    size_t pathlen = strlen(path) + 1;
    if (pathlen > UNIFYCR_MAX_FILENAME) {
        return UNIFYCR_ERR_NAMETOOLONG;
    }
    off_t pos = 0;
    int fid = unifycr_get_fid_from_path(path);
    debug("unifycr_get_fid_from_path() gave %d\n", fid);
    int gfid = -1, rc = 0;
    if (fs_type == UNIFYCR_LOG) {
		if (fid < 0) {
			rc = unifycr_get_global_fid(path, &gfid);
			if (rc != UNIFYCR_SUCCESS) {
				debug("Failed to generate fid for file %s\n", path);
				return UNIFYCR_ERR_IO;
			}
			gfid = abs(gfid);
			unifycr_fattr_t *ptr_meta = NULL;
			rc = get_global_file_meta(gfid, &ptr_meta);
			if (ptr_meta == NULL) {
				fid = -1;
			} else {
				fid = unifycr_fid_create_file(path);
				if (fid < 0){
				   debug("Failed to create new file %s\n", path);
				   return UNIFYCR_ERR_NFILE;
				}
				int store_rc = unifycr_fid_store_alloc(fid);
				if (store_rc != UNIFYCR_SUCCESS) {
					debug("Failed to create storage for file %s\n", path);
					return UNIFYCR_ERR_IO;
				}
				unifycr_filemeta_t* meta = unifycr_get_meta_from_fid(fid);
				meta->real_size = ptr_meta->file_attr.st_size;
				ptr_meta->fid = fid;
				ptr_meta->gfid = gfid;
				ins_file_meta(&unifycr_fattrs,\
						ptr_meta);
				free(ptr_meta);
				}
			} else {
			}
		}
    if (fid < 0) {
        if (flags & O_CREAT) {
            debug("Couldn't find entry for %s in UNIFYCR\n", path);
            debug("unifycr_superblock = %p; free_fid_stack = %p; free_chunk_stack = %p; unifycr_filelist = %p; chunks = %p\n",
                  unifycr_superblock, free_fid_stack, free_chunk_stack, unifycr_filelist, unifycr_chunks
            );
            fid = unifycr_fid_create_file(path);
            if (fid < 0){
               debug("Failed to create new file %s\n", path);
               return UNIFYCR_ERR_NFILE;
            }
            int store_rc = unifycr_fid_store_alloc(fid);
            if (store_rc != UNIFYCR_SUCCESS) {
                debug("Failed to create storage for file %s\n", path);
                return UNIFYCR_ERR_IO;
            }
            if (fs_type == UNIFYCR_LOG) {
 				unifycr_fattr_t *new_fmeta =\
 						(unifycr_fattr_t *)malloc(sizeof(unifycr_fattr_t));
 				strcpy(new_fmeta->filename, path);
 				new_fmeta->fid = fid;
 				new_fmeta->gfid = gfid;
 				set_global_file_meta(new_fmeta);
 				ins_file_meta(&unifycr_fattrs,\
 						new_fmeta);
 				free(new_fmeta);
             }
        } else {
            debug("Couldn't find entry for %s in UNIFYCR\n", path);
            return UNIFYCR_ERR_NOENT;
        }
    } else {
        if ((flags & O_CREAT) && (flags & O_EXCL)) {
            return UNIFYCR_ERR_EXIST;
        }
        if ((flags & O_DIRECTORY) && !unifycr_fid_is_dir(fid)) {
            return UNIFYCR_ERR_NOTDIR;
        }
        if (!(flags & O_DIRECTORY) && unifycr_fid_is_dir(fid)) {
            return UNIFYCR_ERR_NOTDIR;
        }
        if ((flags & O_TRUNC) && (flags & (O_RDWR | O_WRONLY))) {
            unifycr_fid_truncate(fid, 0);
        }
        if (flags & O_APPEND) {
            unifycr_filemeta_t* meta = unifycr_get_meta_from_fid(fid);
            pos = meta->size;
        }
    }
    *outfid = fid;
    *outpos = pos;
    debug("UNIFYCR_open generated fd %d for file %s\n", fid, path);    
    return UNIFYCR_SUCCESS;
}
int unifycr_fid_open ( const char * path , int flags , mode_t mode , int * outfid , off_t * outpos ) { size_t pathlen = strlen ( path ) + 1 ; if ( pathlen > UNIFYCR_MAX_FILENAME ) { return UNIFYCR_ERR_NAMETOOLONG ; } off_t pos = 0 ; int fid = unifycr_get_fid_from_path ( path ) ; debug ( " \n " , fid ) ; int gfid = -1 , rc = 0 ; if ( fs_type == UNIFYCR_LOG ) { if ( fid < 0 ) { rc = unifycr_get_global_fid ( path , & gfid ) ; if ( rc != UNIFYCR_SUCCESS ) { debug ( " \n " , path ) ; return UNIFYCR_ERR_IO ; } gfid = abs ( gfid ) ; unifycr_fattr_t * ptr_meta = NULL ; rc = get_global_file_meta ( gfid , & ptr_meta ) ; if ( ptr_meta == NULL ) { fid = -1 ; } else { fid = unifycr_fid_create_file ( path ) ; if ( fid < 0 ) { debug ( " \n " , path ) ; return UNIFYCR_ERR_NFILE ; } int store_rc = unifycr_fid_store_alloc ( fid ) ; if ( store_rc != UNIFYCR_SUCCESS ) { debug ( " \n " , path ) ; return UNIFYCR_ERR_IO ; } unifycr_filemeta_t * meta = unifycr_get_meta_from_fid ( fid ) ; meta -> real_size = ptr_meta -> file_attr . st_size ; ptr_meta -> fid = fid ; ptr_meta -> gfid = gfid ; ins_file_meta ( & unifycr_fattrs , ptr_meta ) ; free ( ptr_meta ) ; } } else { } } if ( fid < 0 ) { if ( flags & O_CREAT ) { debug ( " \n " , path ) ; debug ( " \n " , unifycr_superblock , free_fid_stack , free_chunk_stack , unifycr_filelist , unifycr_chunks ) ; fid = unifycr_fid_create_file ( path ) ; if ( fid < 0 ) { debug ( " \n " , path ) ; return UNIFYCR_ERR_NFILE ; } int store_rc = unifycr_fid_store_alloc ( fid ) ; if ( store_rc != UNIFYCR_SUCCESS ) { debug ( " \n " , path ) ; return UNIFYCR_ERR_IO ; } if ( fs_type == UNIFYCR_LOG ) { unifycr_fattr_t * new_fmeta = ( unifycr_fattr_t * ) malloc ( sizeof ( unifycr_fattr_t ) ) ; strcpy ( new_fmeta -> filename , path ) ; new_fmeta -> fid = fid ; new_fmeta -> gfid = gfid ; set_global_file_meta ( new_fmeta ) ; ins_file_meta ( & unifycr_fattrs , new_fmeta ) ; free ( new_fmeta ) ; } } else { debug ( " \n " , path ) ; return UNIFYCR_ERR_NOENT ; } } else { if ( ( flags & O_CREAT ) && ( flags & O_EXCL ) ) { return UNIFYCR_ERR_EXIST ; } if ( ( flags & O_DIRECTORY ) && ! unifycr_fid_is_dir ( fid ) ) { return UNIFYCR_ERR_NOTDIR ; } if ( ! ( flags & O_DIRECTORY ) && unifycr_fid_is_dir ( fid ) ) { return UNIFYCR_ERR_NOTDIR ; } if ( ( flags & O_TRUNC ) && ( flags & ( O_RDWR | O_WRONLY ) ) ) { unifycr_fid_truncate ( fid , 0 ) ; } if ( flags & O_APPEND ) { unifycr_filemeta_t * meta = unifycr_get_meta_from_fid ( fid ) ; pos = meta -> size ; } } * outfid = fid ; * outpos = pos ; debug ( " \n " , fid , path ) ; return UNIFYCR_SUCCESS ; }
opens a new file id with specified path, access flags, and permissions,
 fills outfid with file id and outpos with position for current file pointer,
 returns UNIFYCR error code
opens a new file id with specified path access flags and permissions fills outfid with file id and outpos with position for current file pointer returns UNIFYCR error code
/* check that path is short enough */ /* assume that we'll place the file pointer at the start of the file */ /* check whether this file already exists */ /* other process has created this file, but its
				 * attribute is not cached locally,
				 * allocate a file id slot for this existing file */ /* initialize the storage for the file */ /* initialize the global metadata
				 * */ /* file does not exist */ /* create file if O_CREAT is set */ /* allocate a file id slot for this new file */ /* initialize the storage for the file */ /*create a file and send its attribute to key-value store*/ /* ERROR: trying to open a file that does not exist without O_CREATE */ /* file already exists */ /* if O_CREAT and O_EXCL are set, this is an error */ /* ERROR: trying to open a file that exists with O_CREATE and O_EXCL */ /* if O_DIRECTORY is set and fid is not a directory, error */ /* if O_DIRECTORY is not set and fid is a directory, error */ /* if O_TRUNC is set with RDWR or WRONLY, need to truncate file */ /* if O_APPEND is set, we need to place file pointer at end of file */ /* TODO: allocate a free file descriptor and associate it with fid */ /* set in_use flag and file pointer */ /* don't conflict with active system fds that range from 0 - (fd_limit) */