summaryrefslogtreecommitdiff
path: root/src/dir_mem.c
blob: f513ab76cde7d2b0f656dd7e71e74f5b03e4c32d (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* ncdu - NCurses Disk Usage

  Copyright (c) 2007-2020 Yoran Heling

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be included
  in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

#include "global.h"

#include <string.h>
#include <stdlib.h>

#include <khashl.h>


static struct dir *root;   /* root directory struct we're scanning */
static struct dir *curdir; /* directory item that we're currently adding items to */
static struct dir *orig;   /* original directory, when refreshing an already scanned dir */

/* Table of struct dir items with more than one link (in order to detect hard links) */
#define hlink_hash(d)     (kh_hash_uint64((khint64_t)d->dev) ^ kh_hash_uint64((khint64_t)d->ino))
#define hlink_equal(a, b) ((a)->dev == (b)->dev && (a)->ino == (b)->ino)
KHASHL_SET_INIT(KH_LOCAL, hl_t, hl, struct dir *, hlink_hash, hlink_equal)
static hl_t *links = NULL;


/* recursively checks a dir structure for hard links and fills the lookup array */
static void hlink_init(struct dir *d) {
  struct dir *t;

  for(t=d->sub; t!=NULL; t=t->next)
    hlink_init(t);

  if(!(d->flags & FF_HLNKC))
    return;
  int r;
  hl_put(links, d, &r);
}


/* checks an individual file for hard links and updates its cicrular linked
 * list, also updates the sizes of the parent dirs */
static void hlink_check(struct dir *d) {
  struct dir *t, *pt, *par;
  int i;

  /* add to links table */
  khint_t k = hl_put(links, d, &i);

  /* found in the table? update hlnk */
  if(!i) {
    t = kh_key(links, k);
    d->hlnk = t->hlnk == NULL ? t : t->hlnk;
    t->hlnk = d;
  }

  /* now update the sizes of the parent directories,
   * This works by only counting this file in the parent directories where this
   * file hasn't been counted yet, which can be determined from the hlnk list.
   * XXX: This may not be the most efficient algorithm to do this */
  for(i=1,par=d->parent; i&&par; par=par->parent) {
    if(d->hlnk)
      for(t=d->hlnk; i&&t!=d; t=t->hlnk)
        for(pt=t->parent; i&&pt; pt=pt->parent)
          if(pt==par)
            i=0;
    if(i) {
      par->size = adds64(par->size, d->size);
      par->asize = adds64(par->asize, d->asize);
    }
  }
}


/* Add item to the correct place in the memory structure */
static void item_add(struct dir *item) {
  if(!root) {
    root = item;
    /* Make sure that the *root appears to be part of the same dir structure as
     * *orig, otherwise the directory size calculation will be incorrect in the
     * case of hard links. */
    if(orig)
      root->parent = orig->parent;
  } else {
    item->parent = curdir;
    item->next = curdir->sub;
    if(item->next)
      item->next->prev = item;
    curdir->sub = item;
  }
}


static int item(struct dir *dir, const char *name, struct dir_ext *ext, unsigned int nlink) {
  struct dir *t, *item;
  (void)nlink;

  /* Go back to parent dir */
  if(!dir) {
    curdir = curdir->parent;
    return 0;
  }

  if(!root && orig)
    name = orig->name;

  if(!extended_info)
    dir->flags &= ~FF_EXT;
  item = xmalloc(dir->flags & FF_EXT ? dir_ext_memsize(name) : dir_memsize(name));
  memcpy(item, dir, offsetof(struct dir, name));
  strcpy(item->name, name);
  if(dir->flags & FF_EXT)
    memcpy(dir_ext_ptr(item), ext, sizeof(struct dir_ext));

  item_add(item);

  /* Ensure that any next items will go to this directory */
  if(item->flags & FF_DIR)
    curdir = item;

  /* Special-case the name of the root item to be empty instead of "/". This is
   * what getpath() expects. */
  if(item == root && strcmp(item->name, "/") == 0)
    item->name[0] = 0;

  /* Update stats of parents. Don't update the size/asize fields if this is a
   * possible hard link, because hlnk_check() will take care of it in that
   * case. */
  if(item->flags & FF_HLNKC) {
    addparentstats(item->parent, 0, 0, 0, 1);
    hlink_check(item);
  } else if(item->flags & FF_EXT) {
    addparentstats(item->parent, item->size, item->asize, dir_ext_ptr(item)->mtime, 1);
  } else {
    addparentstats(item->parent, item->size, item->asize, 0, 1);
  }

  /* propagate ERR and SERR back up to the root */
  if(item->flags & FF_SERR || item->flags & FF_ERR)
    for(t=item->parent; t; t=t->parent)
      t->flags |= FF_SERR;

  dir_output.size = root->size;
  dir_output.items = root->items;

  return 0;
}


static int final(int fail) {
  hl_destroy(links);
  links = NULL;

  if(fail) {
    freedir(root);
    if(orig) {
      browse_init(orig);
      return 0;
    } else
      return 1;
  }

  /* success, update references and free original item */
  if(orig) {
    root->next = orig->next;
    root->prev = orig->prev;
    if(root->parent && root->parent->sub == orig)
      root->parent->sub = root;
    if(root->prev)
      root->prev->next = root;
    if(root->next)
      root->next->prev = root;
    orig->next = orig->prev = NULL;
    freedir(orig);
  }

  browse_init(root);
  dirlist_top(-3);
  return 0;
}


void dir_mem_init(struct dir *_orig) {
  orig = _orig;
  root = curdir = NULL;
  pstate = ST_CALC;

  dir_output.item = item;
  dir_output.final = final;
  dir_output.size = 0;
  dir_output.items = 0;

  /* Init hash table for hard link detection */
  links = hl_init();
  if(orig)
    hlink_init(getroot(orig));
}