Added cleanup report on exit

This commit is contained in:
luca0N! 2020-06-27 03:08:22 -03:00
parent 1aececb96e
commit 76e4249ea8
No known key found for this signature in database
GPG Key ID: 9F1B99B522287CAB
2 changed files with 42 additions and 5 deletions

41
main.c
View File

@ -29,6 +29,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include <math.h>
// Prints the program license to the user. // Prints the program license to the user.
int printLicense(int full){ int printLicense(int full){
@ -49,7 +50,7 @@ int printLicense(int full){
} }
// Cleans a directory. // Cleans a directory.
void cleanDir(const char* path, unsigned* objectCount){ void cleanDir(const char* path, unsigned* objectCount, unsigned long* totalDeletedBytes){
const char* validPath = path; const char* validPath = path;
//char validPath[sizeof(path) + 1]; //char validPath[sizeof(path) + 1];
//strcpy(validPath, path); //strcpy(validPath, path);
@ -85,7 +86,7 @@ void cleanDir(const char* path, unsigned* objectCount){
// If the item is a directory, call cleanDir on that directory. // If the item is a directory, call cleanDir on that directory.
if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){ if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
cleanDir(fullPath, objectCount); cleanDir(fullPath, objectCount, totalDeletedBytes);
/*int shellReturnCode = shellRemoveDir(fullPath); /*int shellReturnCode = shellRemoveDir(fullPath);
if (shellReturnCode != 0){ if (shellReturnCode != 0){
printf(" %s\n", STR_FAILED_DIR); printf(" %s\n", STR_FAILED_DIR);
@ -95,6 +96,21 @@ void cleanDir(const char* path, unsigned* objectCount){
continue; continue;
} }
// Get the length of the file before deleting it.
HANDLE fileHandle = CreateFile(fullPath, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
LARGE_INTEGER size;
unsigned long length = 0;
if (fileHandle != INVALID_HANDLE_VALUE){
if (GetFileSizeEx(fileHandle, &size)){
length = size.QuadPart;
}
CloseHandle(fileHandle);
}
if (DeleteFile(fullPath) == 0){ if (DeleteFile(fullPath) == 0){
int lastError = GetLastError(); int lastError = GetLastError();
printf(" %s\n", STR_FAILED_FILE); printf(" %s\n", STR_FAILED_FILE);
@ -104,6 +120,7 @@ void cleanDir(const char* path, unsigned* objectCount){
printf(" %s\n", STR_DELETED_FILE); printf(" %s\n", STR_DELETED_FILE);
// Update the deleted object count if the process was successful. // Update the deleted object count if the process was successful.
(*objectCount)++; (*objectCount)++;
(*totalDeletedBytes) += length;
} }
} while(FindNextFile(hFind, &data)); } while(FindNextFile(hFind, &data));
} else { } else {
@ -114,6 +131,7 @@ void cleanDir(const char* path, unsigned* objectCount){
// Gets the user defined directories // Gets the user defined directories
void getAdditionalDirs(bool safeMode){ void getAdditionalDirs(bool safeMode){
unsigned* objectCount = 0; unsigned* objectCount = 0;
unsigned long* totalDeletedBytes = 0;
printf("\n%s\n", STR_FETCHING_ADD_DIRS); printf("\n%s\n", STR_FETCHING_ADD_DIRS);
int dirs; int dirs;
dirs = GetPrivateProfileInt("Pastas", "Pastas", 3, PATH_CFG); dirs = GetPrivateProfileInt("Pastas", "Pastas", 3, PATH_CFG);
@ -192,7 +210,7 @@ void getAdditionalDirs(bool safeMode){
printf("%s [%s] %s\n", STR_FOUND_DIR, dirKey, buffer); printf("%s [%s] %s\n", STR_FOUND_DIR, dirKey, buffer);
if (!safeMode) if (!safeMode)
cleanDir(buffer, &objectCount); cleanDir(buffer, &objectCount, &totalDeletedBytes);
free(buffer); free(buffer);
free(varBuffer); free(varBuffer);
continue; continue;
@ -201,9 +219,24 @@ void getAdditionalDirs(bool safeMode){
printf("%s [%s] %s\n", STR_FOUND_DIR, dirKey, currentPath); printf("%s [%s] %s\n", STR_FOUND_DIR, dirKey, currentPath);
if (!safeMode) if (!safeMode)
cleanDir(currentPath, &objectCount); cleanDir(currentPath, &objectCount, &totalDeletedBytes);
} }
printf(FMT_STR_OBJECT_COUNT, objectCount); printf(FMT_STR_OBJECT_COUNT, objectCount);
// Determine whether we should output the size as bytes, KiB, MiB or GiB.
if (totalDeletedBytes < 1024){
printf(FMT_STR_TOTAL_DELETED_BYTES, totalDeletedBytes);
} else if (totalDeletedBytes < (unsigned long) pow(1024, 2)){
float kib = (unsigned long) totalDeletedBytes / pow(1024, 1);
printf(FMT_STR_TOTAL_DELETED_KIBIBYTES, kib);
} else if (totalDeletedBytes < (unsigned long) pow(1024, 3)){
float mib = (unsigned long) totalDeletedBytes / pow(1024, 2);// / (float) 1024 / (float) 1024;
printf("%lu\n", totalDeletedBytes);
printf(FMT_STR_TOTAL_DELETED_MEBIBYTES, mib);
} else {
float gib = (unsigned long) totalDeletedBytes / pow(1024, 3);
printf(FMT_STR_TOTAL_DELETED_GIBIBYTES, gib);
}
} }
int main(void){ int main(void){

6
str.h
View File

@ -22,7 +22,7 @@
#define STR_LN "--------------------------------------" #define STR_LN "--------------------------------------"
#define STR_USE_WARNING "Aviso: é recomendado fechar todos os programas abertos antes de utilizar este programa para poder remover mais arquivos temporários. Deseja continuar? (S/n)" #define STR_USE_WARNING "Aviso: é recomendado fechar todos os programas abertos antes de utilizar este programa para poder remover mais arquivos temporários. Deseja continuar? (s/n)"
#define STR_FETCHING_ADD_DIRS "Recolhendo pastas adicionais..." #define STR_FETCHING_ADD_DIRS "Recolhendo pastas adicionais..."
@ -38,6 +38,10 @@
#define STR_FAILED_DIR "[Pasta mantida] Não foi possível apagar esta pasta pelo seguinte erro:" #define STR_FAILED_DIR "[Pasta mantida] Não foi possível apagar esta pasta pelo seguinte erro:"
#define FMT_STR_OBJECT_COUNT "[Relatório] %u objetos foram apagados com sucesso.\n" #define FMT_STR_OBJECT_COUNT "[Relatório] %u objetos foram apagados com sucesso.\n"
#define FMT_STR_TOTAL_DELETED_BYTES "[Relatório] %lu baites ou mais foram liberados."
#define FMT_STR_TOTAL_DELETED_KIBIBYTES "[Relatório] %.2f KiB ou mais foram liberados."
#define FMT_STR_TOTAL_DELETED_MEBIBYTES "[Relatório] %.2f MiB ou mais foram liberados."
#define FMT_STR_TOTAL_DELETED_GIBIBYTES "[Relatório] %.2f GiB ou mais foram liberados."
#define STR_MEM_ERR "[Erro fatal] Não foi possível alocar memória. Tente fechar alguns programas abertos e tente novamente." #define STR_MEM_ERR "[Erro fatal] Não foi possível alocar memória. Tente fechar alguns programas abertos e tente novamente."