<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><span style="font-family:arial,sans-serif">On Fri, Sep 25, 2015 at 12:53 AM, Dave Chinner </span><span dir="ltr" style="font-family:arial,sans-serif"><<a href="mailto:david@fromorbit.com" target="_blank">david@fromorbit.com</a>></span><span style="font-family:arial,sans-serif"> wrote:</span><br></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Thu, Sep 24, 2015 at 04:38:49PM +0200, Jan Tulak wrote:<br>
> On Wed, Sep 23, 2015 at 5:36 AM, Dave Chinner <<a href="mailto:david@fromorbit.com">david@fromorbit.com</a>> wrote:<br>
><br>
> > On Tue, Sep 15, 2015 at 11:59:22AM +0200, Jan Tulak wrote:<br>
> > > @@ -202,6 +205,27 @@ find_mountpoint(char *mtab, char *argname, struct<br>
> > stat64 *sb)<br>
> > > }<br>
> > ><br>
> > > while ((t = getmntent(mtabp))) {<br>
> > > +#elif defined(HAVE_GETMNTINFO)<br>
> > > + struct statfs *stats;<br>
> > > + int error, i, count;<br>
> > > + // because "t" is a pointer, but we don't need to use<br>
> > > + // malloc for this usage<br>
> > > + struct mntent t_tmp;<br>
> > > + t = &t_tmp;<br>
> > > +<br>
> > > +<br>
> > > + error = 0;<br>
> > > + if ((count = getmntinfo(&stats, 0)) < 0) {<br>
> > > + fprintf(stderr, _("%s: getmntinfo() failed: %s\n"),<br>
> > > + progname, strerror(errno));<br>
> > > + return 0;<br>
> > > + }<br>
> > > +<br>
> > > + for (i = 0; i < count; i++) {<br>
> > > + mntinfo2mntent(&stats[i], t);<br>
> > > +#else<br>
> > > +# error "How do I extract info about mounted filesystems on this<br>
> > platform?"<br>
> > > +#endif<br>
> ><br>
> > No, please don't do that. Having a loop iterator split across two<br>
> > separate defines is unmaintainable. Write two separate functions<br>
> > with the different loop iterators, then factor the common bit out<br>
> > of them into a single function.<br>
> ><br>
> ><br>
> I did a little refactoring to solve it. What I would like to ask about is<br>
> this:<br>
> When I can put ifdef just inside of a function like fnc(void) { #ifdef...<br>
> #else ... #endif }, with little to no code outside of the ifdef, is it<br>
> better to put the ifdef outside, or keep it inside?<br>
<br>
</div></div>The idea is that the "little differences" are put in functions that<br>
end up in include/<platform>.h or libxfs/<platform>.c, so there are<br>
no ifdefs in any of the application or library code. The build will<br>
automatically include the correct function on the given platform,<br>
and so the application code does not need such ifdefs at all.<br>
<br>
e.g. you could implement these functions to abstract the differences<br>
away from xfs_fsr and any other code that iterates the mount table:<br>
<br>
struct mntent_cursor {<br>
/* variables needed to track iteration of the mtab */<br>
};<br>
<br>
platform_first_mntent()<br>
platform_next_mntent()<br>
platform_end_mntent()<br>
<br>
and so the code would look like:<br>
<br>
struct mntent_cursor cursor;<br>
<br>
mntent = platform_first_mntent(&cursor)<br>
<br>
do {<br>
/* process mntent */<br>
} while (mntent = platform_next_mntent(&cursor, mntent));<br>
<br>
platform_end_mntent(&cursor);<br>
<br>
This completely abstracts the differences related to the the mount<br>
table traversal, and allows the aplication level code to be written<br>
in a clean, easily maintainable fashion...<br></blockquote><div><br></div><div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;display:inline">So something like what I just posted? I added the same code that works for Linux into other platforms other than OS X. </div> <div class="gmail_default" style="font-family:arial,helvetica,sans-serif;display:inline">Because as it was there before, it either works on them, or no one care. And I really don't have an idea how to test it on Irix... :-)</div></div><div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;display:inline"><br></div></div><div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;display:inline">Cheers,</div></div><div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;display:inline">Jan</div></div></div><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div>Jan Tulak<br></div><a href="mailto:jtulak@redhat.com" target="_blank">jtulak@redhat.com</a> / <a href="mailto:jan@tulak.me" target="_blank">jan@tulak.me</a></div></div></div></div>
</div></div>