On Sun, 2006-01-08 at 21:37 +0000, Tim Blechmann wrote:
> thanks for your quick response ...
> 
> > What version of jfsutils are you running?   Older versions have a
> > problem creating an invalid lost+found directory (1.1.4 or older).  If
> > this is the case, running "fsck -f" with a jfsutils-1.1.8 or newer
> > (1.1.10 is the latest) may fix it.
> 
> the live-cd, i used to check the file system uses jfsutils 1.1.8 ...
> any other idea? 

Well, if for some reason fsck is still creating the lost+found directory
wrong for some reason, this is a tool I wrote back before fsck was able
to fix it itself.  It should be run from the root of the problem
filesystem (as root).

It reads the directory one entry at a time and moves that file to a new
directory.  This avoids problems of readdir getting "lost" and returning
entries infinitely.

> thanks .... tim
> 
-- 
David Kleikamp
IBM Linux Technology Center
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

main()
{
	DIR *lost_found;
	struct dirent *entry;
	char from[512];
	char to[512];
	char *from_ptr, *to_ptr;
	int num_moved;

	lost_found = opendir("lost+found");
	if (!lost_found) {
		fprintf(stderr, "lost+found not found.\n");
		exit(1);
	}

	if (mkdir("l+f", 0700)) {
		fprintf(stderr, "Could not create l+f\n");
		exit(1);
	}

	strcpy(from, "lost+found/");
	strcpy(to, "l+f/");
	from_ptr = from + strlen(from);
	to_ptr = to + strlen(to);

	while (1) {
		num_moved = 0;
		rewinddir(lost_found);
		while (entry = readdir(lost_found)) {
			if ((strcmp(entry->d_name, ".") == 0) ||
			    (strcmp(entry->d_name, "..") == 0))
				continue;

			strcpy(from_ptr, entry->d_name);
			strcpy(to_ptr, entry->d_name);
			if (rename(from, to)) {
				fprintf(stderr, "Failed to rename %s to %s\n",
					from, to);
				exit(1);
			}
			num_moved++;
		}
		if (num_moved)
			printf("Moved %d objects from lost+found to l+f\n",
			       num_moved);
		else
			break;
	}

	if (closedir(lost_found)) {
		fprintf(stderr, "closedir failed\n");
		exit(1);
	}

	if (rmdir("lost+found")) {
		fprintf(stderr, "failed to remove lost+found\n");
		exit(1);
	}

	if (rename("l+f", "lost+found")) {
		fprintf(stderr, "failed to rename l+r to lost+found\n");
		exit(1);
	}
	printf("l+f renamed to lost+found\n");

	exit(0);
}

Reply via email to