/*///---------------------------------------------------------------------\\\*/
/*// Copyright (c)               (_)/=\~/_(_)         All rights reserved. \\*/
/*/  2005-2006                (_)  /=\  ~/_  (_)        Released under the  \*/
/*\  Jim Peters            (_)    /=\    ~/_    (_)             GNU GPL v2  /*/
/*\\ http://uazu.net    (_) ____ /=\ ____ ~/_ ____ (_)      http://fsf.org //*/
/*\\\---------------------------------------------------------------------///*/

//
//	Changelog (local-pc-changes) handling.  
//   
//	  void chlog_open(char *idts);
//	  void chlog_close();
//
//	Write a line to the log-file.  This checks the actual
//	filesystem to report an update or deletion for the given file
//	or directory.  For this reason, the call must be made after
//	all changes have been made.
//
//	  void wrlog(char *nam, char *path);
//
//	Compare paths, so '/' compares before all other characters
//
//	  int path_cmp(uchar *p, uchar *q);
//

#ifdef HEADER

#else

#include "all.h"

FILE *chlog;
int chlog_seq;

// Open the log-file to append; fails if already exists
void 
chlog_open(char *idts) {
   struct stat sb;
   char *path= StrDupF("%s/local-pc-changes", idts);

   if (chlog) error("chlog_open() usage error: local-pc-changes file is already open");

   if (0 == stat(path, &sb))
      error("Fatal: changes file already exists:\n  %s", path);

   if (!(chlog= fopen(path, "a")))
      error("Can't create changes file (%s):\n  %s", 
	    strerror(errno), path);

   chlog_seq= 0;
   free(path);
}


// Close log-file
void 
chlog_close() {
   if (fclose(chlog))
      error("Error closing local-pc-changes file (%s)", strerror(errno));
   chlog= 0;
}


// Write a log-line; 'xtra' may point into scratch
static void 
wrlog_out(char *nam, char *xtra) {
   char *p;
   char seq[16];
   char *txt;
   
   sprintf(seq, "%d", chlog_seq++);
   txt= Alloc(strlen(nam) + strlen(seq) + strlen(xtra) + 5);
   sprintf(txt, "%s\t\t%s\t%s\n", nam, seq, xtra);
   for (p= txt; *p != '\t'; p++) if (*p == '/') *p= '\t';

   if (0 > fprintf(chlog, "%s", txt) ||
       0 != fflush(chlog)) 
      error("Error writing to local-pc-changes file (%s)", strerror(errno));
   free(txt);
}
   
// Write a log-entry for deletion or update, selecting and filling in
// details according to the state of the file-system; this way we can
// never put incorrect information into the log by accident
void 
wrlog(char *nam, char *path) {
   char *dsc= smtree_lookup(path, 0, "u");
   if (dsc[0]) wrlog_out(nam, dsc);
}

// Compare paths, ensuring that '/' sorts before anything else
int 
path_cmp(unsigned char *p, unsigned char *q) {
   int ch1, ch2, cmp;
   do {
      ch1= *p++;
      ch2= *q++;
      if (ch1 == '/') ch1= 1;
      if (ch2 == '/') ch2= 1;
      cmp= ch1-ch2;
      if (cmp) return cmp;
   } while (ch1);
   return 0;
}
 
   
#endif

/*///---------------------------------------------------------------------\\\*/
/*// )`( )()/)(`/\`//\`          (_)/=\~/_(_)           '/\\'/\')(\()( )'( \\*/
/*/ ()(')()\)(/`/\\ `         (_)  /=\  ~/_  (_)          ' //\'\)(/()(`)() \*/
/*\  )`()( )(/'            (_)    /=\    ~/_    (_)             `\)( )()'(  /*/
/*\\  ()()(`)(/         (_) ____ /=\ ____ ~/_ ____ (_)          \)(')()()  //*/
/*\\\---------------------------------------------------------------------///*/
