uint32_t ymd_file_filter(const char *name, const char *path, uint32_t keep_mask, mode_t ymd_mode, uid_t ymd_uid, gid_t ymd_gid) { asl_file_t *f = NULL; uint8_t km = keep_mask; uint32_t status, len, dstcount = 0; char src[MAXPATHLEN]; char dst[MAXPATHLEN]; if (snprintf(src, MAXPATHLEN, "%s/%s", path, name) >= MAXPATHLEN) return ASL_STATUS_FAILED; if (snprintf(dst, MAXPATHLEN, "%s/%s", path, name) >= MAXPATHLEN) return ASL_STATUS_FAILED; len = strlen(src) - 3; snprintf(dst + len, 4, "tmp"); //TODO: check if src file is already filtered debug_log(ASL_LEVEL_NOTICE, " filter %s %s ---> %s\n", src, keep_str(km), dst); status = ASL_STATUS_OK; if (dryrun == 0) { status = asl_file_open_read(name, &f); if (status != ASL_STATUS_OK) return status; status = asl_file_filter_level(f, dst, keep_mask, ymd_mode, ymd_uid, ymd_gid, &dstcount, aux_url_callback); asl_file_close(f); } filesystem_unlink(src); if ((status != ASL_STATUS_OK) || (dstcount == 0)) filesystem_unlink(dst); else filesystem_rename(dst, src); return status; }
/* * Copy ASL files by reading and writing each record. */ uint32_t copy_asl_file(const char *src, const char *dst, mode_t mode) { asl_msg_list_t *res; asl_file_t *f; uint32_t status, i; uint64_t mid; size_t rcount; if (src == NULL) return ASL_STATUS_INVALID_ARG; if (dst == NULL) return ASL_STATUS_INVALID_ARG; f = NULL; status = asl_file_open_read(src, &f); if (status != ASL_STATUS_OK) return status; res = NULL; mid = 0; res = asl_file_match(f, NULL, &mid, 0, 0, 0, 1); asl_file_close(f); if (res == NULL) return ASL_STATUS_OK; rcount = asl_msg_list_count(res); if (rcount == 0) { asl_msg_list_release(res); return ASL_STATUS_OK; } f = NULL; status = asl_file_open_write(dst, mode, -1, -1, &f); if (status != ASL_STATUS_OK) return status; if (f == ASL_STATUS_OK) return ASL_STATUS_FAILED; f->flags = ASL_FILE_FLAG_PRESERVE_MSG_ID; for (i = 0; i < rcount; i++) { mid = 0; status = asl_file_save(f, asl_msg_list_get_index(res, i), &mid); if (status != ASL_STATUS_OK) break; } asl_file_close(f); return status; }
uint32_t asl_store_match_start(asl_store_t *s, uint64_t start_id, int32_t direction) { DIR *dp; struct dirent *dent; uint32_t status; asl_file_t *f; char *path; asl_file_list_t *files; if (s == NULL) return ASL_STATUS_INVALID_STORE; if (s->work != NULL) asl_file_list_match_end(s->work); s->work = NULL; files = NULL; /* * Open all readable files */ dp = opendir(s->base_dir); if (dp == NULL) return ASL_STATUS_READ_FAILED; while ((dent = readdir(dp)) != NULL) { if (dent->d_name[0] == '.') continue; path = NULL; asprintf(&path, "%s/%s", s->base_dir, dent->d_name); /* NB asl_file_open_read will fail if path is NULL, if the file is not an ASL store file, or if it isn't readable */ status = asl_file_open_read(path, &f); if (path != NULL) free(path); if ((status != ASL_STATUS_OK) || (f == NULL)) continue; files = asl_file_list_add(files, f); } closedir(dp); s->work = asl_file_list_match_start(files, start_id, direction); if (s->work == NULL) return ASL_STATUS_FAILED; return ASL_STATUS_OK; }
uint32_t asl_store_match_timeout(asl_store_t *s, aslresponse query, aslresponse *res, uint64_t *last_id, uint64_t start_id, uint32_t count, int32_t direction, uint32_t usec) { DIR *dp; struct dirent *dent; uint32_t status; asl_file_t *f; char *path; asl_file_list_t *files; if (s == NULL) return ASL_STATUS_INVALID_STORE; if (res == NULL) return ASL_STATUS_INVALID_ARG; files = NULL; /* * Open all readable files */ dp = opendir(s->base_dir); if (dp == NULL) return ASL_STATUS_READ_FAILED; while ((dent = readdir(dp)) != NULL) { if (dent->d_name[0] == '.') continue; path = NULL; asprintf(&path, "%s/%s", s->base_dir, dent->d_name); /* NB asl_file_open_read will fail if path is NULL, if the file is not an ASL store file, or if it isn't readable */ status = asl_file_open_read(path, &f); if (path != NULL) free(path); if ((status != ASL_STATUS_OK) || (f == NULL)) continue; files = asl_file_list_add(files, f); } closedir(dp); status = asl_file_list_match_timeout(files, query, res, last_id, start_id, count, direction, usec); asl_file_list_close(files); return status; }
/* remove all messages that have an ASLExpireTime key */ static uint32_t do_ASLExpireTime_filter(const char *name) { aslmsg msg; asl_file_t *in, *out; uint32_t status; uint64_t mid; char *inpath, *outpath; struct stat sb; if (name == NULL) return ASL_STATUS_INVALID_ARG; in = NULL; inpath = NULL; asprintf(&inpath, "%s/%s", store_path, name); if (inpath == NULL) return ASL_STATUS_NO_MEMORY; memset(&sb, 0, sizeof(struct stat)); if (stat(inpath, &sb) < 0) { free(inpath); return ASL_STATUS_INVALID_STORE; } status = asl_file_open_read(inpath, &in); if (status != ASL_STATUS_OK) { free(inpath); return ASL_STATUS_OK; } out = NULL; outpath = NULL; asprintf(&outpath, "%s/%s", store_path, TEMP_NAME); if (outpath == NULL) { asl_file_close(in); free(inpath); return ASL_STATUS_NO_MEMORY; } status = asl_file_open_write(outpath, sb.st_mode, sb.st_uid, sb.st_gid, &out); if (status != ASL_STATUS_OK) { asl_file_close(in); free(inpath); free(outpath); return status; } out->flags = ASL_FILE_FLAG_PRESERVE_MSG_ID; msg = NULL; while (asl_file_fetch_next(in, &msg) == ASL_STATUS_OK) { if (msg == NULL) break; mid = 0; if (asl_get(msg, ASL_KEY_EXPIRE_TIME) == NULL) status = asl_file_save(out, msg, &mid); asl_free(msg); msg = NULL; if (status != ASL_STATUS_OK) break; } asl_file_close(in); asl_file_close(out); unlink(inpath); rename(outpath, inpath); free(inpath); free(outpath); return status; }