int file_append(const char* format, ...) { int result = 0; if (!g_fd) { result = file_reset(FALSE); if (result) { goto cleanup; } } char sBuffer[BUFFER_SIZE]; va_list params; va_start(params, format); vsnprintf(sBuffer, BUFFER_SIZE, format, params); va_end(params); size_t count = strlen(sBuffer); size_t s = fwrite(sBuffer, 1, count, g_fd); if (s != count) { ERROR_LOG("Cannot append to file %s. Error(%d): %s\n", g_filename, errno, strerror(errno)); result = 1; goto cleanup; } g_total_op++; if (g_total_op >= g_max_op && g_max_op > 0) { g_file_full = TRUE; } cleanup: return result; }
/** * Perform a full factory reset. * This can be triggered automatically at boot time if * certain sanity checks fail, or via a test mode option. */ void factory_reset (void) { file_reset (); memset (AREA_BASE (permanent), 0, AREA_SIZE (permanent)); callset_invoke (factory_reset); // timestamp_update (&system_timestamps.factory_reset); }
R_API const char * r_magic_buffer(RMagic *ms, const void *buf, size_t nb) { if (file_reset (ms) == -1) return NULL; /* * The main work is done here! * We have the file name and/or the data buffer to be identified. */ if (file_buffer (ms, -1, NULL, buf, nb) == -1) return NULL; return file_getbuffer (ms); }
/** * Perform a full factory reset. * This can be triggered automatically at boot time if * certain sanity checks fail, or via a test mode option. */ void factory_reset (void) { file_reset (); #ifdef __m6809__ memset (AREA_BASE (permanent), 0, AREA_SIZE (permanent)); #else /* TODO - how to clean the permanent area in native mode? */ #endif timestamp_update (&system_timestamps.factory_reset); callset_invoke (factory_reset); }
byte file_close(file *io){ //return error if (io->fp == NULL){ fprintf(stderr, "[error] file_close: io->fp == NULL\n"); return -1; } int i = fclose(io->fp); file_reset(io); if (i != 0){ fprintf(stderr, "[error] file_close: error id [%d]\n", i); return -2; } return 0; }
// TODO: reinitialize all the time R_API RMagic* r_magic_new(int flags) { RMagic *ms = R_NEW0 (RMagic); if (!ms) return NULL; r_magic_setflags (ms, flags); ms->o.buf = ms->o.pbuf = NULL; ms->c.li = malloc ((ms->c.len = 10) * sizeof (*ms->c.li)); if (!ms->c.li) { free (ms); return NULL; } file_reset (ms); ms->mlist = NULL; ms->file = "unknown"; ms->line = 0; return ms; }
/* read (extract) 'zipfile' in current directory */ int zread(const char *zipfile, unsigned long long maxsize) { int rc; const char *args[6]; args[0] = "unzip"; args[1] = "-q"; args[2] = "-d"; args[3] = workdir; args[4] = zipfile; args[5] = NULL; file_reset(); rc = zrun(args[0], args); if (!rc) rc = fill_files(); return rc; }
byte file_open(file *io, char *path, char *mode){ //return error int i; #if NULL_ARG_CHECK if (mode == NULL){ fprintf(stderr, "[error] file_open: mode == NULL\n"); return -1; } #endif if (io->fp != NULL){ file_close(io); } file_reset(io); io->fp = fopen(path, mode); if (io->fp == NULL){ if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a'){ fprintf(stderr, "[error] file_open: mode not in (r, w, a) [%s, %s]\n", path, mode); return -2; } else{ fprintf(stderr, "[error] file_open: file not exist [%s, %s]\n", path, mode); return -3; } } for (i=0; i<3; i++){ if (mode[i] == '\x00'){ break; } else if (mode[i] == 'r'){ io->size = file_get_size(path); io->mode_read = 1; } else if (mode[i] == 'w'){ io->mode_write = 1; } else if (mode[i] == 'a' || mode[i] == '+'){ io->mode_add = 1; } } return 0; }
static const char *file_or_fd(RMagic *ms, const char *inname, int fd) { int ispipe = 0, rv = -1; unsigned char *buf; struct stat sb; int nbytes = 0; /* number of bytes read from a datafile */ /* * one extra for terminating '\0', and * some overlapping space for matches near EOF */ #define SLOP (1 + sizeof(union VALUETYPE)) if (!(buf = malloc (HOWMANY + SLOP))) { return NULL; } if (file_reset (ms) == -1) { goto done; } switch (file_fsmagic (ms, inname, &sb)) { case -1: goto done; /* error */ case 0: break; /* nothing found */ default: rv = 0; goto done; /* matched it and printed type */ } if (!inname) { if (fstat (fd, &sb) == 0 && S_ISFIFO (sb.st_mode)) { ispipe = 1; } } else { int flags = O_RDONLY|O_BINARY; if (stat (inname, &sb) == 0 && S_ISFIFO (sb.st_mode)) { #if O_NONBLOCK flags |= O_NONBLOCK; #endif ispipe = 1; } errno = 0; if ((fd = open (inname, flags)) < 0) { eprintf ("couldn't open file\n"); if (info_from_stat (ms, sb.st_mode) == -1) { goto done; } rv = 0; goto done; } #ifdef O_NONBLOCK if ((flags = fcntl (fd, F_GETFL)) != -1) { flags &= ~O_NONBLOCK; (void)fcntl (fd, F_SETFL, flags); } #endif } /* * try looking at the first HOWMANY bytes */ #ifdef O_NONBLOCK if (ispipe) { ssize_t r = 0; //while ((r = sread(fd, (void *)&buf[nbytes], while ((r = read(fd, (void *)&buf[nbytes], (size_t)(HOWMANY - nbytes))) > 0) { nbytes += r; if (r < PIPE_BUF) { break; } } if (nbytes == 0) { /* We can not read it, but we were able to stat it. */ if (info_from_stat (ms, sb.st_mode) == -1) { goto done; } rv = 0; goto done; } } else { #endif if ((nbytes = read(fd, (char *)buf, HOWMANY)) == -1) { file_error(ms, errno, "cannot read `%s'", inname); goto done; } #ifdef O_NONBLOCK } #endif (void)memset (buf + nbytes, 0, SLOP); /* NUL terminate */ if (file_buffer (ms, fd, inname, buf, (size_t)nbytes) == -1) { goto done; } rv = 0; done: free (buf); close_and_restore (ms, inname, fd, &sb); return rv == 0 ? file_getbuffer(ms) : NULL; }
/* read (extract) 'zipfile' in current directory */ int zread(const char *zipfile, unsigned long long maxsize) { struct filedesc *fdesc; int err, fd; size_t len; struct zip *zip; zip_int64_t z64; zip_uint64_t uz64; unsigned int count, index; struct zip_file *zfile; struct zip_stat zstat; char buffer[32768]; ssize_t sizr, sizw; size_t esize; /* open the zip file */ zip = zip_open(zipfile, ZIP_CHECKCONS, &err); if (!zip) { ERROR("Can't connect to file %s", zipfile); return -1; } z64 = zip_get_num_entries(zip, 0); if (z64 < 0 || z64 > UINT_MAX) { ERROR("too many entries in %s", zipfile); goto error; } count = (unsigned int)z64; /* records the files */ file_reset(); esize = 0; for (index = 0 ; index < count ; index++) { err = zip_stat_index(zip, index, ZIP_FL_ENC_GUESS, &zstat); /* check the file name */ if (!is_valid_filename(zstat.name)) { ERROR("invalid entry %s found in %s", zstat.name, zipfile); goto error; } if (zstat.name[0] == '/') { ERROR("absolute entry %s found in %s", zstat.name, zipfile); goto error; } len = strlen(zstat.name); if (len == 0) { ERROR("empty entry found in %s", zipfile); goto error; } if (zstat.name[len - 1] == '/') /* record */ fdesc = file_add_directory(zstat.name); else { /* get the size */ esize += zstat.size; /* record */ fdesc = file_add_file(zstat.name); } if (!fdesc) goto error; fdesc->zindex = index; } /* check the size */ if (maxsize && esize > maxsize) { ERROR("extracted size %zu greater than allowed size %llu", esize, maxsize); goto error; } /* unpack the recorded files */ assert(count == file_count()); for (index = 0 ; index < count ; index++) { fdesc = file_of_index(index); assert(fdesc != NULL); err = zip_stat_index(zip, fdesc->zindex, ZIP_FL_ENC_GUESS, &zstat); assert(zstat.name[0] != '/'); len = strlen(zstat.name); assert(len > 0); if (zstat.name[len - 1] == '/') { /* directory name */ err = create_directory((char*)zstat.name, MODE_OF_DIRECTORY_CREATION); if (err && errno != EEXIST) goto error; } else { /* file name */ zfile = zip_fopen_index(zip, fdesc->zindex, 0); if (!zfile) { ERROR("Can't open %s in %s", zstat.name, zipfile); goto error; } fd = create_file((char*)zstat.name, MODE_OF_FILE_CREATION, MODE_OF_DIRECTORY_CREATION); if (fd < 0) goto errorz; /* extract */ uz64 = zstat.size; while (uz64) { sizr = zip_fread(zfile, buffer, sizeof buffer); if (sizr < 0) { ERROR("error while reading %s in %s", zstat.name, zipfile); goto errorzf; } sizw = write(fd, buffer, (size_t)sizr); if (sizw < 0) { ERROR("error while writing %s", zstat.name); goto errorzf; } uz64 -= (size_t)sizw; } close(fd); zip_fclose(zfile); } } zip_close(zip); return 0; errorzf: close(fd); errorz: zip_fclose(zfile); error: zip_close(zip); return -1; }