Сделаны правки по утечки памяти

This commit is contained in:
romenskiy 2025-06-16 16:50:30 +03:00
parent baa657e486
commit 228f0baef3
2 changed files with 40 additions and 12 deletions

View file

@ -3,6 +3,6 @@ cmake_minimum_required(VERSION 3.0)
project(webdisk_c LANGUAGES C)
add_executable(webdisk_c main.c cJSON/cJSON.c)
set (CMAKE_C_FLAGS "-O3")
set (CMAKE_C_FLAGS "-g -O3 -ljemalloc -L /usr/lib64/libjemalloc.so")
install(TARGETS webdisk_c RUNTIME DESTINATION bin)

48
main.c
View file

@ -11,8 +11,11 @@
#include <pthread.h>
#include "cJSON/cJSON.h"
#include <jemalloc/jemalloc.h>
unsigned narenas = 4;
#define bool unsigned char
//#define bool unsigned char
#define DEBUG 0
@ -46,6 +49,18 @@ typedef struct metadata {
struct dir_list_struct* jamp;
} stack_element;
void dir_list_struct_init(struct dir_list_struct* dir_list_struct){
dir_list_struct->top = NULL;
dir_list_struct->file = NULL;
dir_list_struct->next = NULL;
}
void meta_data_init(struct metadata* metadata){
metadata->name = NULL;
metadata->dir_list = NULL;
metadata->top = NULL;
metadata->jamp = NULL;
}
// Функция для получения размера файла
long get_file_size(char* size) {
struct stat st;
@ -92,7 +107,8 @@ void format_size(unsigned long long bytes, char *output, size_t output_size) {
struct metadata* metadata_create(bool tipe_dir,char* path, struct metadata* top){
DEBUG_PRINT("Пред mata malloc\n");
struct metadata* mata = malloc(sizeof(struct metadata));
struct metadata* mata = je_malloc(sizeof(struct metadata));
meta_data_init(mata);
DEBUG_PRINT("После mata malloc\n");
mata->name = path;
mata->tipe_dir = tipe_dir;
@ -105,15 +121,15 @@ struct metadata* metadata_create(bool tipe_dir,char* path, struct metadata* top)
mata->size = mata->size;
//struct metadata* mata_2 = mata;
/*
struct tred_pizdec* mata_2 = malloc(sizeof(struct tred_pizdec));
mata_2->name = path;
mata_2->size = &mata->size;
pthread_t thread;
pthread_create(&thread, NULL, get_file_size_multi_tred, (void*)mata_2);
//pthread_join(thread, NULL);
*/
mata->size = get_file_size(path);
pthread_join(thread, NULL);
//mata->size = get_file_size(path);
DEBUG_PRINT("Объём:%li - %s\n", mata->size, mata->name);
}
return mata;
@ -134,10 +150,12 @@ void scanning(struct metadata *mata){
if (errno == 2){
printf("Нет такого файла или каталога!\n");
//printf("%s\n", strerror(errno));
printf("file: %s\n", mata->name);
}
if (errno == 22){
printf("Недопустимый аргумент!\n");
//printf("%s\n", strerror(errno));
printf("file: %s\n", mata->name);
}
if (errno == 13){
printf("Отказ в доступе!\n");
@ -146,6 +164,9 @@ void scanning(struct metadata *mata){
else{
entry = readdir(dp);
}
printf("Нет такого файла или каталога!\n");
//printf("%s\n", strerror(errno));
printf("file: %s\n", mata->name);
DEBUG_PRINT("Two\n");
while(entry != NULL){
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0){
@ -158,10 +179,12 @@ void scanning(struct metadata *mata){
DEBUG_PRINT("mata->name обём: %li\n",(strlen(mata->name) * sizeof(char)));
DEBUG_PRINT("entry->d_name обём: %li\n",(strlen(entry->d_name) * sizeof(char)));
char* putaa = malloc((strlen(mata->name) + 1) * sizeof(char) + (strlen(entry->d_name) + 1) * sizeof(char) + sizeof(char) *2);
char* putaa = malloc(strlen(mata->name) + (strlen(entry->d_name)) + 2);
//snprintf(putaa, path_len, "%s/%s", mata->name, entry->d_name);
//char* putaa = malloc(sizeof(mata->name) + sizeof(entry->d_name) + sizeof(char));
DEBUG_PRINT("putaa готов! \n");
sprintf(putaa, "%s%s%s",mata->name,"/",entry->d_name);
sprintf(putaa, "%s/%s",mata->name,entry->d_name);
DEBUG_PRINT("putaa обединён! \n");
struct metadata* SAS;
if (entry->d_type == DT_DIR){
@ -173,12 +196,14 @@ void scanning(struct metadata *mata){
DEBUG_PRINT("SAS готов! \n");
if (AAA){
put_2 = malloc(sizeof(struct dir_list_struct));
DEBUG_PRINT("put_2 1 готов! \n");
dir_list_struct_init(put_2);
DEBUG_PRINT("put_2 1 готов!\n");
mata->dir_list = put_2;
AAA = 0;
}
else{
put_2->next = malloc(sizeof(struct dir_list_struct));
dir_list_struct_init(put_2->next);
DEBUG_PRINT("put_2 2 готов! \n");
put_2 = put_2->next;
}
@ -301,7 +326,7 @@ typedef struct {
char path[1024];
} Options;
bool parse_args(int argc, char *argv[], Options *opts) {
bool parse_args(int argc, char *argv[], Options *opts) { // II LLM
opts->has_j = 0;
opts->has_r = 0;
opts->r_value = 0;
@ -362,6 +387,7 @@ cJSON *print_statistik(cJSON *main_Array, struct dir_list_struct* list, char* ou
cJSON *content_Array;
do{
format_size(list->file->size, output, sizeof(output));
printf("%s\t%li\t%s\n", output, list->file->size, list->file->name);
DEBUG_PRINT("%s\t%li\t%s\n", output, list->file->size, list->file->name);
if (js_logok){
if (list->file->tipe_dir && (list->file->dir_list != NULL) && (int_depth != i)){
@ -392,6 +418,7 @@ cJSON *print_statistik(cJSON *main_Array, struct dir_list_struct* list, char* ou
}
int main(int argc, char *argv[]){
//mallctl("opt.narenas", NULL, NULL, &narenas, sizeof(narenas));
char *path;
setlocale(LC_ALL, "");
//printf("Your locale is: %s\n", setlocale(LC_CTYPE, NULL));
@ -412,6 +439,7 @@ int main(int argc, char *argv[]){
path = opts.path;
struct metadata * mata = malloc(sizeof(stack_element));
meta_data_init(mata);
mata->name = path;
mata->tipe_dir = 1;
scanning(mata);