lib/Filesys/DiskUsage/Fast.xs

filter: Statementstmt 70.9% 39 / 55 Branchbran 68.4% 26 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 69.8% 65 / 93 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Filesys/DiskUsage/Fast.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 #include "ppport.h"
6  
7 #include <stdio.h>
8 #include <dirent.h>
9 #include <string.h>
10 #include <sys/stat.h>
11 #include <errno.h>
12 #include <unistd.h>
13  
14 #define PACKAGE_NAME "Filesys::DiskUsage::Fast"
15  
16 static char current_dir[MAXPATHLEN] = ".";
17 static char parent_dir[MAXPATHLEN] = "..";
18  
19 static unsigned long long total = 0;
20 unsigned int debug = 0;
21 unsigned int show_warnings = 1;
22 unsigned int sector_size = 0;
23  
24 25896 unsigned long long get_file_size( char *filename ){
25 struct stat stat_buf;
26 char cwd[MAXPATHLEN];
27 unsigned long long result;
28
29 25896 ▶ if( lstat( filename, &stat_buf ) == 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( lstat( filename, &stat_buf ) == 0 ){ 25896 0 25896
30 // ok
31 25896 ▶ if( S_ISLNK( stat_buf.st_mode ) ){
Branch: 1 branch
BranchTrueFalseTotal
if( S_ISLNK( stat_buf.st_mode ) ){ 10200 15696 25896
32 10200 return 0;
33 }
34 else{
35 15696 ▶ if( sector_size > 1 ){
Branch: 1 branch
BranchTrueFalseTotal
if( sector_size > 1 ){ 499 15197 15696
36 // sector
37 499 result = sector_size - 1 + (unsigned long long)stat_buf.st_size;
38 499 result -= result % sector_size;
39
40 499 return result;
41 }
42 else{
43 // real
44 15197 return (unsigned long long)stat_buf.st_size;
45 }
46 }
47 }
48 else{
49 // not ok
50 0 ▶ if( show_warnings ){
Branch: 1 branch
BranchTrueFalseTotal
if( show_warnings ){ 0 0 0
51 0 memset( cwd, '\0', MAXPATHLEN );
52 0 getcwd( cwd, MAXPATHLEN );
53 0 warn( "could not read file %s%s (%s)", cwd, filename, strerror( errno ) );
54 }
55 0 return 0;
56 }
57 }
58  
59 1378 void get_dir_size( char *dirname ){
60 DIR *dir;
61 struct dirent *dp;
62 char newfile[MAXPATHLEN];
63 1378 unsigned long long count = 0;
64 char cwd[MAXPATHLEN];
65 #if defined (__SVR4) && defined (__sun)
66 struct stat s;
67 #endif
68
69 // open dir
70 1378 ▶ if( ( dir = opendir( dirname ) ) == NULL ){
Branch: 1 branch
BranchTrueFalseTotal
if( ( dir = opendir( dirname ) ) == NULL ){ 24 1354 1378
71 24 ▶ if( show_warnings ){
Branch: 1 branch
BranchTrueFalseTotal
if( show_warnings ){ 0 24 24
72 0 memset( cwd, '\0', MAXPATHLEN );
73 0 getcwd( cwd, MAXPATHLEN );
74 0 warn( "could not open dir %s%s (%s)", cwd, dirname, strerror( errno ) );
75 }
76 24 return;
77 }
78
79 1354 ▶ if( chdir( dirname ) == -1 ){
Branch: 1 branch
BranchTrueFalseTotal
if( chdir( dirname ) == -1 ){ 0 1354 1354
80 0 ▶ if( show_warnings )
Branch: 1 branch
BranchTrueFalseTotal
if( show_warnings ) 0 0 0
81 0 warn( "could not chdir %s (%s)", dirname, strerror( errno ) );
82 0 return;
83 }
84
85 31268 ▶ for( dp = readdir( dir ); dp != NULL; dp = readdir( dir ) ){
Branch: 1 branch
BranchTrueFalseTotal
for( dp = readdir( dir ); dp != NULL; dp = readdir( dir ) ){ 29914 1354 31268
86 #if defined (__SVR4) && defined (__sun)
87 stat( dp->d_name, &s );
88 if( S_ISDIR( s.st_mode ) ){
89 #else
90 29914 ▶ if( dp->d_type == DT_DIR ){
Branch: 1 branch
BranchTrueFalseTotal
if( dp->d_type == DT_DIR ){ 4028 25886 29914
91 #endif
92 // dir
93
94 // omit . and ..
95 4028 ▶ if( strcmp( dp->d_name, current_dir ) == 0 || strcmp( dp->d_name, parent_dir ) == 0 ){
Branch: 2 branches
BranchTrueFalseTotal
if( strcmp( dp->d_name, current_dir ) == 0 || strcmp( dp->d_name, parent_dir ) == 0 ){ 2674 1354 4028
if( strcmp( dp->d_name, current_dir ) == 0 || strcmp( dp->d_name, parent_dir ) == 0 ){ 1354 1320 2674
96 2708 continue;
97 }
98
99 1320 get_dir_size( dp->d_name );
100 }
101 else{
102 // file (or fifo, symlink etc.)
103 25886 count = get_file_size( dp->d_name );
104
105 25886 ▶ if( debug )
Branch: 1 branch
BranchTrueFalseTotal
if( debug ) 0 25886 25886
106 0 fprintf( stderr, "%s: %lld\n", dp->d_name, count );
107
108 25886 total += count;
109 }
110 }
111 1354 closedir( dir );
112
113 1354 free( dp );
114
115 1354 ▶ if( chdir("..") == -1 ){
Branch: 1 branch
BranchTrueFalseTotal
if( chdir("..") == -1 ){ 0 1354 1354
116 0 ▶ if( show_warnings )
Branch: 1 branch
BranchTrueFalseTotal
if( show_warnings ) 0 0 0
117 0 warn( "could not chdir to %s (%s)", "..", strerror( errno ) );
118 0 return;
119 }
120 }
121  
122 MODULE = Filesys::DiskUsage::Fast PACKAGE = Filesys::DiskUsage::Fast
123  
124 PROTOTYPES: ENABLE
125  
126 UV
127 du(...)
128 PROTOTYPE: @
129 CODE:
130 int index;
131 char *filepath;
132 struct stat stat_buf;
133 // char string[25];
134 // STRLEN length;
135
136 85 ▶ if( ! items ){
Branch: 1 branch
BranchTrueFalseTotal
if( ! items ){ 0 85 85
137 // RETVAL = SvUV( newSVpv("0", 1) );
138 0 RETVAL = PTR2UV(0);
139 }
140 else{
141 85 total = 0;
142 85 debug = SvTRUE(GvSV(gv_fetchpv(form("%s::Debug", PACKAGE_NAME), TRUE, SVt_PV))) ? 1 : 0;
143 85 show_warnings = SvTRUE(GvSV(gv_fetchpv(form("%s::ShowWarnings", PACKAGE_NAME), TRUE, SVt_PV))) ? 1 : 0;
144 85 sector_size = SvUV(GvSV(gv_fetchpv(form("%s::SectorSize", PACKAGE_NAME), TRUE, SVt_PV)));
145
146 183 ▶ for( index = 0 ; index < items ; index++ ){
Branch: 1 branch
BranchTrueFalseTotal
for( index = 0 ; index < items ; index++ ){ 98 85 183
147 98 SV *stacksv = ST(index);
148 98 filepath = SvPV_nolen(stacksv);
149
150 98 ▶ if( stat( filepath, &stat_buf ) == 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if( stat( filepath, &stat_buf ) == 0 ){ 68 30 98
151 68 ▶ if( S_ISDIR( stat_buf.st_mode ) ){
Branch: 1 branch
BranchTrueFalseTotal
if( S_ISDIR( stat_buf.st_mode ) ){ 58 10 68
152 58 get_dir_size( filepath );
153 }
154 // else if( S_ISLNK( stat_buf.st_mode ) ){
155 // ;
156 // }
157 else{
158 10 total += get_file_size( filepath );
159 }
160 }
161 }
162
163 // see: http://groups.google.com/group/perl.xs/browse_thread/thread/27e7881105eb4329
164 // length = sprintf(string, "%llu", total);
165 // RETVAL = SvUV( newSVpv(string, length) );
166 85 RETVAL = PTR2UV(total);
167 }
168 OUTPUT:
169 RETVAL
↑ Summary blib/lib/Filesys/DiskUsage/Fast.pm »