Varie correzioni, completata revisione capitolo sull'I/O su file
[gapil.git] / listati / InitFile.c
1 ssize_t InitFile(int dirfd, const char *file, const char *buf, size_t size) 
2 {
3     int fd, written, res;
4     char path[PATH_MAX];
5    
6     fd = openat(dirfd, ".", O_TMPFILE|O_RDWR, S_IRUSR|S_IWUSR);
7     if (fd < 0) {
8         perror("Cannot get temporary filedescritor");
9         return(fd);
10     }
11     written = FullWrite(fd, buf, size);
12     if (written < 0) {
13         perror("error writing on tmp file");
14         return(res);
15     }
16     snprintf(path, PATH_MAX, "/proc/self/fd/%d", fd);
17     res = linkat(AT_FDCWD, path, dirfd, file, AT_SYMLINK_FOLLOW);
18     if (res < 0) {
19         perror("error linking the file");
20         return(res);
21     } else {
22         return written;
23     }
24 }