libsyncml  0.5.4
data_sync_common.c
1 /*
2  * libsyncml - A syncml protocol implementation
3  * Copyright (C) 2008 Michael Bell <michael.bell@opensync.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  */
20 
21 #include "data_sync_common.h"
22 #include "libsyncml/syncml_internals.h"
23 #include "libsyncml/sml_error_internals.h"
24 
25 #include "data_sync_devinf.h"
26 
27 SmlBool smlDataSyncIsTimestamp(const char *anchor, SmlBool timestampDefault)
28 {
29  smlTrace(TRACE_ENTRY, "%s(%s, %d)", __func__, VA_STRING(anchor), timestampDefault);
30  if (anchor == NULL || strlen(anchor) < 1)
31  {
32  /* there is no anchor, so the default is sent */
33  smlTrace(TRACE_EXIT, "%s - default", __func__);
34  return timestampDefault;
35  } else {
36  /* last is set in the anchor database */
37  if (strstr(anchor, "Z")) {
38  /* anchor is a timestamp */
39  smlTrace(TRACE_EXIT, "%s - TRUE", __func__);
40  return TRUE;
41  } else {
42  /* last is a number */
43  smlTrace(TRACE_EXIT, "%s - FALSE", __func__);
44  return FALSE;
45  }
46  }
47 }
48 
49 char *smlDataSyncGetNextAnchor(
50  SmlDataSyncDatastore *datastore,
51  const char *last,
52  SmlError **error)
53 {
54  smlTrace(TRACE_ENTRY, "%s(%s)", __func__, VA_STRING(last));
55  CHECK_ERROR_REF
56  SmlBool use_timestamp = TRUE;
57  char *next = NULL;
58 
59  use_timestamp = smlDataSyncIsTimestamp(last,
60  datastore->dsObject->useTimestampAnchor);
61  smlTrace(TRACE_INTERNAL, "%s: use timestamp is %d", __func__, use_timestamp);
62 
63  if (use_timestamp)
64  {
65  /* It is necessary to be sure that the remote device supports UTC. */
66  smlTrace(TRACE_INTERNAL,
67  "%s: session %p, localtime %d, remoteDevInf %p",
68  __func__,
69  datastore->dsObject->session,
70  datastore->dsObject->onlyLocaltime,
71  datastore->dsObject->remoteDevInf);
72  if (datastore->dsObject->session &&
73  !datastore->dsObject->onlyLocaltime &&
74  !datastore->dsObject->remoteDevInf &&
75  !smlDataSyncManageDevInf(datastore->dsObject, FALSE, error))
76  {
77  goto error;
78  }
79  next = smlTryMalloc0(sizeof(char)*17, error);
80  if (!next)
81  goto error;
82  time_t htime = time(NULL);
83  if (datastore->dsObject->onlyLocaltime) {
84  smlTrace(TRACE_INTERNAL, "%s: use localtime", __func__);
85  strftime(next, 17, "%Y%m%dT%H%M%SZ", localtime(&htime));
86  } else {
87  smlTrace(TRACE_INTERNAL, "%s: use UTC", __func__);
88  strftime(next, 17, "%Y%m%dT%H%M%SZ", gmtime(&htime));
89  }
90  } else {
91  if (last == NULL)
92  {
93  next = g_strdup("1");
94  } else {
95  unsigned long count = strtoul(last, NULL, 10);
96  count++;
97  next = g_strdup_printf("%lu", count);
98  }
99  }
100  smlTrace(TRACE_EXIT, "%s(%s)", __func__, VA_STRING(next));
101  return next;
102 error:
103  smlTrace(TRACE_EXIT_ERROR, "%s - %s", __func__, smlErrorPrint(error));
104  return NULL;
105 }
This object represents an OMA DS datastore.
Definition: data_sync.h:93
const char * smlErrorPrint(SmlError **error)
Returns the message of the error.
Definition: sml_error.c:299
void smlTrace(SmlTraceType type, const char *message,...)
Used for tracing the application.
Definition: sml_support.c:120
void * smlTryMalloc0(long n_bytes, SmlError **error)
Safely mallocs.
Definition: sml_support.c:335
Represent an error.