/****************************************************************
*
* Program test_linkat.c:
- * Program to test use of linkat and O_TMPFILE and other combinations
+ * Program to test use of linkat with an O_TMPFILE or linking another file
+ * using AT_EMPTY_PATH or AT_SYMLINK_FOLLOW on /proc/self/fd/N
+ * giving the option to wait to remove source file
*
* Author: Simone Piccardi
* Oct. 2018
#include <sys/types.h>
#include <fcntl.h>
+
/* Help printing routine */
void usage(void);
}
char *path, *dir, *file;
char pattern[] = "prova prova prova\n";
+ char outbuff[2048];
path = strdup(argv[optind]);
file = basename(argv[optind]);
dir = dirname(argv[optind]);
- printf("Working on dir %s file: %s\nwith path: %s\n",
+ printf("Destination on dir %s file: %s\n with path: %s\n",
dir, file, path);
int fd, newfd;
if ( ! srcfile ) {
+ // temp file with O_TMPFILE
fd = open(dir, O_TMPFILE|O_RDWR,S_IRUSR|S_IWUSR);
if ( fd <= 0 )
perror("cannot open TMPFILE");
else
printf("saved %d chars on fd %d\n", i, fd);
} else {
- printf("source is %s\n", srcfile);
+ // user provided source
+ printf("source file is %s\n", srcfile);
if ( (fd = open(srcfile, O_RDONLY)) < 0 ) {
perror("cannot open source");
exit(1);
}
+ printf("waiting %d second to remove the file\n", wait);
+ sleep(wait);
}
newfd = open(dir, O_PATH|O_RDWR);
int err;
err = linkat(fd, "", newfd, file, AT_EMPTY_PATH);
if ( err < 0 ) {
- perror("error on creating TMPFILE");
- printf("cannot link fd %d on fd %d, %s with AT_EMPTY_PATH\n",
+ perror("error on creating TMPFILE with AT_EMPTY_PATH");
+ printf("cannot link fd %d on fd %d/%s with AT_EMPTY_PATH\n",
fd, newfd, file);
char fdpath[PATH_MAX];
snprintf(fdpath, PATH_MAX, "/proc/self/fd/%d", fd);
- printf("fd path %s\n", fdpath);
+ printf("Attempt to use AT_SYMLINK_FOLLOW with path %s\n", fdpath);
err = linkat(AT_FDCWD, fdpath, newfd, file, AT_SYMLINK_FOLLOW);
if ( err < 0 ) {
perror("still error creating TMPFILE");
- sleep(wait);
exit(1);
- }
+ } else {
+ printf("Success, fd %d on fd %d/%s created\n", fd, newfd, file);
+ }
if ( fchmodat(newfd, file,S_IRUSR|S_IWUSR,0) < 0 )
perror("Cannot change permission to new file");
}
*/
void usage(void) {
printf("Program testlinkat : test linkat for a file \n");
+ printf("Create a link to a tempfile or a link to -f indicated file");
printf("Usage:\n");
- printf(" testlinkat [-h] file mode \n");
+ printf(" testlinkat [-h] destfilepathname \n");
printf(" -h print this help\n");
printf(" -w [N] wait N seconds\n");
printf(" -f [file] use file as source\n");