[GRASS5] DB default settings now defined in new mapset

Hi,

I have fixed the lack of default DBF DB settings when
creating a new mapset. Now both the text interface and the
GUI create the dbf/ directory in a newly created mapset
and copy over the VAR file from PERMANENT which contains
the default DB settings.

Fixed in both 6.0-CVS (for 6.0.2) and 6.1-CVS.

A user can certainly modify the settings with db.connect as
before.

Markus

On Tue, Oct 18, 2005 at 11:42:52AM +0200, Markus Neteler wrote:

Hi,

I have fixed the lack of default DBF DB settings when
creating a new mapset. Now both the text interface and the
GUI create the dbf/ directory in a newly created mapset
and copy over the VAR file from PERMANENT which contains
the default DB settings.

Radim suggested to me that it is better to not copy the
PERMANENT/VAR settings, but to generate the VAR file
with DBF settings from scratch.

Unfortunately, I don't manage to program it:
In file lib/init/mke_mapset.c

/* generate DB settings file in new mapset */
        sprintf(buffer,"'%s'/'%s'/VAR", location, mapset);
        if ((fd = creat (buffer, O_RDWR)) < 0) {
                perror (buffer);
                G_fatal_error("Cannot create VAR file in new mapset");
        }

        fprintf (fd, "DB_DRIVER: dbf\n");
        fprintf (fd, "DB_DATABASE: $GISDBASE/$LOCATION_NAME/$MAPSET/dbf/\n");
        fclose (fd);

mke_mapset.c: In function `make_mapset':
mke_mapset.c:33: warning: assignment makes pointer from integer without a cast

My usual string problems... Then it happily segfaults.
Any idea?

Markus

Hello Markus

On Tue, 18 Oct 2005, Markus Neteler wrote:

Unfortunately, I don't manage to program it:
In file lib/init/mke_mapset.c

/* generate DB settings file in new mapset */
       sprintf(buffer,"'%s'/'%s'/VAR", location, mapset);
       if ((fd = creat (buffer, O_RDWR)) < 0) {
               perror (buffer);
               G_fatal_error("Cannot create VAR file in new mapset");
       }

       fprintf (fd, "DB_DRIVER: dbf\n");
       fprintf (fd, "DB_DATABASE: $GISDBASE/$LOCATION_NAME/$MAPSET/dbf/\n");
       fclose (fd);

mke_mapset.c: In function `make_mapset':
mke_mapset.c:33: warning: assignment makes pointer from integer without a cast

My usual string problems... Then it happily segfaults.
Any idea?

I think creat() only returns a file descriptor. But fprintf and fclose have a stream as their first argument. So you need to call fdopen() to derive a stream from the file descriptor returned by creat. I think. But why not just do it all in one step with fopen() instead of creat(). I have never seen creat() used like that.

Also to be more buffer overflow-safe you could write
sprintf(buffer,"'%s'/'%s'/VAR", location, mapset);
instead as
char *buffer;
G_asprintf(&buffer,"'%s'/'%s'/VAR", location, mapset);
/* do something with buffer */
G_free(buffer);

Hope this helps a little bit.

Paul

Hello Paul,

On Tue, Oct 18, 2005 at 04:07:56PM +0100, Paul Kelly wrote:

Hello Markus

On Tue, 18 Oct 2005, Markus Neteler wrote:

>Unfortunately, I don't manage to program it:
>In file lib/init/mke_mapset.c
>
>/* generate DB settings file in new mapset */
> sprintf(buffer,"'%s'/'%s'/VAR", location, mapset);
> if ((fd = creat (buffer, O_RDWR)) < 0) {
> perror (buffer);
> G_fatal_error("Cannot create VAR file in new mapset");
> }
>
> fprintf (fd, "DB_DRIVER: dbf\n");
> fprintf (fd, "DB_DATABASE:
> $GISDBASE/$LOCATION_NAME/$MAPSET/dbf/\n");
> fclose (fd);
>
>mke_mapset.c: In function `make_mapset':
>mke_mapset.c:33: warning: assignment makes pointer from integer without a
>cast
>
>My usual string problems... Then it happily segfaults.
>Any idea?

I think creat() only returns a file descriptor. But fprintf and fclose
have a stream as their first argument. So you need to call fdopen() to
derive a stream from the file descriptor returned by creat. I think. But
why not just do it all in one step with fopen() instead of creat(). I have
never seen creat() used like that.

I was looking at lock.c in the same directory. Maybe it's wrong
as well?

Also to be more buffer overflow-safe you could write
sprintf(buffer,"'%s'/'%s'/VAR", location, mapset);
instead as
char *buffer;
G_asprintf(&buffer,"'%s'/'%s'/VAR", location, mapset);
/* do something with buffer */
G_free(buffer);

Hope this helps a little bit.

Cool - I got it working.

Thanks for the quick help,

Markus

PS: Now I have to learn TCL to fix the GUI as well

On Tue, 18 Oct 2005, Markus Neteler wrote:

I think creat() only returns a file descriptor. But fprintf and fclose
have a stream as their first argument. So you need to call fdopen() to
derive a stream from the file descriptor returned by creat. I think. But
why not just do it all in one step with fopen() instead of creat(). I have
never seen creat() used like that.

I was looking at lock.c in the same directory. Maybe it's wrong
as well?

I had a look and it uses write() instead of fprintf(). So that will work with creat(). I have no idea why it's written like that. Maybe faster.

Paul

Hi again,

update:

I have fixed the lack of default DBF DB settings when
creating a new mapset. Now both the text interface and the
GUI create the dbf/ directory in a newly created mapset
and generate a new VAR file which contains the default DB
settings to DBF.

Fixed again in both 6.0-CVS (for 6.0.2) and 6.1-CVS.

A user can certainly modify the settings with db.connect as
before.

cheers

Markus

I have fixed the lack of default DBF DB settings when
creating a new mapset. Now both the text interface and the
GUI create the dbf/ directory in a newly created mapset
and generate a new VAR file which contains the default DB
settings to DBF.

Fixed again in both 6.0-CVS (for 6.0.2) and 6.1-CVS.

A user can certainly modify the settings with db.connect as
before.

just as a historical note, the v.in.garmin script has a test & shell
code for this.

Hamish