PageRenderTime 51ms CodeModel.GetById 7ms app.highlight 40ms RepoModel.GetById 2ms app.codeStats 0ms

/platform/unix/scaffold/directory.d

http://github.com/wilkie/djehuty
D | 231 lines | 158 code | 57 blank | 16 comment | 32 complexity | 69e086a9bc4b5eece184f889677b4d25 MD5 | raw file
  1/*
  2 * directory.d
  3 *
  4 * This Scaffold holds the Directory implementations for the Linux platform
  5 *
  6 * Author: Dave Wilkinson
  7 *
  8 */
  9
 10module scaffold.directory;
 11
 12import platform.unix.common;
 13import binding.c;
 14
 15import platform.vars.directory;
 16import platform.vars.file;
 17
 18import io.file;
 19import io.console;
 20
 21import core.definitions;
 22import core.string;
 23import core.main;
 24import data.list;
 25
 26bool DirectoryOpen(ref DirectoryPlatformVars dirVars, ref string path) {
 27	string pn;
 28	if (path == "") {
 29		pn = "/";
 30	}
 31	else {
 32		pn = path.dup;
 33	}
 34
 35	pn ~= '\0';
 36
 37	dirVars.dir = opendir(pn.ptr);
 38	return (dirVars.dir !is null);
 39}
 40
 41bool DirectoryCreate(ref DirectoryPlatformVars dirVars, ref string path) {
 42	if (DirectoryFileIsDir(path)) {
 43		return false;
 44	}
 45
 46	string makedir = "mkdir " ~ path ~ "\0";
 47
 48	system(makedir.ptr);
 49
 50	return DirectoryOpen(dirVars,path);
 51
 52}
 53bool DirectoryClose(ref DirectoryPlatformVars dirVars) {
 54	closedir(dirVars.dir);
 55	return true;
 56}
 57
 58string DirectoryGetBinary() {
 59	return "/usr/bin";
 60}
 61
 62string DirectoryGetAppData() {
 63	return "/usr/share/" ~ Djehuty.app.name;
 64}
 65
 66string DirectoryGetTempData() {
 67	return "/tmp/djp" ~ toStr(getpid());
 68}
 69
 70string DirectoryGetUserData() {
 71	static string cached;
 72
 73	// user data: $HOME/.{appname}
 74
 75	if (cached is null) {
 76		char* result;
 77		result = getenv("HOME\0"c.ptr);
 78
 79		char[] homePath;
 80
 81		char* cur = result;
 82
 83		int i;
 84		for(i = 0; *cur != '\0'; cur++, i++) {}
 85
 86		if (i != 0) {
 87			homePath = result[0..i];
 88			cached = homePath ~ "/." ~ Djehuty.app.name;
 89		}
 90		else {
 91			cached = "";
 92		}
 93	}
 94
 95	return cached;
 96}
 97
 98string DirectoryGetApp() {
 99	// Store result
100	static string cached = null;
101
102	if (cached is null) {
103		string procPath = "/proc/" ~ toStr(getpid()) ~ "/exe\0";
104
105		size_t ret = -1;
106		int len = 256;
107		char[] path;
108
109		while (ret == -1) {
110			path = new char[len];
111			ret = readlink(procPath.ptr, path.ptr, len-1);
112			len <<= 1;
113			if (ret == -1 && len > 32000) {
114				// Error, path is too long
115				cached = "";
116				return cached.dup;
117			}
118		}
119
120		cached = path[0..ret].dup;
121	}
122
123	return cached.dup;
124}
125
126string DirectoryGetCWD() {
127	uint len = 512;
128	char[] chrs;
129
130	char* ptr;
131
132	do {
133		chrs = new char[len+1];
134		ptr = getcwd(chrs.ptr, len);
135		len <<= 1;
136	} while (ptr is null);
137
138	foreach (int i, chr; chrs) {
139		if (chr == '\0') {
140			chrs = chrs[0..i];
141			break;
142		}
143	}
144
145	return chrs.dup;
146}
147
148bool DirectoryFileIsDir(string path) {
149	string newPath = path.dup;
150	newPath ~= '\0';
151
152	struct_stat inode;
153
154	if (stat(newPath.ptr, &inode) != -1) {
155		if (S_ISDIR(inode.st_mode)) {
156			return true;
157		}
158	}
159
160	return false;
161}
162bool DirectoryMove(ref string path, string newPath) {
163	string exec = "mv " ~ path ~ " " ~ newPath ~ "\0";
164
165	system(exec.ptr);
166	return true;
167}
168
169bool DirectoryCopy(ref string path, string newPath) {
170	string exec = "cp -r " ~ path ~ " " ~ newPath ~ "\0";
171
172	system(exec.ptr);
173	return true;
174}
175
176bool DirectoryRename(ref string path, ref string newName) {
177	string npath = path.dup;
178	npath ~= '\0';
179
180	string str;
181
182	foreach_reverse(int i, chr; path) {
183		if (chr == '/') {
184			// truncate
185			str = path[0..(i+1)].dup;
186			break;
187		}
188	}
189
190	if (str is null) { return false; }
191
192	str ~= newName;
193	str ~= '\0';
194	
195	rename(npath.ptr, str.ptr);
196	return true;
197}
198
199string[] DirectoryList(ref DirectoryPlatformVars dirVars, ref string path) {
200	if (!DirectoryOpen(dirVars, path)) { return null; } 
201
202	dirent* dir;
203	string[] list;
204
205	// Retrieve first directory
206	dir = readdir(dirVars.dir);
207
208	while(dir !is null) {
209		// Caculate Length of d_name
210		int len;
211
212		foreach(chr; dir.d_name) {
213			if (chr == '\0') {
214				break;
215			}
216			len++;
217		}
218
219		// Add to list
220		if (dir.d_name[0..len] != "." && dir.d_name[0..len] != "..") {
221			list ~= dir.d_name[0..len].dup;
222		}
223
224		// Retrieve next item in the directory
225		dir = readdir(dirVars.dir);
226	}
227
228	DirectoryClose(dirVars);
229
230	return list;
231}