Переглянути джерело

can calculate folder sizes

Kevin Bradley 1 тиждень тому
батько
коміт
4b9e8b90f6

+ 14 - 8
bootstrapTool.xcodeproj/project.pbxproj

@@ -15,6 +15,7 @@
 		325A24BF20E83F95001D1BAD /* NSObject+Additions.m in Sources */ = {isa = PBXBuildFile; fileRef = 325A24B220E83EF8001D1BAD /* NSObject+Additions.m */; };
 		325A24C020E83F97001D1BAD /* NSString+Additions.m in Sources */ = {isa = PBXBuildFile; fileRef = 325A24B420E83EF8001D1BAD /* NSString+Additions.m */; };
 		325A24C120E83FCA001D1BAD /* InputPackageFile.m in Sources */ = {isa = PBXBuildFile; fileRef = 325A24B820E83EF8001D1BAD /* InputPackageFile.m */; };
+		32A1E8702D1F56110004A28A /* NSFileManager+Size.m in Sources */ = {isa = PBXBuildFile; fileRef = 32A1E86F2D1F56110004A28A /* NSFileManager+Size.m */; };
 		32B2C838236BC8270085F065 /* Universalize.m in Sources */ = {isa = PBXBuildFile; fileRef = 32B2C837236BC8270085F065 /* Universalize.m */; };
 		4C52701C2ACA7ECE0052FCAF /* strrep.c in Sources */ = {isa = PBXBuildFile; fileRef = 4C52701B2ACA7ECE0052FCAF /* strrep.c */; };
 		4C56B59D29E670DD00D47844 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C56B59C29E670DD00D47844 /* main.m */; };
