Skip to content

Commit f749a18

Browse files
Compress all debug menu exports
1 parent d3e9b70 commit f749a18

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

Monal/Classes/DebugView.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,21 @@ struct LogFilesView: View {
4848
Section(header: Text("Logfiles")) {
4949
ForEach(sortedLogFileInfos, id: \.self) { logFileInfo in
5050
Button(logFileInfo.fileName) {
51-
fileURL = URL(fileURLWithPath: logFileInfo.filePath)
51+
self.fileURL = HelperTools.compressFile(atPath:logFileInfo.filePath, withLevel:9)
5252
}
5353
}
5454
}
5555
Section(header: Text("Database Files")) {
5656
Button("Main Database") {
5757
if let dbFile = DataLayer.sharedInstance().exportDB() {
58-
self.fileURL = URL(fileURLWithPath: dbFile)
58+
self.fileURL = HelperTools.compressFile(atPath:dbFile, withLevel:4)
5959
} else {
6060
showingDBExportFailedAlert = true
6161
}
6262
}
6363
Button("IPC Database") {
6464
if let dbFile = HelperTools.exportIPCDatabase() {
65-
self.fileURL = URL(fileURLWithPath: dbFile)
65+
self.fileURL = HelperTools.compressFile(atPath:dbFile, withLevel:4)
6666
} else {
6767
showingDBExportFailedAlert = true
6868
}

Monal/Classes/HelperTools.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ void swizzle(Class c, SEL orig, SEL new);
231231

232232
+(void) updateCurrentLogfilePath:(NSString*) logfilePath;
233233

234+
+(NSURL* _Nullable) compressFileAtPath:(NSString*) path withLevel:(NSInteger) level;
235+
234236
@end
235237

236238
NS_ASSUME_NONNULL_END

Monal/Classes/HelperTools.m

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <objc/runtime.h>
2121
#include <objc/message.h>
2222
#include <objc/objc-exception.h>
23+
#import <zlib.h>
2324
#import <sys/qos.h>
2425
#import <BackgroundTasks/BackgroundTasks.h>
2526
#import <CommonCrypto/CommonDigest.h>
@@ -3303,4 +3304,67 @@ +(NSURLSession*) createEphemeralURLSession
33033304
return [NSURLSession sessionWithConfiguration:sessionConfig];
33043305
}
33053306

3307+
+(NSURL* _Nullable) compressFileAtPath:(NSString*) path withLevel:(NSInteger) level
3308+
{
3309+
uint8_t buffer[65536];
3310+
3311+
NSFileManager* fileManager = [NSFileManager defaultManager];
3312+
NSString* filename = path.lastPathComponent;
3313+
NSString* gzipPath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.gz", filename]];
3314+
3315+
DDLogInfo(@"Compressing file at '%@' into '%@'...", path, gzipPath);
3316+
3317+
NSError* error = nil;
3318+
if([fileManager fileExistsAtPath:gzipPath])
3319+
[fileManager removeItemAtPath:gzipPath error:&error];
3320+
if(error != nil)
3321+
{
3322+
DDLogError(@"Could not delete old leftover gzip file at '%@': %@", gzipPath, error);
3323+
return nil;
3324+
}
3325+
3326+
NSInputStream* input = [NSInputStream inputStreamWithFileAtPath:path];
3327+
if(!input)
3328+
{
3329+
DDLogError(@"Could not open file to compress: %@", path);
3330+
return nil;
3331+
}
3332+
[input open];
3333+
3334+
FILE* outputFile = fopen([gzipPath fileSystemRepresentation], "wb");
3335+
if(!outputFile)
3336+
{
3337+
DDLogError(@"Could not open gzip output file: %@", gzipPath);
3338+
[input close];
3339+
return nil;
3340+
}
3341+
3342+
gzFile gzOutput = gzdopen(fileno(outputFile), [NSString stringWithFormat:@"%ldwb", (long)level].UTF8String);
3343+
if(!gzOutput)
3344+
{
3345+
DDLogError(@"Could not create gzip stream for output file: %@", gzipPath);
3346+
fclose(outputFile);
3347+
[input close];
3348+
return nil;
3349+
}
3350+
3351+
NSInteger bytesRead;
3352+
while((bytesRead = [input read:buffer maxLength:sizeof(buffer)]) > 0)
3353+
{
3354+
if(gzwrite(gzOutput, buffer, (unsigned int)bytesRead) != bytesRead)
3355+
{
3356+
DDLogError(@"Failed to write gzip data to output file: %@", gzipPath);
3357+
gzclose(gzOutput);
3358+
[input close];
3359+
return nil;
3360+
}
3361+
}
3362+
3363+
gzclose(gzOutput);
3364+
[input close];
3365+
3366+
return [NSURL fileURLWithPath:gzipPath];
3367+
3368+
}
3369+
33063370
@end

0 commit comments

Comments
 (0)