[net.unix-wizards] Summary of how to remove file with NULL names

megabyte@chinet.UUCP (Mark E. Sunderlin) (04/21/86)

Here is the consensis of the net on how to remove files with
strange and weird characters in there names.  Thanks to all the
below who responded to my queery.

uokmet!kwthomas,mtgzy!ecl,necis!geo,pegasus!hansen,BRL.ARPA!gwyn
jplgodo!steve ,and Robert Sanford whose address I lost in
the depths of vi and lines over 80 columns long.

>The files have characters over \177 (decimal = 127) in them.  /bin/sh doesn't
>expand such files correctly, as the upper bit is an internal flag meaning
>"this character is quoted" in sh.  So it loses the upper bits on the final
>parse pass or thereabouts.  So "rm" gets a nonexistent file name.
>---------------------------------------------------------------------
>Here is a program that uses the "unlink" system call to remove
>files with funky names. Be careful when using this program
>because it can remove just about anything it wants. You describe
>the desired file(s) by using valid filename characters or a
>backslash followed by three characters in the range of [0-7]
>(octal (gasp!) representation of the character). One example will
>follow the program.
>
>/*************************/
>/*
>	rmjunk.c
>
>	author: Robert Sanford
>	usage: rmjunk file [file ...]
>*/
>
>
>#include <stdio.h>
>#include <errno.h>
>
>
>main ( ac, av )
>	int ac;
>	char **av;
>{
>	char *ip;
>	char *op;
>	char buf[ 200 ];
>
>	for (av++; *av; av++)
>	{
>		for (ip = *av, op = buf; *ip;)
>			if (*ip == '\\')
>			{
>				*op++ = ((ip[1] - '0')*64) + ((ip[2] - '0')*8) +
>					(ip[3] - '0');
>				ip += 4;
>			}
>			else
>				*op++ = *ip++;
>		*op = 0;
>		if (unlink( buf ) < 0)
>		{
>			xdump( stderr, buf );
>			fprintf(stderr, "rmjunk(%s): FAILED errno=%d\n", *av,
>				errno);
>		}
>	}
>}
>
>xdump ( fp, bp )
>	FILE *fp;
>	char *bp;
>{
>	fputc( '"', fp );
>
>	for (; *bp; bp++)
>		if (*bp >= ' ' && *bp <= '~')
>			fputc( *bp, fp );
>		else
>			fprintf(fp, "\\%03o", *bp);
>
>	fputc( '"', fp );
>	fputc( '\n', fp );
>}
>/*************************/
>
>The best way to identify the exact name of the file you want to remove
>is to dump the directory:
>
>	od -c .
>
>0000000 005   `   .  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
>0000020  \0 002   .   .  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
>0000040 005   a   .   p   r   o   f   i   l   e  \0  \0  \0  \0  \0  \0
>0000100 005   c   .   n   e   w   s   r   c  \0  \0  \0  \0  \0  \0  \0
>0000140 005   s   .   m   a   c   s  \0  \0  \0  \0  \0  \0  \0  \0  \0
>0000160 005   }   .   n   e   w   s   _   t   i   m   e  \0  \0  \0  \0
>0000420 005 345   r   e   a   l   t   i   m   e   2  \0  \0  \0  \0  \0
>0000440 005 346  \001 e   p   a   r   t   2  \0  \0  \0  \0  \0  \0  \0
>
>(I'm assuming you have Sys.V fixed size directory entries.) In this example
>the last entry is garbage and needs to be removed. (The first two bytes of
>each entry are the i-node number. Isn't it convenient that the length of
>a directory entry is the same as the number of characters "od" displays
>on a line !) Using my "rmjunk" program, you would enter:
>
>	unlink '\001epart2'
>
>This SHOULD yield the desired results. :-)
-- 
_________________________________________________________________________
UUCP:	(1) seismo!dolqci!irsdcp!scsnet!sunder		Mark E. Sunderlin
	(2) ihnp4!chinet!megabyte			aka Dr. Megabyte
CIS:	74026,3235					(202) 634-2529
Quote:	"No matter where you go, There you are"		   (9-4 EST)
Mail:	IRS 1111 Constitution Ave. NW  PM:S:D:NO Washington, DC 20224  

ksbszabo@watvlsi.UUCP (Kevin Szabo) (04/22/86)

>I had some programs blow on me and they opened a file with
>a null file name. Almost as if I had done a 'open("",O_READ)' or such.
>Now, I have a few files in my directory that show up with names like
>!wvwu or u!wv.  I tried a 'rm -i *' and I got a 'rm: !wvu non-existent'
>error.

We had null-named file trouble on our 4.1 BSD based work station; an
OD of the directory showed the names to really be the null string!
I think someone forgot to SYNC before shutting the machine down.
I hope my solution is applicable to you.

These are the steps I followed:

1) fsck the file system
2) find the Inode numbers of the weirdly named files. (ls -i)
3) use the program 'clri' to clear the inodes of the bad files.
4) run fsck again and let it clean up the directories, free list and
   reference counts. (you lose the files UNLESS you zap the parent directory,
   in which case you get a whole bunch of unreferenced inodes that go
   into lost+found).

Null file names can be tricky because many systems interpret "" as
equivalent to "." (i.e. the current directory).  We had some null-named
directories in our file system; it was a confusing mess to figure
out.  It looked like I had a directory linked to itself, but
really LS was just getting the wrong info when it opened up the directory
called "".  I found that 'od' on the directory was useful to find these
problems.

Another useful proceedure is using 'rm -i -r' on the directory.
This technique keeps the shell from seeing the file names and
mangling non-ascii characters (8th bit = 1, etc.).

	Kevin Szabo'
-- 
Kevin Szabo'  ihnp4!watmath!watvlsi!ksbszabo  (VLSI Group,U of Waterloo,Ont,Can)

jwp@uwmacc.UUCP (Jeffrey W Percival) (04/23/86)

In article <306@chinet.UUCP> megabyte@chinet.UUCP (Mark E. Sunderlin) writes:
>Here is the consensis of the net on how to remove files with
>strange and weird characters in their names.
>
>uokmet!kwthomas,mtgzy!ecl,necis!geo,pegasus!hansen,BRL.ARPA!gwyn
>jplgodo!steve ,and Robert Sanford whose address I lost in
>the depths of vi and lines over 80 columns long.
>
>>The files have characters over \177 (decimal = 127) in them.
>>Here is a program that uses the "unlink" system call to remove
>>files with funky names.

I didn't see the original posting, but this does NOT work in 2.8BSD.
I had many such files created by a user doing a "split -2" on a
many thousand line file thinking it would result in 2 files, not
many 2-line files, and split merrily created files with bizarre names.
The *only* thing that worked was doing a clri on each file, then
doing a filecheck to clean things up.  I even ran a program that
opened and read the directory file, to make sure that the argument
to unlink() was *exactly* right, but to no avail.

Just thought you'd like to know.

p.s. Many thanks to all the folks who advised me on this problem last year...
-- 
	Jeff Percival ...!uwvax!uwmacc!jwp

mike@whuxl.UUCP (BALDWIN) (04/23/86)

> Here is the consensis of the net on how to remove files with
> strange and weird characters in there names.  Thanks to all the
> below who responded to my queery.
> 
> [rmjunk.c to remove files with eighth bit set]

There is a much simpler way to do this than using od -c and rmjunk.
First, find the i-number of the file using "ls -bi".  The -b flag
prints funny chars in \ddd notation (BSD or SVR2).  Then, if the
i-number is 341, say, do "find . -inum 341 -exec rm {} \;" to remove
it.
-- 
						Michael Baldwin
			(not the opinions of)	AT&T Bell Laboratories
						{at&t}!whuxl!mike