/* Deletes NAME from DIR. Returns true if successful, false on failure, which occurs only if there is no file with the given NAME. */ bool dir_remove (struct dir *dir, const char *name) { struct dir_entry e; struct inode *inode = NULL; bool success = false; off_t ofs; ASSERT (dir != NULL); ASSERT (name != NULL); lock_acquire (&dir->l); /* Find directory entry. */ if (!lookup (dir, name, &e, &ofs)) goto done; /* Open inode. */ inode = inode_open (e.inode_sector); if (inode == NULL) goto done; /* If directory, make sure it's empty */ if (inode_is_directory (inode)) { struct dir *inner = dir_open (inode_open (e.inode_sector)); int size = dir_size (inner); dir_close (inner); if (size != 0) goto done; } /* Erase directory entry. */ e.in_use = false; if (inode_write_at (dir->inode, &e, sizeof e, ofs) != sizeof e) goto done; /* Remove inode. */ success = inode_remove (inode); done: lock_release (&dir->l); inode_close (inode); return success; }
/* get the size of all files in a directory */ int dir_size(string file) { mixed **info; int *sizes, size, i, sz; argcheck(file, 1, "string"); info = get_dir(file + "/*"); sizes = info[1]; size = 1; /* 1K for directory itself */ i = sizeof(sizes); while (--i >= 0) { sz = sizes[i]; size += (sz > 0) ? (sz + 1023) >> 10 : (sz == 0) ? 1 : dir_size(file + "/" + info[0][i]); } return size; }
quint64 tf::dir_size(const QString & str) { quint64 sizex = 0; QFileInfo str_info(str); if (str_info.isDir()) { QDir dir(str); QFileInfoList list = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::Hidden | QDir::NoSymLinks | QDir::NoDotAndDotDot); for (int i = 0; i < list.size(); ++i) { QFileInfo fileInfo = list.at(i); if(fileInfo.isDir()) sizex += dir_size(fileInfo.absoluteFilePath()); else sizex += fileInfo.size(); } } return sizex; }
void tf::PTabVolumeInfo::recheck_button_clicked() { float size = dir_size(vp_path->text()); if(size < 1000) vp_size->setText((iim::strprintf("%.1f", size ) + " Bytes").c_str()); else { size /= 1000; if(size < 1000) vp_size->setText((iim::strprintf("%.1f", size ) + " KB").c_str()); else { size /= 1000; if(size < 1000) vp_size->setText((iim::strprintf("%.1f", size ) + " MB").c_str()); else { size /= 1000; if(size < 1000) vp_size->setText((iim::strprintf("%.1f", size ) + " GB").c_str()); } } } }
int list_dir_internal(stralloc* dir, char type) { size_t l; struct dir_s d; stralloc pre; int dtype; int is_dir, is_symlink; size_t len; #if !WINDOWS_NATIVE struct stat st; static dev_t root_dev; #endif char *name, *s; (void)type; while(dir->len > 1 && IS_DIRSEP(dir->s[dir->len - 1])) dir->len--; stralloc_nul(dir); #if !WINDOWS_NATIVE if(root_dev == 0) { if(stat(dir->s, &st) != -1) { root_dev = st.st_dev; } } #endif if(dir_open(&d, dir->s) != 0) { buffer_puts(buffer_2, "ERROR: Opening directory "); buffer_putsa(buffer_2, dir); buffer_puts(buffer_2, " failed!\n"); buffer_flush(buffer_2); goto end; } if(dir->s[dir->len - 1] != DIRSEP_C) stralloc_cats(dir, DIRSEP_S); l = dir->len; while((name = dir_read(&d))) { unsigned int mode = 0, nlink = 0, uid = 0, gid = 0; uint64 size = 0, mtime = 0; dtype = dir_type(&d); dir->len = l; if(str_equal(name, "") || str_equal(name, ".") || str_equal(name, "..")) { continue; } stralloc_readyplus(dir, str_len(name) + 1); str_copy(dir->s + dir->len, name); dir->len += str_len(name); is_symlink = !!(dtype & D_SYMLINK); #if !WINDOWS_NATIVE if(!opt_deref && lstat(dir->s, &st) != -1) { if(root_dev && st.st_dev) { if(st.st_dev != root_dev) { continue; } } } #endif #if !WINDOWS_NATIVE if(S_ISLNK(st.st_mode)) { stat(dir->s, &st); } mode = st.st_mode; #endif if(dtype) { is_dir = !!(dtype & D_DIRECTORY); } else { #if WINDOWS_NATIVE is_dir = 0; #else is_dir = !!S_ISDIR(mode); #endif } if(dtype & D_SYMLINK) is_symlink = 1; #if !WINDOWS_NATIVE nlink = st.st_nlink; uid = st.st_uid; gid = st.st_gid; size = st.st_size; mtime = st.st_mtime; #else mode = (is_dir ? 0040000 : 0100000) | (is_symlink ? 0120000 : 0); #if USE_READDIR if(!is_dir) { size = dir_size(&d); /* dir_INTERNAL(&d)->dir_entry->d_name); */ mtime = dir_time(&d); } else { mtime = 0; size = 0; } #else size = dir_size(&d); mtime = dir_time(&d, D_TIME_MODIFICATION); #endif #endif if(opt_list && size >= opt_minsize) { stralloc_init(&pre); /* Mode string */ mode_str(&pre, mode); stralloc_catb(&pre, " ", 1); /* num links */ make_num(&pre, nlink, 3); stralloc_catb(&pre, " ", 1); /* uid */ make_num(&pre, uid, 0); stralloc_catb(&pre, " ", 1); /* gid */ make_num(&pre, gid, 0); stralloc_catb(&pre, " ", 1); /* size */ make_num(&pre, size, 6); stralloc_catb(&pre, " ", 1); /* time */ make_num(&pre, mtime, 0); /* make_time(&pre, mtime, 10); */ stralloc_catb(&pre, " ", 1); } /* fprintf(stderr, "%d %08x\n", is_dir, dir_ATTRS(&d)); */ if(is_dir) stralloc_catc(dir, opt_separator); if(dir->len > MAX_PATH) { buffer_puts(buffer_2, "ERROR: Directory "); buffer_putsa(buffer_2, dir); buffer_puts(buffer_2, " longer than MAX_PATH (" STRINGIFY(MAX_PATH) ")!\n"); /*buffer_putulong(buffer_2, MAX_PATH); buffer_puts(buffer_2, ")!\n");*/ buffer_flush(buffer_2); goto end; } s = dir->s; len = dir->len; if(len >= 2 && s[0] == '.' && IS_DIRSEP(s[1])) { len -= 2; s += 2; } if(opt_list && size >= opt_minsize) buffer_putsa(buffer_1, &pre); if(opt_relative_to) { size_t sz = str_len(opt_relative_to); if(str_diffn(s, opt_relative_to, sz) == 0) { s += sz; len -= sz; while(*s == '\\' || *s == '/') { s++; len--; } } } if(size >= opt_minsize) { buffer_put(buffer_1, s, len); buffer_put(buffer_1, "\n", 1); buffer_flush(buffer_1); } if(is_dir && (opt_deref || !is_symlink)) { dir->len--; list_dir_internal(dir, 0); } } end: dir_close(&d); return 0; }
/* perform integrity check on instances directory, including the cache: * remove any files from non-running Eucalyptus instances, delete files * from cache that are not complete, return the amount of bytes used up by * everything */ long long scFSCK (bunchOfInstances ** instances) { long long total_size = 0; struct stat mystat; if (instances==NULL) return -1; logprintfl (EUCAINFO, "checking the integrity of instances directory (%s)\n", sc_instance_path); /* let us not 'rm -rf /' accidentally */ if (strlen(sc_instance_path)<2 || sc_instance_path[0]!='/' ) { logprintfl (EUCAFATAL, "error: instances directory cannot be /, sorry\n"); return -1; } if (stat (sc_instance_path, &mystat) < 0) { logprintfl (EUCAFATAL, "error: could not stat %s\n", sc_instance_path); return -1; } total_size += mystat.st_size; DIR * insts_dir; if ((insts_dir=opendir(sc_instance_path))==NULL) { logprintfl (EUCAFATAL, "error: could not open instances directory %s\n", sc_instance_path); return -1; } /*** run through all users ***/ char * cache_path = NULL; struct dirent * inst_dir_entry; while ((inst_dir_entry=readdir(insts_dir))!=NULL) { char * uname = inst_dir_entry->d_name; char user_path [BUFSIZE]; struct dirent * user_dir_entry; DIR * user_dir; if (!strcmp(".", uname) || !strcmp("..", uname)) continue; snprintf (user_path, BUFSIZE, "%s/%s", sc_instance_path, uname); if ((user_dir=opendir(user_path))==NULL) { logprintfl (EUCAWARN, "warning: unopeneable directory %s\n", user_path); continue; } /*** run through all instances of a user ***/ while ((user_dir_entry=readdir(user_dir))!=NULL) { char * iname = user_dir_entry->d_name; if (!strcmp(".", iname) || !strcmp("..", iname)) continue; char instance_path [BUFSIZE]; snprintf (instance_path, BUFSIZE, "%s/%s", user_path, iname); if (!strcmp("cache", iname) && !strcmp(EUCALYPTUS_ADMIN, uname)) { /* cache is in admin's dir */ if (cache_path) { logprintfl (EUCADEBUG, "Found a second cache_path?\n"); free(cache_path); } cache_path = strdup (instance_path); continue; } /* spare directories of running instances, but count their usage */ if (find_instance (instances, iname)) { long long bytes = dir_size (instance_path); if (bytes>0) { logprintfl (EUCAINFO, "- running instance %s directory, size=%d\n", iname, bytes); total_size += bytes; } else if (bytes==0) { logprintfl (EUCAWARN, "warning: empty instance directory %s\n", instance_path); } else { logprintfl (EUCAWARN, "warning: non-standard instance directory %s\n", instance_path); } continue; } /* looks good - destroy it */ if (vrun ("rm -rf %s", instance_path)) { logprintfl (EUCAWARN, "warning: failed to remove %s\n", instance_path); } } closedir (user_dir); } closedir (insts_dir); /*** scan the cache ***/ long long cache_bytes = init_cache (cache_path); free (cache_path); if (cache_bytes < 0) { return -1; } return total_size + cache_bytes; }
BOOST_FOREACH(const std::string& dir_path, dirs) { // FIXME: this could result in infinite recursion with symlinks!! res += dir_size(dir_path); }
int main(int argc, char * const argv[]) { std::string dir("/var/log/messages"); size_t max_dir_size = 2000*1024; size_t max_file_size = 200*1024; if(max_dir_size < max_file_size) throw std::logic_error("Files size are inconsistant. max_dir must be > max_file"); size_t boot_count = 0; bool boot_count_set = false; bool single_rotate_mode = false; bool output_filename_mode = false; int opt; const char * opts = "O:s:d:n:fg"; while((opt = getopt(argc, argv, opts)) != -1) { switch(opt) { case 'O': dir = std::string(optarg); break; case 's': max_file_size = std::stoi(optarg)*1024; break; case 'd': max_dir_size = std::stoi(optarg)*1024; case 'n': boot_count = std::stoi(optarg); boot_count_set = true; break; case 'f': single_rotate_mode = true; break; case 'g': output_filename_mode = true; break; default: help(std::cerr); exit(EXIT_FAILURE); break; } } if(!boot_count_set) { std::cerr << "Invalid configuration.\n"; help(std::cerr); exit(EXIT_FAILURE); } log_pattern default_log; default_log.boot_count = boot_count; default_log.prefix = "log"; std::stringstream ss; ss << dir << '/' << default_log.active_name(); std::string const full_path = ss.str(); if(single_rotate_mode) { rotate(dir, boot_count); } else if(output_filename_mode) { std::cout << full_path << '\n'; } else { std::cout << "Managing " << full_path << " in dir " << dir << " with maxsize " << max_file_size << " and " << max_dir_size << '\n'; while(1) { { //Open a new scope for RAII. We want the ifd destructor to close the file descriptors. inotify_fd ifd(full_path, IN_CLOSE); ifd.wait(); } if(file_size(full_path) > max_file_size) { std::cout << "File is too big. Rotating\n"; rotate(dir, boot_count); } while(dir_size(dir) > max_dir_size) { std::cout << "Directory is too big. Rotating\n"; delete_oldest(dir); } } } }