From 73cf8658dacf406d6781cfb3b69ff428e47df943 Mon Sep 17 00:00:00 2001 From: "Brett T. Warden" Date: Thu, 26 Jan 2023 14:29:27 -0800 Subject: [PATCH 1/2] Add a newline after hostname entry Always add a newline after printing the hostname: entry, just in case userdata API call returns *just* the hostname. Update the test data to just such a bare hostname. Fixes #59 --- src/ucd-data-fetch.c | 15 ++++++++++----- tests/fetch_data/hostname | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/ucd-data-fetch.c b/src/ucd-data-fetch.c index d45ccdf..ffa87f1 100644 --- a/src/ucd-data-fetch.c +++ b/src/ucd-data-fetch.c @@ -445,11 +445,16 @@ int main(int argc, char *argv[]) { } /* don't write part #2 if 404 or some non-error */ - if ((result != 2) && (write_lines(out, f, cl, "hostname: ") != 0)) { - close(out); - fclose(f); - unlink(outpath); - FAIL("write_lines()"); + if (result != 2) { + if (write_lines(out, f, cl, "hostname: ") != 0) { + close(out); + fclose(f); + unlink(outpath); + FAIL("write_lines()"); + } + + /* Write an extra linefeed in case userdata hostname didn't end with one */ + write(out, "\n", 1); } /* cleanup */ diff --git a/tests/fetch_data/hostname b/tests/fetch_data/hostname index f162297..14c672e 100644 --- a/tests/fetch_data/hostname +++ b/tests/fetch_data/hostname @@ -1 +1 @@ -myhostname +myhostname \ No newline at end of file From 49696964288f2f5ec3f9c733553d39a2ac151936 Mon Sep 17 00:00:00 2001 From: "Brett T. Warden" Date: Thu, 26 Jan 2023 15:56:41 -0800 Subject: [PATCH 2/2] Make write_lines add a newline when necessary --- src/ucd-data-fetch.c | 29 ++++++++++++----------------- tests/fetch_data/expected | 1 - 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/ucd-data-fetch.c b/src/ucd-data-fetch.c index ffa87f1..d27a17d 100644 --- a/src/ucd-data-fetch.c +++ b/src/ucd-data-fetch.c @@ -227,6 +227,13 @@ static int write_lines(int out, FILE *f, size_t cl, const char *prefix) if (write(out, buf, len) < (ssize_t)len) { return 1; } + + /* Make sure this line ends with a newline when we write it */ + if (buf[len-1] != '\n') { + if (write(out, "\n", 1) < (ssize_t)1) { + return 1; + } + } } } @@ -384,13 +391,6 @@ int main(int argc, char *argv[]) { FAIL("write_lines()"); } - /* Write an extra linefeed in case this didn't end with one */ - if (write(out, "\n", 1) < (ssize_t) 1) { - close(out); - fclose(f); - unlink(outpath); - FAIL("write()"); - } close(sockfd); /* reopen socket */ @@ -445,16 +445,11 @@ int main(int argc, char *argv[]) { } /* don't write part #2 if 404 or some non-error */ - if (result != 2) { - if (write_lines(out, f, cl, "hostname: ") != 0) { - close(out); - fclose(f); - unlink(outpath); - FAIL("write_lines()"); - } - - /* Write an extra linefeed in case userdata hostname didn't end with one */ - write(out, "\n", 1); + if ((result != 2) && (write_lines(out, f, cl, "hostname: ") != 0)) { + close(out); + fclose(f); + unlink(outpath); + FAIL("write_lines()"); } /* cleanup */ diff --git a/tests/fetch_data/expected b/tests/fetch_data/expected index f29a748..3bb9691 100644 --- a/tests/fetch_data/expected +++ b/tests/fetch_data/expected @@ -6,7 +6,6 @@ ssh_authorized_keys: - SSH_TEST_KEY_STRING_1 - SSH_TEST_KEY_STRING_2 - SSH_TEST_KEY_STRING_3 - hostname: myhostname #cloud-config