Merge branch 'master' of drop.maemo.org:/git/monky
[monky] / src / dbus / dbus-uuidgen.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-uuidgen.c  The guts of the dbus-uuidgen binary live in libdbus, in this file.
3  *
4  * Copyright (C) 2006  Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23 #include "dbus-uuidgen.h"
24 #include "dbus-internals.h"
25 #include "dbus-string.h"
26 #include "dbus-protocol.h"
27
28 #ifdef DBUS_WIN
29 #error "dbus-uuidgen should not be needed on Windows"
30 #endif
31
32 /**
33  * @defgroup DBusInternalsUuidgen dbus-uuidgen implementation
34  * @ingroup DBusInternals
35  * @brief Functions for dbus-uuidgen binary
36  *
37  * These are not considered part of the ABI, and if you call them
38  * you will get screwed by future changes.
39  * 
40  * @{
41  */
42
43 static dbus_bool_t
44 return_uuid (DBusGUID   *uuid,
45              char      **uuid_p,
46              DBusError  *error)
47 {
48   if (uuid_p)
49     {
50       DBusString encoded;
51
52       if (!_dbus_string_init (&encoded))
53         {
54           _DBUS_SET_OOM (error);
55           return FALSE;
56         }
57
58       if (!_dbus_uuid_encode (uuid, &encoded) ||
59           !_dbus_string_steal_data (&encoded, uuid_p))
60         {
61           _DBUS_SET_OOM (error);
62           _dbus_string_free (&encoded);
63           return FALSE;
64         }
65       _dbus_string_free (&encoded);
66     }
67   return TRUE;
68 }
69
70 /**
71  * For use by the dbus-uuidgen binary ONLY, do not call this.
72  * We can and will change this function without modifying
73  * the libdbus soname.
74  *
75  * @param filename the file or #NULL for the machine ID file
76  * @param uuid_p out param to return the uuid
77  * @param create_if_not_found whether to create it if not already there
78  * @param error error return
79  * @returns #FALSE if error is set
80  */
81 dbus_bool_t
82 dbus_internal_do_not_use_get_uuid (const char *filename,
83                                    char      **uuid_p,
84                                    dbus_bool_t create_if_not_found,
85                                    DBusError  *error)
86 {
87   DBusGUID uuid;
88   
89   if (filename)
90     {
91       DBusString filename_str;
92       _dbus_string_init_const (&filename_str, filename);
93       if (!_dbus_read_uuid_file (&filename_str, &uuid, create_if_not_found, error))
94         goto error;
95     }
96   else
97     {
98       if (!_dbus_read_local_machine_uuid (&uuid, create_if_not_found, error))
99         goto error;
100     }
101
102   if (!return_uuid(&uuid, uuid_p, error))
103     goto error;
104
105   return TRUE;
106   
107  error:
108   _DBUS_ASSERT_ERROR_IS_SET (error);
109   return FALSE;
110 }
111
112 /**
113  * For use by the dbus-uuidgen binary ONLY, do not call this.
114  * We can and will change this function without modifying
115  * the libdbus soname.
116  *
117  * @param uuid_p out param to return the uuid
118  * @returns #FALSE if no memory
119  */
120 dbus_bool_t
121 dbus_internal_do_not_use_create_uuid (char      **uuid_p)
122 {
123   DBusGUID uuid;
124
125   _dbus_generate_uuid (&uuid);
126   return return_uuid (&uuid, uuid_p, NULL);
127 }
128
129 /** @} */