/* 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);
#endif
/*=============================================================*/
-/* */
-/* *** A l l S y s t e m s *** */
-/* */
+/* */
+/* *** A l l S y s t e m s *** */
+/* */
/*=============================================================*/
* 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);
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++;
* 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;
/* ***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
/*
*/
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);
/*=============================================================*/
-/* */
-/* * * * U n i x * * * * */
-/* */
+/* */
+/* * * * U n i x * * * * */
+/* */
/*=============================================================*/
#ifndef HAVE_CYGWIN
*/
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;
}
/*=============================================================*/
-/* */
-/* * * * W i n 3 2 * * * * */
-/* */
+/* */
+/* * * * W i n 3 2 * * * * */
+/* */
/*=============================================================*/
#ifdef HAVE_CYGWIN
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;
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;
}
FILE_ATTRIBUTE_OFFLINE| \
FILE_ATTRIBUTE_READONLY| \
FILE_ATTRIBUTE_SYSTEM| \
- FILE_ATTRIBUTE_TEMPORARY)
+ FILE_ATTRIBUTE_TEMPORARY)
/*
* 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;
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 {
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;
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);
}
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);
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 */
--- /dev/null
+/*
+ * 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);
+}