mtree(8) reporting of file modes

Ian Lepore freebsd at damnhippie.dyndns.org
Tue Mar 6 21:36:20 UTC 2012


On Tue, 2012-03-06 at 12:41 -0800, David Wolfskill wrote:
> As I mentioned in
> <http://docs.FreeBSD.org/cgi/mid.cgi?20120306000520.GS1519>, at work,
> we're trying to use mtree(8) to do reality checks on server
> configuration/provisioning.  (We are not proposing the use of mtree to
> actually enforce a particular configuration -- we are only considering
> using it to generate specification files, then check aa given system
> against those specification files.)
> 
> I had thought it odd (after running "mtree -c") that most of the entries
> in the resulting specification file failed to mention the mode of the
> file; this was the catalyst for the above-cited message.
> 
> In the mean time, I started poking at the sources.
> 
> Caveat: I'm not really a C programmer; the  bulk of my background is in
> sysadmin-type positions (though I've been doing other stuff for the last
> 4 years).
> 
> Anyway, I fairly quickly focused my attention on
> src/usr.sbin/mtree/create.c, in particular, on the statf() function
> therein.
> 
> Most of this part of the code is barely changed since 4.4 Lite; the most
> recent change to the section in question (lines 207 - 208 from the
> version in head as of r232599) was made by rgrimes@ back in 1994.
> 
> So I presume that there's something I'm overlooking or otherwise
> missing, since the folks who have been here before were certainly more
> clueful than I am.
> 
> But the code in question:
> 
> ...
> 206         }
> 207         if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
> 208                 output(indent, &offset, "mode=%#o", p->fts_statp->st_mode & MBITS);
> ...
> 
> is what outputs the "mode" to standard output.
> 
> Here is (the bulk of) what I found:
> 
> * The "keys & F_MODE" term merely tests to see if we are interested
>   in reporting the file mode.  (By default, we are.)
> 
> * "p->fts_statp->st_mode" refers to the "st_mode" returned from stat()
>   for the file presently being examined.
> 
> * MBITS is a mask of "mode bits" about which we care; it is defined
>   (in mtree.h) as "(S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)".
>   These are defined in sys/stat.h; MBITS, thus, works out to 0007777.
> 
> * mode is set to the (masked) mode of the (immediately) enclosing
>   directory when it is visited in pre-order.  (This is done in statd().)
> 
> As a result, we only report the mode of a file if it differs from the
> mode of its parent directory.
> 
> Huh??!?
> 
> 
> Maybe I'm confused, but certainly for my present purposes, and likely in
> general, I'd think it would make sense to just always report the file
> mode.
> 
> A way to do that would be to change the above excerpt to read:
> 
> ...
> 206         }
> 207         if (keys & F_MODE)
> 208                 output(indent, &offset, "mode=%#o", p->fts_statp->st_mode & MBITS);
> ...
> 
> 
> Another alternative, in case there are use cases for the existing
> behavior, would be to provide either another "key" or a command-line
> flag that says "give me all the modes".
> 
> Am I the only one who would find such a change useful?
> 
> Thanks for any reality checks. :-}
> 
> Peace,
> david

At a glance I think the idea here is that when it outputs the directory
entry it outputs a /set line that has the directory's mode in it, and
then as it does the files in that directory it only needs to output a
mode= clause for a file if it differs from the most recent /set line.
(This is based on studying the code for about 30 seconds, so don't take
it as gospel.)

-- Ian




More information about the freebsd-hackers mailing list