scalar-diagnose: move 'get_disk_info()' to 'compat/'

Move 'get_disk_info()' function into 'compat/'. Although Scalar-specific code is generally not part of the main Git tree, 'get_disk_info()' will be used in subsequent patches by additional callers beyond 'scalar diagnose'. This patch prepares for that change, at which point this platform-specific code should be part of 'compat/' as a matter of convention. The function is copied *mostly* verbatim, with two exceptions: * '#ifdef WIN32' is replaced with '#ifdef GIT_WINDOWS_NATIVE' to allow 'statvfs' to be used with Cygwin. * the 'struct strbuf buf' and 'int res' (as well as their corresponding cleanup & return) are moved outside of the '#ifdef' block. Signed-off-by: Victoria Dye <vdye@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Victoria Dye committed Aug 12, 2022 at 20:10 UTC 435a2535b72aa0e3a2e152841fe79c5b65a6e8c0
3 files changed +58 -52
compat/disk.h new
+56
@@ -0,0 +1,56 @@
1 +#ifndef COMPAT_DISK_H
2 +#define COMPAT_DISK_H
3 +
4 +#include "git-compat-util.h"
5 +
6 +static int get_disk_info(struct strbuf *out)
7 +{
8 + struct strbuf buf = STRBUF_INIT;
9 + int res = 0;
10 +
11 +#ifdef GIT_WINDOWS_NATIVE
12 + char volume_name[MAX_PATH], fs_name[MAX_PATH];
13 + DWORD serial_number, component_length, flags;
14 + ULARGE_INTEGER avail2caller, total, avail;
15 +
16 + strbuf_realpath(&buf, ".", 1);
17 + if (!GetDiskFreeSpaceExA(buf.buf, &avail2caller, &total, &avail)) {
18 + error(_("could not determine free disk size for '%s'"),
19 + buf.buf);
20 + res = -1;
21 + goto cleanup;
22 + }
23 +
24 + strbuf_setlen(&buf, offset_1st_component(buf.buf));
25 + if (!GetVolumeInformationA(buf.buf, volume_name, sizeof(volume_name),
26 + &serial_number, &component_length, &flags,
27 + fs_name, sizeof(fs_name))) {
28 + error(_("could not get info for '%s'"), buf.buf);
29 + res = -1;
30 + goto cleanup;
31 + }
32 + strbuf_addf(out, "Available space on '%s': ", buf.buf);
33 + strbuf_humanise_bytes(out, avail2caller.QuadPart);
34 + strbuf_addch(out, '\n');
35 +#else
36 + struct statvfs stat;
37 +
38 + strbuf_realpath(&buf, ".", 1);
39 + if (statvfs(buf.buf, &stat) < 0) {
40 + error_errno(_("could not determine free disk size for '%s'"),
41 + buf.buf);
42 + res = -1;
43 + goto cleanup;
44 + }
45 +
46 + strbuf_addf(out, "Available space on '%s': ", buf.buf);
47 + strbuf_humanise_bytes(out, (off_t)stat.f_bsize * (off_t)stat.f_bavail);
48 + strbuf_addf(out, " (mount flags 0x%lx)\n", stat.f_flag);
49 +#endif
50 +
51 +cleanup:
52 + strbuf_release(&buf);
53 + return res;
54 +}
55 +
56 +#endif /* COMPAT_DISK_H */
contrib/scalar/scalar.c
+1 -52
@@ -13,6 +13,7 @@
13 #include "help.h"
14 #include "archive.h"
15 #include "object-store.h"
16 +#include "compat/disk.h"
17
18 /*
19 * Remove the deepest subdirectory in the provided path string. Path must not
@@ -309,58 +310,6 @@ static int add_directory_to_archiver(struct strvec *archiver_args,
310 return res;
311 }
312
312 -#ifndef WIN32
313 -#include <sys/statvfs.h>
314 -#endif
315 -
316 -static int get_disk_info(struct strbuf *out)
317 -{
318 -#ifdef WIN32
319 - struct strbuf buf = STRBUF_INIT;
320 - char volume_name[MAX_PATH], fs_name[MAX_PATH];
321 - DWORD serial_number, component_length, flags;
322 - ULARGE_INTEGER avail2caller, total, avail;
323 -
324 - strbuf_realpath(&buf, ".", 1);
325 - if (!GetDiskFreeSpaceExA(buf.buf, &avail2caller, &total, &avail)) {
326 - error(_("could not determine free disk size for '%s'"),
327 - buf.buf);
328 - strbuf_release(&buf);
329 - return -1;
330 - }
331 -
332 - strbuf_setlen(&buf, offset_1st_component(buf.buf));
333 - if (!GetVolumeInformationA(buf.buf, volume_name, sizeof(volume_name),
334 - &serial_number, &component_length, &flags,
335 - fs_name, sizeof(fs_name))) {
336 - error(_("could not get info for '%s'"), buf.buf);
337 - strbuf_release(&buf);
338 - return -1;
339 - }
340 - strbuf_addf(out, "Available space on '%s': ", buf.buf);
341 - strbuf_humanise_bytes(out, avail2caller.QuadPart);
342 - strbuf_addch(out, '\n');
343 - strbuf_release(&buf);
344 -#else
345 - struct strbuf buf = STRBUF_INIT;
346 - struct statvfs stat;
347 -
348 - strbuf_realpath(&buf, ".", 1);
349 - if (statvfs(buf.buf, &stat) < 0) {
350 - error_errno(_("could not determine free disk size for '%s'"),
351 - buf.buf);
352 - strbuf_release(&buf);
353 - return -1;
354 - }
355 -
356 - strbuf_addf(out, "Available space on '%s': ", buf.buf);
357 - strbuf_humanise_bytes(out, (off_t)stat.f_bsize * (off_t)stat.f_bavail);
358 - strbuf_addf(out, " (mount flags 0x%lx)\n", stat.f_flag);
359 - strbuf_release(&buf);
360 -#endif
361 - return 0;
362 -}
363 -
313 /* printf-style interface, expects `<key>=<value>` argument */
314 static int set_config(const char *fmt, ...)
315 {
git-compat-util.h
+1
@@ -258,6 +258,7 @@ static inline int is_xplatform_dir_sep(int c)
258 #include <sys/resource.h>
259 #include <sys/socket.h>
260 #include <sys/ioctl.h>
261 +#include <sys/statvfs.h>
262 #include <termios.h>
263 #ifndef NO_SYS_SELECT_H
264 #include <sys/select.h>