summaryrefslogtreecommitdiff
path: root/ext/repodata_diskusage.c
blob: fd9c5cc2522d22e52386ccc51e9367915c9402e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * repodata_diskusage.c
 *
 * Small helper to convert diskusage data from sustags or rpmmd
 *
 * Copyright (c) 2017, Novell Inc.
 *
 * This program is licensed under the BSD license, read LICENSE.BSD
 * for further information
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "pool.h"
#include "repo.h"
#include "util.h"
#include "repodata_diskusage.h"

/* The queue contains (dirid, kbytes, inodes) triplets */

static int
add_diskusage_sortfn(const void *ap, const void *bp, void *dp)
{
  return *(Id *)ap - *(Id *)bp;
}

void
repodata_add_diskusage(Repodata *data, Id handle, Queue *q)
{
  int i, j;
  Dirpool *dp = &data->dirpool;

  /* Sort in dirid order. This ensures that parents come before
   * their children. */
  if (q->count > 3)
    solv_sort(q->elements, q->count / 3, 3 * sizeof(Id), add_diskusage_sortfn, 0);
  for (i = 3; i < q->count; i += 3)
    {
      /* subtract data from parent */
      Id did = q->elements[i];
      if (i + 3 < q->count && q->elements[i + 3] == did)
	{
	  /* identical directory entry! zero this one */
	  q->elements[i + 1] = 0;
	  q->elements[i + 2] = 0;
	  continue;
	}
      while (did)
	{
	  did = dirpool_parent(dp, did);
	  for (j = i - 3; j >= 0; j -= 3)
	    if (q->elements[j] == did)
	      break;
	  if (j >= 0)
	    {
	      if ((unsigned int)q->elements[j + 1] > (unsigned int)q->elements[i + 1])
		q->elements[j + 1] -= q->elements[i + 1];
	      else
		q->elements[j + 1] = 0;
	      if ((unsigned int)q->elements[j + 2] > (unsigned int)q->elements[i + 2])
		q->elements[j + 2] -= q->elements[i + 2];
	      else
		q->elements[j + 2] = 0;
	      break;
	    }
	}
    }
  /* now commit data */
  for (i = 0; i < q->count; i += 3)
    if (q->elements[i + 1] || q->elements[i + 2])
      repodata_add_dirnumnum(data, handle, SOLVABLE_DISKUSAGE, q->elements[i], q->elements[i + 1], q->elements[i + 2]);
  /* empty queue */
  queue_empty(q);
}