@@ -81,6 +82,8 @@
 		325A24BA20E83F46001D1BAD /* ErrorReturn.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ErrorReturn.h; sourceTree = "<group>"; };
 		325A24BB20E83F46001D1BAD /* ErrorReturn.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ErrorReturn.m; sourceTree = "<group>"; };
 		325A24C220E84033001D1BAD /* Defines.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = Defines.h; path = Classes/Defines.h; sourceTree = "<group>"; };
+		32A1E86E2D1F56110004A28A /* NSFileManager+Size.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSFileManager+Size.h"; sourceTree = "<group>"; };
+		32A1E86F2D1F56110004A28A /* NSFileManager+Size.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSFileManager+Size.m"; sourceTree = "<group>"; };
 		32B2C837236BC8270085F065 /* Universalize.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Universalize.m; sourceTree = "<group>"; };
 		32B2C839236D1BD40085F065 /* Universalize.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Universalize.h; sourceTree = "<group>"; };
 		4C52701A2ACA7ECE0052FCAF /* strrep.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = strrep.h; sourceTree = "<group>"; };
@@ -152,24 +155,26 @@
 		325A24AC20E83EF8001D1BAD /* Classes */ = {
 			isa = PBXGroup;
 			children = (
-				32B2C839236D1BD40085F065 /* Universalize.h */,
-				32B2C837236BC8270085F065 /* Universalize.m */,
 				325A24BA20E83F46001D1BAD /* ErrorReturn.h */,
 				325A24BB20E83F46001D1BAD /* ErrorReturn.m */,
-				325A24AD20E83EF8001D1BAD /* StatusPackageModel.h */,
-				325A24AE20E83EF8001D1BAD /* StatusPackageModel.m */,
 				325A24AF20E83EF8001D1BAD /* HelperClass.h */,
 				325A24B020E83EF8001D1BAD /* HelperClass.m */,
-				325A24B120E83EF8001D1BAD /* NSObject+Additions.h */,
-				325A24B220E83EF8001D1BAD /* NSObject+Additions.m */,
-				325A24B320E83EF8001D1BAD /* NSString+Additions.h */,
-				325A24B420E83EF8001D1BAD /* NSString+Additions.m */,
 				325A24B520E83EF8001D1BAD /* InputPackage.h */,
 				325A24B620E83EF8001D1BAD /* InputPackage.m */,
 				325A24B720E83EF8001D1BAD /* InputPackageFile.h */,
 				325A24B820E83EF8001D1BAD /* InputPackageFile.m */,
+				32A1E86E2D1F56110004A28A /* NSFileManager+Size.h */,
+				32A1E86F2D1F56110004A28A /* NSFileManager+Size.m */,
+				325A24B120E83EF8001D1BAD /* NSObject+Additions.h */,
+				325A24B220E83EF8001D1BAD /* NSObject+Additions.m */,
+				325A24B320E83EF8001D1BAD /* NSString+Additions.h */,
+				325A24B420E83EF8001D1BAD /* NSString+Additions.m */,
+				325A24AD20E83EF8001D1BAD /* StatusPackageModel.h */,
+				325A24AE20E83EF8001D1BAD /* StatusPackageModel.m */,
 				4C52701A2ACA7ECE0052FCAF /* strrep.h */,
 				4C52701B2ACA7ECE0052FCAF /* strrep.c */,
+				32B2C839236D1BD40085F065 /* Universalize.h */,
+				32B2C837236BC8270085F065 /* Universalize.m */,
 			);
 			path = Classes;
 			sourceTree = "<group>";
@@ -305,6 +310,7 @@
 				325A24BD20E83F7E001D1BAD /* InputPackage.m in Sources */,
 				325A24BC20E83F46001D1BAD /* ErrorReturn.m in Sources */,
 				325A24B920E83F09001D1BAD /* HelperClass.m in Sources */,
+				32A1E8702D1F56110004A28A /* NSFileManager+Size.m in Sources */,
 				325A24C120E83FCA001D1BAD /* InputPackageFile.m in Sources */,
 				325A24BF20E83F95001D1BAD /* NSObject+Additions.m in Sources */,
 				4C52701C2ACA7ECE0052FCAF /* strrep.c in Sources */,

BIN
bootstrapTool.xcodeproj/project.xcworkspace/xcuserdata/kevinbradley.xcuserdatad/UserInterfaceState.xcuserstate


+ 10 - 0
bootstrapTool.xcodeproj/xcuserdata/kevinbradley.xcuserdatad/xcschemes/xcschememanagement.plist

@@ -9,6 +9,16 @@
 			<key>orderHint</key>
 			<integer>0</integer>
 		</dict>
+		<key>chartest.xcscheme_^#shared#^_</key>
+		<dict>
+			<key>orderHint</key>
+			<integer>2</integer>
+		</dict>
+		<key>universalize.xcscheme_^#shared#^_</key>
+		<dict>
+			<key>orderHint</key>
+			<integer>1</integer>
+		</dict>
 	</dict>
 	<key>SuppressBuildableAutocreation</key>
 	<dict>

+ 2 - 0
bootstrapTool/Classes/Defines.h

@@ -7,6 +7,7 @@
 #import "InputPackage.h"
 #import "InputPackageFile.h"
 #import "StatusPackageModel.h"
+#import "NSFileManager+Size.h"
 
 typedef NS_ENUM(NSInteger, BSPackageFileType)
 {
@@ -25,4 +26,5 @@ typedef NS_ENUM(NSInteger, BSPackageFileType)
 
 #define DLog(format, ...) CFShow((__bridge CFStringRef)[NSString stringWithFormat:format, ## __VA_ARGS__]);
 #define FM [NSFileManager defaultManager]
+#define FANCY_BYTES(B) [NSByteCountFormatter stringFromByteCount:B countStyle:NSByteCountFormatterCountStyleFile]
 #endif

+ 5 - 0
bootstrapTool/Classes/NSFileManager+Size.h

@@ -0,0 +1,5 @@
+@interface NSFileManager(Util)
++ (NSUInteger)sizeForFolderAtPath:(NSString *)source;
++ (CGFloat)availableSpaceForPath:(NSString *)source;
++ (void)ls:(const char *)name completion:(void(^)(NSInteger size, NSInteger count))block;
+@end

+ 114 - 0
bootstrapTool/Classes/NSFileManager+Size.m

@@ -0,0 +1,114 @@
+
+#import "NSFileManager+Size.h"
+#include <sys/stat.h>
+#include <dirent.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+@implementation NSFileManager(Util)
+
+//https://stackoverflow.com/questions/50105231/how-do-i-recursively-go-through-folders-and-count-total-file-size
+
+long long do_ls(const char *name) {
+    DIR *dir_ptr;
+    struct dirent *direntp;
+    struct stat info;
+    long long total = 0;
+    if (stat(name, &info)) {
+        fprintf(stderr, "ls01: cannot stat %s\n", name);
+        return 0;
+    }
+    if (S_ISDIR(info.st_mode)) {
+        if ((dir_ptr = opendir(name)) == NULL) {
+            fprintf(stderr, "ls01: cannot open directory %s\n", name);
+        } else {
+            while ((direntp = readdir(dir_ptr)) != NULL) {
+                char *pathname;
+                
+                /* ignore current and parent directories */
+                if (!strcmp(direntp->d_name, ".") || !strcmp(direntp->d_name, ".."))
+                    continue;
+                
+                pathname = malloc(strlen(name) + 1 + strlen(direntp->d_name) + 1);
+                if (pathname == NULL) {
+                    fprintf(stderr, "ls01: cannot allocated memory\n");
+                    exit(1);
+                }
+                sprintf(pathname, "%s/%s", name, direntp->d_name);
+                total += do_ls(pathname);
+                free(pathname);
+            }
+            closedir(dir_ptr);
+        }
+    } else {
+        total = info.st_size;
+    }
+    //printf("file count: %i\n", fileCount);
+    //printf("%10lld  %s\n", total, name);
+    return total;
+}
+
+//same code but with a completion block to offer a non-blocking solution
+
++ (void)ls:(const char *)name completion:(void(^)(NSInteger size, NSInteger count))block {
+    DIR *dir_ptr;
+    struct dirent *direntp;
+    struct stat info;
+    __block long long total = 0;
+    __block int fileCount = 0;
+    if (stat(name, &info)) {
+        fprintf(stderr, "ls01: cannot stat %s\n", name);
+        return;
+    }
+    if (S_ISDIR(info.st_mode)) {
+        if ((dir_ptr = opendir(name)) == NULL) {
+            fprintf(stderr, "ls01: cannot open directory %s\n", name);
+        } else {
+            while ((direntp = readdir(dir_ptr)) != NULL) {
+                char *pathname;
+
+                /* ignore current and parent directories */
+                if (!strcmp(direntp->d_name, ".") || !strcmp(direntp->d_name, ".."))
+                    continue;
+
+                pathname = malloc(strlen(name) + 1 + strlen(direntp->d_name) + 1);
+                if (pathname == NULL) {
+                    fprintf(stderr, "ls01: cannot allocate memory\n");
+                    exit(1);
+                }
+                sprintf(pathname, "%s/%s", name, direntp->d_name);
+                [self ls:pathname completion:^(NSInteger size, NSInteger count) {
+                    total+=size;
+                    fileCount+=count;
+                }];
+                free(pathname);
+            }
+            closedir(dir_ptr);
+        }
+    } else {
+        total = info.st_size;
+        fileCount++;
+    }
+    //printf("file count: %i\n", fileCount);
+    //printf("%10lld  %s\n", total, name);
+    if (block) {
+        block(total,fileCount);
+    }
+}
+
++ (NSUInteger)sizeForFolderAtPath:(NSString *)source {
+    return do_ls([source UTF8String]);
+}
+
++ (CGFloat)availableSpaceForPath:(NSString *)source {
+    NSError *error = nil;
+    NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfFileSystemForPath:source error:&error];
+    if (error) {
+        //DLog(@"error: %@", error);
+        attrs = [[NSFileManager defaultManager] attributesOfFileSystemForPath:@"." error:&error];
+    }
+    return [[attrs objectForKey:NSFileSystemFreeSize] floatValue];
+}
+
+@end

+ 14 - 1
bootstrapTool/main.m

@@ -16,7 +16,7 @@
 #import <sys/utsname.h>
 #import "HelperClass.h"
 
-#define OPTION_FLAGS "o:i:ld:hcrbp:su:yn:a:Ce:fL:UP"
+#define OPTION_FLAGS "o:i:ld:hcrbp:su:yn:a:Ce:fL:UPS:"
 
 char *progname;
 char *path;
@@ -42,6 +42,7 @@ static struct option longopts[] = {
     { "contents",                  required_argument,      NULL,   'L' },
     { "unpack",                    no_argument,            NULL,   'U' },
     { "passive",                   no_argument,            NULL,   'P' },
+    { "size",                      required_argument,      NULL,   'S' },
     { NULL,                        0,                      NULL,    0  }
 };
 
@@ -86,6 +87,7 @@ int main(int argc, char **argv) {
         NSString *packagesFolder = nil;
         NSString *arch = nil;
         NSString *excludeFile = nil;
+        NSString *targetFolder = nil;
         BOOL listPackage = FALSE;
         BOOL repackage = FALSE;
         BOOL skip = FALSE;
@@ -190,6 +192,10 @@ int main(int argc, char **argv) {
                 case 'P':
                     passive = TRUE;
                     break;
+                
+                case 'S':
+                    targetFolder = [NSString stringWithUTF8String:optarg];
+                    break;
                     
                 default:
                     cmd_help();
@@ -203,6 +209,13 @@ int main(int argc, char **argv) {
             //yDLog(@"bootstrap path: %@?", bootstrapPath);
             //return 0;
         }
+        
+        if (targetFolder) { //determine & print folder size
+            NSString *output = FANCY_BYTES([NSFileManager sizeForFolderAtPath:targetFolder]);
+            DLog(@"%@: %@", targetFolder, output);
+            return 0;
+        }
+        
         if (up) {
             NSArray *contents = [FM contentsOfDirectoryAtPath:up error:nil];
             NSString *newFile = [[up lastPathComponent] stringByAppendingPathExtension:@"plist"];