static int hls_delete_old_segments(HLSContext *hls) {



    HLSSegment *segment, *previous_segment = NULL;

    float playlist_duration = 0.0f;

    int ret = 0, path_size, sub_path_size;

    char *dirname = NULL, *p, *sub_path;

    char *path = NULL;



    segment = hls->segments;

    while (segment) {

        playlist_duration += segment->duration;

        segment = segment->next;

    }



    segment = hls->old_segments;

    while (segment) {

        playlist_duration -= segment->duration;

        previous_segment = segment;

        segment = previous_segment->next;

        if (playlist_duration <= -previous_segment->duration) {

            previous_segment->next = NULL;

            break;

        }

    }



    if (segment && !hls->use_localtime_mkdir) {

        if (hls->segment_filename) {

            dirname = av_strdup(hls->segment_filename);

        } else {

            dirname = av_strdup(hls->avf->filename);

        }

        if (!dirname) {

            ret = AVERROR(ENOMEM);

            goto fail;

        }

        p = (char *)av_basename(dirname);

        *p = '\0';

    }



    while (segment) {

        av_log(hls, AV_LOG_DEBUG, "deleting old segment %s\n",

                                  segment->filename);

        path_size =  (hls->use_localtime_mkdir ? 0 : strlen(dirname)) + strlen(segment->filename) + 1;

        path = av_malloc(path_size);

        if (!path) {

            ret = AVERROR(ENOMEM);

            goto fail;

        }



        if (hls->use_localtime_mkdir)

            av_strlcpy(path, segment->filename, path_size);

        else { // segment->filename contains basename only

            av_strlcpy(path, dirname, path_size);

            av_strlcat(path, segment->filename, path_size);

        }



        if (unlink(path) < 0) {

            av_log(hls, AV_LOG_ERROR, "failed to delete old segment %s: %s\n",

                                     path, strerror(errno));

        }



        if (segment->sub_filename[0] != '\0') {

            sub_path_size = strlen(dirname) + strlen(segment->sub_filename) + 1;

            sub_path = av_malloc(sub_path_size);

            if (!sub_path) {

                ret = AVERROR(ENOMEM);

                goto fail;

            }



            av_strlcpy(sub_path, dirname, sub_path_size);

            av_strlcat(sub_path, segment->sub_filename, sub_path_size);

            if (unlink(sub_path) < 0) {

                av_log(hls, AV_LOG_ERROR, "failed to delete old segment %s: %s\n",

                                         sub_path, strerror(errno));

            }

            av_free(sub_path);

        }

        av_freep(&path);

        previous_segment = segment;

        segment = previous_segment->next;

        av_free(previous_segment);

    }



fail:

    av_free(path);

    av_free(dirname);



    return ret;

}

static int hls_delete_old_segments(HLSContext *hls) {



    HLSSegment *segment, *previous_segment = NULL;

    float playlist_duration = 0.0f;

    int ret = 0, path_size, sub_path_size;

    char *dirname = NULL, *p, *sub_path;

    char *path = NULL;



    segment = hls->segments;

    while (segment) {

        playlist_duration += segment->duration;

        segment = segment->next;

    }



    segment = hls->old_segments;

    while (segment) {

        playlist_duration -= segment->duration;

        previous_segment = segment;

        segment = previous_segment->next;

        if (playlist_duration <= -previous_segment->duration) {

            previous_segment->next = NULL;

            break;

        }

    }



    if (segment && !hls->use_localtime_mkdir) {

        if (hls->segment_filename) {

            dirname = av_strdup(hls->segment_filename);

        } else {

            dirname = av_strdup(hls->avf->filename);

        }

        if (!dirname) {

            ret = AVERROR(ENOMEM);

            goto fail;

        }

        p = (char *)av_basename(dirname);

        *p = '\0';

    }



    while (segment) {

        av_log(hls, AV_LOG_DEBUG, "deleting old segment %s\n",

                                  segment->filename);

        path_size =  (hls->use_localtime_mkdir ? 0 : strlen(dirname)) + strlen(segment->filename) + 1;

        path = av_malloc(path_size);

        if (!path) {

            ret = AVERROR(ENOMEM);

            goto fail;

        }



        if (hls->use_localtime_mkdir)

            av_strlcpy(path, segment->filename, path_size);

        else { 

            av_strlcpy(path, dirname, path_size);

            av_strlcat(path, segment->filename, path_size);

        }



        if (unlink(path) < 0) {

            av_log(hls, AV_LOG_ERROR, "failed to delete old segment %s: %s\n",

                                     path, strerror(errno));

        }



        if (segment->sub_filename[0] != '\0') {

            sub_path_size = strlen(dirname) + strlen(segment->sub_filename) + 1;

            sub_path = av_malloc(sub_path_size);

            if (!sub_path) {

                ret = AVERROR(ENOMEM);

                goto fail;

            }



            av_strlcpy(sub_path, dirname, sub_path_size);

            av_strlcat(sub_path, segment->sub_filename, sub_path_size);

            if (unlink(sub_path) < 0) {

                av_log(hls, AV_LOG_ERROR, "failed to delete old segment %s: %s\n",

                                         sub_path, strerror(errno));

            }

            av_free(sub_path);

        }

        av_freep(&path);

        previous_segment = segment;

        segment = previous_segment->next;

        av_free(previous_segment);

    }



fail:

    av_free(path);

    av_free(dirname);



    return ret;

}

        if (segment->sub_filename[0] != '\0') {             sub_path_size = strlen(dirname) + strlen(segment->sub_filename) + 1;
static int FUNC_0(HLSContext *VAR_0) {



    HLSSegment *segment, *previous_segment = NULL;

    float VAR_1 = 0.0f;

    int VAR_2 = 0, VAR_3, VAR_4;

    char *VAR_5 = NULL, *VAR_6, *VAR_7;

    char *VAR_8 = NULL;



    segment = VAR_0->segments;

    while (segment) {

        VAR_1 += segment->duration;

        segment = segment->next;

    }



    segment = VAR_0->old_segments;

    while (segment) {

        VAR_1 -= segment->duration;

        previous_segment = segment;

        segment = previous_segment->next;

        if (VAR_1 <= -previous_segment->duration) {

            previous_segment->next = NULL;

            break;

        }

    }



    if (segment && !VAR_0->use_localtime_mkdir) {

        if (VAR_0->segment_filename) {

            VAR_5 = av_strdup(VAR_0->segment_filename);

        } else {

            VAR_5 = av_strdup(VAR_0->avf->filename);

        }

        if (!VAR_5) {

            VAR_2 = AVERROR(ENOMEM);

            goto fail;

        }

        VAR_6 = (char *)av_basename(VAR_5);

        *VAR_6 = '\0';

    }



    while (segment) {

        av_log(VAR_0, AV_LOG_DEBUG, "deleting old segment %s\n",

                                  segment->filename);

        VAR_3 =  (VAR_0->use_localtime_mkdir ? 0 : strlen(VAR_5)) + strlen(segment->filename) + 1;

        VAR_8 = av_malloc(VAR_3);

        if (!VAR_8) {

            VAR_2 = AVERROR(ENOMEM);

            goto fail;

        }



        if (VAR_0->use_localtime_mkdir)

            av_strlcpy(VAR_8, segment->filename, VAR_3);

        else { 

            av_strlcpy(VAR_8, VAR_5, VAR_3);

            av_strlcat(VAR_8, segment->filename, VAR_3);

        }



        if (unlink(VAR_8) < 0) {

            av_log(VAR_0, AV_LOG_ERROR, "failed to delete old segment %s: %s\n",

                                     VAR_8, strerror(errno));

        }



        if (segment->sub_filename[0] != '\0') {

            VAR_4 = strlen(VAR_5) + strlen(segment->sub_filename) + 1;

            VAR_7 = av_malloc(VAR_4);

            if (!VAR_7) {

                VAR_2 = AVERROR(ENOMEM);

                goto fail;

            }



            av_strlcpy(VAR_7, VAR_5, VAR_4);

            av_strlcat(VAR_7, segment->sub_filename, VAR_4);

            if (unlink(VAR_7) < 0) {

                av_log(VAR_0, AV_LOG_ERROR, "failed to delete old segment %s: %s\n",

                                         VAR_7, strerror(errno));

            }

            av_free(VAR_7);

        }

        av_freep(&VAR_8);

        previous_segment = segment;

        segment = previous_segment->next;

        av_free(previous_segment);

    }



fail:

    av_free(VAR_8);

    av_free(VAR_5);



    return VAR_2;

}

static int FUNC_0(HLSContext *VAR_0) { HLSSegment *segment, *previous_segment = NULL; float VAR_1 = 0.0f; int VAR_2 = 0, VAR_3, VAR_4; char *VAR_5 = NULL, *VAR_6, *VAR_7; char *VAR_8 = NULL; segment = VAR_0->segments; while (segment) { VAR_1 += segment->duration; segment = segment->next; } segment = VAR_0->old_segments; while (segment) { VAR_1 -= segment->duration; previous_segment = segment; segment = previous_segment->next; if (VAR_1 <= -previous_segment->duration) { previous_segment->next = NULL; break; } } if (segment && !VAR_0->use_localtime_mkdir) { if (VAR_0->segment_filename) { VAR_5 = av_strdup(VAR_0->segment_filename); } else { VAR_5 = av_strdup(VAR_0->avf->filename); } if (!VAR_5) { VAR_2 = AVERROR(ENOMEM); goto fail; } VAR_6 = (char *)av_basename(VAR_5); *VAR_6 = '\0'; } while (segment) { av_log(VAR_0, AV_LOG_DEBUG, "deleting old segment %s\n",
segment->filename); VAR_3 =  (VAR_0->use_localtime_mkdir ? 0 : strlen(VAR_5)) + strlen(segment->filename) + 1; VAR_8 = av_malloc(VAR_3); if (!VAR_8) { VAR_2 = AVERROR(ENOMEM); goto fail; } if (VAR_0->use_localtime_mkdir)
av_strlcpy(VAR_8, segment->filename, VAR_3); else { av_strlcpy(VAR_8, VAR_5, VAR_3); av_strlcat(VAR_8, segment->filename, VAR_3); } if (unlink(VAR_8) < 0) { av_log(VAR_0, AV_LOG_ERROR, "failed to delete old segment %s: %s\n",
VAR_8, strerror(errno)); } if (segment->sub_filename[0] != '\0') { VAR_4 = strlen(VAR_5) + strlen(segment->sub_filename) + 1; VAR_7 = av_malloc(VAR_4); if (!VAR_7) { VAR_2 = AVERROR(ENOMEM); goto fail; } av_strlcpy(VAR_7, VAR_5, VAR_4); av_strlcat(VAR_7, segment->sub_filename, VAR_4); if (unlink(VAR_7) < 0) { av_log(VAR_0, AV_LOG_ERROR, "failed to delete old segment %s: %s\n",
VAR_7, strerror(errno)); } av_free(VAR_7); } av_freep(&VAR_8); previous_segment = segment; segment = previous_segment->next; av_free(previous_segment); } fail:
av_free(VAR_8); av_free(VAR_5); return VAR_2; }