F GETPATH

From Attie's Wiki
Revision as of 09:31, 23 December 2013 by Attie (Talk | contribs)

Jump to: navigation, search

This is untested code...

	char fname[PATH_MAX];
#if 0
	/* this would be nice... */
	if (fcntl(fd, F_GETPATH, fname) == -1) {
		strerror_r(errno, es, ES_LEN);
		fprintf(stderr, "fcntl(): %s\n", es);
		return -1;
	}
#else
	{
		const char t[PATH_MAX];
		struct stat a, b;
		snprintf(t, sizeof(t), "/proc/self/fd/%d", fd);
		if (readlink(t, fname, sizeof(fname)) == -1) {
			strerror_r(errno, es, ES_LEN);
			fprintf(stderr, "readlink(%s): %s\n", t, es);
			return -1;
		}
 
		if (stat(fname, &a) == -1) {
			strerror_r(errno, es, ES_LEN);
			fprintf(stderr, "stat(%s): %s\n", fname, es);
			return -1;
		}
		if (stat(t, &b) == -1) {
			strerror_r(errno, es, ES_LEN);
			fprintf(stderr, "stat(%s): %s\n", t, es);
			return -1;
		}
 
		if (a.st_dev != b.st_dev || a.st_ino != b.st_ino) {
			fprintf(stderr, "get_db(): tempoary file lost...\n");
			return -1;
		}
	}
#endif
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox