1 | /*
|
---|
2 | * Copyright (c) 2008 Tim Post
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /*
|
---|
30 | * TODO: Options that people would expect, such as specifying the access time,
|
---|
31 | * etc.
|
---|
32 | */
|
---|
33 |
|
---|
34 | #include <stdio.h>
|
---|
35 | #include <stdlib.h>
|
---|
36 | #include <stdint.h>
|
---|
37 | #include <dirent.h>
|
---|
38 | #include <str.h>
|
---|
39 | #include <getopt.h>
|
---|
40 | #include <errno.h>
|
---|
41 | #include <vfs/vfs.h>
|
---|
42 |
|
---|
43 | #include "config.h"
|
---|
44 | #include "errors.h"
|
---|
45 | #include "util.h"
|
---|
46 | #include "entry.h"
|
---|
47 | #include "touch.h"
|
---|
48 | #include "cmds.h"
|
---|
49 |
|
---|
50 | static const char *cmdname = "touch";
|
---|
51 |
|
---|
52 | static struct option const long_options[] = {
|
---|
53 | { "no-create", no_argument, 0, 'c' },
|
---|
54 | { 0, 0, 0, 0 }
|
---|
55 | };
|
---|
56 |
|
---|
57 | /* Dispays help for touch in various levels */
|
---|
58 | void help_cmd_touch(unsigned int level)
|
---|
59 | {
|
---|
60 | if (level == HELP_SHORT) {
|
---|
61 | printf("`%s' updates access times of files\n", cmdname);
|
---|
62 | } else {
|
---|
63 | help_cmd_touch(HELP_SHORT);
|
---|
64 | printf("Usage: `%s' [-c|--no-create] <file>...\n\n"
|
---|
65 | "If the file does not exist it will be created empty,\n"
|
---|
66 | "unless -c (--no-create) is supplied.\n\n"
|
---|
67 | "Options:\n"
|
---|
68 | " -c, --no-create Do not create new files\n",
|
---|
69 | cmdname);
|
---|
70 | }
|
---|
71 |
|
---|
72 | return;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /* Main entry point for touch, accepts an array of arguments */
|
---|
76 | int cmd_touch(char **argv)
|
---|
77 | {
|
---|
78 | unsigned int argc = cli_count_args(argv);
|
---|
79 | unsigned int i = 0;
|
---|
80 | unsigned int ret = 0;
|
---|
81 | int c;
|
---|
82 | int longind;
|
---|
83 | bool no_create = false;
|
---|
84 | vfs_stat_t file_stat;
|
---|
85 | int fd = -1;
|
---|
86 | char *buff = NULL;
|
---|
87 |
|
---|
88 | DIR *dirp;
|
---|
89 |
|
---|
90 | for (c = 0, optreset = 1, optind = 0, longind = 0; c != -1; ) {
|
---|
91 | c = getopt_long(argc, argv, "c", long_options, &longind);
|
---|
92 | switch (c) {
|
---|
93 | case 'c':
|
---|
94 | no_create = true;
|
---|
95 | break;
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | if (argc - optind < 1) {
|
---|
100 | printf("%s: Incorrect number of arguments. Try `help %s extended'\n",
|
---|
101 | cmdname, cmdname);
|
---|
102 | return CMD_FAILURE;
|
---|
103 | }
|
---|
104 |
|
---|
105 | for (i = optind; argv[i] != NULL; i++) {
|
---|
106 | buff = str_dup(argv[i]);
|
---|
107 | if (buff == NULL) {
|
---|
108 | cli_error(CL_ENOMEM, "Out of memory");
|
---|
109 | ret++;
|
---|
110 | continue;
|
---|
111 | }
|
---|
112 |
|
---|
113 | dirp = opendir(buff);
|
---|
114 | if (dirp) {
|
---|
115 | cli_error(CL_ENOTSUP, "`%s' is a directory", buff);
|
---|
116 | closedir(dirp);
|
---|
117 | free(buff);
|
---|
118 | ret++;
|
---|
119 | continue;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /* Check whether file exists if -c (--no-create) option is given */
|
---|
123 | if ((!no_create) ||
|
---|
124 | ((no_create) && (vfs_stat_path(buff, &file_stat) == EOK))) {
|
---|
125 | errno_t rc = vfs_lookup(buff, WALK_REGULAR | WALK_MAY_CREATE, &fd);
|
---|
126 | if (rc != EOK) {
|
---|
127 | fd = -1;
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (fd < 0) {
|
---|
132 | cli_error(CL_EFAIL, "Could not update or create `%s'", buff);
|
---|
133 | free(buff);
|
---|
134 | ret++;
|
---|
135 | continue;
|
---|
136 | } else {
|
---|
137 | vfs_put(fd);
|
---|
138 | fd = -1;
|
---|
139 | }
|
---|
140 |
|
---|
141 | free(buff);
|
---|
142 | }
|
---|
143 |
|
---|
144 | if (ret)
|
---|
145 | return CMD_FAILURE;
|
---|
146 | else
|
---|
147 | return CMD_SUCCESS;
|
---|
148 | }
|
---|