From c5e37e726177856fa06bcc2b4294fc63ae6fa1ef Mon Sep 17 00:00:00 2001 From: Michael Vrable Date: Sat, 21 Apr 2007 11:05:28 -0700 Subject: [PATCH] Add URI-style escaping of filename characters. --- Makefile | 2 +- format.cc | 36 ++++++++++++++++++++++++++++++++++++ format.h | 15 +++++++++++++++ scandir.cc | 3 ++- tarstore.cc | 2 +- 5 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 format.cc create mode 100644 format.h diff --git a/Makefile b/Makefile index eb83a3d..b71e37b 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ CXXFLAGS=-O -Wall -D_FILE_OFFSET_BITS=64 -g -pg \ `pkg-config --cflags $(PACKAGES)` LDFLAGS=-g -pg -ltar `pkg-config --libs $(PACKAGES)` -SRCS=scandir.cc sha1.cc store.cc tarstore.cc +SRCS=format.cc scandir.cc sha1.cc store.cc tarstore.cc OBJS=$(SRCS:.cc=.o) scandir : $(OBJS) diff --git a/format.cc b/format.cc new file mode 100644 index 0000000..e6e11bc --- /dev/null +++ b/format.cc @@ -0,0 +1,36 @@ +/* LBS: An LFS-inspired filesystem backup system + * Copyright (C) 2007 Michael Vrable + * + * Utility functions for converting various datatypes to text format (and + * later, for parsing them back, perhaps). + */ + +#include +#include + +#include +#include + +using std::string; + +/* Perform URI-style escaping of a string. Bytes which cannot be represented + * directly are encoded in the form %xx (where "xx" is a string of two + * hexadecimal digits). */ +string uri_encode(const string &in) +{ + string out; + + for (size_t i = 0; i < in.length(); i++) { + unsigned char c = in[i]; + + if (c > '%' && c <= 0x7f) { + out += c; + } else { + char buf[4]; + sprintf(buf, "%%%02x", c); + out += buf; + } + } + + return out; +} diff --git a/format.h b/format.h new file mode 100644 index 0000000..6e6784b --- /dev/null +++ b/format.h @@ -0,0 +1,15 @@ +/* LBS: An LFS-inspired filesystem backup system + * Copyright (C) 2006 Michael Vrable + * + * Utility functions for converting various datatypes to text format (and + * later, for parsing them back, perhaps). + */ + +#ifndef _LBS_FORMAT_H +#define _LBS_FORMAT_H + +#include + +std::string uri_encode(const std::string &in); + +#endif // _LBS_TARSTORE_H diff --git a/scandir.cc b/scandir.cc index 2559dc1..c1bedeb 100644 --- a/scandir.cc +++ b/scandir.cc @@ -16,6 +16,7 @@ #include #include +#include "format.h" #include "store.h" #include "tarstore.h" #include "sha1.h" @@ -140,7 +141,7 @@ void scanfile(const string& path, ostream &metadata) printf("%s\n", path.c_str()); - metadata << "name: " << path << "\n"; + metadata << "name: " << uri_encode(path) << "\n"; metadata << "mode: " << (stat_buf.st_mode & 07777) << "\n"; metadata << "atime: " << stat_buf.st_atime << "\n"; metadata << "ctime: " << stat_buf.st_ctime << "\n"; diff --git a/tarstore.cc b/tarstore.cc index 2c2f523..6511074 100644 --- a/tarstore.cc +++ b/tarstore.cc @@ -1,5 +1,5 @@ /* LBS: An LFS-inspired filesystem backup system - * Copyright (C) 2006 Michael Vrable + * Copyright (C) 2007 Michael Vrable * * Backup data is stored in a collection of objects, which are grouped together * into segments for storage purposes. This implementation of the object store -- 2.20.1