Fossil SCM

Add the file_directory_size() utility function for measuring the number of objects in a directory.

drh 2018-06-25 20:26 trunk
Commit 94e846d282b0f7af94969b3b3f50bd46f6b8cabfffa7c9676bbdd091257edaa8
1 file changed +48
+48
--- src/file.c
+++ src/file.c
@@ -1614,5 +1614,53 @@
16141614
#else
16151615
while( z[0]=='/' && z[1]=='/' ) z++;
16161616
#endif
16171617
return z;
16181618
}
1619
+
1620
+/*
1621
+** Count the number of objects (files and subdirectores) in a given
1622
+** directory. Return the count. Return -1 of the object is not a
1623
+** directory.
1624
+*/
1625
+int file_directory_size(const char *zDir, const char *zGlob, int omitDotFiles){
1626
+ char *zNative;
1627
+ DIR *d;
1628
+ int n = -1;
1629
+ zNative = fossil_utf8_to_path(zDir,1);
1630
+ d = opendir(zNative);
1631
+ if( d ){
1632
+ struct dirent *pEntry;
1633
+ n = 0;
1634
+ while( (pEntry=readdir(d))!=0 ){
1635
+ if( pEntry->d_name[0]==0 ) continue;
1636
+ if( omitDotFiles && pEntry->d_name[0]=='.' ) continue;
1637
+ if( zGlob && sqlite3_strglob(zGlob, pEntry->d_name)!=0 ) continue;
1638
+ n++;
1639
+ }
1640
+ closedir(d);
1641
+ }
1642
+ fossil_path_free(zNative);
1643
+ return n;
1644
+}
1645
+
1646
+/*
1647
+** COMMAND: test-dir-size
1648
+**
1649
+** Usage: %fossil test-dir-size NAME [GLOB] [--nodots]
1650
+**
1651
+** Return the number of objects in the directory NAME. If GLOB is
1652
+** provided, then only count objects that match the GLOB pattern.
1653
+** if --nodots is specified, omit files that begin with ".".
1654
+*/
1655
+void test_dir_size_cmd(void){
1656
+ int omitDotFiles = find_option("nodots",0,0)!=0;
1657
+ const char *zGlob;
1658
+ const char *zDir;
1659
+ verify_all_options();
1660
+ if( g.argc!=3 && g.argc!=4 ){
1661
+ usage("NAME [GLOB] [-nodots]");
1662
+ }
1663
+ zDir = g.argv[2];
1664
+ zGlob = g.argc==4 ? g.argv[3] : 0;
1665
+ fossil_print("%d\n", file_directory_size(zDir, zGlob, omitDotFiles));
1666
+}
16191667
--- src/file.c
+++ src/file.c
@@ -1614,5 +1614,53 @@
1614 #else
1615 while( z[0]=='/' && z[1]=='/' ) z++;
1616 #endif
1617 return z;
1618 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1619
--- src/file.c
+++ src/file.c
@@ -1614,5 +1614,53 @@
1614 #else
1615 while( z[0]=='/' && z[1]=='/' ) z++;
1616 #endif
1617 return z;
1618 }
1619
1620 /*
1621 ** Count the number of objects (files and subdirectores) in a given
1622 ** directory. Return the count. Return -1 of the object is not a
1623 ** directory.
1624 */
1625 int file_directory_size(const char *zDir, const char *zGlob, int omitDotFiles){
1626 char *zNative;
1627 DIR *d;
1628 int n = -1;
1629 zNative = fossil_utf8_to_path(zDir,1);
1630 d = opendir(zNative);
1631 if( d ){
1632 struct dirent *pEntry;
1633 n = 0;
1634 while( (pEntry=readdir(d))!=0 ){
1635 if( pEntry->d_name[0]==0 ) continue;
1636 if( omitDotFiles && pEntry->d_name[0]=='.' ) continue;
1637 if( zGlob && sqlite3_strglob(zGlob, pEntry->d_name)!=0 ) continue;
1638 n++;
1639 }
1640 closedir(d);
1641 }
1642 fossil_path_free(zNative);
1643 return n;
1644 }
1645
1646 /*
1647 ** COMMAND: test-dir-size
1648 **
1649 ** Usage: %fossil test-dir-size NAME [GLOB] [--nodots]
1650 **
1651 ** Return the number of objects in the directory NAME. If GLOB is
1652 ** provided, then only count objects that match the GLOB pattern.
1653 ** if --nodots is specified, omit files that begin with ".".
1654 */
1655 void test_dir_size_cmd(void){
1656 int omitDotFiles = find_option("nodots",0,0)!=0;
1657 const char *zGlob;
1658 const char *zDir;
1659 verify_all_options();
1660 if( g.argc!=3 && g.argc!=4 ){
1661 usage("NAME [GLOB] [-nodots]");
1662 }
1663 zDir = g.argv[2];
1664 zGlob = g.argc==4 ? g.argv[3] : 0;
1665 fossil_print("%d\n", file_directory_size(zDir, zGlob, omitDotFiles));
1666 }
1667

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button