From d4b200fbb770f8d6f7d325c41806abe0f42c2027 Mon Sep 17 00:00:00 2001 From: Kern Sibbald Date: Wed, 16 Apr 2003 20:36:06 +0000 Subject: [PATCH] Home page change. Tweak to Win32 Backup API git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@443 91ce42f0-d328-0410-95d8-f526ca767f89 --- bacula/src/findlib/attribs.c | 222 ++++++++++++++++++++------------ bacula/src/findlib/bfile.c | 8 +- bacula/src/lib/signal.c | 2 +- bacula/src/lib/util.c | 4 +- bacula/src/lib/watchdog.c | 3 +- bacula/src/stored/bextract.c | 4 +- bacula/src/tools/.cvsignore | 1 + bacula/src/tools/Makefile.in | 9 +- bacula/src/tools/testfind.c | 4 +- bacula/src/tools/testls.c | 236 +++++++++++++++++++++++++++++++++++ 10 files changed, 395 insertions(+), 98 deletions(-) create mode 100755 bacula/src/tools/testls.c diff --git a/bacula/src/findlib/attribs.c b/bacula/src/findlib/attribs.c index 1e21ac8b5e..a57b02a950 100755 --- a/bacula/src/findlib/attribs.c +++ b/bacula/src/findlib/attribs.c @@ -37,8 +37,8 @@ /* Forward referenced subroutines */ static int set_win32_attributes(void *jcr, char *fname, char *ofile, char *lname, - int type, int stream, struct stat *statp, - char *attribsEx, BFILE *ofd); + int type, int stream, struct stat *statp, + char *attribsEx, BFILE *ofd); void unix_name_to_win32(POOLMEM **win32_name, char *name); void win_error(void *jcr, char *prefix, POOLMEM *ofile); HANDLE bget_handle(BFILE *bfd); @@ -50,9 +50,9 @@ HANDLE bget_handle(BFILE *bfd); #endif /*=============================================================*/ -/* */ -/* *** A l l S y s t e m s *** */ -/* */ +/* */ +/* *** A l l S y s t e m s *** */ +/* */ /*=============================================================*/ @@ -64,7 +64,7 @@ void encode_stat(char *buf, struct stat *statp, uint32_t LinkFI) * NOTE: we should use rdev as major and minor device if * it is a block or char device (S_ISCHR(statp->st_mode) * or S_ISBLK(statp->st_mode)). In all other cases, - * it is not used. + * it is not used. * */ p += to_base64((int64_t)statp->st_dev, p); @@ -115,7 +115,7 @@ decode_stat(char *buf, struct stat *statp, uint32_t *LinkFI) p += from_base64(&val, p); statp->st_dev = val; - p++; /* skip space */ + p++; /* skip space */ p += from_base64(&val, p); statp->st_ino = val; p++; @@ -179,31 +179,31 @@ decode_stat(char *buf, struct stat *statp, uint32_t *LinkFI) * ofile is the output filename (may be in a different directory) * * Returns: 1 on success - * 0 on failure + * 0 on failure */ int set_attributes(void *jcr, char *fname, char *ofile, char *lname, - int type, int stream, struct stat *statp, - char *attribsEx, BFILE *ofd) + int type, int stream, struct stat *statp, + char *attribsEx, BFILE *ofd) { - struct utimbuf ut; + struct utimbuf ut; mode_t old_mask; int stat = 1; #ifdef HAVE_CYGWIN if (set_win32_attributes(jcr, fname, ofile, lname, type, stream, - statp, attribsEx, ofd)) { + statp, attribsEx, ofd)) { return 1; } /* * If Windows stuff failed, e.g. attempt to restore Unix file - * to Windows, simply fall through and we will do it the + * to Windows, simply fall through and we will do it the * universal way. */ #endif old_mask = umask(0); if (is_bopen(ofd)) { - bclose(ofd); /* first close file */ + bclose(ofd); /* first close file */ } ut.actime = statp->st_atime; @@ -212,33 +212,33 @@ int set_attributes(void *jcr, char *fname, char *ofile, char *lname, /* ***FIXME**** optimize -- don't do if already correct */ /* * For link, change owner of link using lchown, but don't - * try to do a chmod as that will update the file behind it. + * try to do a chmod as that will update the file behind it. */ if (type == FT_LNK) { /* Change owner of link, not of real file */ if (lchown(ofile, statp->st_uid, statp->st_gid) < 0) { Jmsg2(jcr, M_WARNING, 0, "Unable to set file owner %s: ERR=%s\n", - ofile, strerror(errno)); - stat = 0; + ofile, strerror(errno)); + stat = 0; } } else { if (chown(ofile, statp->st_uid, statp->st_gid) < 0) { Jmsg2(jcr, M_WARNING, 0, "Unable to set file owner %s: ERR=%s\n", - ofile, strerror(errno)); - stat = 0; + ofile, strerror(errno)); + stat = 0; } if (chmod(ofile, statp->st_mode) < 0) { Jmsg2(jcr, M_WARNING, 0, "Unable to set file modes %s: ERR=%s\n", - ofile, strerror(errno)); - stat = 0; + ofile, strerror(errno)); + stat = 0; } /* FreeBSD user flags */ #ifdef HAVE_CHFLAGS if (chflags(ofile, statp->st_flags) < 0) { Jmsg2(jcr, M_WARNING, 0, "Unable to set file flags %s: ERR=%s\n", - ofile, strerror(errno)); - stat = 0; + ofile, strerror(errno)); + stat = 0; } #endif /* @@ -246,8 +246,8 @@ int set_attributes(void *jcr, char *fname, char *ofile, char *lname, */ if (utime(ofile, &ut) < 0) { Jmsg2(jcr, M_ERROR, 0, "Unable to set file times %s: ERR=%s\n", - ofile, strerror(errno)); - stat = 0; + ofile, strerror(errno)); + stat = 0; } } umask(old_mask); @@ -256,9 +256,9 @@ int set_attributes(void *jcr, char *fname, char *ofile, char *lname, /*=============================================================*/ -/* */ -/* * * * U n i x * * * * */ -/* */ +/* */ +/* * * * U n i x * * * * */ +/* */ /*=============================================================*/ #ifndef HAVE_CYGWIN @@ -269,7 +269,7 @@ int set_attributes(void *jcr, char *fname, char *ofile, char *lname, */ int encode_attribsEx(void *jcr, char *attribsEx, FF_PKT *ff_pkt) { - *attribsEx = 0; /* no extended attributes */ + *attribsEx = 0; /* no extended attributes */ return STREAM_UNIX_ATTRIBUTES; } @@ -282,9 +282,9 @@ void SetServicePrivileges(void *jcr) /*=============================================================*/ -/* */ -/* * * * W i n 3 2 * * * * */ -/* */ +/* */ +/* * * * W i n 3 2 * * * * */ +/* */ /*=============================================================*/ #ifdef HAVE_CYGWIN @@ -297,7 +297,7 @@ int encode_attribsEx(void *jcr, char *attribsEx, FF_PKT *ff_pkt) WIN32_FILE_ATTRIBUTE_DATA atts; ULARGE_INTEGER li; - attribsEx[0] = 0; /* no extended attributes */ + attribsEx[0] = 0; /* no extended attributes */ if (NoGetFileAttributesEx) { return STREAM_UNIX_ATTRIBUTES; @@ -305,7 +305,7 @@ int encode_attribsEx(void *jcr, char *attribsEx, FF_PKT *ff_pkt) unix_name_to_win32(&ff_pkt->sys_fname, ff_pkt->fname); if (!GetFileAttributesEx(ff_pkt->sys_fname, GetFileExInfoStandard, - (LPVOID)&atts)) { + (LPVOID)&atts)) { win_error(jcr, "GetFileAttributesEx:", ff_pkt->sys_fname); return STREAM_WIN32_ATTRIBUTES; } @@ -340,7 +340,7 @@ int encode_attribsEx(void *jcr, char *attribsEx, FF_PKT *ff_pkt) FILE_ATTRIBUTE_OFFLINE| \ FILE_ATTRIBUTE_READONLY| \ FILE_ATTRIBUTE_SYSTEM| \ - FILE_ATTRIBUTE_TEMPORARY) + FILE_ATTRIBUTE_TEMPORARY) /* @@ -350,12 +350,12 @@ int encode_attribsEx(void *jcr, char *attribsEx, FF_PKT *ff_pkt) * ofile is the output filename (may be in a different directory) * * Returns: 1 on success - * 0 on failure + * 0 on failure */ static int set_win32_attributes(void *jcr, char *fname, char *ofile, char *lname, - int type, int stream, struct stat *statp, - char *attribsEx, BFILE *ofd) + int type, int stream, struct stat *statp, + char *attribsEx, BFILE *ofd) { char *p = attribsEx; int64_t val; @@ -364,10 +364,10 @@ int set_win32_attributes(void *jcr, char *fname, char *ofile, char *lname, int stat; POOLMEM *win32_ofile; - if (!p || !*p) { /* we should have attributes */ + if (!p || !*p) { /* we should have attributes */ Dmsg2(100, "Attributes missing. of=%s ofd=%d\n", ofile, ofd->fid); if (is_bopen(ofd)) { - bclose(ofd); + bclose(ofd); } return 0; } else { @@ -376,17 +376,17 @@ int set_win32_attributes(void *jcr, char *fname, char *ofile, char *lname, p += from_base64(&val, p); atts.dwFileAttributes = val; - p++; /* skip space */ + p++; /* skip space */ p += from_base64(&val, p); li.QuadPart = val; atts.ftCreationTime.dwLowDateTime = li.LowPart; atts.ftCreationTime.dwHighDateTime = li.HighPart; - p++; /* skip space */ + p++; /* skip space */ p += from_base64(&val, p); li.QuadPart = val; atts.ftLastAccessTime.dwLowDateTime = li.LowPart; atts.ftLastAccessTime.dwHighDateTime = li.HighPart; - p++; /* skip space */ + p++; /* skip space */ p += from_base64(&val, p); li.QuadPart = val; atts.ftLastWriteTime.dwLowDateTime = li.LowPart; @@ -406,15 +406,15 @@ int set_win32_attributes(void *jcr, char *fname, char *ofile, char *lname, if (!is_bopen(ofd)) { Dmsg1(100, "File not open: %s\n", ofile); - bopen(ofd, ofile, O_WRONLY|O_BINARY, 0); /* attempt to open the file */ + bopen(ofd, ofile, O_WRONLY|O_BINARY, 0); /* attempt to open the file */ } if (is_bopen(ofd)) { Dmsg1(100, "SetFileTime %s\n", ofile); stat = SetFileTime(bget_handle(ofd), - &atts.ftCreationTime, - &atts.ftLastAccessTime, - &atts.ftLastWriteTime); + &atts.ftCreationTime, + &atts.ftLastAccessTime, + &atts.ftLastWriteTime); if (stat != 1) { win_error(jcr, "SetFileTime:", win32_ofile); } @@ -436,19 +436,37 @@ void win_error(void *vjcr, char *prefix, POOLMEM *win32_ofile) DWORD lerror = GetLastError(); LPTSTR msg; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER| - FORMAT_MESSAGE_FROM_SYSTEM, - NULL, - lerror, - 0, - (LPTSTR)&msg, - 0, - NULL); + FORMAT_MESSAGE_FROM_SYSTEM, + NULL, + lerror, + 0, + (LPTSTR)&msg, + 0, + NULL); Dmsg3(100, "Error in %s on file %s: ERR=%s\n", prefix, win32_ofile, msg); strip_trailing_junk(msg); Jmsg3(jcr, M_INFO, 0, _("Error in %s file %s: ERR=%s\n"), prefix, win32_ofile, msg); LocalFree(msg); } +void win_error(void *vjcr, char *prefix, DWORD lerror) +{ + JCR *jcr = (JCR *)vjcr; + LPTSTR msg; + FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER| + FORMAT_MESSAGE_FROM_SYSTEM, + NULL, + lerror, + 0, + (LPTSTR)&msg, + 0, + NULL); + strip_trailing_junk(msg); + Jmsg2(jcr, M_INFO, 0, _("Error in %s: ERR=%s\n"), prefix, msg); + LocalFree(msg); +} + + /* Cygwin API definition */ extern "C" void cygwin_conv_to_win32_path(const char *path, char *win32_path); @@ -467,60 +485,98 @@ void unix_name_to_win32(POOLMEM **win32_name, char *name) void SetServicePrivileges(void *jcr) { HANDLE hToken; - TOKEN_PRIVILEGES tkp; - POOLMEM *dummy = "*none*"; + TOKEN_PRIVILEGES tkp, tkpPrevious; + DWORD cbPrevious = sizeof(TOKEN_PRIVILEGES); + DWORD lerror; // Get a token for this process. if (!OpenProcessToken(GetCurrentProcess(), - TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) { - win_error(jcr, "OpenProcessToken", dummy); + TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) { + win_error(jcr, "OpenProcessToken", GetLastError()); /* Forge on anyway */ } +#ifdef xxx // Get the LUID for the security privilege. if (!LookupPrivilegeValue(NULL, SE_SECURITY_NAME, &tkp.Privileges[0].Luid)) { - win_error(jcr, "LookupPrivilegeValue", dummy); + win_error(jcr, "LookupPrivilegeValue", GetLastError()); } - tkp.PrivilegeCount = 1; // one privilege to set - tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; + tkp.PrivilegeCount = 1; + tkp.Privileges[0].Attributes = 0; + /* Get the privilege */ + AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(TOKEN_PRIVILEGES), + &tkpPrevious, &cbPrevious); + lerror = GetLastError(); + if (lerror != ERROR_SUCCESS) { + win_error(jcr, "AdjustTokenPrivileges get SECURITY_NAME", lerror); + } + + tkpPrevious.PrivilegeCount = 1; + tkpPrevious.Privileges[0].Attributes |= (SE_PRIVILEGE_ENABLED); - // Get the security privilege for this process. - if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(TOKEN_PRIVILEGES), - (PTOKEN_PRIVILEGES)NULL, (PDWORD)0)) { - win_error(jcr, "AdjustTokenPrivileges SE_PRIVILEGE_ENABLED", dummy); -// MessageBox(NULL, "Get security priv failed: AdjustTokePrivileges", "backup", MB_OK); + /* Set the security privilege for this process. */ + AdjustTokenPrivileges(hToken, FALSE, &tkpPrevious, sizeof(TOKEN_PRIVILEGES), + (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL); + lerror = GetLastError(); + if (lerror != ERROR_SUCCESS) { + win_error(jcr, "AdjustTokenPrivileges set SECURITY_NAME", lerror); } +#endif // Get the LUID for the backup privilege. if (!LookupPrivilegeValue(NULL, SE_BACKUP_NAME, &tkp.Privileges[0].Luid)) { - win_error(jcr, "LookupPrivilegeValue", dummy); + win_error(jcr, "LookupPrivilegeValue", GetLastError()); } - tkp.PrivilegeCount = 1; // one privilege to set - tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; + tkp.PrivilegeCount = 1; + tkp.Privileges[0].Attributes = 0; + /* Get the current privilege */ + AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(TOKEN_PRIVILEGES), + &tkpPrevious, &cbPrevious); + lerror = GetLastError(); + if (lerror != ERROR_SUCCESS) { + win_error(jcr, "AdjustTokenPrivileges get BACKUP_NAME", lerror); + } + + tkpPrevious.PrivilegeCount = 1; + tkpPrevious.Privileges[0].Attributes |= (SE_PRIVILEGE_ENABLED); - // Get the backup privilege for this process. - if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(TOKEN_PRIVILEGES), - (PTOKEN_PRIVILEGES)NULL, (PDWORD)0)) { - win_error(jcr, "AdjustTokenPrivileges SE_BACKUP_NAME", dummy); -// MessageBox(NULL, "Get backup priv failed: AdjustTokePrivileges", "backup", MB_OK); + /* Set the backup privilege for this process. */ + AdjustTokenPrivileges(hToken, FALSE, &tkpPrevious, sizeof(TOKEN_PRIVILEGES), + (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL); + lerror = GetLastError(); + if (lerror != ERROR_SUCCESS) { + win_error(jcr, "AdjustTokenPrivileges set BACKUP_NAME", lerror); } // Get the LUID for the restore privilege. if (!LookupPrivilegeValue(NULL, SE_RESTORE_NAME, &tkp.Privileges[0].Luid)) { - win_error(jcr, "LookupPrivilegeValue", dummy); + win_error(jcr, "LookupPrivilegeValue", GetLastError()); } - tkp.PrivilegeCount = 1; // one privilege to set - tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; + tkp.PrivilegeCount = 1; + tkp.Privileges[0].Attributes = 0; + /* Get the privilege */ + AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(TOKEN_PRIVILEGES), + &tkpPrevious, &cbPrevious); + lerror = GetLastError(); + if (lerror != ERROR_SUCCESS) { + win_error(jcr, "AdjustTokenPrivileges get RESTORE_NAME", lerror); + } + + tkpPrevious.PrivilegeCount = 1; + tkpPrevious.Privileges[0].Attributes |= (SE_PRIVILEGE_ENABLED); - // Get the restore privilege for this process. - if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(TOKEN_PRIVILEGES), - (PTOKEN_PRIVILEGES)NULL, (PDWORD)0)) { - win_error(jcr, "AdjustTokenPrivileges SE_RESTORE_NAME", dummy); -// MessageBox(NULL, "Get restore priv failed: AdjustTokePrivileges", "restore", MB_OK); + /* Set the security privilege for this process. */ + AdjustTokenPrivileges(hToken, FALSE, &tkpPrevious, sizeof(TOKEN_PRIVILEGES), + (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL); + lerror = GetLastError(); + if (lerror != ERROR_SUCCESS) { + win_error(jcr, "AdjustTokenPrivileges set RESTORE_NAME", lerror); } + CloseHandle(hToken); } +// MessageBox(NULL, "Get restore priv failed: AdjustTokePrivileges", "restore", MB_OK); -#endif /* HAVE_CYGWIN */ +#endif /* HAVE_CYGWIN */ diff --git a/bacula/src/findlib/bfile.c b/bacula/src/findlib/bfile.c index 45a1edb320..e11541de5d 100644 --- a/bacula/src/findlib/bfile.c +++ b/bacula/src/findlib/bfile.c @@ -73,10 +73,10 @@ int bopen(BFILE *bfd, const char *fname, int flags, mode_t mode) if (flags & O_WRONLY) { /* creating */ bfd->fh = CreateFile(win32_fname, WRITE_OWNER|WRITE_DAC|ACCESS_SYSTEM_SECURITY, /* access */ - 0, /* shared mode */ + FILE_SHARE_READ | FILE_SHARE_WRITE, /* shared mode */ NULL, /* SecurityAttributes */ CREATE_ALWAYS, /* CreationDisposition */ - FILE_FLAG_BACKUP_SEMANTICS, /* Flags and attributes */ + FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, /* Flags and attributes */ NULL); /* TemplateFile */ if (bfd->fh == INVALID_HANDLE_VALUE) { @@ -88,10 +88,10 @@ int bopen(BFILE *bfd, const char *fname, int flags, mode_t mode) } else { bfd->fh = CreateFile(win32_fname, READ_CONTROL|ACCESS_SYSTEM_SECURITY, /* access */ - 0, /* shared mode */ + FILE_SHARE_READ | FILE_SHARE_WRITE, /* shared mode */ NULL, /* SecurityAttributes */ OPEN_EXISTING, /* CreationDisposition */ - FILE_FLAG_BACKUP_SEMANTICS, /* Flags and attributes */ + FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, /* Flags and attributes */ NULL); /* TemplateFile */ if (bfd->fh == INVALID_HANDLE_VALUE) { diff --git a/bacula/src/lib/signal.c b/bacula/src/lib/signal.c index 3a513d7542..d4eeaec514 100644 --- a/bacula/src/lib/signal.c +++ b/bacula/src/lib/signal.c @@ -244,7 +244,7 @@ void init_signals(void terminate(int sig)) sigaction(SIGPIPE, &sigignore, NULL); - sigaction(SIGCHLD, &sigignore, NULL); + sigaction(SIGCHLD, &sighandle, NULL); sigaction(SIGCONT, &sigignore, NULL); sigaction(SIGPROF, &sigignore, NULL); sigaction(SIGWINCH, &sigignore, NULL); diff --git a/bacula/src/lib/util.c b/bacula/src/lib/util.c index a1230d9123..6d3e3b6ae9 100644 --- a/bacula/src/lib/util.c +++ b/bacula/src/lib/util.c @@ -364,8 +364,8 @@ char *encode_mode(mode_t mode, char *buf) { char *cp = buf; - *cp++ = S_ISDIR(mode) ? 'd' : S_ISBLK(mode) ? 'b' : S_ISCHR(mode) ? 'c' : - S_ISLNK(mode) ? 'l' : '-'; + *cp++ = S_ISDIR(mode) ? 'd' : S_ISBLK(mode) ? 'b' : S_ISCHR(mode) ? 'c' : + S_ISLNK(mode) ? 'l' : S_ISFIFO(mode) ? 'f' : S_ISSOCK(mode) ? 's' : '-'; *cp++ = mode & S_IRUSR ? 'r' : '-'; *cp++ = mode & S_IWUSR ? 'w' : '-'; *cp++ = (mode & S_ISUID diff --git a/bacula/src/lib/watchdog.c b/bacula/src/lib/watchdog.c index 2bb599daec..5f5ac6638b 100755 --- a/bacula/src/lib/watchdog.c +++ b/bacula/src/lib/watchdog.c @@ -101,9 +101,8 @@ int stop_watchdog(void) { int stat; - P(mutex); quit = TRUE; - + P(mutex); if ((stat = pthread_cond_signal(&timer)) != 0) { V(mutex); return stat; diff --git a/bacula/src/stored/bextract.c b/bacula/src/stored/bextract.c index 826a247d49..4730545698 100644 --- a/bacula/src/stored/bextract.c +++ b/bacula/src/stored/bextract.c @@ -102,7 +102,7 @@ int main (int argc, char *argv[]) memset(ff, 0, sizeof(FF_PKT)); init_include_exclude_files(ff); - binit(&bfd); + binit(&bfd, 0); while ((ch = getopt(argc, argv, "b:c:d:e:i:?")) != -1) { switch (ch) { @@ -374,7 +374,7 @@ static void record_cb(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, DEV_RECORD *rec) extract = FALSE; stat = create_file(jcr, fname, ofile, lname, type, stream, - &statp, attribsEx, &bfd, REPLACE_ALWAYS); + &statp, attribsEx, &bfd, REPLACE_ALWAYS, 0); switch (stat) { case CF_ERROR: case CF_SKIP: diff --git a/bacula/src/tools/.cvsignore b/bacula/src/tools/.cvsignore index e0430bf75c..28a8201264 100644 --- a/bacula/src/tools/.cvsignore +++ b/bacula/src/tools/.cvsignore @@ -1,3 +1,4 @@ +testls 1 Makefile dbcheck diff --git a/bacula/src/tools/Makefile.in b/bacula/src/tools/Makefile.in index 80b0c8f5af..d833303a7f 100644 --- a/bacula/src/tools/Makefile.in +++ b/bacula/src/tools/Makefile.in @@ -36,7 +36,7 @@ EXTRAOBJS = @OBJLIST@ .c.o: $(CXX) $(DEFS) $(DEBUG) -c $(CPPFLAGS) -I$(srcdir) -I$(basedir) $(DINCLUDE) $(CFLAGS) $< #------------------------------------------------------------------------- -all: Makefile smtp dbcheck testfind +all: Makefile smtp dbcheck testfind testls @echo "==== Make of tools is good ====" @echo " " @@ -49,6 +49,11 @@ dbcheck: dbcheck.o ../lib/libbac.a ../cats/libsql.a testfind: ../findlib/libfind.a ../lib/libbac.a $(FINDOBJS) $(CXX) -g $(LDFLAGS) -L. -L../lib -L../findlib -o $@ $(FINDOBJS) \ $(LIBS) $(DLIB) -lfind -lbac -lm + +testls: ../findlib/libfind.a ../lib/libbac.a testls.o + $(CXX) -g $(LDFLAGS) -L. -L../lib -L../findlib -o $@ testls.o \ + $(LIBS) $(DLIB) -lfind -lbac -lm + Makefile: $(srcdir)/Makefile.in $(topdir)/config.status @@ -57,7 +62,7 @@ Makefile: $(srcdir)/Makefile.in $(topdir)/config.status clean: @$(RMF) smtp core core.* a.out *.o *.bak *~ *.intpro *.extpro 1 2 3 - @$(RMF) testfind dbcheck + @$(RMF) testfind dbcheck testls realclean: clean @$(RMF) tags diff --git a/bacula/src/tools/testfind.c b/bacula/src/tools/testfind.c index 91bf387b06..b79081afbf 100644 --- a/bacula/src/tools/testfind.c +++ b/bacula/src/tools/testfind.c @@ -46,8 +46,8 @@ static void usage() "Usage: testfind [-d debug_level] [-] [pattern1 ...]\n" " -a print extended attributes (Win32 debug)\n" " -dnn set debug level to nn\n" -" -e specify file of exclude patters\n" -" -i specify file of include patters\n" +" -e specify file of exclude patterns\n" +" -i specify file of include patterns\n" " - read pattern(s) from stdin\n" " -? print this message.\n" "\n" diff --git a/bacula/src/tools/testls.c b/bacula/src/tools/testls.c new file mode 100755 index 0000000000..51d178023c --- /dev/null +++ b/bacula/src/tools/testls.c @@ -0,0 +1,236 @@ +/* + * Test program for listing files during regression testing + */ + +/* + Copyright (C) 2000-2003 Kern Sibbald and John Walker + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of + the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public + License along with this program; if not, write to the Free + Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + MA 02111-1307, USA. + + */ + +#include "bacula.h" +#include "findlib/find.h" + + +/* Global variables */ +int attrs = 0; + +static JCR *jcr; + + +static int print_file(FF_PKT *ff, void *pkt); +static void print_ls_output(char *fname, char *link, int type, struct stat *statp); + +static void usage() +{ + fprintf(stderr, _( +"\n" +"Usage: testls [-d debug_level] [-] [pattern1 ...]\n" +" -a print extended attributes (Win32 debug)\n" +" -dnn set debug level to nn\n" +" -e specify file of exclude patterns\n" +" -i specify file of include patterns\n" +" - read pattern(s) from stdin\n" +" -? print this message.\n" +"\n" +"Patterns are file inclusion -- normally directories.\n" +"Debug level >= 1 prints each file found.\n" +"Debug level >= 10 prints path/file for catalog.\n" +"Errors always printed.\n" +"Files/paths truncated is number with len > 255.\n" +"Truncation is only in catalog.\n" +"\n")); + + exit(1); +} + + +int +main (int argc, char *const *argv) +{ + FF_PKT *ff; + char name[1000]; + int i, ch, hard_links; + char *inc = NULL; + char *exc = NULL; + FILE *fd; + + while ((ch = getopt(argc, argv, "ad:e:i:?")) != -1) { + switch (ch) { + case 'a': /* print extended attributes *debug* */ + attrs = 1; + break; + + case 'd': /* set debug level */ + debug_level = atoi(optarg); + if (debug_level <= 0) { + debug_level = 1; + } + break; + + case 'e': /* exclude patterns */ + exc = optarg; + break; + + case 'i': /* include patterns */ + inc = optarg; + break; + + case '?': + default: + usage(); + + } + } + argc -= optind; + argv += optind; + + jcr = new_jcr(sizeof(JCR), NULL); + + ff = init_find_files(); + if (argc == 0 && !inc) { + add_fname_to_include_list(ff, 0, "/"); /* default to / */ + } else { + for (i=0; i < argc; i++) { + if (strcmp(argv[i], "-") == 0) { + while (fgets(name, sizeof(name)-1, stdin)) { + strip_trailing_junk(name); + add_fname_to_include_list(ff, 0, name); + } + continue; + } + add_fname_to_include_list(ff, 0, argv[i]); + } + } + if (inc) { + fd = fopen(inc, "r"); + if (!fd) { + printf("Could not open include file: %s\n", inc); + exit(1); + } + while (fgets(name, sizeof(name)-1, fd)) { + strip_trailing_junk(name); + add_fname_to_include_list(ff, 0, name); + } + fclose(fd); + } + + if (exc) { + fd = fopen(exc, "r"); + if (!fd) { + printf("Could not open exclude file: %s\n", exc); + exit(1); + } + while (fgets(name, sizeof(name)-1, fd)) { + strip_trailing_junk(name); + add_fname_to_exclude_list(ff, name); + } + fclose(fd); + } + find_files(jcr, ff, print_file, NULL); + hard_links = term_find_files(ff); + + free_jcr(jcr); + close_memory_pool(); + sm_dump(False); + exit(0); +} + +static int print_file(FF_PKT *ff, void *pkt) +{ + + switch (ff->type) { + case FT_LNKSAVED: + case FT_REGE: + case FT_REG: + case FT_LNK: + case FT_DIR: + case FT_SPEC: + print_ls_output(ff->fname, ff->link, ff->type, &ff->statp); + break; + case FT_NOACCESS: + printf(_("Err: Could not access %s: %s\n"), ff->fname, strerror(errno)); + break; + case FT_NOFOLLOW: + printf(_("Err: Could not follow ff->link %s: %s\n"), ff->fname, strerror(errno)); + break; + case FT_NOSTAT: + printf(_("Err: Could not stat %s: %s\n"), ff->fname, strerror(errno)); + break; + case FT_NOCHG: + printf(_("Skip: File not saved. No change. %s\n"), ff->fname); + break; + case FT_ISARCH: + printf(_("Err: Attempt to backup archive. Not saved. %s\n"), ff->fname); + break; + case FT_NORECURSE: + printf(_("Recursion turned off. Directory not entered. %s\n"), ff->fname); + break; + case FT_NOFSCHG: + printf(_("Skip: File system change prohibited. Directory not entered. %s\n"), ff->fname); + break; + case FT_NOOPEN: + printf(_("Err: Could not open directory %s: %s\n"), ff->fname, strerror(errno)); + break; + default: + printf(_("Err: Unknown file ff->type %d: %s\n"), ff->type, ff->fname); + break; + } + return 1; +} + +static void print_ls_output(char *fname, char *link, int type, struct stat *statp) +{ + char buf[1000]; + char ec1[30]; + char *p, *f; + int n; + + p = encode_mode(statp->st_mode, buf); + n = sprintf(p, " %2d ", (uint32_t)statp->st_nlink); + p += n; + n = sprintf(p, "%-4d %-4d", (int)statp->st_uid, (int)statp->st_gid); + p += n; + n = sprintf(p, "%7.7s ", edit_uint64(statp->st_size, ec1)); + p += n; + if (S_ISCHR(statp->st_mode) || S_ISBLK(statp->st_mode)) { + n = sprintf(p, "%4x ", (int)statp->st_rdev); + } else { + n = sprintf(p, " "); + } + p += n; + if (type != FT_LNK) { + p = encode_time(statp->st_mtime, p); + } else { + p = encode_time(0, p); + } + *p++ = ' '; + /* Copy file name */ + for (f=fname; *f && (p-buf) < (int)sizeof(buf); ) + *p++ = *f++; + if (type == FT_LNK) { + *p++ = '-'; + *p++ = '>'; + *p++ = ' '; + /* Copy link name */ + for (f=link; *f && (p-buf) < (int)sizeof(buf); ) + *p++ = *f++; + } + *p++ = '\n'; + *p = 0; + fputs(buf, stdout); +} -- 2.39.5