[Libguestfs] [PATCH 1/2] New API: vfs_min_size
Maxim Perevedentsev
mperevedentsev at virtuozzo.com
Tue Oct 20 10:28:23 UTC 2015
On 10/19/2015 07:45 PM, Pino Toscano wrote:
> On Monday 19 October 2015 17:05:02 Maxim Perevedentsev wrote:
>
>> + const char *size_pattern = "You might resize at ",
>> + *full_pattern = "Volume is full",
>> + *cluster_size_pattern = "Cluster size",
>> + *volume_size_pattern = "Current volume size:";
>> + int is_full = 0;
>> + int32_t cluster_size = 0;
>> +
>> + /* FS may be marked for check, so force ntfsresize */
>> + r = command (&out, &err, str_ntfsresize, "--info", "-ff", device, NULL);
>> +
>> + lines = split_lines (out);
>> + if (lines == NULL)
>> + return -1;
>> +
>> + if (verbose) {
>> + for (i = 0; lines[i] != NULL; ++i)
>> + fprintf (stderr, "ntfs_min_size: lines[%zu] = \"%s\"\n", i, lines[i]);
>> + }
>> +
>> + if (r == -1) {
>> + /* If volume is full, ntfsresize returns error. */
>> + for (i = 0; lines[i] != NULL; ++i) {
>> + if (strstr (lines[i], full_pattern))
> Better use STRPREFIX here, which is what we use all around libguestfs.
This is not at the beginning of line.
>
>> + is_full = 1;
>> + else if ((p = strstr (lines[i], cluster_size_pattern))) {
>> + if (sscanf (p + strlen(cluster_size_pattern),
>> + "%*[ ]:%" SCNd32, &cluster_size) != 1) {
>> + reply_with_error("Cannot parse cluster size");
>> + return -1;
>> + }
>> + }
>> + else if ((p = strstr (lines[i], volume_size_pattern))) {
>> + if (sscanf (p + strlen(volume_size_pattern),
>> + "%" SCNd64, &volume_size) != 1) {
>> + reply_with_error("Cannot parse volume size");
>> + return -1;
>> + }
> It sounds like these scans of the lines could be done using the
> analyze_line helper in btrfs.c (which would need to be moved as common
> code in guestfd.c). This way, the parsing of numbers could use
> xstrtoul/xstrtoull (see btrfs.c:do_btrfs_subvolume_list) instead of
> sscanf.
This is the only place where it could be used for now. Does it worth
moving the code? I guess this code won't be shorter or simpler when
using analyze_line.
>> + }
>> +
>> + reply_with_error ("%s", err);
>> + return -1;
>> + }
>> +
>> + for (i = 0; lines[i] != NULL; ++i) {
>> + if ((p = strstr (lines[i], size_pattern))) {
>> + if (sscanf (p + strlen(size_pattern), "%" SCNd64, &ret) != 1) {
>> + reply_with_error("Cannot parse minimum size");
>> + return -1;
> Same notes as above wrt using analyze_line and xstrtoul.
No separator here. The string is "You might resize at 1231231 bytes"
--
Your sincerely,
Maxim Perevedentsev
More information about the Libguestfs
mailing list