1

I'm trying to create a splitted TAR file and reading the progress by checking the size of the directory. (I cannot use PV command).

Can someone explain me why if in tarService.java I change the command to:

String cmd="su -c tar -cp -C /dev . | split -b 1G -a 3 - "+ fname;

The process in MainActivity never log anything until the other process is finished, while with:

String cmd="su -c tar -cpf " + fname + " -C /dev .";

It works!

Take care in both cases the tar file is created.

MainActivitiy.java

    public void startBackup(View v){

       back= new BackgroundTask().execute();

    }

    private class BackgroundTask extends AsyncTask<Void, Integer, String> {


        @Override
        protected String doInBackground(Void... arg0) {
            backup = new Intent(MainActivity.this, tarService.class);
            backup.putExtra("fname", "test.tar);    
            status=999;
            startService(backup);
            while (status == 999) {
                        try {
                            Process process = Runtime.getRuntime().exec("du -sk /dev");
                            BufferedReader bufferedReader = new BufferedReader(
                                    new InputStreamReader(process.getInputStream()));
                            String line;
                            while ((line = bufferedReader.readLine()) != null) {
                                Log.d(TAG,line);
                            }
                        }
            }
        }

    }

tarService.java

protected void onHandleIntent(Intent i) {
        try{
            location=i.getStringExtra("location");
            fname=i.getStringExtra("fname");
            // THIS WORKS
            String cmd="su -c tar -cpf " + fname + " -C /dev .";
            Process process = Runtime.getRuntime().exec(cmd);
            int code=process.waitFor();
            Intent intent = new Intent("BackupProcess");
            intent.putExtra("status", code);
            Log.d(TAG,Integer.toString(code));
            LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
            throw new RuntimeException(e);
        }
 }

SOLVED

There was a code error, I was passing to du command the file name too, but with split the filename is different.

  • Because PIPE uses the output of the parent command as in input; so, there is no point you can get logging details simultaneously using the pipe command. – Am_I_Helpful Aug 20 '17 at 21:29
  • In mainActivity after running the service that execute the tar command, I run another process that read the folder size, without the pipe while the other processes is creating the tar file it shows me the folder size, but with the pipe it doesn't. I know the pipe get the parent command as input, but I'm trying to read the size in another process,I'm wrong? – Marco Migozzi Aug 20 '17 at 23:05

1 Answers1

2

exec() doesn't start a shell, so it doesn't understand shell syntax like redirections and pipes. If you use shell syntax you have to start a shell, via sh -c ....

user207421
  • 305,947
  • 44
  • 307
  • 483