File: dir_export.c

package info (click to toggle)
ncdu 1.13-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 660 kB
  • sloc: ansic: 3,544; sh: 1,231; makefile: 41
file content (190 lines) | stat: -rw-r--r-- 5,170 bytes parent folder | download
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
/* ncdu - NCurses Disk Usage

  Copyright (c) 2007-2018 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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>


static FILE *stream;

/* Stack of device IDs, also used to keep track of the level of nesting */
struct stack {
  uint64_t *list;
  int size, top;
} stack;


static void output_string(const char *str) {
  for(; *str; str++) {
    switch(*str) {
    case '\n': fputs("\\n", stream); break;
    case '\r': fputs("\\r", stream); break;
    case '\b': fputs("\\b", stream); break;
    case '\t': fputs("\\t", stream); break;
    case '\f': fputs("\\f", stream); break;
    case '\\': fputs("\\\\", stream); break;
    case '"':  fputs("\\\"", stream); break;
    default:
      if((unsigned char)*str <= 31 || (unsigned char)*str == 127)
        fprintf(stream, "\\u00%02x", *str);
      else
        fputc(*str, stream);
      break;
    }
  }
}


static void output_int(uint64_t n) {
  char tmp[20];
  int i = 0;

  do
    tmp[i++] = n % 10;
  while((n /= 10) > 0);

  while(i--)
    fputc(tmp[i]+'0', stream);
}


static void output_info(struct dir *d, const char *name, struct dir_ext *e) {
  if(!extended_info || !(d->flags & FF_EXT))
    e = NULL;

  fputs("{\"name\":\"", stream);
  output_string(name);
  fputc('"', stream);

  /* No need for asize/dsize if they're 0 (which happens with excluded or failed-to-stat files) */
  if(d->asize) {
    fputs(",\"asize\":", stream);
    output_int((uint64_t)d->asize);
  }
  if(d->size) {
    fputs(",\"dsize\":", stream);
    output_int((uint64_t)d->size);
  }

  if(d->dev != nstack_top(&stack, 0)) {
    fputs(",\"dev\":", stream);
    output_int(d->dev);
  }
  fputs(",\"ino\":", stream);
  output_int(d->ino);

  if(e) {
    fputs(",\"uid\":", stream);
    output_int(e->uid);
    fputs(",\"gid\":", stream);
    output_int(e->gid);
    fputs(",\"mode\":", stream);
    output_int(e->mode);
    fputs(",\"mtime\":", stream);
    output_int(e->mtime);
  }

  /* TODO: Including the actual number of links would be nicer. */
  if(d->flags & FF_HLNKC)
    fputs(",\"hlnkc\":true", stream);
  if(d->flags & FF_ERR)
    fputs(",\"read_error\":true", stream);
  /* excluded/error'd files are "unknown" with respect to the "notreg" field. */
  if(!(d->flags & (FF_DIR|FF_FILE|FF_ERR|FF_EXL|FF_OTHFS)))
    fputs(",\"notreg\":true", stream);
  if(d->flags & FF_EXL)
    fputs(",\"excluded\":\"pattern\"", stream);
  else if(d->flags & FF_OTHFS)
    fputs(",\"excluded\":\"othfs\"", stream);

  fputc('}', stream);
}


/* Note on error handling: For convenience, we just keep writing to *stream
 * without checking the return values of the functions. Only at the and of each
 * item() call do we check for ferror(). This greatly simplifies the code, but
 * assumes that calls to fwrite()/fput./etc don't do any weird stuff when
 * called with a stream that's in an error state. */
static int item(struct dir *item, const char *name, struct dir_ext *ext) {
  if(!item) {
    nstack_pop(&stack);
    if(!stack.top) { /* closing of the root item */
      fputs("]]", stream);
      return fclose(stream);
    } else /* closing of a regular directory item */
      fputs("]", stream);
    return ferror(stream);
  }

  dir_output.items++;

  /* File header.
   * TODO: Add scan options? */
  if(!stack.top) {
    fputs("[1,1,{\"progname\":\""PACKAGE"\",\"progver\":\""PACKAGE_VERSION"\",\"timestamp\":", stream);
    output_int((uint64_t)time(NULL));
    fputc('}', stream);
  }

  fputs(",\n", stream);
  if(item->flags & FF_DIR)
    fputc('[', stream);

  output_info(item, name, ext);

  if(item->flags & FF_DIR)
    nstack_push(&stack, item->dev);

  return ferror(stream);
}


static int final(int fail) {
  nstack_free(&stack);
  return fail ? 1 : 1; /* Silences -Wunused-parameter */
}


int dir_export_init(const char *fn) {
  if(strcmp(fn, "-") == 0)
    stream = stdout;
  else if((stream = fopen(fn, "w")) == NULL)
    return 1;

  nstack_init(&stack);

  pstate = ST_CALC;
  dir_output.item = item;
  dir_output.final = final;
  dir_output.size = 0;
  dir_output.items = 0;
  return 0;
}