int direct_io_fsync(int fd) { if(DIRECT_IO_READ_FILED == fd){ return sync_from_file(); }else if(DIRECT_IO_WRITE_FILED == fd){ return sync_to_file(); } return -1; }
/* sync everything to disk */ void sync_all() { char c, *oldstack; oldstack = stack; /* sync the cache first */ sync_whole_cache(); for(c='a'; c<='z'; c++) sync_to_file(c, 0); log("sync", "Full Sync Completed."); }
void direct_io_close(int fd) { if(DIRECT_IO_READ_FILED == fd && read_fd){ close(read_fd); read_fd = -1; strcpy(read_file, "null"); }else if(DIRECT_IO_WRITE_FILED == fd){ sync_to_file(); // sync buffer to file close(write_fd); write_fd = -1; strcpy(write_file, "null"); } }
void do_sync() { int origin; action="doing sync"; sync_counter=SYNC_TIME; origin=synct; while(!update[synct]) { synct=(synct+1)%26; if (synct==origin) break; } if (update[synct]) { sync_to_file(synct+'a',0); synct=(synct+1)%26; } }
int direct_io_seek(int fd, off_t offset, int whence) { if(SEEK_SET == whence){ // only supported // int file_locate = 0; // int file_size = 0; if(DIRECT_IO_WRITE_FILED == fd){ //同步缓冲 sync_to_file(); //移动指针 //--计算文件总大小 // int file_size = lseek(write_fd, 0, SEEK_END); // assert(offset <= file_size); //--计算块索引 int file_block_index = offset / DIRECT_IO_BUFFER_SZ; //--计算缓冲中的索引 write_pos = offset % DIRECT_IO_BUFFER_SZ; //--读入缓冲 lseek(write_fd, file_block_index * DIRECT_IO_BUFFER_SZ, SEEK_SET); int read_ret = read(write_fd, s_rw_buf.write_buf, DIRECT_IO_BUFFER_SZ); assert(read_ret != -1); lseek(write_fd, -1 * read_ret, SEEK_CUR); // int sync_size = 0; // // backup current location // file_locate = lseek(write_fd, 0, SEEK_CUR); // // get the max file size // file_size = lseek(write_fd, 0, SEEK_END); // //DIO_TRACE("size = %d", file_size); // if(!(offset < file_size)){ // // reset pointer // lseek(write_fd, file_locate, SEEK_SET); // errno = ESPIPE; // return -ESPIPE; // } // //update_to_file(); // sync_to_file(); // lseek(write_fd, offset, SEEK_SET); // // very important // sync_size = read_from_file(fd, s_rw_buf.write_buf, DIRECT_IO_BUFFER_SZ); // lseek(write_fd, -1 * sync_size - 1, SEEK_CUR); // write_pos = 0; }else if(DIRECT_IO_READ_FILED == fd){ //移动指针 //--计算文件总大小 int file_block_index_cur = lseek(read_fd, 0, SEEK_CUR) / DIRECT_IO_BUFFER_SZ - 1; // int file_size = lseek(read_fd, 0, SEEK_END); // assert(offset <= file_size); //--计算块索引 int file_block_index = offset / DIRECT_IO_BUFFER_SZ; //--计算缓冲中的索引 read_pos = offset % DIRECT_IO_BUFFER_SZ; if(file_block_index != file_block_index_cur) { //--读入缓冲 lseek(read_fd, file_block_index * DIRECT_IO_BUFFER_SZ, SEEK_SET); int read_ret = read(read_fd, s_rw_buf.read_buf, DIRECT_IO_BUFFER_SZ); assert(read_ret != -1); // DIO_TRACE("read from file,pid=%d,file_block_index=%d,file_block_index_cur=%d" // , getpid(), file_block_index, file_block_index_cur); } // // backup current location // file_locate = lseek(read_fd, 0, SEEK_CUR); // // get the max file size // file_size = lseek(read_fd, 0, SEEK_END); // //DIO_TRACE("size = %d", file_size); // if(!(offset < file_size)){ // // reset pointer // lseek(read_fd, file_locate, SEEK_SET); // errno = ESPIPE; // return -ESPIPE; // } // // seek to object position and update buffer // lseek(read_fd, offset, SEEK_SET); // update_from_file(); }else{ errno = EPERM; return -EPERM; } return 0; } errno = EPERM; return -EPERM; }