|
1 | 1 | namespace Cryptor {
|
2 | 2 | public class Gocrypt {
|
3 | 3 | public static void init_vault (string path, string password, bool reverse) throws Error {
|
4 |
| - string standard_error; |
5 |
| - int status; |
6 |
| - |
7 |
| - var pfile = write_password_file (password); |
8 |
| - try { |
9 |
| - string cmd = "gocryptfs -init -q "; |
10 |
| - if (reverse) { |
11 |
| - cmd += "-reverse "; |
12 |
| - } |
13 |
| - cmd += " -passfile " + pfile.get_path () + " -- " + path; |
14 |
| - Process.spawn_command_line_sync (cmd, null, out standard_error, out status); |
15 |
| - } catch (Error e) { |
16 |
| - throw e; |
17 |
| - } finally { |
18 |
| - if (pfile != null) { |
19 |
| - try { |
20 |
| - pfile.delete (); |
21 |
| - } catch (Error e) { |
22 |
| - } |
23 |
| - } |
| 4 | + string ? standard_error; |
| 5 | + string cmd = "gocryptfs -init -q"; |
| 6 | + if (reverse) { |
| 7 | + cmd += "-reverse"; |
24 | 8 | }
|
| 9 | + cmd += " -- " + path; |
| 10 | + |
| 11 | + var sp = new Subprocess.newv (cmd.split (" "), SubprocessFlags.STDIN_PIPE | SubprocessFlags.STDERR_PIPE | SubprocessFlags.STDOUT_PIPE); |
| 12 | + sp.communicate_utf8 (password + "\n" + password + "\n", null, null, out standard_error); |
| 13 | + var status = sp.get_exit_status (); |
| 14 | + |
25 | 15 | if (standard_error != null && standard_error != "") {
|
26 | 16 | throw new Error (Quark.from_string ("Cryptor"), status, remove_color (standard_error));
|
27 | 17 | }
|
28 | 18 | }
|
29 | 19 |
|
30 | 20 | public static void mount_vault (string path, string mountpoint, string password, bool ro, bool reverse, string custom_options) throws Error {
|
31 |
| - string standard_error; |
32 |
| - int status; |
33 |
| - |
34 |
| - var pfile = write_password_file (password); |
35 |
| - var cmd = "gocryptfs -q "; |
| 21 | + string ? standard_error; |
| 22 | + var cmd = "gocryptfs -q"; |
36 | 23 | if (ro) {
|
37 |
| - cmd += "-ro "; |
| 24 | + cmd += "-ro"; |
38 | 25 | }
|
39 | 26 | if (reverse) {
|
40 |
| - cmd += "-reverse "; |
| 27 | + cmd += "-reverse"; |
41 | 28 | }
|
42 | 29 | if (custom_options != "") {
|
43 |
| - cmd += custom_options + " "; |
44 |
| - } |
45 |
| - cmd += " -passfile " + pfile.get_path () + " -- " + path + " " + mountpoint; |
46 |
| - try { |
47 |
| - Process.spawn_command_line_sync (cmd, null, out standard_error, out status); |
48 |
| - } catch (Error e) { |
49 |
| - throw e; |
50 |
| - } finally { |
51 |
| - if (pfile != null) { |
52 |
| - try { |
53 |
| - pfile.delete (); |
54 |
| - } catch (Error e) { |
55 |
| - } |
56 |
| - } |
| 30 | + cmd += custom_options; |
57 | 31 | }
|
| 32 | + cmd += " -- " + path + " " + mountpoint; |
| 33 | + var sp = new Subprocess.newv (cmd.split (" "), SubprocessFlags.STDIN_PIPE | SubprocessFlags.STDERR_PIPE | SubprocessFlags.STDOUT_PIPE); |
| 34 | + sp.communicate_utf8 (password + "\n", null, null, out standard_error); |
| 35 | + var status = sp.get_exit_status (); |
| 36 | + |
58 | 37 | if (standard_error != null && standard_error != "") {
|
59 | 38 | throw new Error (Quark.from_string ("Cryptor"), status, remove_color (standard_error));
|
60 | 39 | }
|
@@ -88,40 +67,8 @@ namespace Cryptor {
|
88 | 67 | return standard_output;
|
89 | 68 | }
|
90 | 69 |
|
91 |
| - private static File write_password_file (string password) throws Error { |
92 |
| - FileIOStream ps; |
93 |
| - var temp_file = File.new_tmp (null, out ps); |
94 |
| - var dos = new DataOutputStream (ps.output_stream); |
95 |
| - dos.put_string (password); |
96 |
| - dos.close (); |
97 |
| - return temp_file; |
98 |
| - } |
99 |
| - |
100 | 70 | private static string remove_color (string str) {
|
101 | 71 | return str.replace ("\033[31m", "").replace ("\033[33m", "").replace ("\033[0m", "");
|
102 | 72 | }
|
103 |
| - |
104 |
| - /* |
105 |
| - public static void init_vault_stdin (string path, string password) throws Error { |
106 |
| -
|
107 |
| - string[] com = { "gocryptfs", "-init", path }; |
108 |
| - Pid pid; |
109 |
| - int sin, sout, serr; |
110 |
| -
|
111 |
| - Process.spawn_async_with_pipes (Environment.get_current_dir (), com, Environ.get (), SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD, null, out pid, out sin, out sout, out serr); |
112 |
| -
|
113 |
| - var output = FileStream.fdopen (sout, "r"); |
114 |
| - string ? o; |
115 |
| - while ((o = output.read_line ()) != null) { |
116 |
| - print ("%s\n", o); |
117 |
| - if (o == "Reading Password from stdin") { |
118 |
| - var input = FileStream.fdopen (sin, "w"); |
119 |
| - input.write (password.data); |
120 |
| - input.write ({ '\n' }); |
121 |
| - input.write (password.data); |
122 |
| - } |
123 |
| - } |
124 |
| - } |
125 |
| - */ |
126 | 73 | }
|
127 | 74 | }
|
0 commit comments