From anarcat@anarcat.dyndns.org  Wed Oct 10 09:32:00 2001
Return-Path: <anarcat@anarcat.dyndns.org>
Received: from tomts12-srv.bellnexxia.net (tomts12.bellnexxia.net [209.226.175.56])
	by hub.freebsd.org (Postfix) with ESMTP id 8B63937B406
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 10 Oct 2001 09:31:59 -0700 (PDT)
Received: from khan.anarcat.dyndns.org ([65.94.128.156])
          by tomts12-srv.bellnexxia.net
          (InterMail vM.4.01.03.16 201-229-121-116-20010115) with ESMTP
          id <20011010163158.CJOP10438.tomts12-srv.bellnexxia.net@khan.anarcat.dyndns.org>
          for <FreeBSD-gnats-submit@freebsd.org>;
          Wed, 10 Oct 2001 12:31:58 -0400
Received: from shall.anarcat.dyndns.org (shall.anarcat.dyndns.org [192.168.0.1])
	by khan.anarcat.dyndns.org (Postfix) with ESMTP id CC9DC1893
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 10 Oct 2001 12:31:54 -0400 (EDT)
Received: by shall.anarcat.dyndns.org (Postfix, from userid 1000)
	id 3258220B50; Wed, 10 Oct 2001 12:32:06 -0400 (EDT)
Message-Id: <20011010163206.3258220B50@shall.anarcat.dyndns.org>
Date: Wed, 10 Oct 2001 12:32:06 -0400 (EDT)
From: The Anarcat <anarcat@anarcat.dyndns.org>
Reply-To: The Anarcat <anarcat@anarcat.dyndns.org>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: [patch] add free_space(chunk) to libdisk
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         31201
>Category:       kern
>Synopsis:       [libdisk] [patch] add free_space(chunk) to libdisk
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Wed Oct 10 09:40:00 PDT 2001
>Closed-Date:    
>Last-Modified:  Sun Oct 23 22:51:59 GMT 2005
>Originator:     The Anarcat
>Release:        FreeBSD 4.4-STABLE i386
>Organization:
Nada, Inc
>Environment:
System: FreeBSD shall.anarcat.dyndns.org 4.4-STABLE FreeBSD 4.4-STABLE #7: Sat Sep 15 00:41:38 EDT 2001 anarcat@shall.anarcat.dyndns.org:/usr/obj/usr/src/sys/SHALL i386

>Description:

There is no function available to see the "free space" available on a
given slice, where free space is defined as per
src/release/sysinstall/label.c:255 (free = size - sum of used parts)

>How-To-Repeat:

n/a

>Fix:

patch libdisk to include sysinstall' space_free. note that this makes
use of errno to signal an error, this might not be proper style since
size args are usually u_long and we're using plain long here to signal
the error (-1).

diff -c /usr/src/lib/libdisk/chunk.c.orig /usr/src/lib/libdisk/chunk.c
*** /usr/src/lib/libdisk/chunk.c.orig	Wed Oct 10 12:29:42 2001
--- /usr/src/lib/libdisk/chunk.c	Wed Oct 10 12:29:42 2001
***************
*** 16,21 ****
--- 16,22 ----
  #include <string.h>
  #include <sys/types.h>
  #include <err.h>
+ #include <errno.h>
  #include "libdisk.h"
  
  #define new_chunk() memset(malloc(sizeof(struct chunk)), 0, sizeof(struct chunk))
***************
*** 327,332 ****
--- 328,350 ----
  	if (c->flags & CHUNK_IS_ROOT)		ret[i++] = 'R';
  	ret[i++] = '\0';
  	return ret;
+ }
+ 
+ /* return free space of that chunk */
+ long
+ free_space(struct chunk *c)
+ {
+     struct chunk *c1;
+     int sz = c->size;
+     if (!c) {
+       errno = EFAULT;
+       return -1;
+     }
+     for (c1 = c->part; c1; c1 = c1->next) {
+ 	if (c1->type != unused)
+ 	    sz -= c1->size;
+     }
+     return sz;
  }
  
  void


diff -c /usr/src/lib/libdisk/libdisk.h.orig /usr/src/lib/libdisk/libdisk.h
*** /usr/src/lib/libdisk/libdisk.h.orig	Wed Oct 10 12:30:42 2001
--- /usr/src/lib/libdisk/libdisk.h	Wed Oct 10 12:30:43 2001
***************
*** 156,161 ****
--- 156,173 ----
  /* Create a chunk with the specified paramters
   */
  
+ long
+ free_space(struct chunk *c);
+ /* 
+  * return the free space available in this chunk
+  *
+  * free = total - sum "not unused" partitions
+  *
+  * returns the space or -1 if an error occured
+  *
+  * EFAULT: bad chunk pointer given
+  */
+ 
  void
  All_FreeBSD(struct disk *d, int force_all);
  /* Make one FreeBSD chunk covering the entire disk;
>Release-Note:
>Audit-Trail:
>Unformatted:
