Signed-off-by: Anatolij Gustschin <agust@xxxxxxx> --- ptp.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 77 insertions(+), 0 deletions(-) diff --git a/ptp.c b/ptp.c index 7f46faf..e187fce 100644 --- a/ptp.c +++ b/ptp.c @@ -2756,6 +2756,81 @@ static size_t get_string(iconv_t ic, char *buf, const char *s, size_t len) return ret; } +static void clean_up(const char *path) +{ + struct dirent *dentry; + DIR *d; + char file_name[256]; + int ret; + + ret = chdir(path); + if (ret < 0) + return; + + d = opendir("."); + + while ((dentry = readdir(d))) { + struct stat fstat; + char *dot; + int lsize, fsize; + + dot = strrchr(dentry->d_name, '.'); + + if (!dot || dot == dentry->d_name) + continue; + + if (strcasecmp(dot, ".lock")) + continue; + + *dot = '\0'; + snprintf(file_name, sizeof(file_name), "%s", dentry->d_name); + *dot = '.'; + + ret = stat(dentry->d_name, &fstat); + if (ret < 0) { + fprintf(stderr, "%s: stat %s: %s\n", + __func__, dentry->d_name, strerror(errno)); + continue; + } + + lsize = fstat.st_size; + + ret = stat(file_name, &fstat); + if (ret < 0) { + fprintf(stderr, "%s: stat %s: %s\n", + __func__, file_name, strerror(errno)); + ret = unlink(dentry->d_name); + if (ret < 0) + fprintf(stderr, "%s: %s: %s\n", + __func__, dentry->d_name, + strerror(errno)); + continue; + } + + fsize = fstat.st_size; + if (lsize == fsize) { + if (verbose) + printf("remove %s, %s\n", + dentry->d_name, file_name); + ret = unlink(dentry->d_name); + if (ret < 0) + fprintf(stderr, "%s: %s: %s\n", + __func__, dentry->d_name, strerror(errno)); + ret = unlink(file_name); + if (ret < 0) + fprintf(stderr, "%s: %s: %s\n", + __func__, file_name, strerror(errno)); + } else { + if (verbose) + printf("remove %s\n", dentry->d_name); + ret = unlink(dentry->d_name); + if (ret < 0) + fprintf(stderr, "%s: %s: %s\n", + __func__, dentry->d_name, strerror(errno)); + } + } +} + static int enum_objects(const char *path) { struct dirent *dentry; @@ -2980,6 +3055,8 @@ int main(int argc, char *argv[]) root = argv[argc - 1]; + clean_up(root); + /* * if a client doesn't ask for storage info (as seen with some * older SW versions, e.g. on Ubuntu 8.04), then the free space -- 1.6.3.3 -- To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html