Skip to content

Commit

Permalink
Fix a problem with the implementation of -B
Browse files Browse the repository at this point in the history
  • Loading branch information
luist18 committed Apr 10, 2020
1 parent 7aeb544 commit e50ef46
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 30 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ with:
| Directory and file size communication with pipes| ![https://img.shields.io/badge/great-%20-brightgreen](https://img.shields.io/badge/great-%20-brightgreen) | N/a|
| Directory handling with different a processes | ![https://img.shields.io/badge/great-%20-brightgreen](https://img.shields.io/badge/great-%20-brightgreen) | N/a|
| Show all files and directories (option -a) | ![https://img.shields.io/badge/great-%20-brightgreen](https://img.shields.io/badge/great-%20-brightgreen) | N/a |
| Signal handling | ![https://img.shields.io/badge/good-%20-yellowgreen](https://img.shields.io/badge/good-%20-yellowgreen) | |
| Signal handling | ![https://img.shields.io/badge/good-%20-yellowgreen](https://img.shields.io/badge/good-%20-yellowgreen) |N/a|
| Log information | ![https://img.shields.io/badge/great-%20-brightgreen](https://img.shields.io/badge/great-%20-brightgreen) |N/a|
| Different size according to block size or bytes | ![https://img.shields.io/badge/ok-%20-yellowgreen](https://img.shields.io/badge/ok-%20-yellowgreen) |Different implementation of -B|
| Different size according to block size or bytes | ![https://img.shields.io/badge/great-%20-brightgreen](https://img.shields.io/badge/great-%20-brightgreen) |Different implementation of -B|

## Authors

Expand Down
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ FILES = $(SRC_DIR)/main.c $(SRC_DIR)/util.c $(SRC_DIR)/simpledu.c $(SRC_DIR)/log

make: ${FILES}
@mkdir -p $(BIN_DIR)
@$(CC) -Wall -o $(BIN_DIR)/simpledu ${FILES}
@$(CC) -Wall -o $(BIN_DIR)/simpledu ${FILES} -lm
67 changes: 52 additions & 15 deletions src/simpledu.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

void simpledu(struct flags *flags, int *fd);
pid_t treatDir(int *new_fd, struct flags *flags, struct dirent *dirent);
pid_t treatLink(int *new_fd, struct flags *flags, struct dirent *dirent, char *line, int *total_size);
void closeDir(int *old_fd, int *new_fd, DIR *dir, struct flags *flags, int *total_size);
pid_t treatLink(int *new_fd, struct flags *flags, struct dirent *dirent, char *line, double *total_size);
void closeDir(int *old_fd, int *new_fd, DIR *dir, struct flags *flags, double *total_size);

void run(struct flags *flags)
{
Expand All @@ -37,7 +37,7 @@ void simpledu(struct flags *flags, int *old_fd)
struct dirent *dirent;
struct stat stat_entry;
DIR *dir;
int total_size = 0;
double total_size = 0;

// opens the directory with the name given on path (always updated)
if ((dir = opendir(flags->path)) == NULL)
Expand Down Expand Up @@ -70,7 +70,14 @@ void simpledu(struct flags *flags, int *old_fd)
// starts the conting of the directory size at the "." subidr, this is, first of all counts the size of the actual directroy
if (!strcmp(dirent->d_name, "."))
{
total_size += stat_entry.st_size;
if (!flags->bytes)
{
total_size += stat_entry.st_blocks / 2.0;
}
else
{
total_size += stat_entry.st_size;
}
continue;
}

Expand All @@ -88,9 +95,19 @@ void simpledu(struct flags *flags, int *old_fd)
else if (S_ISREG(stat_entry.st_mode)) // when the entry is a file
{
// updates the size and prints it if the max depth was not reached already
total_size += stat_entry.st_size;
double size;
if (!flags->bytes)
{
size = stat_entry.st_blocks / 2.0;
total_size += size;
}
else
{
size = stat_entry.st_size;
total_size += size;
}
if (!(flags->current_depth > flags->max_depth && flags->max_depth > 0))
printFile(dirent->d_name, flags, stat_entry.st_size);
printFile(dirent->d_name, flags, size);

logEntry(flags, stat_entry.st_size, file);
}
Expand All @@ -99,9 +116,19 @@ void simpledu(struct flags *flags, int *old_fd)
// If the links flag is off the link must be treated as a file
if (!flags->dereference)
{
total_size += stat_entry.st_size;
double size;
if (!flags->bytes)
{
size = stat_entry.st_blocks / 2.0;
total_size += size;
}
else
{
size = stat_entry.st_size;
total_size += size;
}
if (!(flags->current_depth > flags->max_depth && flags->max_depth > 0))
printFile(dirent->d_name, flags, stat_entry.st_size);
printFile(dirent->d_name, flags, size);
}
else
{
Expand Down Expand Up @@ -169,7 +196,7 @@ pid_t treatDir(int *new_fd, struct flags *flags, struct dirent *dirent)
*
* @return the process id (will return two)
*/
pid_t treatLink(int *new_fd, struct flags *flags, struct dirent *dirent, char *file, int *total_size)
pid_t treatLink(int *new_fd, struct flags *flags, struct dirent *dirent, char *file, double *total_size)
{
char linkpath_buffer[MAX_DIR_NAME_SIZE];
int linkpath_bytes = readlink(file, linkpath_buffer, MAX_DIR_NAME_SIZE);
Expand All @@ -190,10 +217,20 @@ pid_t treatLink(int *new_fd, struct flags *flags, struct dirent *dirent, char *f

if (S_ISREG(stat_entry.st_mode)) // when the entry is a file
{
*total_size += stat_entry.st_size;
double size;
if (!flags->bytes)
{
size = stat_entry.st_blocks / 2;
*total_size += size;
}
else
{
size = stat_entry.st_size;
*total_size += size;
}
if (!(flags->current_depth > flags->max_depth && flags->max_depth > 0))
{
printLink(file, &tmp_flags, stat_entry.st_size);
printLink(file, &tmp_flags, size);
}
return getpid();
}
Expand Down Expand Up @@ -229,7 +266,7 @@ pid_t treatLink(int *new_fd, struct flags *flags, struct dirent *dirent, char *f
/**
* Counts the size of all subdirectories, prints the current directory information and closes it
*/
void closeDir(int *old_fd, int *new_fd, DIR *dir, struct flags *flags, int *total_size)
void closeDir(int *old_fd, int *new_fd, DIR *dir, struct flags *flags, double *total_size)
{
// closes the directory
if (closedir(dir) == -1)
Expand All @@ -241,14 +278,14 @@ void closeDir(int *old_fd, int *new_fd, DIR *dir, struct flags *flags, int *tota
int status;
int pid;

int tmp;
double tmp;

// waits for remaining files
while ((pid = wait(&status)) > 0)
{
logExit(status);
// reads all the sizes from the subdirs
read(new_fd[READ], &tmp, sizeof(int));
read(new_fd[READ], &tmp, sizeof(double));

logRecvPipe(tmp);

Expand All @@ -262,7 +299,7 @@ void closeDir(int *old_fd, int *new_fd, DIR *dir, struct flags *flags, int *tota
printDir(flags->path, flags, *total_size);

// writes its total size in the pipe so the above directory can count it
write(old_fd[WRITE], total_size, sizeof(int));
write(old_fd[WRITE], total_size, sizeof(double));

logSendPipe(*total_size);
}
19 changes: 10 additions & 9 deletions src/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

#include <string.h>
#include <unistd.h>
#include <math.h>

void printFile(char *name, struct flags *flags, long size)
void printFile(char *name, struct flags *flags, double size)
{
if (!flags->all)
return;

if (!flags->bytes)
size /= flags->block_size;
size *= (1024.0 / (flags->block_size * 1.0));

char line[1024];

Expand All @@ -23,14 +24,14 @@ void printFile(char *name, struct flags *flags, long size)
}

char tmp[200];
sprintf(tmp, "%-ld\t%s\n", size, line);
sprintf(tmp, "%-ld\t%s\n", (long) ceil(size), line);
write(STDOUT_FILENO, tmp, strlen(tmp));
}

void printDir(char *name, struct flags *flags, long size)
void printDir(char *name, struct flags *flags, double size)
{
if (!flags->bytes)
size /= flags->block_size;
size *= (1024.0 / flags->block_size);

char line[1024];

Expand All @@ -44,16 +45,16 @@ void printDir(char *name, struct flags *flags, long size)
}

char tmp[200];
sprintf(tmp, "%-ld\t%s\n", size, line);
sprintf(tmp, "%-ld\t%s\n", (long) ceil(size), line);
write(STDOUT_FILENO, tmp, strlen(tmp));
}

void printLink(char *name, struct flags *flags, long size)
void printLink(char *name, struct flags *flags, double size)
{
if (!flags->bytes)
size /= flags->block_size;
size *= (1024.0 / flags->block_size);

char tmp[200];
sprintf(tmp, "%-ld\t%s\n", size, name);
sprintf(tmp, "%-ld\t%s\n", (long) ceil(size), name);
write(STDOUT_FILENO, tmp, strlen(tmp));
}
6 changes: 3 additions & 3 deletions src/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @param flags the executable's flags
* @param size the size of the file, in bytes
*/
void printFile(char *name, struct flags *flags, long size);
void printFile(char *name, struct flags *flags, double size);

/**
* @brief Prints a directory according to the flags.
Expand All @@ -29,7 +29,7 @@ void printFile(char *name, struct flags *flags, long size);
* @param flags the executable's flags
* @param size the total size of the directory, in bytes
*/
void printDir(char *name, struct flags *flags, long size);
void printDir(char *name, struct flags *flags, double size);

/**
* @breif Prints a directory according to the flags.
Expand All @@ -38,6 +38,6 @@ void printDir(char *name, struct flags *flags, long size);
* @param flags the executable's flags
* @param size the total size of the directory, in bytes
*/
void printLink(char *name, struct flags *flags, long size);
void printLink(char *name, struct flags *flags, double size);

#endif

0 comments on commit e50ef46

Please sign in to comment.