Browse Source

fixed memory leaks

master
Mathieu Serandour 11 months ago
parent
commit
567026f016
  1. 2
      kernel/fs/vfs.h
  2. 26
      kernel/fs/vfs_dirs.c
  3. 13
      kernel/fs/vfs_files.c

2
kernel/fs/vfs.h

@ -36,7 +36,7 @@ fs_t *vfs_open(const char *path, fast_dirent_t *dir);
/**
* @brief unmount a partition
* @brief unmount a fs
* from the vfs
*
* @param path the path of the

26
kernel/fs/vfs_dirs.c

@ -188,7 +188,8 @@ static int remove_vdir(vdir_t *vdir)
memmove(
vd->children + i,
vd->children + i + 1,
vd->n_children - i - 1);
(vd->n_children - i - 1) * sizeof(vdir_t)
);
vd->n_children--;
@ -220,7 +221,6 @@ static int remove_vdir(vdir_t *vdir)
static int unmount(vdir_t *vdir)
{
fs_t *fs = vdir->fs;
assert(vdir);
if (vdir->n_children)
@ -234,15 +234,18 @@ static int unmount(vdir_t *vdir)
{
log_warn(
"cannot unmount partition %s: %u open files",
fs->n_open_files, fs->part->name);
fs->part->name, fs->n_open_files);
return 0;
}
free(fs->part->mount_point);
fs->part->mount_point = NULL;
if (vdir == &vfs_root)
vfs_root.fs = NULL;
else
assert(remove_vdir(vdir));
free_vtree(vdir);
// remove cache entries related with this thing
for (unsigned i = 0; i < cache_size; i++)
@ -254,11 +257,10 @@ static int unmount(vdir_t *vdir)
}
}
fs->unmount(fs);
return 1;
}
static void free_cache(void)
{
for (unsigned i = 0; i < cache_size; i++)
@ -698,6 +700,7 @@ fs_t *vfs_open(const char *path, fast_dirent_t *dir)
if (!vdir)
{
free(pathbuf);
// there is no wat the dir could
// exist
return NULL;
@ -864,6 +867,8 @@ int vfs_mount(disk_part_t *part, const char *path)
}
new->fs = fs;
part->mount_point = malloc(strlen(tmp.path) + 1);
strcpy(part->mount_point, tmp.path);
//test_file_read_seek();
@ -875,12 +880,15 @@ int vfs_mount(disk_part_t *part, const char *path)
int vfs_unmount(const char *path)
{
log_warn("vfs_unmount(%s)", path);
// do stuf!
char *pbuf = malloc(strlen(path) + 1);
simplify_path(pbuf, path);
vdir_t *vdir = get_fs_vdir(pbuf);
free(pbuf);
if (!vdir)
return 0;
log_info("unmounting %s", path);
@ -888,7 +896,6 @@ int vfs_unmount(const char *path)
}
struct DIR *vfs_opendir(const char *path)
{
/**
@ -1014,11 +1021,14 @@ int vfs_update_metadata(
fast_dirent_t parent_dirent;
fs_t* fs = vfs_open(pathbuf, &parent_dirent);
assert(fs); // assert that the parent exists
assert(parent_dirent.type == DT_DIR);
assert(parent_dirent.file_size == 0);
return fs->update_dirent(fs, parent_dirent.ino, file_name, addr, file_size);
int ret = fs->update_dirent(fs, parent_dirent.ino, file_name, addr, file_size);
free(pathbuf);
return ret;
}

13
kernel/fs/vfs_files.c

@ -199,6 +199,9 @@ file_handle_t* create_handler(
file_ent->n_insts * sizeof(file_handle_t *));
file_ent->fhs[file_ent->n_insts - 1] = handler;
fs->n_open_files++;
return handler;
}
@ -239,7 +242,7 @@ file_handle_t *vfs_open_file(const char *path) {
return NULL;
}
return create_handler(fs, &dirent, path);
}
@ -269,8 +272,6 @@ void vfs_close_file(file_handle_t *handle)
= handle->open_vfile;
open_file->n_insts--;
flush(open_file);
@ -279,7 +280,6 @@ void vfs_close_file(file_handle_t *handle)
// free the vfile
free(open_file->path);
free(open_file->fhs);
free(open_file);
// remove open_file from
// the table
@ -301,7 +301,8 @@ void vfs_close_file(file_handle_t *handle)
break;
}
}
open_files = realloc(open_files, n_open_files * sizeof(struct file_ent));
assert(found);
}
@ -335,7 +336,6 @@ void vfs_close_file(file_handle_t *handle)
}
free(handle);
fs->n_open_files--;
}
@ -636,7 +636,6 @@ size_t vfs_write_file(const void *ptr, size_t size, size_t nmemb,
// update file size
stream->open_vfile->file_size = MAX(stream->open_vfile->file_size, stream->file_offset);
invalidate_handlers_buffer(stream->open_vfile, stream);
return nmemb;

Loading…
Cancel
Save