Skip to content

Commit c4c945c

Browse files
committed
Use "Subprocess" for mount and init
This removed the need of storing the password in a temporary file. Instead we just pass it via stdin. Signed-off-by: moson-mo <[email protected]>
1 parent 29a0734 commit c4c945c

File tree

1 file changed

+20
-73
lines changed

1 file changed

+20
-73
lines changed

src/Gocrypt.vala

+20-73
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,39 @@
11
namespace Cryptor {
22
public class Gocrypt {
33
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";
248
}
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+
2515
if (standard_error != null && standard_error != "") {
2616
throw new Error (Quark.from_string ("Cryptor"), status, remove_color (standard_error));
2717
}
2818
}
2919

3020
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";
3623
if (ro) {
37-
cmd += "-ro ";
24+
cmd += "-ro";
3825
}
3926
if (reverse) {
40-
cmd += "-reverse ";
27+
cmd += "-reverse";
4128
}
4229
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;
5731
}
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+
5837
if (standard_error != null && standard_error != "") {
5938
throw new Error (Quark.from_string ("Cryptor"), status, remove_color (standard_error));
6039
}
@@ -88,40 +67,8 @@ namespace Cryptor {
8867
return standard_output;
8968
}
9069

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-
10070
private static string remove_color (string str) {
10171
return str.replace ("\033[31m", "").replace ("\033[33m", "").replace ("\033[0m", "");
10272
}
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-
*/
12673
}
12774
}

0 commit comments

Comments
 (0)