commit fc0e968

Devine Lu Linvega  ·  2025-01-22 19:16:02 +0000 UTC
parent b41bbed
Cleaning up file directory view
2 files changed,  +35, -30
+10, -3
 1@@ -10,17 +10,22 @@
 2 	;file .File/name DEO2
 3 	#0004 .File/length DEO2
 4 	;stat-mem DUP2 .File/stat DEO2
 5-	<pstr>
 6+	<pstr> #0a18 DEO
 7 	( | stat missing )
 8 	;file-missing .File/name DEO2
 9 	#0004 .File/length DEO2
10 	;stat-mem DUP2 .File/stat DEO2
11-	<pstr>
12+	<pstr> #0a18 DEO
13 	( | dir )
14 	;dir .File/name DEO2
15 	#0200 .File/length DEO2
16 	;mem DUP2 .File/read DEO2
17 	<pstr>
18+	( | dir nested )
19+	;dir-nested .File/name DEO2
20+	#0200 .File/length DEO2
21+	;mem-nested DUP2 .File/read DEO2
22+	<pstr>
23 	BRK
24 
25 @<pstr> ( str* -- )
26@@ -32,8 +37,10 @@
27 @file "README.md $1
28 @file-missing "WHAT.md $1
29 @dir ". $1
30+@dir-nested "src/ $1
31 
32 @stat-mem $5
33 
34-@mem "........
35+@mem $200
36+@mem-nested $200
37 
+25, -27
  1@@ -46,8 +46,7 @@ WITH REGARD TO THIS SOFTWARE.
  2 typedef struct {
  3 	FILE *f;
  4 	DIR *dir;
  5-	char current_filename[4096];
  6-	struct dirent *de;
  7+	char filepath[4096];
  8 	enum { IDLE,
  9 		FILE_READ,
 10 		FILE_WRITE,
 11@@ -91,13 +90,13 @@ put_stat(char *dest, Uint16 len, int size, int err, int dir, int capsize)
 12 }
 13 
 14 static Uint16
 15-put_statfile(char *dest, Uint16 len, const char *pathname, const char *basename)
 16+put_statfile(char *dest, Uint16 len, const char *filepath, const char *basename)
 17 {
 18 	int err, dir;
 19 	struct stat st;
 20 	if(len < strlen(basename) + 8)
 21 		return 0;
 22-	err = stat(pathname, &st);
 23+	err = stat(filepath, &st);
 24 	dir = S_ISDIR(st.st_mode);
 25 	dest += put_stat(dest, 4, st.st_size, err, dir, 1);
 26 	return snprintf(dest, len, dir ? " %s/\n" : " %s\n", basename) + 4;
 27@@ -110,7 +109,6 @@ file_reset(UxnFile *c)
 28 		fclose(c->f), c->f = NULL;
 29 	if(c->dir != NULL)
 30 		closedir(c->dir), c->dir = NULL;
 31-	c->de = NULL;
 32 	c->state = IDLE;
 33 	c->outside_sandbox = 0;
 34 }
 35@@ -118,32 +116,32 @@ file_reset(UxnFile *c)
 36 static Uint16
 37 file_read_dir(UxnFile *c, char *dest, Uint16 len)
 38 {
 39+	struct dirent *de = readdir(c->dir);
 40 	static char pathname[4352];
 41 	char *p = dest;
 42-	if(c->de == NULL) c->de = readdir(c->dir);
 43-	for(; c->de != NULL; c->de = readdir(c->dir)) {
 44+	for(; de != NULL; de = readdir(c->dir)) {
 45 		Uint16 n;
 46-		if(c->de->d_name[0] == '.' && c->de->d_name[1] == '\0')
 47+		if(de->d_name[0] == '.' && de->d_name[1] == '\0')
 48 			continue;
 49-		if(strcmp(c->de->d_name, "..") == 0) {
 50+		if(strcmp(de->d_name, "..") == 0) {
 51 			/* hide "sandbox/.." */
 52 			char cwd[PATH_MAX] = {'\0'}, *t;
 53 			/* Note there's [currently] no way of chdir()ing from uxn, so $PWD
 54 			 * is always the sandbox top level. */
 55 			getcwd(cwd, sizeof(cwd));
 56-			/* We already checked that c->current_filename exists so don't need a wrapper. */
 57-			t = realpath(c->current_filename, NULL);
 58+			/* We already checked that c->filepath exists so don't need a wrapper. */
 59+			t = realpath(c->filepath, NULL);
 60 			if(strcmp(cwd, t) == 0) {
 61 				free(t);
 62 				continue;
 63 			}
 64 			free(t);
 65 		}
 66-		if(strlen(c->current_filename) + 1 + strlen(c->de->d_name) < sizeof(pathname))
 67-			snprintf(pathname, sizeof(pathname), "%s/%s", c->current_filename, c->de->d_name);
 68+		if(strlen(c->filepath) + 1 + strlen(de->d_name) < sizeof(pathname))
 69+			snprintf(pathname, sizeof(pathname), "%s/%s", c->filepath, de->d_name);
 70 		else
 71 			pathname[0] = '\0';
 72-		n = put_statfile(p, len, pathname, c->de->d_name);
 73+		n = put_statfile(p, len, pathname, de->d_name);
 74 		if(!n) break;
 75 		p += n;
 76 		len -= n;
 77@@ -191,10 +189,10 @@ file_check_sandbox(UxnFile *c)
 78 {
 79 	char *x, *rp, cwd[PATH_MAX] = {'\0'};
 80 	x = getcwd(cwd, sizeof(cwd));
 81-	rp = retry_realpath(c->current_filename);
 82+	rp = retry_realpath(c->filepath);
 83 	if(rp == NULL || (x && pathcmp(cwd, rp, strlen(cwd)) != 0)) {
 84 		c->outside_sandbox = 1;
 85-		fprintf(stderr, "file warning: blocked attempt to access %s outside of sandbox\n", c->current_filename);
 86+		fprintf(stderr, "file warning: blocked attempt to access %s outside of sandbox\n", c->filepath);
 87 	}
 88 	free(rp);
 89 }
 90@@ -204,8 +202,8 @@ file_init(UxnFile *c, Uint16 addr)
 91 {
 92 	size_t max_len = 0x10000 - addr;
 93 	char *filename = (char *)&uxn.ram[addr];
 94-	char *p = c->current_filename;
 95-	size_t len = sizeof(c->current_filename);
 96+	char *p = c->filepath;
 97+	size_t len = sizeof(c->filepath);
 98 	file_reset(c);
 99 	if(len > max_len) len = max_len;
100 	while(len) {
101@@ -215,7 +213,7 @@ file_init(UxnFile *c, Uint16 addr)
102 		}
103 		len--;
104 	}
105-	c->current_filename[0] = '\0';
106+	c->filepath[0] = '\0';
107 	return 0;
108 }
109 
110@@ -228,9 +226,9 @@ file_read(UxnFile *c, Uint16 addr, int len)
111 	if(c->outside_sandbox) return 0;
112 	if(c->state != FILE_READ && c->state != DIR_READ) {
113 		file_reset(c);
114-		if((c->dir = opendir(c->current_filename)) != NULL)
115+		if((c->dir = opendir(c->filepath)) != NULL)
116 			c->state = DIR_READ;
117-		else if((c->f = fopen(c->current_filename, "rb")) != NULL)
118+		else if((c->f = fopen(c->filepath, "rb")) != NULL)
119 			c->state = FILE_READ;
120 	}
121 	if(c->state == FILE_READ)
122@@ -280,12 +278,12 @@ file_write(UxnFile *c, Uint16 addr, Uint16 len, Uint8 flags)
123 	if(len > 0x10000 - addr)
124 		len = 0x10000 - addr;
125 	if(c->outside_sandbox) return 0;
126-	ensure_parent_dirs(c->current_filename);
127+	ensure_parent_dirs(c->filepath);
128 	if(c->state != FILE_WRITE && c->state != DIR_WRITE) {
129 		file_reset(c);
130-		if(is_dir_path(c->current_filename))
131+		if(is_dir_path(c->filepath))
132 			c->state = DIR_WRITE;
133-		else if((c->f = fopen(c->current_filename, (flags & 0x01) ? "ab" : "wb")) != NULL)
134+		else if((c->f = fopen(c->filepath, (flags & 0x01) ? "ab" : "wb")) != NULL)
135 			c->state = FILE_WRITE;
136 	}
137 	if(c->state == FILE_WRITE) {
138@@ -293,7 +291,7 @@ file_write(UxnFile *c, Uint16 addr, Uint16 len, Uint8 flags)
139 			ret = 0;
140 	}
141 	if(c->state == DIR_WRITE) {
142-		ret = dir_exists(c->current_filename);
143+		ret = dir_exists(c->filepath);
144 	}
145 	return ret;
146 }
147@@ -305,7 +303,7 @@ file_stat(UxnFile *c, Uint16 addr, Uint16 len)
148 	struct stat st;
149 	if(c->outside_sandbox)
150 		return 0;
151-	err = stat(c->current_filename, &st);
152+	err = stat(c->filepath, &st);
153 	dir = S_ISDIR(st.st_mode);
154 	return put_stat(&uxn.ram[addr], len, st.st_size, err, dir, 0);
155 }
156@@ -313,7 +311,7 @@ file_stat(UxnFile *c, Uint16 addr, Uint16 len)
157 static Uint16
158 file_delete(UxnFile *c)
159 {
160-	return c->outside_sandbox ? 0 : unlink(c->current_filename);
161+	return c->outside_sandbox ? 0 : unlink(c->filepath);
162 }
163 
164 /* file registers */