extern "C" void open_file (const char *pathname, int flags, struct client client) { uint32_t file_id; struct decafs_file_stat stat; int cursor; // If the file does not exist if ((decafs_file_sstat ((char *)pathname, &stat, client)) == FILE_NOT_FOUND) { // If we are going to write to the file, create it if (flags & O_RDWR) { printf ("\tfile not found... creating now\n"); // Create the file struct timeval time; gettimeofday(&time, NULL); // change 4th param to get_replica_size() // implement in vmeta file_id = add_file ((char *)pathname, get_stripe_size(), get_chunk_size(), get_chunk_size(), time, client); } // We can't read from nothing! else { if (send_open_result (client, FILE_NOT_FOUND_FOR_READING) < 0) { printf ("\tOpen result could not reach client.\n"); } return; } } else { file_id = stat.file_id; } printf ("\tfile %s has id %d.\n", pathname, file_id); // If we're opening with read only, obtain a read lock if (flags & O_RDWR) { // if we can't get a write lock, return that the file is in use so we can't // open it if (get_exclusive_lock (client, file_id) < 0) { if (send_open_result (client, FILE_IN_USE) < 0) { printf ("\tOpen result could not reach client.\n"); } return; } printf ("\tobtained a write lock.\n"); } // obtain a write lock else { // if we can't get a read lock, return that the file is in use so we can't // open it if (get_shared_lock (client, file_id) < 0) { if (send_open_result (client, FILE_IN_USE) < 0) { printf ("\tOpen result could not reach client.\n"); } return; } printf ("\tobtained a read lock.\n"); } cursor = new_file_cursor (file_id, client); if (flags & O_APPEND) { printf ("\tfile opened with O_APPEND, moving cursor to EOF.\n"); set_file_cursor (cursor, stat.size, client); } if (send_open_result (client, cursor) < 0) { printf ("\tOpen result could not reach client.\n"); } }
// open_file extern "C" void open_file (const char *pathname, int flags, struct client client) { send_open_result(client, 1); }