- int ret = -1;
- int dirfd = open (path,O_RDONLY); // A handle to speed up
- // chdir
- if (dirfd == -1){
- fprintf (stderr,"Can't open directory %s (%s)\n",path
- ,strerror(errno));
- }else{
- DIR *dir;
-
- fchdir (dirfd);
- dir = opendir (".");
- if (dir == NULL){
- fprintf (stderr,"Can't open (opendir) directory %s (%s)\n",path
- ,strerror(errno));
- }else{
- struct stat dirst;
- struct dirent *ent;
- longlong dirsize = 0;
-
- ret = 0;
- lstat (".",&dirst);
- while ((ent=readdir(dir))!=NULL){
- struct stat st;
- if (lstat(ent->d_name,&st)==-1){
- fprintf (stderr,"Can't stat %s/%s (%s)\n",path
- ,ent->d_name,strerror(errno));
- ret = -1;
- break;
- }else if (S_ISREG(st.st_mode)){
- if (st.st_nlink == 1){
- dirsize += st.st_size;
- }
- }else if (S_ISDIR(st.st_mode) && st.st_dev == dirst.st_dev){
- if (strcmp(ent->d_name,".")!=0
- && strcmp(ent->d_name,"..")!=0){
- char *tmp = malloc(strlen(path) + strlen(ent->d_name) + 2);
- if (tmp==0) ret=-1;
- else {
- strcpy(tmp, path);
- strcat(tmp, "/");
- strcat(tmp, ent->d_name);
- ret = vdu_onedir(tmp,&dirsize);
- free(tmp);
- fchdir (dirfd);
- }
- }
- }
- }
- closedir (dir);
- *size += dirsize;
+ struct stat dirst, st;
+ struct dirent *ent;
+ char *name;
+ DIR *dir;
+ int dirfd;
+ longlong dirsize, dirinodes, dirblocks;
+
+ dirsize = dirinodes = dirblocks = 0;
+
+ // A handle to speed up chdir
+ if ((dirfd = open (path,O_RDONLY)) == -1) {
+ fprintf (stderr,"Can't open directory %s\n",path);
+ panic("open failed");
+ }
+
+ if (fchdir (dirfd) == -1) {
+ fprintf (stderr,"Can't fchdir directory %s\n",path);
+ panic("fchdir failed");
+ }
+
+ if (fstat (dirfd,&dirst) != 0) {
+ fprintf (stderr,"Can't lstat directory %s\n",path);
+ panic("lstat failed");
+ }
+
+ if ((dir = opendir (".")) == NULL) {
+ fprintf (stderr,"Can't open (opendir) directory %s\n",path);
+ panic("opendir failed");
+ }
+
+
+ /* Walk the directory entries and compute the sum of inodes,
+ blocks, and disk space used. This code will recursively descend
+ down the directory structure. */
+
+ while ((ent=readdir(dir))!=NULL){
+ if (lstat(ent->d_name,&st)==-1){
+ fprintf (stderr,"Can't stat %s/%s\n",path,ent->d_name);
+ warning("lstat failed");
+ continue;
+ }
+
+ dirinodes ++;
+
+ if (S_ISREG(st.st_mode)){
+ if (st.st_nlink > 1){
+ long flags;
+ int fd, res;
+
+ if ((fd = open(ent->d_name,O_RDONLY))==-1) {
+ fprintf (stderr,"Can't open file %s/%s\n",path,ent->d_name);
+ warning ("open failed");
+ continue;