Example #1
0
gboolean
synce_app_man_create_program_list(IRAPISession *session, GList **list, SynceAppManBusyFunc busy_func, gpointer busy_data, GError **error)
{
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  CEOSVERSIONINFO version_info;
  HRESULT hr;
  DWORD last_error;

  version_info.dwOSVersionInfoSize = sizeof(CEOSVERSIONINFO);
  if (!IRAPISession_CeGetVersionEx(session, &version_info)) {
    if (FAILED(hr = IRAPISession_CeRapiGetError(session))) {
      g_set_error(error,
		  SYNCE_APP_MAN_ERROR,
		  SYNCE_APP_MAN_ERROR_RAPI_TERM,
		  _("Unable to obtain application list: Unable to determine OS version: %s"),
		  synce_strerror(hr));
      return FALSE;
    }

    last_error = IRAPISession_CeGetLastError(session);
    g_set_error(error,
		SYNCE_APP_MAN_ERROR,
		SYNCE_APP_MAN_ERROR_RAPI,
		_("Unable to obtain application list: Unable to determine OS version: %s"),
		synce_strerror(last_error));
    return FALSE;
  }

  if ((version_info.dwMajorVersion < 5) || ((version_info.dwMajorVersion == 5) && (version_info.dwMinorVersion == 0))) {
    return create_program_list_legacy(session, list, busy_func, busy_data, error);
  }

  return create_program_list(session, list, busy_func, busy_data, error);
}
int rapi_mkdir(char *dir) {
    HRESULT hr;
    WCHAR* wide_path = NULL;
    char *path = NULL;
    
    path = (char *) strdup(dir);
    
    hr = CeRapiInit();

    if (RAPI_FAILED(hr))
    {
        fprintf(stderr, "Unable to initialize RAPI: %s\n",
                synce_strerror(hr));
        if (wide_path)
            wstr_free_string(wide_path);

        if (path)
            free(path);

        CeRapiUninit();
        return (-1);
    }

    convert_to_backward_slashes(path);
    wide_path = wstr_from_utf8(path);
    wide_path = adjust_remote_path(wide_path, true);
    
    if (!CeCreateDirectory(wide_path, NULL))
    {
        fprintf(stderr, "Failed to create directory '%s': %s\n",
                path,
                synce_strerror(CeGetLastError()));
        if (wide_path)
            wstr_free_string(wide_path);

        if (path)
            free(path);

        CeRapiUninit();
        return (-2);
    }

    if (wide_path)
        wstr_free_string(wide_path);

    if (path)
        free(path);

    CeRapiUninit();
    return 0;
}
Example #3
0
static gboolean
app_uninstall_legacy(IRAPISession *session, const gchar *program, GError **error)
{
  HRESULT hr;
  DWORD last_error;
  BOOL result;

  WCHAR* wide_command = NULL;
  WCHAR* wide_parameters = NULL;
  PROCESS_INFORMATION info;
  gchar *command = NULL;

  command = g_strdup("unload.exe");
  wide_command = wstr_from_utf8(command);
  wide_parameters = wstr_from_utf8(program);
  memset(&info, 0, sizeof(info));

  result = IRAPISession_CeCreateProcess(session, wide_command, wide_parameters,
			   NULL, NULL, FALSE, 0, NULL, NULL, NULL,
			   &info);
  IRAPISession_CeCloseHandle(session, info.hProcess);
  IRAPISession_CeCloseHandle(session, info.hThread);
  wstr_free_string(wide_command);
  wstr_free_string(wide_parameters);
  g_free(command);

  if (result)
    return TRUE;

  if (FAILED(hr = IRAPISession_CeRapiGetError(session))) {
    g_set_error(error,
		SYNCE_APP_MAN_ERROR,
		SYNCE_APP_MAN_ERROR_RAPI_TERM,
		_("Failed to execute uninstaller: %s"),
		synce_strerror(hr));
    return FALSE;
  }

  last_error = IRAPISession_CeGetLastError(session);
  g_set_error(error,
	      SYNCE_APP_MAN_ERROR,
	      SYNCE_APP_MAN_ERROR_RAPI,
	      _("Failed to execute uninstaller: %s"),
	      synce_strerror(last_error));
  return FALSE;
}
Example #4
0
gboolean
synce_app_man_uninstall(IRAPISession *session, const gchar *program, GError **error)
{
  CEOSVERSIONINFO version_info;
  HRESULT hr;
  DWORD last_error;

  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  g_debug("%s: requested removal of program %s", G_STRFUNC, program);

  version_info.dwOSVersionInfoSize = sizeof(CEOSVERSIONINFO);
  if (!IRAPISession_CeGetVersionEx(session, &version_info)) {
    if (FAILED(hr = IRAPISession_CeRapiGetError(session))) {
      g_set_error(error,
		  SYNCE_APP_MAN_ERROR,
		  SYNCE_APP_MAN_ERROR_RAPI_TERM,
		  _("Unable to uninstall application: Unable to determine OS version: %s"),
		  synce_strerror(hr));
      return FALSE;
    }

    last_error = IRAPISession_CeGetLastError(session);
    g_set_error(error,
		SYNCE_APP_MAN_ERROR,
		SYNCE_APP_MAN_ERROR_RAPI,
		_("Unable to uninstall application: Unable to determine OS version: %s"),
		synce_strerror(last_error));
    return FALSE;
  }

  if ((version_info.dwMajorVersion < 5) || ((version_info.dwMajorVersion == 5) && (version_info.dwMinorVersion == 0))) {
    return app_uninstall_legacy(session, program, error);
  }

  return app_uninstall(session, program, error);
}
int rapi_copy(char *source, char *dest)
{
	int result = 1;
    HRESULT hr;
    time_t start;
    time_t duration;
    size_t bytes_copied = 0;

    hr = CeRapiInit();

    if (RAPI_FAILED(hr))
    {
        fprintf(stderr, "Unable to initialize RAPI: %s\n",
                synce_strerror(hr));
        goto exit;
    }

    if (!dest)
    {
        char* p;

        if (is_remote_file(source))
        {

            for (p = source + strlen(source); p != source; p--)
            {
                if (*p == '/' || *p == '\\')
                {
                    dest = (char *) strdup(p+1);
                    break;
                }
            }

            if (!dest || '\0' == dest[0])
            {
                fprintf(stderr, "Unable to extract destination filename from source path '%s'\n",
                        source);
                goto exit;
            }
        }
        else
        {
            WCHAR mydocuments[MAX_PATH];
            char* mydocuments_ascii = NULL;
            p = strrchr(source, '/');

            if (p)
                p++;
            else
                p = source;

            if ('\0' == *p)
            {
                fprintf(stderr, "Unable to extract destination filename from source path '%s'\n",
                        source);
                goto exit;
            }

            if (!CeGetSpecialFolderPath(CSIDL_PERSONAL, sizeof(mydocuments), mydocuments))
            {
                fprintf(stderr, "Unable to get the \"My Documents\" path.\n");
                goto exit;
            }

            dest = (char *) calloc(1, 1 + wstr_strlen(mydocuments) + 1 + strlen(p) + 1);

            mydocuments_ascii = wstr_to_ascii(mydocuments);

            strcat(dest, ":");
            strcat(dest, mydocuments_ascii);
            strcat(dest, "\\");
            strcat(dest, p);

            wstr_free_string(mydocuments_ascii);
        }
    }


    if (0 == strcmp(source, dest))
    {
        fprintf(stderr, "You don't want to copy a file to itself.\n");
        goto exit;
    }

    if (is_remote_file(source) && is_remote_file(dest))
    {
        /*
         *          * Both are remote; use CeCopyFile()
         *                   */
        if (!remote_copy(source, dest))
            goto exit;
    }
    else
    {
        start = time(NULL);

        /*
         *          * At least one is local, Use the AnyFile functions
         *                   */
        if (!anyfile_copy(source, dest,  &bytes_copied))
            goto exit;

    }

	result = 0;
exit:

    CeRapiUninit();
    return result;
}
GList* setup_rapi_and_create_list(GList *list, GtkWidget *progressbar) 
{
    HRESULT hr;
    LONG result;
    HKEY parent_key;
    WCHAR* parent_key_name = NULL;
    WCHAR* value_name = NULL;
    DWORD i;
    bool smartphone = false;

    hr = CeRapiInit();

    if (RAPI_FAILED(hr))
    {
        fprintf(stderr, "Unable to initialize RAPI: %s\n", 
                synce_strerror(hr));
        return (list);
    }

    /* Update the progressbar */
    if (progressbar != NULL) {
        gtk_progress_bar_pulse(GTK_PROGRESS_BAR(progressbar));
        while (gtk_events_pending()) {
            gtk_main_iteration();
        }
    }
    /* Path on SmartPhone 2002 */
    parent_key_name = wstr_from_ascii("Security\\AppInstall");

    result = CeRegOpenKeyEx(HKEY_LOCAL_MACHINE, parent_key_name, 0, 0, &parent_key);

    if (ERROR_SUCCESS == result)
    {
        smartphone = true;
    }
    else
    {
        smartphone = false;
        wstr_free_string(parent_key_name);

        /* Path on Pocket PC 2002 */
        parent_key_name = wstr_from_ascii("Software\\Apps");

        result = CeRegOpenKeyEx(HKEY_LOCAL_MACHINE, parent_key_name, 0, 0, &parent_key);

        if (ERROR_SUCCESS != result)
        {
            fprintf(stderr, "Unable to open parent registry key: %s\n", 
                    synce_strerror(result));
            return(list);
        }
    }
    value_name = wstr_from_ascii("Instl");

    /* Update the progressbar */
    if (progressbar != NULL) {
        gtk_progress_bar_pulse(GTK_PROGRESS_BAR(progressbar));
        while (gtk_events_pending()) {
            gtk_main_iteration();
        }
    }

    for (i = 0; ; i++)
    {
        WCHAR wide_name[MAX_PATH];
        DWORD name_size = sizeof(wide_name);
        HKEY program_key;
        DWORD installed = 0;
        DWORD value_size = sizeof(installed);

        result = CeRegEnumKeyEx(parent_key, i, wide_name, &name_size, 
                NULL, NULL, NULL, NULL);

        /* Update the progressbar */
        if (progressbar != NULL) {
            gtk_progress_bar_pulse(GTK_PROGRESS_BAR(progressbar));
            while (gtk_events_pending()) {
                gtk_main_iteration();
            }
        }
        if (ERROR_SUCCESS != result)
            break;

        if (smartphone)
        {
            char* name = wstr_to_utf8(wide_name);
            list = g_list_append(list,g_strdup(name));
            wstr_free_string(name);
        }
        else
        {
            result = CeRegOpenKeyEx(parent_key, wide_name, 0, 0, &program_key);
            if (ERROR_SUCCESS != result)
                continue;

            result = CeRegQueryValueEx(program_key, value_name, NULL, NULL,
                    (LPBYTE)&installed, &value_size);

            if (ERROR_SUCCESS == result && installed)
            {
                char* name = wstr_to_utf8(wide_name);
                list = g_list_append(list,g_strdup(name));
                wstr_free_string(name);
            }
            CeRegCloseKey(program_key);
        }

    }

    CeRegCloseKey(parent_key);

    return (list);
}
Example #7
0
int main(int argc, char** argv)
{
	int result = 1;
        RapiConnection* connection = NULL;
	char* source = NULL;
	char* dest = NULL;
	HRESULT hr;
	time_t start;
	time_t duration;
	size_t bytes_copied = 0;
	
	if (!handle_parameters(argc, argv, &source, &dest))
		goto exit;

        if ((connection = rapi_connection_from_path(devpath)) == NULL)
        {
          fprintf(stderr, "%s: Could not find configuration at path '%s'\n", 
                  argv[0],
                  devpath?devpath:"(Default)");
          goto exit;
        }
        rapi_connection_select(connection);
	hr = CeRapiInit();

	if (FAILED(hr))
	{
		fprintf(stderr, "%s: Unable to initialize RAPI: %s\n", 
				argv[0],
				synce_strerror(hr));
		goto exit;
	}

	if (!dest)
	{
		char* p;

		if (is_remote_file(source))
		{

			for (p = source + strlen(source); p != source; p--)
			{
				if (*p == '/' || *p == '\\')
				{
					dest = strdup(p+1);
					break;
				}
			}

			if (!dest || '\0' == dest[0])
			{
				fprintf(stderr, "%s: Unable to extract destination filename from source path '%s'\n",
						argv[0], source);
				goto exit;
			}
		}
		else
		{
			WCHAR mydocuments[MAX_PATH];
			char* mydocuments_ascii = NULL;
			p = strrchr(source, '/');

			if (p)
				p++;
			else
				p = source;

			if ('\0' == *p)
			{
				fprintf(stderr, "%s: Unable to extract destination filename from source path '%s'\n",
						argv[0], source);
				goto exit;
			}

			if (!CeGetSpecialFolderPath(CSIDL_PERSONAL, sizeof(mydocuments), mydocuments))
			{
				fprintf(stderr, "%s: Unable to get the \"My Documents\" path.\n",
						argv[0]);
				goto exit;
			}

			dest = calloc(1, 1 + wstr_strlen(mydocuments) + 1 + strlen(p) + 1);
			
			mydocuments_ascii = wstr_to_current(mydocuments);
			
			strcat(dest, ":");
			strcat(dest, mydocuments_ascii);
			strcat(dest, "\\");
			strcat(dest, p);
			
			wstr_free_string(mydocuments_ascii);
		}
	}

	if (0 == strcmp(source, dest))
	{
		fprintf(stderr, "You don't want to copy a file to itself.\n");
		goto exit;
	}

	if (is_remote_file(source) && is_remote_file(dest))
	{
		/*
		 * Both are remote; use CeCopyFile()
		 */
		if (!remote_copy(source, dest))
			goto exit;
	}
	else
	{
		start = time(NULL);

			/*
		 * At least one is local, Use the AnyFile functions
		 */
		if (!anyfile_copy(source, dest, argv[0], &bytes_copied))
			goto exit;

		duration = time(NULL) - start;

		if (0 == duration)
			printf("File copy took less than one second!\n");
		else
			printf("File copy of %i bytes took %li minutes and %li seconds, that's %li bytes/s.\n",
					bytes_copied, duration / 60, duration % 60, bytes_copied / duration);

	}

	result = 0;

exit:
	if (source)
		free(source);

	if (dest)
		free(dest);

	CeRapiUninit();
	return result;
}
Example #8
0
HRESULT rapi_context_connect(RapiContext* context)
{
    HRESULT result = E_FAIL;
    SynceInfo* info = NULL;

    if (context->is_initialized)
    {
        /* Fail immediately */
            return CERAPI_E_ALREADYINITIALIZED;
    }

    if (context->info)
        info = context->info;
    else
        info = synce_info_new(NULL);
    if (!info)
    {
        synce_error("Failed to get connection info");
        goto fail;
    }

    const char *transport = synce_info_get_transport(info);
    /*
     *  original dccm or vdccm, sanity checking
     */
    if (transport == NULL || ( strcmp(transport, "odccm") != 0 && strcmp(transport, "udev") != 0 ) ) {
        pid_t dccm_pid = 0;
        if (!(dccm_pid = synce_info_get_dccm_pid(info)))
        {
            synce_error("DCCM PID entry not found for current connection");
            goto fail;
        }

        if (kill(dccm_pid, 0) < 0)
        {
            if (errno != EPERM)
            {
                synce_error("DCCM not running with pid %i", synce_info_get_dccm_pid(info));
                goto fail;
            }
        }

        if (!synce_info_get_device_ip(info))
        {
            synce_error("IP entry not found for current connection");
            goto fail;
        }
    }

    /*
     *  original dccm or vdccm
     */
    if (transport == NULL || strncmp(transport,  "ppp", 3) == 0) {
        /*
         *  original dccm or vdccm
         */
        if ( !synce_socket_connect(context->socket, synce_info_get_device_ip(info), RAPI_PORT) )
        {
            synce_error("failed to connect to %s", synce_info_get_device_ip(info));
            goto fail;
        }

        const char *password = synce_info_get_password(info);
        if (password && strlen(password))
        {
            bool password_correct = false;

            if (!synce_password_send(context->socket, password, (unsigned char)synce_info_get_key(info)))
            {
                synce_error("failed to send password");
                result = E_ACCESSDENIED;
                goto fail;
            }

            if (!synce_password_recv_reply(context->socket, 1, &password_correct))
            {
                synce_error("failed to get password reply");
                result = E_ACCESSDENIED;
                goto fail;
            }

            if (!password_correct)
            {
                synce_error("invalid password");
                result = E_ACCESSDENIED;
                goto fail;
            }
        }
        context->rapi_ops = &rapi_ops;
    } else {
        /*
         *  odccm, udev, or proxy ?
         */
#if ENABLE_ODCCM_SUPPORT
        if (strcmp(transport, "odccm") == 0) {
	  int fd = -1;
	  HRESULT fd_result = get_connection_from_udev_or_odccm(info, &fd);
	  if (fd_result != S_OK)
	  {
	    synce_error("failed to get context fd from odccm: %08x: %s", fd_result, synce_strerror(HRESULT_CODE(fd_result)));
	    result = fd_result;
	    goto fail;
	  }
	  synce_socket_take_descriptor(context->socket, fd);
        }
        else
#endif
#if ENABLE_UDEV_SUPPORT
        if (strcmp(transport, "udev") == 0) {
	  int fd = -1;
	  HRESULT fd_result = get_connection_from_udev_or_odccm(info, &fd);
	  if (fd_result != S_OK)
	  {
	    synce_error("failed to get context fd from udev: %08x: %s", fd_result, synce_strerror(HRESULT_CODE(fd_result)));
	    result = fd_result;
	    goto fail;
	  }
	  synce_socket_take_descriptor(context->socket, fd);
        }
        else
#endif
	if ( !synce_socket_connect_proxy(context->socket, synce_info_get_device_ip(info)) )
        {
            synce_error("failed to connect to proxy for %s", synce_info_get_device_ip(info));
            goto fail;
        }

	/* rapi 2 seems to be used on devices with OS version of 5.1 or greater */

        unsigned int os_major = 0, os_minor = 0;
        synce_info_get_os_version(info, &os_major, &os_minor);
	if ((os_major > 4) && (os_minor > 0))
	  context->rapi_ops = &rapi2_ops;
	else
	  context->rapi_ops = &rapi_ops;
    }

    if (!context->info)
    {
      context->info = info;
      context->own_info = true;
    }

    context->is_initialized = true;
    result = S_OK;

fail:
    if (!context->info)
        synce_info_destroy(info);
    return result;
}
Example #9
0
int main(int argc, char** argv)
{
	int result = 1;
        RapiConnection* connection = NULL;
	char* program = NULL;
	char* parameters = NULL;
	HRESULT hr;
	WCHAR* wide_program = NULL;
	WCHAR* wide_parameters = NULL;
	PROCESS_INFORMATION info;

	if (!handle_parameters(argc, argv, &program, &parameters))
		goto exit;

        if ((connection = rapi_connection_from_path(devpath)) == NULL)
        {
          fprintf(stderr, "%s: Could not find configuration at path '%s'\n", 
                  argv[0],
                  devpath?devpath:"(Default)");
          goto exit;
        }
        rapi_connection_select(connection);
	hr = CeRapiInit();

	if (FAILED(hr))
	{
		fprintf(stderr, "%s: Unable to initialize RAPI: %s\n", 
				argv[0],
				synce_strerror(hr));
		goto exit;
	}

	convert_to_backward_slashes(program);
	wide_program = wstr_from_current(program);
	if (parameters)
		wide_parameters = wstr_from_current(parameters);

	memset(&info, 0, sizeof(info));
	
	if (!CeCreateProcess(
				wide_program,
				wide_parameters,
				NULL,
				NULL,
				false,
				0,
				NULL,
				NULL,
				NULL,
				&info
				))
	{
		fprintf(stderr, "%s: Failed to execute '%s': %s\n", 
				argv[0],
				program,
				synce_strerror(CeGetLastError()));
		goto exit;
	}

	CeCloseHandle(info.hProcess);
	CeCloseHandle(info.hThread);

	result = 0;

exit:
	wstr_free_string(wide_program);
	wstr_free_string(wide_parameters);

	if (program)
		free(program);

	if (parameters)
		free(parameters);

	CeRapiUninit();
	return result;
}
Example #10
0
gboolean
synce_app_man_install(IRAPISession *session, const gchar *filepath, SynceAppManBusyFunc busy_func, gpointer busy_data, GError **error)
{
  gchar *install_path = NULL;
  SYSTEM_INFO system;
  gboolean result;
  WCHAR* wide_program = NULL;
  PROCESS_INFORMATION info;
  BOOL rapi_res;
  HRESULT hr;
  DWORD last_error;
  gchar *tmpdir = NULL;
  GList *cab_list = NULL;

  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  if (busy_func)
    (*busy_func)(busy_data);

  memset(&system, 0, sizeof(system));
  IRAPISession_CeGetSystemInfo(session, &system);

#if GLIB_CHECK_VERSION(2,30,0)
  tmpdir = g_dir_make_tmp("sti_XXXXXX", error);
  if (!tmpdir) {
    g_prefix_error(error, _("Failed to create temp directory '%s': "), tmpdir);
    result = FALSE;
    goto exit;
  }
  g_chmod(tmpdir, 0700);
#else
  tmpdir = tempnam(g_get_tmp_dir(), "sti");
  if (g_mkdir(tmpdir, 0700) != 0) {
    g_set_error(error,
		G_FILE_ERROR,
		g_file_error_from_errno(errno),
		_("Failed to create temp directory '%s': %s"),
                tmpdir,
		g_strerror(errno));
    result = FALSE;
    goto exit;
  }
#endif

  cab_list = extract_with_orange(filepath, tmpdir, system.dwProcessorType);
  if (!cab_list) {
    g_set_error(error,
		SYNCE_APP_MAN_ERROR,
		SYNCE_APP_MAN_ERROR_INVALID_INSTALL_FILE,
		_("No CAB files found"));
    return FALSE;
  }

  /* Do some install things */

  install_path = get_install_dir(session, error);
  if (!install_path) {
    result = FALSE;
    goto exit;
  }

  GList *tmplist = g_list_first(cab_list);
  while(tmplist) {
    gchar *cabname = tmplist->data;
    g_debug("%s: copying file %s to device", G_STRFUNC, cabname);

    if (!copy_to_device(session, cabname, install_path, busy_func, busy_data, error)) {
      result = FALSE;
      goto exit;
    }

    tmplist = g_list_next(tmplist);
  }

  wide_program = wstr_from_utf8("wceload.exe");
  memset(&info, 0, sizeof(info));

  rapi_res = IRAPISession_CeCreateProcess(session, wide_program, NULL, 
			     NULL, NULL, false, 0,
			     NULL, NULL, NULL, 
			     &info);
  wstr_free_string(wide_program);
  IRAPISession_CeCloseHandle(session, info.hProcess);
  IRAPISession_CeCloseHandle(session, info.hThread);

  if (!rapi_res) {
    if (FAILED(hr = IRAPISession_CeRapiGetError(session))) {
      g_set_error(error,
		  SYNCE_APP_MAN_ERROR,
		  SYNCE_APP_MAN_ERROR_RAPI_TERM,
		  _("Failed to execute installer: %s"),
		  synce_strerror(hr));
      result = FALSE;
      goto exit;
    }

    last_error = IRAPISession_CeGetLastError(session);
    g_set_error(error,
		SYNCE_APP_MAN_ERROR,
		SYNCE_APP_MAN_ERROR_RAPI,
		_("Failed to execute installer: %s"),
		synce_strerror(last_error));
    result = FALSE;
    goto exit;
  }

  g_debug("%s: successfully installed %s on device", G_STRFUNC, filepath);
  result = TRUE;

exit:

  tmplist = g_list_first(cab_list);
  while(tmplist)
    {
      g_unlink((gchar *)(tmplist->data));
      tmplist = g_list_next(tmplist);
    }

  if (tmpdir) {
          g_rmdir(tmpdir);
          g_free(tmpdir);
  }

  return result;
}
Example #11
0
static gboolean
copy_to_device(IRAPISession *session, const gchar *source, const gchar *dest_dir, SynceAppManBusyFunc busy_func, gpointer busy_data, GError **error)
{
  gboolean result = TRUE;
  gsize bytes_read;
  guchar* buffer = NULL;
  WCHAR* wide_filename = NULL;
  HANDLE dest_handle;
  HRESULT hr;
  DWORD last_error;
  BOOL retval;
  DWORD bytes_written;;
  FILE *src_handle;
  gint errnum;

  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  if (!(buffer = (guchar *) g_malloc(BUFFER_SIZE))) {
    g_warning("%s: Failed to allocate file copy buffer", G_STRFUNC);
    g_set_error(error,
		SYNCE_APP_MAN_ERROR,
		SYNCE_APP_MAN_ERROR_FAILED,
		_("Failed to copy file \"%s\" to device: failed to allocate buffer"),
		source);
    result = FALSE;
    goto exit;
  }

  gchar *source_base = g_strrstr(source, "/") + 1;
  gchar *device_filename = g_strdup_printf("%s/%s", dest_dir, source_base);
  wide_filename = wstr_from_utf8(device_filename);
  g_free(device_filename);

  dest_handle = IRAPISession_CeCreateFile(session, wide_filename, GENERIC_WRITE, 0, NULL,
			     CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  wstr_free_string(wide_filename);
  if (INVALID_HANDLE_VALUE == dest_handle) {
    if (FAILED(hr = IRAPISession_CeRapiGetError(session))) {
      g_set_error(error,
		  SYNCE_APP_MAN_ERROR,
		  SYNCE_APP_MAN_ERROR_RAPI_TERM,
		  _("Failed to copy file \"%s\" to device: failed to open destination file: %s"),
		  source,
		  synce_strerror(hr));
      result = FALSE;
      goto exit;
    }

    last_error = IRAPISession_CeGetLastError(session);
    g_set_error(error,
		SYNCE_APP_MAN_ERROR,
		SYNCE_APP_MAN_ERROR_RAPI,
		_("Failed to copy file \"%s\" to device: failed to open destination file: %s"),
		source,
		synce_strerror(last_error));
    result = FALSE;
    goto exit;
  }

  src_handle = fopen(source, "r");
  if (src_handle == NULL) {
    errnum = errno;
    g_set_error(error,
		SYNCE_APP_MAN_ERROR,
		SYNCE_APP_MAN_ERROR_FAILED,
		_("Failed to copy file \"%s\" to device: failed to open source file: %s"),
		source,
		g_strerror(errnum));
    result = FALSE;
    goto exit;
  }

  while(feof(src_handle) == 0)
    {
      if (busy_func)
	(*busy_func)(busy_data);

      bytes_read = fread(buffer, 1, BUFFER_SIZE, src_handle);
      if (bytes_read != BUFFER_SIZE) {
	if (ferror(src_handle)) {
	  g_set_error(error,
		      SYNCE_APP_MAN_ERROR,
		      SYNCE_APP_MAN_ERROR_FAILED,
		      _("Failed to copy file \"%s\" to device: failed to read from source file"),
		      source);
	  result = FALSE;
	  goto exit;
	}

	/* End of file */
      }
    
      retval = IRAPISession_CeWriteFile(session, dest_handle, buffer, bytes_read, &bytes_written, NULL);
      if (!retval) {
	if (FAILED(hr = IRAPISession_CeRapiGetError(session))) {
	  g_set_error(error,
		      SYNCE_APP_MAN_ERROR,
		      SYNCE_APP_MAN_ERROR_RAPI_TERM,
		      _("Failed to copy file \"%s\" to device: failed to write to destination file: %s"),
		      source,
		      synce_strerror(hr));
	  result = FALSE;
	  goto exit;
	}

	last_error = IRAPISession_CeGetLastError(session);
	g_set_error(error,
		    SYNCE_APP_MAN_ERROR,
		    SYNCE_APP_MAN_ERROR_RAPI,
		    _("Failed to copy file \"%s\" to device: failed to write to destination file: %s"),
		    source,
		    synce_strerror(last_error));
	result = FALSE;
	goto exit;
      }

      if (bytes_written != bytes_read) {
	g_set_error(error,
		    SYNCE_APP_MAN_ERROR,
		    SYNCE_APP_MAN_ERROR_FAILED,
		    _("Failed to copy file \"%s\" to device: failed to write to destination file"),
		    source);
	result = FALSE;
	goto exit;
      }
    }

exit:
  g_free(buffer);

  if (src_handle)
    fclose(src_handle);

  if (dest_handle != INVALID_HANDLE_VALUE)
    IRAPISession_CeCloseHandle(session, dest_handle);

  return result;
}
Example #12
0
static gboolean
create_program_list(IRAPISession *session, GList **list, SynceAppManBusyFunc busy_func, gpointer busy_data, GError **error)
{
  GList *prog_list = NULL;

  xmlDocPtr doc = NULL;
  xmlDocPtr reply_doc = NULL;
  xmlNodePtr parent = NULL;
  xmlNodePtr root_node = NULL, node = NULL, reply_node = NULL;
  xmlChar *doc_string = NULL;
  gint doc_size;

  LPWSTR config_w = NULL;
  LPWSTR reply_w = NULL;
  HRESULT result;

  gchar *reply = NULL;
  gchar *prop = NULL;

  doc = xmlNewDoc((xmlChar *)"1.0");
  root_node = xmlNewNode(NULL, (xmlChar *)"wap-provisioningdoc");
  xmlDocSetRootElement(doc, root_node);
  parent = root_node;

  node = xmlNewNode(NULL, (xmlChar *)"characteristic-query");
  xmlNewProp(node, (xmlChar *)"recursive", (xmlChar *)"false");
  xmlNewProp(node, (xmlChar *)"type", (xmlChar *)"UnInstall");
  xmlAddChild(parent, node);

  xmlDocDumpMemoryEnc(doc, &doc_string, &doc_size, "utf-8");
  g_debug("%s: CeProcessConfig request is \n%s", G_STRFUNC, doc_string);

  config_w = wstr_from_utf8((char *)doc_string);
  xmlFree(doc_string);
  xmlFreeDoc(doc);

  result = IRAPISession_CeProcessConfig(session, config_w, CONFIG_PROCESS_DOCUMENT, &reply_w);

  wstr_free_string(config_w);

  if (result != 0) {
    g_set_error(error,
		SYNCE_APP_MAN_ERROR,
		SYNCE_APP_MAN_ERROR_RAPI,
		_("Unable to obtain application list: %s"),
		synce_strerror(result));
    return FALSE;
  }

  reply = wstr_to_utf8(reply_w);
  wstr_free_string(reply_w);

  reply_doc = xmlReadMemory(reply, strlen(reply), "reply.xml", NULL, 0);

  xmlDocDumpMemoryEnc(reply_doc, &doc_string, &doc_size, "utf-8");
  g_debug("%s: CeProcessConfig response is \n%s", G_STRFUNC, doc_string);
  xmlFree(doc_string);

  reply_node = NULL;
  node = xmlDocGetRootElement(reply_doc);
  node = node->children;

  while(node)
    {
      if (node->type == XML_ELEMENT_NODE)
	{
	  reply_node = node;
	}
      node = node->next;
    }
  if (!reply_node) {
    xmlFreeDoc(reply_doc);
    g_set_error(error,
		SYNCE_APP_MAN_ERROR,
		SYNCE_APP_MAN_ERROR_RAPI,
		_("Unable to obtain application list: Unexpected reply XML structure"));

    return FALSE;
  }

  node = reply_node->children;
  while (node) {
    prop = (gchar *)xmlGetProp(node, (xmlChar *)"type");
    g_strstrip(prop);
    prog_list = g_list_append(prog_list, prop);

    node = node->next;
  }
  xmlFreeDoc(reply_doc);

  *list = prog_list;
  return TRUE;
}
Example #13
0
static gchar*
get_install_dir(IRAPISession *session, GError **error)
{
  WCHAR* wide_path = NULL;
  gchar *path = NULL;
  gchar *tmppath = NULL;
  BOOL result;
  CE_FIND_DATA entry;
  HANDLE handle;
  HRESULT hr;
  DWORD last_error;

  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  path = g_strdup("\\Storage\\Windows\\AppMgr");

  wide_path = wstr_from_utf8(path);
  handle = IRAPISession_CeFindFirstFile(session, wide_path, &entry);
  wstr_free_string(wide_path);

  if (handle != INVALID_HANDLE_VALUE) {
    IRAPISession_CeFindClose(session, handle);
  } else {
    g_free(path);

    if (FAILED(hr = IRAPISession_CeRapiGetError(session))) {
      g_set_error(error,
		  SYNCE_APP_MAN_ERROR,
		  SYNCE_APP_MAN_ERROR_RAPI_TERM,
		  _("Unable to determine installer directory: %s"),
		  synce_strerror(hr));
      return NULL;
    }

    last_error = IRAPISession_CeGetLastError(session);
    if ((last_error != ERROR_PATH_NOT_FOUND) && (last_error != ERROR_NO_MORE_FILES)) {
      g_set_error(error,
		  SYNCE_APP_MAN_ERROR,
		  SYNCE_APP_MAN_ERROR_RAPI,
		  _("Unable to determine installer directory: %s"),
		  synce_strerror(last_error));
      return NULL;
    }

    path = g_strdup("\\Windows\\AppMgr");

    wide_path = wstr_from_utf8(path);
    handle = IRAPISession_CeFindFirstFile(session, wide_path, &entry);
    wstr_free_string(wide_path);

    if (handle != INVALID_HANDLE_VALUE) {
      IRAPISession_CeFindClose(session, handle);
    } else {
      if (FAILED(hr = IRAPISession_CeRapiGetError(session))) {
	g_free(path);
	g_set_error(error,
		    SYNCE_APP_MAN_ERROR,
		    SYNCE_APP_MAN_ERROR_RAPI_TERM,
		    _("Unable to determine installer directory: %s"),
		    synce_strerror(hr));
	return NULL;
      }

      last_error = IRAPISession_CeGetLastError(session);
      if ((last_error != ERROR_PATH_NOT_FOUND) && (last_error != ERROR_NO_MORE_FILES)) {
	g_free(path);
	g_set_error(error,
		    SYNCE_APP_MAN_ERROR,
		    SYNCE_APP_MAN_ERROR_RAPI,
		    _("Unable to determine installer directory: %s"),
		    synce_strerror(last_error));
	return NULL;
      }

      wide_path = wstr_from_utf8(path);

      result = IRAPISession_CeCreateDirectory(session, wide_path, NULL);
      wstr_free_string(wide_path);
      if (!result) {
	if (FAILED(hr = IRAPISession_CeRapiGetError(session))) {
	  g_free(path);
	  g_set_error(error,
		      SYNCE_APP_MAN_ERROR,
		      SYNCE_APP_MAN_ERROR_RAPI_TERM,
		      _("Unable to determine installer directory: %s"),
		      synce_strerror(hr));
	  return NULL;
	}

	last_error = IRAPISession_CeGetLastError(session);
	g_free(path);
	g_set_error(error,
		    SYNCE_APP_MAN_ERROR,
		    SYNCE_APP_MAN_ERROR_RAPI,
		    _("Unable to determine installer directory: %s"),
		    synce_strerror(last_error));
	return NULL;
      }
    }
  }

  tmppath = g_strdup_printf("%s\\Install", path);
  g_free(path);
  path = tmppath;

  wide_path = wstr_from_utf8(path);
  handle = IRAPISession_CeFindFirstFile(session, wide_path, &entry);
  wstr_free_string(wide_path);

  if (handle != INVALID_HANDLE_VALUE) {
    IRAPISession_CeFindClose(session, handle);
  } else {
    if (FAILED(hr = IRAPISession_CeRapiGetError(session))) {
      g_free(path);
      g_set_error(error,
		  SYNCE_APP_MAN_ERROR,
		  SYNCE_APP_MAN_ERROR_RAPI_TERM,
		  _("Unable to determine installer directory: %s"),
		  synce_strerror(hr));
      return NULL;
    }

    last_error = IRAPISession_CeGetLastError(session);
    if ((last_error != ERROR_PATH_NOT_FOUND) && (last_error != ERROR_NO_MORE_FILES)) {
      g_free(path);
      g_set_error(error,
		  SYNCE_APP_MAN_ERROR,
		  SYNCE_APP_MAN_ERROR_RAPI,
		  _("Unable to determine installer directory: %s"),
		  synce_strerror(last_error));
      return NULL;
    }

    wide_path = wstr_from_utf8(path);

    result = IRAPISession_CeCreateDirectory(session, wide_path, NULL);
    wstr_free_string(wide_path);
    if (!result) {
      if (FAILED(hr = IRAPISession_CeRapiGetError(session))) {
	g_free(path);
	g_set_error(error,
		    SYNCE_APP_MAN_ERROR,
		    SYNCE_APP_MAN_ERROR_RAPI_TERM,
		    _("Unable to determine installer directory: %s"),
		    synce_strerror(hr));
	return NULL;
      }

      last_error = IRAPISession_CeGetLastError(session);
      g_free(path);
      g_set_error(error,
		  SYNCE_APP_MAN_ERROR,
		  SYNCE_APP_MAN_ERROR_RAPI,
		  _("Unable to determine installer directory: %s"),
		  synce_strerror(last_error));
      return NULL;
    }
  }

  g_debug("%s: install dir is %s", G_STRFUNC, path);
  return path;
}
Example #14
0
static gboolean
create_program_list_legacy(IRAPISession *session, GList **list, SynceAppManBusyFunc busy_func, gpointer busy_data, GError **error)
{
  LONG rapi_result;
  gboolean result = TRUE;
  HKEY app_parent_key;
  const gchar *app_parent_key_name_utf8 = NULL;
  WCHAR* app_parent_key_name_wide = NULL;
  WCHAR* value_name = NULL;
  DWORD app_count;
  gboolean smartphone = FALSE;
  GList *tmp_list = NULL;
  gchar *app_name = NULL;
  WCHAR app_name_wide[MAX_PATH];
  DWORD app_name_size = MAX_PATH;
  HKEY app_key;
  DWORD installed = 0;
  DWORD value_size = sizeof(installed);


  if (busy_func)
    (*busy_func)(busy_data);

  app_parent_key_name_utf8 = REG_PATH_SMARTPHONE_2002;
  app_parent_key_name_wide = wstr_from_utf8(app_parent_key_name_utf8);

  rapi_result = IRAPISession_CeRegOpenKeyEx(session, HKEY_LOCAL_MACHINE, app_parent_key_name_wide, 0, 0, &app_parent_key);
  wstr_free_string(app_parent_key_name_wide);

  if (ERROR_SUCCESS == rapi_result) {
    smartphone = TRUE;
  } else {
    smartphone = FALSE;

    app_parent_key_name_utf8 = REG_PATH_POCKETPC_2002;
    app_parent_key_name_wide = wstr_from_utf8(app_parent_key_name_utf8);

    rapi_result = IRAPISession_CeRegOpenKeyEx(session, HKEY_LOCAL_MACHINE, app_parent_key_name_wide, 0, 0, &app_parent_key);
    wstr_free_string(app_parent_key_name_wide);

    if (ERROR_SUCCESS != rapi_result) {
      g_set_error(error,
		  SYNCE_APP_MAN_ERROR,
		  SYNCE_APP_MAN_ERROR_RAPI,
		  _("Unable to open applications registry key: %s"),
		  synce_strerror(rapi_result));
      result = FALSE;
      goto exit;
    }
  }

  value_name = wstr_from_utf8("Instl");

  if (busy_func)
    (*busy_func)(busy_data);

  for (app_count = 0; ; app_count++) {
    app_name_size = MAX_PATH;
    installed = 0;
    value_size = sizeof(installed);

    rapi_result = IRAPISession_CeRegEnumKeyEx(session, app_parent_key, app_count, app_name_wide, &app_name_size, 
				 NULL, NULL, NULL, NULL);
    if (rapi_result == ERROR_NO_MORE_ITEMS)
      break;

    if (rapi_result != ERROR_SUCCESS) {
      g_warning("%s: Unable to enumerate registry key '%s': %s", G_STRFUNC, app_parent_key_name_utf8, synce_strerror(rapi_result));
      g_set_error(error,
		  SYNCE_APP_MAN_ERROR,
		  SYNCE_APP_MAN_ERROR_RAPI,
		  _("Unable to enumerate registry key '%s': %s"),
		  app_parent_key_name_utf8,
		  synce_strerror(rapi_result));
      result = FALSE;
      break;
    }

    if (busy_func)
      (*busy_func)(busy_data);

    if (smartphone) {
      app_name = wstr_to_utf8(app_name_wide);
      tmp_list = g_list_append(tmp_list, app_name);
    } else {
      rapi_result = IRAPISession_CeRegOpenKeyEx(session, app_parent_key, app_name_wide, 0, 0, &app_key);
      if (ERROR_SUCCESS != rapi_result)
	continue;

      rapi_result = IRAPISession_CeRegQueryValueEx(session, app_key, value_name, NULL, NULL,
				 (LPBYTE)&installed, &value_size);

      if ((ERROR_SUCCESS == rapi_result) && installed) {
	app_name = wstr_to_utf8(app_name_wide);
	tmp_list = g_list_append(tmp_list, app_name);
      }
      IRAPISession_CeRegCloseKey(session, app_key);
    }

  }

  IRAPISession_CeRegCloseKey(session, app_parent_key);
  wstr_free_string(value_name);

exit:
  if (!result) {
    GList *list_iter = g_list_first(tmp_list);
    while (list_iter != NULL) {
      g_free(list_iter->data);
      list_iter = g_list_next(list_iter);
    }
    g_list_free(tmp_list);
  } else {
    *list = tmp_list;
  }
  return result;
}
int rapi_run(char *program, char *parameters)
{
	int result = 1;
	HRESULT hr;
	WCHAR* wide_program = NULL;
	WCHAR* wide_parameters = NULL;
	PROCESS_INFORMATION info;
    char *tmpprogram = NULL;

    tmpprogram = (char *) strdup(program);
    
	hr = CeRapiInit();

	if (RAPI_FAILED(hr))
	{
		fprintf(stderr, "Unable to initialize RAPI: %s\n", 
				synce_strerror(hr));
		goto exit;
	}

	convert_to_backward_slashes(tmpprogram);
	wide_program = wstr_from_utf8(tmpprogram);
	if (parameters)
		wide_parameters = wstr_from_utf8(parameters);

	memset(&info, 0, sizeof(info));
	
	if (!CeCreateProcess(
				wide_program,
				wide_parameters,
				NULL,
				NULL,
				false,
				0,
				NULL,
				NULL,
				NULL,
				&info
				))
	{
		fprintf(stderr, "Failed to execute '%s': %s\n", 
				tmpprogram,
				synce_strerror(CeGetLastError()));
		goto exit;
	}

	CeCloseHandle(info.hProcess);
	CeCloseHandle(info.hThread);

	result = 0;

exit:
	wstr_free_string(wide_program);
	wstr_free_string(wide_parameters);

	if (tmpprogram)
		free(tmpprogram);

	CeRapiUninit();
	return result;
}
int main(int argc, char** argv)
{
  int result = 0;
  RapiConnection* connection = NULL;
  HRESULT hr;
  char *shortcut, *target;
  WCHAR* wide_shortcut = NULL;
  WCHAR* wide_target = NULL;
  WCHAR *tmp, *tmp_quote;
  int tmpsize;

  if (!handle_parameters(argc, argv, &shortcut, &target))
    goto exit;

  if ((connection = rapi_connection_from_path(devpath)) == NULL)
  {
    fprintf(stderr, "%s: Could not find configuration at path '%s'\n", 
            argv[0],
            devpath?devpath:"(Default)");
    goto exit;
  }
  rapi_connection_select(connection);
  hr = CeRapiInit();

  if (FAILED(hr))
  {
    fprintf(stderr, "%s: Unable to initialize RAPI: %s\n", 
            argv[0],
        synce_strerror(hr));
    result = 1;
    goto exit;
  }


  convert_to_backward_slashes(shortcut);
  wide_shortcut = wstr_from_current(shortcut);
  wide_shortcut = adjust_remote_path(wide_shortcut, true);

  convert_to_backward_slashes(target);
  wide_target = wstr_from_current(target);
  wide_target = adjust_remote_path(wide_target, true);
  /* Wrap target in quotes.  This is required for paths with spaces (for some reason) */
  tmp_quote = wstr_from_current("\"");
  tmpsize = (wstrlen(wide_target) + 3) * sizeof(WCHAR);
  tmp = malloc(tmpsize);
  if (!tmp)
    goto exit;
  wstrcpy(tmp,tmp_quote);
  if (!wstr_append(tmp,wide_target,tmpsize))
    goto exit;
  if (!wstr_append(tmp,tmp_quote,tmpsize))
    goto exit;
  wstr_free_string(wide_target);
  wstr_free_string(tmp_quote);
  wide_target = tmp;
  
  BOOL res = CeSHCreateShortcut(wide_shortcut, wide_target);
  if (!res)
  {
    fprintf(stderr, "%s: Unable to create shortcut to '%s' at '%s': %s\n",
            argv[0],target,shortcut,
        synce_strerror(hr));
    result = 1;
    goto exit;
  }

 exit:
  wstr_free_string(wide_shortcut);
  wstr_free_string(wide_target);
  
  if (shortcut)
    free(shortcut);
  
  if (target)
    free(target);

  CeRapiUninit();
  rapi_connection_destroy(connection);
  
  return result;
}
Example #17
0
int main(int argc, char** argv)
{
	int result = 1;
        RapiConnection* connection = NULL;
	char* source = NULL;
	char* dest = NULL;
	HRESULT hr;
	WCHAR* wide_source = NULL;
	WCHAR* wide_dest = NULL;
	
	if (!handle_parameters(argc, argv, &source, &dest))
		goto exit;

        if ((connection = rapi_connection_from_path(devpath)) == NULL)
        {
          fprintf(stderr, "%s: Could not find configuration at path '%s'\n", 
                  argv[0],
                  devpath?devpath:"(Default)");
          goto exit;
        }
        rapi_connection_select(connection);
	hr = CeRapiInit();

	if (FAILED(hr))
	{
		fprintf(stderr, "%s: Unable to initialize RAPI: %s\n", 
				argv[0],
				synce_strerror(hr));
		goto exit;
	}

	convert_to_backward_slashes(source);
	wide_source = wstr_from_current(source);
	wide_source = adjust_remote_path(wide_source, true);

	convert_to_backward_slashes(dest);
	wide_dest   = wstr_from_current(dest);
	wide_dest   = adjust_remote_path(wide_dest, true);

	if (!CeMoveFile(wide_source, wide_dest))
	{
		fprintf(stderr, "%s: Cannot move '%s' to '%s': %s\n", 
				argv[0],
				source,
				dest,
				synce_strerror(CeGetLastError()));
		goto exit;
	}

	result = 0;

exit:
	wstr_free_string(wide_source);
	wstr_free_string(wide_dest);

	if (source)
		free(source);

	if (dest)
		free(dest);

	CeRapiUninit();
	return result;
}