Trattazione di linkat e openat con O_TMPFILE, con programmi di test ed
[gapil.git] / sources / test_initfile.c
1 /* test_initfile.c
2  * 
3  * Copyright (C) 2019 Simone Piccardi
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /****************************************************************
20  *
21  * Program test_linkat.c: 
22  * Program to test use of linkat with an O_TMPFILE or linking another file 
23  * using AT_EMPTY_PATH or AT_SYMLINK_FOLLOW on /proc/self/fd/N
24  * giving the option to wait to remove source file
25  *
26  * Author: Simone Piccardi
27  * Oct. 2018
28  *
29  * Usage: testlinkat -h give all info's
30  *
31  ****************************************************************/
32 /* 
33  * Include needed headers
34  */
35 #define _GNU_SOURCE
36 #include <errno.h>       /* error definitions and routines */ 
37 #include <stdlib.h>      /* C standard library */
38 #include <unistd.h>      /* unix standard library */
39 #include <stdio.h>       /* standard I/O library */
40 #include <string.h>      /* C strings library */
41 #include <limits.h>
42 #include <libgen.h>      /* needed for dirname e basename */
43 #include <sys/stat.h>
44 #include <sys/types.h>
45 #include <fcntl.h>
46
47 #include "Gapil.h"
48 #include "macros.h"
49
50 /* Help printing routine */
51 void usage(void);
52
53 int main(int argc, char *argv[])
54 {
55 /* 
56  * Variables definition  
57  */
58     /*
59      * Input section: decode command line parameters 
60      * Use getopt function
61      */
62     int i;
63     opterr = 0;  /* don't want writing to stderr */
64     while ( (i = getopt(argc, argv, "hw:f:")) != -1) {
65         switch (i) {
66         /* 
67          * Handling options 
68          */ 
69         case 'h':   /* help option */
70             printf("Wrong -h option use\n");
71             usage();
72             return -1;
73             break;
74         case '?':   /* unrecognized options */
75             printf("Unrecognized options -%c\n",optopt);
76             usage();
77         default:    /* should not reached */
78             usage();
79         }
80     }
81     /* ***********************************************************
82      * 
83      *           Options processing completed
84      *
85      *                Main code beginning
86      * 
87      * ***********************************************************/
88     /* There must be 1 remaing parameters */
89     if ( (argc-optind) != 2 )  {
90         printf("From %d arguments, removed %d options\n", argc, optind);
91         usage();
92     }
93     char *path, *dir, *file;
94     path = strdup(argv[optind]);
95     file = basename(argv[optind]);
96     dir = dirname(argv[optind]);
97     printf("Destination on dir %s file: %s\n            with path: %s\n",
98            dir, file, path);
99     int newfd, res, count;
100     count = strlen(argv[optind+1]);
101     newfd = open(dir, O_PATH|O_RDWR);
102     res = InitFile(newfd, file, argv[optind+1], count);
103     free(path);
104     return 0;
105 }
106 /*
107  * routine to print usage info and exit
108  */
109 void usage(void) {
110     printf("Program test_initfile : test initfile  \n");
111     printf("Create a link to a tempfile or a link to -f indicated file");
112     printf("Usage:\n");
113     printf("  test_initfile [-h] pathname 'test to be written on pathname' \n");
114     printf("  -h           print this help\n");
115     
116     exit(1);
117 }
118