1 | // $Id: OptionFile.cc 1628 2008-11-17 21:24:18Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2007 Jari Häkkinen, Peter Johansson |
---|
5 | Copyright (C) 2008 Peter Johansson |
---|
6 | |
---|
7 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
8 | |
---|
9 | The yat library is free software; you can redistribute it and/or |
---|
10 | modify it under the terms of the GNU General Public License as |
---|
11 | published by the Free Software Foundation; either version 3 of the |
---|
12 | License, or (at your option) any later version. |
---|
13 | |
---|
14 | The yat library is distributed in the hope that it will be useful, |
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License |
---|
20 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
21 | */ |
---|
22 | |
---|
23 | #include "OptionFile.h" |
---|
24 | #include "Exception.h" |
---|
25 | #include "FileUtil.h" |
---|
26 | |
---|
27 | #include <string> |
---|
28 | |
---|
29 | namespace theplu { |
---|
30 | namespace yat { |
---|
31 | namespace utility { |
---|
32 | |
---|
33 | |
---|
34 | OptionFile::OptionFile(CommandLine& cmd, std::string flag, std::string desc, |
---|
35 | bool required, bool exist, std::string bits) |
---|
36 | : OptionArg<std::string>(cmd, flag, desc, required), exist_(exist), |
---|
37 | bits_(bits) {} |
---|
38 | |
---|
39 | |
---|
40 | OptionFile::~OptionFile(void) |
---|
41 | { |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | void OptionFile::do_validate2() const |
---|
46 | { |
---|
47 | if (!present()) |
---|
48 | return; |
---|
49 | FileUtil fu(value().c_str()); |
---|
50 | bool exists=false; |
---|
51 | try { |
---|
52 | exists = fu.exists(); |
---|
53 | } |
---|
54 | catch (IO_error& e) { |
---|
55 | std::stringstream ss; |
---|
56 | ss << "cannot stat `" << value() << "': " << strerror(errno); |
---|
57 | errno = 0; |
---|
58 | throw cmd_error(ss.str()); |
---|
59 | } |
---|
60 | if (exist_ && !exists){ |
---|
61 | std::stringstream ss; |
---|
62 | ss << "cannot stat `" << value() << "': No such file or directory"; |
---|
63 | throw cmd_error(ss.str()); |
---|
64 | } |
---|
65 | if (fu.permissions(bits_)) { |
---|
66 | // Peter, this loop is stupid but I wanna differentiate the error message |
---|
67 | for (std::string::const_iterator iter; iter!=bits_.end(); ++iter){ |
---|
68 | if (*iter=='r' && fu.permissions("r")){ |
---|
69 | std::stringstream ss; |
---|
70 | ss << "cannot open `" << value() << "' for reading: Permission denied"; |
---|
71 | throw cmd_error(ss.str()); |
---|
72 | } |
---|
73 | else if (*iter=='w' && fu.permissions("w")){ |
---|
74 | std::stringstream ss; |
---|
75 | ss << "cannot create file `" << value() |
---|
76 | << "': Permission denied"; |
---|
77 | throw cmd_error(ss.str()); |
---|
78 | } |
---|
79 | else if (*iter=='d' && fu.permissions("d")){ |
---|
80 | std::stringstream ss; |
---|
81 | ss << value() |
---|
82 | << "': Not a directory"; |
---|
83 | throw cmd_error(ss.str()); |
---|
84 | } |
---|
85 | } |
---|
86 | std::stringstream ss; |
---|
87 | ss << value() << ": Permission denied"; |
---|
88 | throw cmd_error(ss.str()); |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | }}} // of namespace utility, yat, and theplu |
---|