/* * A common log output function. */ int log_print(const char *filename, int line, const char *prefix, const char *fmt, ...) { va_list fmtargs; time_t t; struct tm *tt; FILE *of = stdout; /* Print the error message(s) */ t = time(NULL); tt = localtime(&t); fprintf(of, "[%02d/%02d/%04d %02d:%02d:%02d] %s: %s(%d): ", tt->tm_mon + 1, tt->tm_mday, tt->tm_year + 1900, tt->tm_hour, tt->tm_min, tt->tm_sec, prefix, find_filename(filename), line); va_start(fmtargs, fmt); vfprintf(of, fmt, fmtargs); va_end(fmtargs); fprintf(of, "\n"); fflush(of); return OK; }
static void fix_filename(const char *original, char *fixed) { const char *start; int i; int len; start = original; if (original[0] == ':') { start = &original[1]; } fixed[MAX_PATH-1] = 0; strncpy(fixed, start, MAX_PATH); /* check to see if strncpy overwrote the terminator */ if (fixed[MAX_PATH-1] != 0) { fixed[MAX_PATH-1] = 0; fprintf(stderr, "ERROR: file truncation error: %s -> %s\n", original, fixed); } len = strlen(fixed); for (i = 0; i < len; i++) { if (fixed[i] == ':') { fixed[i] = '/'; } } /* here we would try to see if the file is available (game dir), else try another dir really, this function also needs a flag to indicate whether it should only go to local (write) or both (read) */ if (find_filename(fixed) == 0) { fprintf(stderr, "find failed: %s\n", fixed); } }
/* * Other special preprocesses, such as wmftogif. * The content of the 'filename' parameter should be * replace if the preprocess converts from one file to * another. Return value indicates whether any second * preprocess has been performed. */ static gboolean preprocess_second(gchar *filename) { guchar *ext, *tempfile, newfile[256]; gint pid, status; ext = strrchr(filename, '.'); if (ext == NULL) return FALSE; if (strcmp(ext, ".wmf") == 0) { /* try to preprocess it into /tmp */ if ((tempfile = find_filename(filename)) == NULL) return FALSE; strcpy(newfile, "/tmp/"); strncat(newfile, tempfile, ext - tempfile); strcat(newfile, ".gif"); if ((pid = fork()) < 0) return FALSE; if (pid == 0) { /* child process */ FILE* f; if (!(f = fopen("/dev/null","w"))) { _exit(127); } if (-1 == dup2(fileno(f),fileno(stderr))) { _exit(127); } execlp ("wmftogif", "wmftogif", filename, newfile, NULL); _exit(127); } else { /* parent process */ waitpid(pid, &status, 0); strcpy(filename, newfile); return TRUE; } } return FALSE; }
int fs_open(const char *pathname, int flags) { /* 在我们的实现中可以忽略flags */ int i = find_filename(pathname); state_array[i].opened = true; //i is the right place, set in find_filename state_array[i].offset = 0; return i; //used as fd }
/* Given the originfile and newfile, generating a delta and patch to originfile(at the server side anyway) para1: originfile: an original file newfile: new version file basefile: file to be patched with delta on server resfile: newly generated file on server return delta file name */ int compute_delta(char *originfile, char *newfile, char *basefile, char *resfile, char *file_delta_path) { // generate signature rs_stats_t stats; rs_result rsyncresult; rs_signature_t *sumset; char filename[NAME_LEN]; char resname[NAME_LEN]; char file_sig_path[PATH_LEN]; strcpy(file_sig_path, TMP_SIG); //char *file_delta_path = (char *) malloc (PATH_LEN * sizeof(char)); strcpy(file_delta_path, TMP_DELTA); FILE *ofile, *sigfile, *deltafile, *nfile; printf("compute_delta:%s %s %s %s\n", originfile, newfile, basefile, resfile); ofile = fopen(originfile, "rb"); if(ofile == NULL) { printf("origin file %s open error.\n", originfile); return 1; } find_filename(basefile, filename); find_filename(resfile, resname); strcat(file_sig_path, filename); sigfile = fopen(file_sig_path, "wb+"); rsyncresult = rs_sig_file(ofile, sigfile, RS_DEFAULT_BLOCK_LEN, RS_DEFAULT_STRONG_LEN, &stats); if (rsyncresult != RS_DONE) { printf("generate signature Failed!\n"); return 1; } rs_log_stats(&stats); // load sig file fseek(sigfile, 0, 0); rsyncresult = rs_loadsig_file(sigfile, &sumset, &stats, ofile); //rsyncresult = rs_loadsig_file(sigfile, &sumset, &stats); // fix a bug //fclose(ofile); fclose(sigfile); remove(file_sig_path); if (rsyncresult != RS_DONE) { printf("load signature into librsync Failed!\n"); return 1; } rsyncresult = rs_build_hash_table(sumset); if (rsyncresult != RS_DONE) { printf("build hash table in librsync Failed!\n"); rs_free_sumset(sumset); return 1; } rs_log_stats(&stats); // compute delta strcat(file_delta_path, filename); char rand_c[4] = {'\0'}; rand_c[0] = (char)(rand() % 26 + 97); rand_c[1] = (char)(rand() % 26 + 97); rand_c[2] = (char)(rand() % 26 + 97); strcat(file_delta_path, rand_c); deltafile = fopen(file_delta_path, "wb+"); nfile = fopen(newfile,"rb"); if(nfile == NULL) { printf("new file %s open error.\n", newfile); return 1; } rsyncresult = rs_delta_file(sumset, nfile, deltafile, &stats); fclose(ofile); fclose(nfile); fclose(deltafile); if (rsyncresult != RS_DONE) { printf("delta calculation Failed!\n"); rs_free_sumset(sumset); return 1; } rs_log_stats(&stats); rs_free_sumset(sumset); return 0; }