ref: 57bffefe7e30fae10caae16aa36fcb1150ddc46f
parent: fae5aebbaf7a6061b4a0d3f90df86153383ce44d
author: Jacob Moody <moody@posixcafe.org>
date: Mon Nov 24 01:03:14 EST 2025
ls: work for non root directories
--- a/ls.c
+++ b/ls.c
@@ -18,6 +18,7 @@
S3 s3;
int i;
char path[512];
+ char *pp;
int p[2];
Biobuf *b[2];
Xelem *x;
@@ -24,6 +25,7 @@
tmfmtinstall();
fmtinstall('H', encodefmt);+ s3fmtinstall();
i = parseargs(&s3, argc, argv);
argc -= i;
argv += i;
@@ -42,7 +44,12 @@
b[0] = Bfdopen(p[0], OWRITE);
if(b[0] == nil)
sysfatal("Bfdopen: %r");- download(&s3, path, b[0], s3get);
+ if(path[0] == '\0')
+ pp = path;
+ else
+ pp = smprint("?list-type=2&prefix=%U", path);+ fprint(2, "%s\n", pp);
+ download(&s3, pp, b[0], s3get);
Bterm(b[0]);
exits(nil);
default:
--- a/s3.c
+++ b/s3.c
@@ -6,6 +6,41 @@
#include <auth.h>
#include "s3.h"
+/*
+ * S3 requires that all reserved characters within a query
+ * are URL-encoded, despite the spec not mandating it.
+ */
+static int
+queryvalfmt(Fmt *f)
+{+ static char hex[] = "0123456789ABCDEF";
+ char *s;
+ int n;
+
+ s = va_arg(f->args, char*);
+ n = 0;
+ while(*s != '\0'){+ switch(*s){+ case 'a'...'z':
+ case 'A'...'Z':
+ case '0'...'9':
+ n += fmtprint(f, "%c", *s);
+ break;
+ default:
+ n += fmtprint(f, "%%%c%c", hex[(*s)>>4], hex[(*s)&15]);
+ break;
+ }
+ s++;
+ }
+ return n;
+}
+
+void
+s3fmtinstall(void)
+{+ fmtinstall('U', queryvalfmt);+}
+
typedef struct {uchar *payhash;
char *mime;
--- a/s3.h
+++ b/s3.h
@@ -24,3 +24,6 @@
void hclose(Hcon *con);
int hdone(Hcon *con);
+
+#pragma varargck type "U" char*
+void s3fmtinstall(void);
--
⑨