Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

duplicated fs, hiding cruft, porting issues #9

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(BSD true)
elseif(${CMAKE_SYSTEM_NAME} MATCHES "DragonFly")
set(BSD true)
elseif(${CMAKE_SYSTEM_NAME} MATCHES "kFreeBSD")
set(LINUX true)
elseif(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
set(BSD true)
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
Expand All @@ -34,6 +36,8 @@ elseif(${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD")
set(BSD true)
elseif(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
set(SOLARIS true)
elseif(${CMAKE_SYSTEM_NAME} MATCHES "GNU")
set(LINUX true)
else()
message(FATAL_ERROR "Unsupported platform: ${CMAKE_SYSTEM_NAME}")
endif()
Expand Down
80 changes: 61 additions & 19 deletions src/dfc.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ struct maxwidths max;
int aflag, bflag, cflag, dflag, eflag, fflag, hflag, iflag, lflag, mflag,
nflag, oflag, pflag, qflag, sflag, tflag, uflag, vflag, wflag;
int Mflag, Tflag, Wflag;
int tty_width;
char unitflag;

int
Expand All @@ -63,7 +64,6 @@ main(int argc, char *argv[])
struct list queue;
struct display sdisp;
int ch;
int tty_width;
int ret = EXIT_SUCCESS;
char *fsnfilter = NULL;
char *fstfilter = NULL;
Expand Down Expand Up @@ -419,10 +419,6 @@ main(int argc, char *argv[])
/* fetch information about the currently mounted filesystems */
fetch_info(&queue);

/* cannot display all information if tty is too narrow */
if (!fflag && tty_width > 0 && !eflag)
auto_adjust(tty_width);

/* actually displays the info we have got */
disp(&queue, fstfilter, fsnfilter, &sdisp);

Expand Down Expand Up @@ -523,40 +519,86 @@ disp(struct list *lst, const char *fstfilter, const char *fsnfilter,
}
}

/* only required for html, json and tex export */
if (sdisp->init)
sdisp->init();

/* legend on top */
if (!nflag)
sdisp->print_header();

/* sort the list */
if (qflag)
lst->head = msort(lst->head);

p = lst->head;
for (p = lst->head; p; p = p->next) {
/* ignored unless proven otherwise */
p->ignored = 1;

while (p != NULL) {
/* ignore when needed */
if (!aflag && (is_mnt_ignore(p) == 1)) {
p = delete_struct_and_get_next(p);
continue;
}

/* ignore /run mounts unless they're dangerously full */
if (!aflag && !strncmp(p->mntdir, "/run", 4) && p->perctused < 50) {
continue;
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I guess it is fine as-is. It won't hurt platforms which are not concerned by this anyway.

/* filtering on fs type */
if (tflag && (fsfilter(p->fstype, fstfilter, nmt) == 0)) {
p = delete_struct_and_get_next(p);
continue;
}
/* filtering on fs name */
if (pflag && (fsfilter(p->fsname, fsnfilter, nmn) == 0)) {
p = delete_struct_and_get_next(p);
continue;
}

/* skip remote file systems */
if (lflag && is_remote(p)) {
continue;
}

p->ignored = 0;
}

for (p = lst->head; p; p = p->next) {
if (aflag || p->ignored)
continue;

/* doesn't have a device backing store? */
if (p->fsname[0] != '/')
continue;

struct fsmntinfo *r;
for (r = lst->head; r; r = r->next) {
if (p != r && !r->ignored && !strcmp(p->fsname, r->fsname))
switch (lencmp(p, r))
{
case -1:
default:
/* mounted twice on same dir? */
r->ignored = 1;
break;
case 1:
p->ignored = 1;
}
}
}

for (p = lst->head; p; p = p->next) {
if (!p->ignored)
update_maxwidth(p);
}

/* cannot display all information if tty is too narrow */
if (!fflag && tty_width > 0 && !eflag)
auto_adjust(tty_width);

/* only required for html, json and tex export */
if (sdisp->init)
sdisp->init();

/* legend on top */
if (!nflag)
sdisp->print_header();

p = lst->head;

while (p != NULL) {
if (p->ignored) {
p = delete_struct_and_get_next(p);
continue;
}
Expand Down Expand Up @@ -599,7 +641,7 @@ disp(struct list *lst, const char *fstfilter, const char *fsnfilter,
if (iflag) {
ifitot += (double)p->files;
ifatot += (double)p->favail;
#if defined(__linux__)
#if defined(__linux__) || defined(__GLIBC__)
sdisp->print_inodes((uint64_t)(p->files),
(uint64_t)(p->favail));
#else
Expand Down
3 changes: 2 additions & 1 deletion src/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ struct fsmntinfo {
double used; /* fs used size */

/* infos to get from statvfs(3) */
#if defined(__linux__)
#if defined(__linux__) || defined(__GLIBC__)
int flags; /* XXX: does not exist on Linux */
unsigned long bsize; /* file system block size */
unsigned long frsize; /* fragment size */
Expand Down Expand Up @@ -133,6 +133,7 @@ struct fsmntinfo {
fsfilcnt_t favail; /* # of available inodes */
#endif /* __sun */

int ignored;
/* pointer to the next element of the list */
struct fsmntinfo *next;
};
Expand Down
2 changes: 0 additions & 2 deletions src/platform/services-bsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,6 @@ fetch_info(struct list *lst)

/* enqueue the element into the queue */
enqueue(lst, *fmi);

update_maxwidth(fmi);
}
free(fmi);
}
Expand Down
9 changes: 6 additions & 3 deletions src/platform/services-linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,22 @@
*
* Linux implemention of services.
*/
#ifdef __linux__

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#if defined(__linux__) || defined(__GLIBC__)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to move the check down? I'd like it to be at the very top because this file is linux/glibc specific. But I'm being picky, I'll move it back at the top after merging.

Copy link
Contributor Author

@kilobyte kilobyte Jul 31, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The__GLIBC__ define comes from one of these headers, not from gcc, so the order does matter. I'm not sure what happens on Linux libcs other than glibc, like musl or dietlibc.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'll fire up a VM with Alpine Linux and see how it behaves.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, it builds fine in Alpine Linux which uses musl. I'll leave it as is then.


#ifdef NLS_ENABLED
#include <locale.h>
#include <libintl.h>
#endif /* NLS_ENABLED */

#include <mntent.h>
#include <sys/statvfs.h>
#include <errno.h>

#include "extern.h"
#include "services.h"
Expand Down Expand Up @@ -101,6 +103,9 @@ fetch_info(struct list *lst)
continue;
/* get infos from statvfs */
if (statvfs(entbuf->mnt_dir, &vfsbuf) == -1) {
/* show only "real" errors, not lack of permissions */
if (errno == EACCES)
continue;
/* display a warning when a FS cannot be stated */
(void)fprintf(stderr, _("WARNING: %s was skipped "
"because it could not be stated"),
Expand Down Expand Up @@ -152,8 +157,6 @@ fetch_info(struct list *lst)

/* enqueue the element into the queue */
enqueue(lst, *fmi);

update_maxwidth(fmi);
}
/* we need to close the mtab file now */
if (fclose(mtab) == EOF)
Expand Down
2 changes: 0 additions & 2 deletions src/platform/services-solaris.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ fetch_info(struct list *lst)

fmi->next = NULL;
enqueue(lst, *fmi);

update_maxwidth(fmi);
}
if (ret > 0) {
(void)fprintf(stderr, "An error occured while reading the "
Expand Down
19 changes: 19 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,23 @@ msort(struct fsmntinfo *fmi)
return fmi;
}

/*
* Compare lengths of mount points, then their paths.
* -1,0,1 are returned as in *cmp.
*/
int
lencmp(struct fsmntinfo *a, struct fsmntinfo *b)
{
size_t la = strlen(a->mntdir);
size_t lb = strlen(b->mntdir);

if (la < lb)
return -1;
if (la > lb)
return 1;
return strcmp(a->mntdir, b->mntdir);
}

/*
* Get the with of TTY and retun it.
* 0 is returned if stdout is not a tty.
Expand Down Expand Up @@ -807,9 +824,11 @@ is_pseudofs(const char *type)
"devpts",
"devtmpfs",
"dlmfs",
"fdescfs",
"fuse.gvfs-fuse-daemon",
"fusectl",
"hugetlbfs",
"linprocfs",
"mqueue",
"nfsd",
"none",
Expand Down
1 change: 1 addition & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ double cvrt(double n);
int fsfilter(const char *fs, const char *filter, int nm);
int cmp(struct fsmntinfo *a, struct fsmntinfo *b);
struct fsmntinfo * msort(struct fsmntinfo *fmi);
int lencmp(struct fsmntinfo *a, struct fsmntinfo *b);
int getttywidth(void);
void init_maxwidths(void);
int get_req_width(double fs_size);
Expand Down