sm_dump(false);
Pmsg0(-1, "==== Finish ====\n");
};
+
+bool _stat_ok(const char *file, int l, const char *fpath)
+{
+ struct stat statbuf;
+ return _ok(file, l, "stat_ok", stat(fpath, &statbuf) == 0, fpath);
+}
+
+bool _stat_nok(const char *file, int l, const char *fpath)
+{
+ struct stat statbuf;
+ return _ok(file, l, "stat_nok", stat(fpath, &statbuf) != 0, fpath);
+}
+
+void fsu_rmdir(const char *dirPath)
+{
+#ifndef HAVE_WIN32
+ POOLMEM *syscmd = get_pool_memory(PM_MESSAGE);
+ Mmsg(syscmd, "rm -r \"%s\"", dirPath);
+ int rc = system(syscmd);
+ if (rc < 0) {
+ printf("ERROR : Could not remove Directory: %s\n", dirPath);
+ exit(-1);
+ }
+ free_and_null_pool_memory(syscmd);
+#else
+ POOLMEM *syscmd = get_pool_memory(PM_MESSAGE);
+ Mmsg(syscmd, "rd /s /q \"%s\"", dirPath);
+ int rc = system(syscmd);
+ if (rc < 0) {
+ printf("ERROR : Could not remove Directory: %s\n", dirPath);
+ exit(-1);
+ }
+ free_and_null_pool_memory(syscmd);
+#endif
+}
+
+void fsu_rmfile(const char *fpath)
+{
+ int rc = unlink(fpath);
+
+ if (rc == -1) {
+ switch(errno) {
+ case EACCES:
+ printf("(Unlink %s) Permission Denied\n", fpath);
+ break;
+ case ENOENT:
+ printf("(Unlink %s) File not found\n", fpath);
+ break;
+ case EBUSY:
+ printf("(Unlink %s) File busy\n", fpath);
+ break;
+ default:
+ printf("(Unlink %s) Default Error\n", fpath);
+ break;
+ }
+
+ exit(-1);
+ }
+}
+
+void fsu_touch(const char *fpath)
+{
+#ifndef HAVE_WIN32
+ POOLMEM *syscmd = get_pool_memory(PM_MESSAGE);
+ Mmsg(syscmd, "touch \"%s\"", fpath);
+ int rc = system(syscmd);
+ if (rc < 0) {
+ printf("ERROR : Could not touch file: %s\n", fpath);
+ exit(-1);
+ }
+ free_and_null_pool_memory(syscmd);
+#else
+ POOLMEM *syscmd = get_pool_memory(PM_MESSAGE);
+ Mmsg(syscmd, "echo.> \"%s\"", fpath);
+ int rc = system(syscmd);
+ if (rc < 0) {
+ printf("ERROR : Could not touch file: %s\n", fpath);
+ exit(-1);
+ }
+ free_and_null_pool_memory(syscmd);
+#endif
+}
+
+void fsu_mvfile(char *src, char *dst)
+{
+#ifndef HAVE_WIN32
+ POOLMEM *syscmd = get_pool_memory(PM_MESSAGE);
+ Mmsg(syscmd, "mv \"%s\" \"%s\"", src, dst);
+ int rc = system(syscmd);
+ if (rc < 0) {
+ printf("ERROR : Could not move file %s into %s. RC = %d\n", src, dst, rc);
+ exit(-1);
+ }
+ free_and_null_pool_memory(syscmd);
+#else
+ char *win_src = bstrdup(src);
+ unix_to_win_path(win_src);
+ char *win_dst = bstrdup(dst);
+ unix_to_win_path(win_dst);
+ POOLMEM *syscmd = get_pool_memory(PM_MESSAGE);
+ Mmsg(syscmd, "move \"%s\" \"%s\"", win_src, win_dst);
+ int rc = system(syscmd);
+ if (rc < 0) {
+ printf("ERROR : Could not move file %s into %s. RC = %d\n", win_src, win_dst, rc);
+ exit(-1);
+ }
+ free_and_null_pool_memory(syscmd);
+ free(win_src);
+ free(win_dst);
+#endif
+}
+
+void fsu_cpfile(char *src, char *dst)
+{
+#ifndef HAVE_WIN32
+ POOLMEM *syscmd = get_pool_memory(PM_MESSAGE);
+ Mmsg(syscmd, "cp \"%s\" \"%s\"", src, dst);
+ int rc = system(syscmd);
+ if (rc < 0) {
+ printf("ERROR : Could not move file %s into %s. RC = %d\n", src, dst, rc);
+ exit(-1);
+ }
+ free_and_null_pool_memory(syscmd);
+#else
+ char *win_src = bstrdup(src);
+ unix_to_win_path(win_src);
+ char *win_dst = bstrdup(dst);
+ unix_to_win_path(win_dst);
+ POOLMEM *syscmd = get_pool_memory(PM_MESSAGE);
+ Mmsg(syscmd, "copy \"%s\" \"%s\"", win_src, win_dst);
+ int rc = system(syscmd);
+ if (rc < 0) {
+ printf("ERROR : Could not move file %s into %s. RC = %d\n", win_src, win_dst, rc);
+ exit(-1);
+ }
+ free_and_null_pool_memory(syscmd);
+#endif
+}
+
+void fsu_mkdir(const char *path)
+{
+ if (mkdir(path, 0777) != 0) {
+ printf("ERROR : Could not create Directory: %s\n", path);
+ exit(-1);
+ }
+}
+
+void fsu_mkpath(const char *newPath)
+{
+#ifndef HAVE_WIN32
+ POOLMEM *syscmd = get_pool_memory(PM_MESSAGE);
+ Mmsg(syscmd, "mkdir -p \"%s\"", newPath);
+ int rc = system(syscmd);
+ if (rc < 0) {
+ printf("ERROR : Could not create path %s. Error: %d\n",
+ newPath, rc);
+ exit(-1);
+ }
+ free_and_null_pool_memory(syscmd);
+#else
+ POOLMEM *fullPath = get_pool_memory(PM_MESSAGE);
+ Mmsg(fullPath, "%s", dirPath, newPath);
+ int rc = _mkdir(fullPath);
+ if (rc < 0) {
+ printf("ERROR : Could not create path %s. Error: %s\n",
+ newPath, strerror(errno));
+ exit(-1);
+ }
+ free_and_null_pool_memory(fullPath);
+#endif
+}
+
+void fsu_mkfile(const char *fpath, const char *fcontents)
+{
+ FILE *fp = fopen(fpath, "wb");
+
+ if (!fp) {
+ printf("ERROR : Could not open File: %s\n", fpath);
+ exit(-1);
+ }
+
+ if (fprintf(fp, "%s", fcontents) < 0) {
+ printf("ERROR : Could not write File: %s\n", fpath);
+ exit(-1);
+ }
+
+ fclose(fp);
+}
+
+void fsu_mkfile(const char *fpath)
+{
+ fsu_mkfile(fpath, fpath);
+}
+
+void unix_to_win_path(char *path)
+{
+ for (int i = 0; path[i] != '\0'; i++) {
+ if (path[i] == '/') {
+ path[i] = '\\';
+ }
+ }
+ return;
+}
#ifndef _UNITTESTS_H_
#define _UNITTESTS_H_
+#include "bacula.h"
+
// Test success if value x is not zero
#define ok(x, label) _ok(__FILE__, __LINE__, #x, (x), label)
// Test success if value x is zero
#define nok(x, label) _nok(__FILE__, __LINE__, #x, (x), label)
+// Test success if value x is a valid path
+#define stat_ok(x) _stat_ok(__FILE__, __LINE__, (x))
+// Test success if value x is not a valid path
+#define stat_nok(x) _stat_nok(__FILE__, __LINE__, (x))
+
#define is(x, y, label) _is(__FILE__, __LINE__, #x, (x), (y), label)
#define isnt(x, y, label) _isnt(__FILE__, __LINE__, #x, (x), (y), label)
bool _isnt(const char *file, int l, const char *op, const char *str, const char *str2, const char *label);
bool _is(const char *file, int l, const char *op, int64_t nb, int64_t nb2, const char *label);
bool _isnt(const char *file, int l, const char *op, int64_t nb, int64_t nb2, const char *label);
+bool _stat_ok(const char *file, int l, const char *path);
+bool _stat_nok(const char *file, int l, const char *path);
int report();
void terminate(int sig);
void prolog(const char *name, bool lmgr=false, bool motd=true);
void configure(uint64_t v) { configure_test(v); };
};
+/* POOL_MEM subclass with convenience methods */
+class bstring : public POOL_MEM {
+public:
+
+ bstring() : POOL_MEM() {};
+ bstring(const char *str) : POOL_MEM(str) {};
+ bstring(const bstring &fmt, ...): POOL_MEM() {
+ va_list arg_ptr;
+ int len, maxlen;
+
+ for (;;) {
+ maxlen = this->max_size() - 1;
+ va_start(arg_ptr, fmt);
+ len = bvsnprintf(this->c_str(), maxlen, fmt, arg_ptr);
+ va_end(arg_ptr);
+ if (len < 0 || len >= (maxlen-5)) {
+ this->realloc_pm(maxlen + maxlen/2);
+ continue;
+ }
+ break;
+ }
+ };
+
+ bstring &operator=(const char * str) {
+ pm_strcpy(*this, str);
+ return *this;
+ };
+
+ // Allows cast to "char *"
+ operator char *() const {
+ return c_str();
+ };
+
+ int append(const char *str) {
+ return strcat(str);
+ }
+};
+
+/* Set of utilitary functions to manipulate the filesystem */
+void fsu_mkfile(const char *path);
+void fsu_mkfile(const char *path, const char *fcontents);
+void fsu_mkdir(const char *path);
+void fsu_mkpath(const char *path);
+void fsu_rmdir(const char *path);
+void fsu_rmfile(const char *path);
+void fsu_touch(const char *path);
+void fsu_mvfile(const char *src, const char *dst);
+void fsu_cpfile(const char *src, const char *dst);
+void unix_to_win_path(char *path);
+
#endif /* _UNITTESTS_H_ */