static int construct_file_queue ( int status_dir_index, int log_dir_index ) { DIR *status_dirp; struct dirent *dp; struct stat sb; char name[256]; char path[256]; status_dirp = opendir (status_dirs[status_dir_index].c_str ()); if ( status_dirp == NULL ) { return - 1; } for ( ; ; ) { dp = readdir (status_dirp); if ( dp == NULL ) { break; } if ( strcmp (dp->d_name, ".") == 0 || strcmp (dp->d_name, "..") == 0 || strcmp (dp->d_name, "total_status.log") == 0 ) { continue; } sprintf (name, "%s/%s", status_dirs[status_dir_index].c_str (), dp->d_name); sprintf (path, "%s/%s", log_dirs[log_dir_index].c_str (), dp->d_name); int fd = open (name, O_RDWR); if ( fd == - 1 ) { continue; } fstat (fd, &sb); if ( sb.st_size != 10000000 ) { continue; } char *status = ( char * ) mmap (NULL, sb.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); close (fd); std::string bitmap_str (( status + 3 * sizeof (uint32_t ) ), sb.st_size - 3 * sizeof (uint32_t )); std::bitset<FILE_BIT_MAX> bitmap (bitmap_str); File *file = new File (path, bitmap); file->set_index (status_dir_index); file->set_addr (status); std::string status_path (name); file->set_status_path (status_path); file_map[path] = file; file_queue[status_dir_index].push_back (file); } closedir (status_dirp); return 0; }
static void construct_file_queues ( const char *status_dir, const char *dir ) { DIR *dirp; struct dirent *dp; dirp = opendir (dir); if ( dirp == NULL ) return; struct stat sb; for ( ; ; ) { dp = readdir (dirp); if ( dp == NULL ) { break; } if ( strcmp (dp->d_name, ".") == 0 || strcmp (dp->d_name, "..") == 0 ) { continue; } std::string path (dir); path.append (dp->d_name); lstat (path.c_str (), &sb); if ( ! S_ISDIR (sb.st_mode) ) { continue; } std::string log_path (dir); log_path.append (dp->d_name); std::string status_path (status_dir); status_path.append (dp->d_name); hosts.push_back (dp->d_name); log_dirs.push_back (log_path); mkdir (status_path.c_str (), S_IRWXU | S_IRWXG | S_IRWXO); status_dirs.push_back (status_path); std::string status_total_file (status_path); status_total_file.append ("/total_status.log"); int fd = open (status_total_file.c_str (), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); fstat (fd, &sb); int status_file_size = sb.st_size; if ( status_file_size == 0 ) { const char *buf = "0000000000000000"; write (fd, ( void * ) buf, 2 * sizeof (uint64_t )); } char *addr = ( char * ) mmap (NULL, 2 * sizeof (uint64_t ), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if ( addr == MAP_FAILED ) { return; } if ( status_file_size == 0 ) { *( uint64_t * ) addr = 0; *( uint64_t * ) ( addr + sizeof (uint64_t ) ) = 0; } else { uint64_t has_sync = * ( uint64_t * ) ( addr + sizeof (uint64_t ) ); *( uint64_t * ) addr = has_sync; } close (fd); total_status_addrs.push_back (addr); std::deque<File *> files; file_queue.push_back (files); int *pipe_fd = ( int * ) calloc (2, sizeof (int )); pipe (pipe_fd); pipe_fds.push_back (pipe_fd); } closedir (dirp); int size = status_dirs.size (); for ( int i = 0; i < size; i ++ ) { construct_file_queue (i, i); } }