]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/bsys.c
const char * additions
[bacula/bacula] / bacula / src / lib / bsys.c
index 38b592f7eb34b7346e70fb9500b8ce2443ded890..57a7c02ab0536b88ea307da1997f8ecc01c5f8d7 100644 (file)
@@ -66,7 +66,7 @@ void *bmalloc(size_t size)
 }
 #endif
 
-void *b_malloc(char *file, int line, size_t size)
+void *b_malloc(const char *file, int line, size_t size)
 {
   void *buf;
 
@@ -108,7 +108,7 @@ void *bcalloc (size_t size1, size_t size2)
 /*
  * Implement snprintf
  */
-int bsnprintf(char *str, size_t size, const  char  *fmt,  ...) 
+int bsnprintf(char *str, int32_t size, const char *fmt,  ...) 
 {
 #ifdef HAVE_VSNPRINTF
    va_list   arg_ptr;
@@ -143,7 +143,7 @@ int bsnprintf(char *str, size_t size, const  char  *fmt,  ...)
 /*
  * Implement vsnprintf()
  */
-int bvsnprintf(char *str, size_t size, const char  *format, va_list ap)
+int bvsnprintf(char *str, int32_t size, const char  *format, va_list ap)
 {
 #ifdef HAVE_VSNPRINTF
    int len;
@@ -190,7 +190,7 @@ struct tm *localtime_r(const time_t *timep, struct tm *tm)
 #endif /* HAVE_LOCALTIME_R */
 
 #ifndef HAVE_READDIR_R
-
+#ifndef HAVE_WIN32
 #include <dirent.h>
 
 int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
@@ -217,8 +217,9 @@ int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
     }
     V(mutex);
     return stat;
-}
 
+}
+#endif
 #endif /* HAVE_READDIR_R */
 
 #ifdef xxxxxxxxxx_STRERROR_R
@@ -282,7 +283,7 @@ void _v(char *file, int line, pthread_mutex_t *m)
 }
 #endif /* DEBUG_MUTEX */
 
-#ifndef HAVE_CYGWIN
+#if !defined(HAVE_CYGWIN) && !defined(HAVE_WIN32)
 static int del_pid_file_ok = FALSE;
 #endif
 
@@ -291,7 +292,7 @@ static int del_pid_file_ok = FALSE;
  */
 void create_pid_file(char *dir, char *progname, int port)
 {
-#ifndef HAVE_CYGWIN
+#if !defined(HAVE_CYGWIN) && !defined(HAVE_WIN32)
    int pidfd, len;
    int oldpid;
    char  pidbuf[20];
@@ -299,10 +300,10 @@ void create_pid_file(char *dir, char *progname, int port)
    struct stat statp;
 
    Mmsg(&fname, "%s/%s.%d.pid", dir, progname, port);
-   if (stat(fname, &statp) == 0) {
+   if (stat(mp_chr(fname), &statp) == 0) {
       /* File exists, see what we have */
       *pidbuf = 0;
-      if ((pidfd = open(fname, O_RDONLY)) < 0 || 
+      if ((pidfd = open(mp_chr(fname), O_RDONLY)) < 0 || 
           read(pidfd, &pidbuf, sizeof(pidbuf)) < 0 ||
            sscanf(pidbuf, "%d", &oldpid) != 1) {
          Emsg2(M_ERROR_TERM, 0, _("Cannot open pid file. %s ERR=%s\n"), fname, strerror(errno));
@@ -313,10 +314,10 @@ void create_pid_file(char *dir, char *progname, int port)
               progname, oldpid, fname);
       }
       /* He is not alive, so take over file ownership */
-      unlink(fname);                 /* remove stale pid file */
+      unlink(mp_chr(fname));                 /* remove stale pid file */
    }
    /* Create new pid file */
-   if ((pidfd = open(fname, O_CREAT | O_TRUNC | O_WRONLY, 0644)) >= 0) {
+   if ((pidfd = open(mp_chr(fname), O_CREAT | O_TRUNC | O_WRONLY, 0644)) >= 0) {
       len = sprintf(pidbuf, "%d\n", (int)getpid());
       write(pidfd, pidbuf, len);
       close(pidfd);
@@ -333,7 +334,7 @@ void create_pid_file(char *dir, char *progname, int port)
  */
 int delete_pid_file(char *dir, char *progname, int port)
 {
-#ifndef HAVE_CYGWIN
+#if !defined(HAVE_CYGWIN)  && !defined(HAVE_WIN32)
    POOLMEM *fname = get_pool_memory(PM_FNAME);
 
    if (!del_pid_file_ok) {
@@ -342,7 +343,7 @@ int delete_pid_file(char *dir, char *progname, int port)
    }
    del_pid_file_ok = FALSE;
    Mmsg(&fname, "%s/%s.%d.pid", dir, progname, port);
-   unlink(fname);
+   unlink(mp_chr(fname));
    free_pool_memory(fname);
 #endif
    return 1;
@@ -389,12 +390,12 @@ static pthread_mutex_t timer_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_cond_t timer = PTHREAD_COND_INITIALIZER;
 
 /*
- * This routine will sleep (sec, msec).  Note, however, that if a 
+ * This routine will sleep (sec, microsec).  Note, however, that if a 
  *   signal occurs, it will return early.  It is up to the caller
  *   to recall this routine if he/she REALLY wants to sleep the
  *   requested time.
  */
-int bmicrosleep(time_t sec, long msec)
+int bmicrosleep(time_t sec, long usec)
 {
    struct timespec timeout;
    struct timeval tv;
@@ -402,7 +403,7 @@ int bmicrosleep(time_t sec, long msec)
    int stat;
 
    timeout.tv_sec = sec;
-   timeout.tv_nsec = msec * 1000;
+   timeout.tv_nsec = usec * 1000;
 
 #ifdef HAVE_NANOSLEEP
    stat = nanosleep(&timeout, NULL);
@@ -438,3 +439,46 @@ strtoll(const char *ptr, char **endptr, int base)
    return (long long int)strtod(ptr, endptr);  
 }
 #endif
+
+/*
+ * Bacula's implementation of fgets(). The difference is that it handles
+ *   being interrupted by a signal (e.g. a SIGCHLD).
+ */
+#undef fgetc
+char *bfgets(char *s, int size, FILE *fd)
+{
+   char *p = s;
+   int ch;      
+   *p = 0;
+   for (int i=0; i < size-1; i++) {
+      do {
+        errno = 0;
+        ch = fgetc(fd);
+      } while (ch == -1 && (errno == EINTR || errno == EAGAIN));
+      if (ch == -1) {
+        if (i == 0) {
+           return NULL;
+        } else {
+           return s;
+        }
+      }
+      *p++ = ch;
+      *p = 0;
+      if (ch == '\n') {
+        break;
+      }
+   }
+   return s;
+}
+
+/*
+ * Make a "unique" filename.  It is important that if
+ *   called again with the same "what" that the result
+ *   will be identical. This allows us to use the file
+ *   without saving its name, and re-generate the name
+ *   so that it can be deleted.
+ */
+void make_unique_filename(POOLMEM **name, int Id, char *what)
+{
+   Mmsg(name, "%s/%s.%s.%d.tmp", working_directory, my_name, what, Id);
+}