Hey Andy (and list), According to the inotify man page, the name buffer will always be null-terminated. Furthermore, the name buffer seems to be allocated in chunks of 16 bytes. I have not found an official confirmation for that.
I the way to go would be: char lastchar = in->name[strlen(in->name)-1]; I have extended your test code by the following two lines: if(in->name == NULL) continue; printf("DD: strlen: %lu\n", strlen(in->name)); char lastchar = in->name[strlen(in->name)-1]; printf("DD: strlen last char: %c\n", lastchar); Here are results obtained with your + my debug output: For a short filename without ~ ADH: len: 16 ADH: name: shortfilename ADH: last char: DD: strlen: 13 DD: strlen last char: e For a short filename with ~ ADH: len: 16 ADH: name: shortfilename~ ADH: last char: DD: strlen: 14 DD: strlen last char: ~ For a long filename without ~ ADH: len: 32 ADH: name: veryverylongfilename ADH: last char: DD: strlen: 20 DD: strlen last char: e For a long filename with ~ ADH: len: 32 ADH: name: veryverylongfilename~ ADH: last char: DD: strlen: 21 DD: strlen last char: ~ Best regards, Dominik On 11.02.2018 12:57, Andy Hawkins wrote: > Hi, > > In article <slrnp80aau.nkj.a...@xcp-mailnews.gently.org.uk>, > Andy Hawkins<a...@gently.org.uk> wrote: >> In inotify.c, around line 236 is the following code block: >> >> /* ignore emacs backups and dotfiles */ >> if (in->len == 0 || >> in->name[in->len - 1] == '~' || >> (in->name[0] == '#' && in->name[in->len - 1] == '#') || >> in->name[0] == '.') >> continue; >> >> However, if I create a file called 'fred~' in the directory I've specified >> using dhcp-hostsdir I still get an event in syslog that shows this file is >> being processed: >> >> Feb 11 11:14:34 xcp-gateway dnsmasq[1039]: inotify, new or changed file >> /etc/dnsmasq/dhcp-hosts.d/fred~ > Ok, I've done some debugging. I added the following lines: > > my_syslog(LOG_INFO, "ADH: len: %d", in->len); > my_syslog(LOG_INFO, "ADH: name: %s", in->name); > my_syslog(LOG_INFO, "ADH: last char: %c", in->name[in->len - 1]); > > And I get the following output: > > dnsmasq: ADH: len: 16 > dnsmasq: ADH: name: fred4~ > dnsmasq: ADH: last char: > > So, it appears that the length in in->len is being interpreted correctly. > > According to the inotify man page: > > The len field counts all of the bytes in name, including the null > bytes; the length of each inotify_event structure is thus > sizeof(struct inotify_event)+len. > > So in fact, 'len' seems to be a fixed length, irrespective of the length of > the file name in the 'name' field. > > It looks like the check should actually be something like: > > /* ignore emacs backups and dotfiles */ > if (in->len == 0 || > in->name[strlen(in->name) - 1] == '~' || > (in->name[0] == '#' && in->name[strlen(in->name) - 1] == '#') || > in->name[0] == '.') > > I guess you may need to check that there's a null in the name somewhere > before using strlen, otherwise you might end up running off the end of the > string. I don't know inotify well enough to know if there's guaranteed to be > a null in there somewhere. The manpage does say that the name field is null > terminated, but I don't know if that's guaranteed or not. > > I could have a look at submitting a patch, but my editor is showing some > very strange indentation of the source, so I suspect I have my tab settings > incorrect. What is the standard setting for tabs on the dnasmasq source > files? > > Thanks > > Andy > > > _______________________________________________ > Dnsmasq-discuss mailing list > Dnsmasq-discuss@lists.thekelleys.org.uk > http://lists.thekelleys.org.uk/mailman/listinfo/dnsmasq-discuss _______________________________________________ Dnsmasq-discuss mailing list Dnsmasq-discuss@lists.thekelleys.org.uk http://lists.thekelleys.org.uk/mailman/listinfo/dnsmasq-discuss