mirror of
https://github.com/AltraMayor/f3.git
synced 2025-09-12 08:36:02 -04:00
Made f3write remove old F3 files in order
Messages like "Removing old file 0001.fff ..." were issued in a file system dependent order. This is a cosmetic change that takes advantage of the function ls_my_files previously created for f3read.
This commit is contained in:
parent
246f528e0a
commit
f7d57f76e0
63
f3read.c
63
f3read.c
@ -1,8 +1,6 @@
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <inttypes.h>
|
||||
#include <sys/types.h>
|
||||
#include <dirent.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
@ -207,67 +205,6 @@ static void iterate_files(const char *path, const int *files, int progress)
|
||||
printf("Average reading speed: %.2f %s/s\n", read_speed, unit);
|
||||
}
|
||||
|
||||
static int number_from_filename(const char *filename)
|
||||
{
|
||||
char str[FILENAME_NUM_DIGITS + 1];
|
||||
assert(is_my_file(filename));
|
||||
strncpy(str, filename, FILENAME_NUM_DIGITS);
|
||||
str[FILENAME_NUM_DIGITS] = '\0';
|
||||
return strtol(str, NULL, 10) - 1;
|
||||
}
|
||||
|
||||
/* Don't call this function directly, use ls_my_files instead. */
|
||||
static int *__ls_my_files(DIR *dir, int *pcount, int *pindex)
|
||||
{
|
||||
struct dirent *entry;
|
||||
const char *filename;
|
||||
|
||||
entry = readdir(dir);
|
||||
if (!entry) {
|
||||
int *ret = malloc(sizeof(const int) * (*pcount + 1));
|
||||
*pindex = *pcount - 1;
|
||||
ret[*pcount] = -1;
|
||||
closedir(dir);
|
||||
return ret;
|
||||
}
|
||||
|
||||
filename = entry->d_name;
|
||||
if (is_my_file(filename)) {
|
||||
int my_index;
|
||||
int *ret;
|
||||
(*pcount)++;
|
||||
ret = __ls_my_files(dir, pcount, &my_index);
|
||||
ret[my_index] = number_from_filename(filename);
|
||||
*pindex = my_index - 1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
return __ls_my_files(dir, pcount, pindex);
|
||||
}
|
||||
|
||||
/* To be used with qsort(3). */
|
||||
static int cmpintp(const void *p1, const void *p2)
|
||||
{
|
||||
return *(const int *)p1 - *(const int *)p2;
|
||||
}
|
||||
|
||||
static const int *ls_my_files(const char *path)
|
||||
{
|
||||
DIR *dir = opendir(path);
|
||||
int my_count;
|
||||
int my_index;
|
||||
int *ret;
|
||||
|
||||
if (!dir)
|
||||
err(errno, "Can't open path %s", path);
|
||||
|
||||
my_count = 0;
|
||||
ret = __ls_my_files(dir, &my_count, &my_index);
|
||||
assert(my_index == -1);
|
||||
qsort(ret, my_count, sizeof(*ret), cmpintp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc == 2) {
|
||||
|
34
f3write.c
34
f3write.c
@ -4,7 +4,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/statvfs.h>
|
||||
@ -13,7 +12,6 @@
|
||||
#include <err.h>
|
||||
#include <alloca.h>
|
||||
#include <math.h>
|
||||
#include <dirent.h>
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
@ -441,28 +439,18 @@ static int fill_fs(const char *path, int progress)
|
||||
|
||||
static void unlink_old_files(const char *path)
|
||||
{
|
||||
DIR *ptr_dir;
|
||||
struct dirent *entry;
|
||||
const char *filename;
|
||||
|
||||
ptr_dir = opendir(path);
|
||||
if (!ptr_dir)
|
||||
err(errno, "Can't open path %s", path);
|
||||
|
||||
entry = readdir(ptr_dir);
|
||||
while (entry) {
|
||||
filename = entry->d_name;
|
||||
if (is_my_file(filename)) {
|
||||
char full_fn[PATH_MAX];
|
||||
assert(snprintf(full_fn, sizeof(full_fn), "%s/%s",
|
||||
path, filename) < sizeof(full_fn));
|
||||
printf("Removing old file %s ...\n", filename);
|
||||
if (unlink(full_fn))
|
||||
err(errno, "Can't remove file %s", full_fn);
|
||||
}
|
||||
entry = readdir(ptr_dir);
|
||||
const int *files = ls_my_files(path);
|
||||
const int *number = files;
|
||||
while (*number >= 0) {
|
||||
char full_fn[PATH_MAX];
|
||||
const char *filename;
|
||||
full_fn_from_number(full_fn, &filename, path, *number);
|
||||
printf("Removing old file %s ...\n", filename);
|
||||
if (unlink(full_fn))
|
||||
err(errno, "Can't remove file %s", full_fn);
|
||||
number++;
|
||||
}
|
||||
closedir(ptr_dir);
|
||||
free((void *)files);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
|
67
utils.c
67
utils.c
@ -1,3 +1,9 @@
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <err.h>
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
const char *adjust_unit(double *ptr_bytes)
|
||||
@ -27,6 +33,67 @@ void full_fn_from_number(char *full_fn, const char **filename,
|
||||
*filename = full_fn + strlen(path) + 1;
|
||||
}
|
||||
|
||||
static int number_from_filename(const char *filename)
|
||||
{
|
||||
char str[FILENAME_NUM_DIGITS + 1];
|
||||
assert(is_my_file(filename));
|
||||
strncpy(str, filename, FILENAME_NUM_DIGITS);
|
||||
str[FILENAME_NUM_DIGITS] = '\0';
|
||||
return strtol(str, NULL, 10) - 1;
|
||||
}
|
||||
|
||||
/* Don't call this function directly, use ls_my_files instead. */
|
||||
static int *__ls_my_files(DIR *dir, int *pcount, int *pindex)
|
||||
{
|
||||
struct dirent *entry;
|
||||
const char *filename;
|
||||
|
||||
entry = readdir(dir);
|
||||
if (!entry) {
|
||||
int *ret = malloc(sizeof(const int) * (*pcount + 1));
|
||||
*pindex = *pcount - 1;
|
||||
ret[*pcount] = -1;
|
||||
closedir(dir);
|
||||
return ret;
|
||||
}
|
||||
|
||||
filename = entry->d_name;
|
||||
if (is_my_file(filename)) {
|
||||
int my_index;
|
||||
int *ret;
|
||||
(*pcount)++;
|
||||
ret = __ls_my_files(dir, pcount, &my_index);
|
||||
ret[my_index] = number_from_filename(filename);
|
||||
*pindex = my_index - 1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
return __ls_my_files(dir, pcount, pindex);
|
||||
}
|
||||
|
||||
/* To be used with qsort(3). */
|
||||
static int cmpintp(const void *p1, const void *p2)
|
||||
{
|
||||
return *(const int *)p1 - *(const int *)p2;
|
||||
}
|
||||
|
||||
const int *ls_my_files(const char *path)
|
||||
{
|
||||
DIR *dir = opendir(path);
|
||||
int my_count;
|
||||
int my_index;
|
||||
int *ret;
|
||||
|
||||
if (!dir)
|
||||
err(errno, "Can't open path %s", path);
|
||||
|
||||
my_count = 0;
|
||||
ret = __ls_my_files(dir, &my_count, &my_index);
|
||||
assert(my_index == -1);
|
||||
qsort(ret, my_count, sizeof(*ret), cmpintp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifdef APPLE_MAC
|
||||
|
||||
#include <stdio.h>
|
||||
|
Loading…
x
Reference in New Issue
Block a user