Move all command implementations into a single 'commands' directory.
[pspp] / src / language / commands / permissions.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004, 2010, 2011 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24
25 #include "data/settings.h"
26 #include "language/command.h"
27 #include "language/lexer/lexer.h"
28 #include "libpspp/cast.h"
29 #include "libpspp/i18n.h"
30 #include "libpspp/message.h"
31 #include "libpspp/misc.h"
32 #include "libpspp/str.h"
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36
37 enum PER {PER_RO, PER_RW};
38
39 int change_permissions(const char *file_name, enum PER per);
40
41
42 /* Parses the PERMISSIONS command. */
43 int
44 cmd_permissions (struct lexer *lexer, struct dataset *ds UNUSED)
45 {
46   if (settings_get_safer_mode ())
47     {
48       lex_next_error (lexer, -1, -1,
49                       _("This command not allowed when the %s option is set."),
50                       "SAFER");
51       return 0;
52     }
53
54   char  *fn = NULL;
55   const char *str = NULL;
56   lex_match (lexer, T_SLASH);
57
58   if (lex_match_id (lexer, "FILE"))
59     lex_match (lexer, T_EQUALS);
60
61   str = lex_tokcstr (lexer);
62   if (str)
63     fn = strdup (str);
64
65   if (!lex_force_match (lexer, T_STRING) || str == NULL)
66     goto error;
67
68   lex_match (lexer, T_SLASH);
69
70   if (! lex_match_id (lexer, "PERMISSIONS"))
71     goto error;
72
73   lex_match (lexer, T_EQUALS);
74
75   if (lex_match_id (lexer, "READONLY"))
76     {
77       if (! change_permissions (fn, PER_RO))
78         goto error;
79     }
80   else if (lex_match_id (lexer, "WRITEABLE"))
81     {
82       if (! change_permissions (fn, PER_RW))
83         goto error;
84     }
85   else
86     {
87       lex_error_expecting (lexer, "WRITEABLE", "READONLY");
88       goto error;
89     }
90
91   free (fn);
92
93   return CMD_SUCCESS;
94
95  error:
96
97   free(fn);
98
99   return CMD_FAILURE;
100 }
101
102
103
104 int
105 change_permissions (const char *file_name, enum PER per)
106 {
107   char *locale_file_name;
108   struct stat buf;
109   mode_t mode;
110
111   locale_file_name = utf8_to_filename (file_name);
112   if (-1 == stat(locale_file_name, &buf))
113     {
114       const int errnum = errno;
115       msg (SE, _("Cannot stat %s: %s"), file_name, strerror(errnum));
116       free (locale_file_name);
117       return 0;
118     }
119
120   if (per == PER_RW)
121     mode = buf.st_mode | 0200;
122   else
123     mode = buf.st_mode & ~0222;
124
125   if (-1 == chmod(locale_file_name, mode))
126
127     {
128       const int errnum = errno;
129       msg (SE, _("Cannot change mode of %s: %s"), file_name, strerror(errnum));
130       free (locale_file_name);
131       return 0;
132     }
133
134   free (locale_file_name);
135
136   return 1;
137 }