idx
int64
0
167k
question
stringlengths
60
3.56k
target
stringlengths
5
1.43k
len_question
int64
21
889
len_target
int64
3
529
167,100
func getChildPID ( ppid int ) ( int , error ) { var pid int // If possible, get the child in O(1). Fallback on O(n) when the kernel does not have // either CONFIG_PROC_CHILDREN or CONFIG_CHECKPOINT_RESTORE _ , err := os . Stat ( " " ) if err == nil { b , err := ioutil . ReadFile ( fmt . Sprintf ( " " , ppid , ppid ) ) if err == nil { children := strings . SplitN ( string ( b ) , " " , 2 ) if len ( children ) == 2 && children [ 1 ] != " " { return - 1 , fmt . Errorf ( " " , ppid ) } if _ , err := fmt . Sscanf ( children [ 0 ] , " " , & pid ) ; err == nil { return pid , nil } } return - 1 , ErrChildNotReady { } } // Fallback on the slower method fdir , err := os . Open ( `/proc` ) if err != nil { return - 1 , err } defer fdir . Close ( ) for { fi , err := fdir . Readdir ( 1 ) if err == io . EOF { break } if err != nil { return - 1 , err } if len ( fi ) == 0 { // See https://github.com/rkt/rkt/issues/3109#issuecomment-242209246 continue } var pid64 int64 if pid64 , err = strconv . ParseInt ( fi [ 0 ] . Name ( ) , 10 , 0 ) ; err != nil { continue } filename := fmt . Sprintf ( " " , pid64 ) statBytes , err := ioutil . ReadFile ( filename ) if err != nil { // The process just died? It's not the one we want then. continue } statFields := strings . SplitN ( string ( statBytes ) , " " , 5 ) if len ( statFields ) != 5 { return - 1 , fmt . Errorf ( " " , filename ) } if statFields [ 3 ] == fmt . Sprintf ( " " , ppid ) { return int ( pid64 ) , nil } } return - 1 , ErrChildNotReady { } }
Returns the pid of the child or ErrChildNotReady if not ready
489
14
167,101
func ( p * Pod ) getAppsHashes ( ) ( [ ] types . Hash , error ) { apps , err := p . getApps ( ) if err != nil { return nil , err } var hashes [ ] types . Hash for _ , a := range apps { hashes = append ( hashes , a . Image . ID ) } return hashes , nil }
getAppsHashes returns a list of the app hashes in the pod
75
14
167,102
func ( p * Pod ) getApps ( ) ( schema . AppList , error ) { _ , pm , err := p . PodManifest ( ) if err != nil { return nil , err } return pm . Apps , nil }
getApps returns a list of apps in the pod
49
10
167,103
func ( p * Pod ) getDirNames ( path string ) ( [ ] string , error ) { dir , err := p . openFile ( path , syscall . O_RDONLY | syscall . O_DIRECTORY ) if err != nil { return nil , errwrap . Wrap ( errors . New ( " " ) , err ) } defer dir . Close ( ) ld , err := dir . Readdirnames ( 0 ) if err != nil { return nil , errwrap . Wrap ( errors . New ( " " ) , err ) } return ld , nil }
getDirNames returns the list of names from a pod s directory
126
13
167,104
func ( p * Pod ) Sync ( ) error { cfd , err := p . Fd ( ) if err != nil { return errwrap . Wrap ( fmt . Errorf ( " " , p . UUID . String ( ) ) , err ) } if err := sys . Syncfs ( cfd ) ; err != nil { return errwrap . Wrap ( fmt . Errorf ( " " , p . UUID . String ( ) ) , err ) } return nil }
Sync syncs the pod data . By now it calls a syncfs on the filesystem containing the pod s directory .
100
23
167,105
func WalkPods ( dataDir string , include IncludeMask , f func ( * Pod ) ) error { if err := initPods ( dataDir ) ; err != nil { return err } ls , err := listPods ( dataDir , include ) if err != nil { return errwrap . Wrap ( errors . New ( " " ) , err ) } sort . Strings ( ls ) for _ , uuid := range ls { u , err := types . NewUUID ( uuid ) if err != nil { fmt . Fprintf ( os . Stderr , " " , uuid , err ) continue } p , err := getPod ( dataDir , u ) if err != nil { fmt . Fprintf ( os . Stderr , " " , uuid , err ) continue } // omit pods found in unrequested states // this is to cover a race between listPods finding the uuids and pod states changing // it's preferable to keep these operations lock-free, for example a `rkt gc` shouldn't block `rkt run`. if p . isEmbryo && include & IncludeEmbryoDir == 0 || p . isExitedGarbage && include & IncludeExitedGarbageDir == 0 || p . isGarbage && include & IncludeGarbageDir == 0 || p . isPrepared && include & IncludePreparedDir == 0 || ( ( p . isPreparing || p . isAbortedPrepare ) && include & IncludePrepareDir == 0 ) || p . isRunning ( ) && include & IncludeRunDir == 0 { p . Close ( ) continue } f ( p ) p . Close ( ) } return nil }
WalkPods iterates over the included directories calling function f for every pod found . The pod will be closed after the function f is executed .
355
29
167,106
func ( p * Pod ) PodManifest ( ) ( [ ] byte , * schema . PodManifest , error ) { pmb , err := p . readFile ( " " ) if err != nil { return nil , nil , errwrap . Wrap ( errors . New ( " " ) , err ) } pm := & schema . PodManifest { } if err = pm . UnmarshalJSON ( pmb ) ; err != nil { return nil , nil , errwrap . Wrap ( errors . New ( " " ) , err ) } return pmb , pm , nil }
PodManifest reads the pod manifest returns the raw bytes and the unmarshalled object .
123
18
167,107
func ( p * Pod ) AppImageManifest ( appName string ) ( * schema . ImageManifest , error ) { appACName , err := types . NewACName ( appName ) if err != nil { return nil , err } imb , err := ioutil . ReadFile ( common . AppImageManifestPath ( p . Path ( ) , * appACName ) ) if err != nil { return nil , err } aim := & schema . ImageManifest { } if err := aim . UnmarshalJSON ( imb ) ; err != nil { return nil , errwrap . Wrap ( fmt . Errorf ( " " , appName ) , err ) } return aim , nil }
AppImageManifest returns an ImageManifest for the app .
148
13
167,108
func ( p * Pod ) CreationTime ( ) ( time . Time , error ) { if ! ( p . isPrepared || p . isRunning ( ) || p . IsAfterRun ( ) ) { return time . Time { } , nil } t , err := p . getModTime ( " " ) if err == nil { return t , nil } if ! os . IsNotExist ( err ) { return t , err } // backwards compatibility with rkt before v1.20 return p . getModTime ( " " ) }
CreationTime returns the time when the pod was created . This happens at prepare time .
114
18
167,109
func ( p * Pod ) StartTime ( ) ( time . Time , error ) { var ( t time . Time retErr error ) if ! p . isRunning ( ) && ! p . IsAfterRun ( ) { // hasn't started return t , nil } // check pid and ppid since stage1s can choose one xor the other for _ , ctimeFile := range [ ] string { " " , " " } { t , err := p . getModTime ( ctimeFile ) if err == nil { return t , nil } // if there's an error starting the pod, it can go to "exited" without // creating a ppid/pid file, so ignore not-exist errors. if ! os . IsNotExist ( err ) { retErr = err } } return t , retErr }
StartTime returns the time when the pod was started .
177
11
167,110
func ( p * Pod ) GCMarkedTime ( ) ( time . Time , error ) { if ! p . isGarbage && ! p . isExitedGarbage { return time . Time { } , nil } // At this point, the pod is in either exited-garbage dir, garbage dir or gone already. podPath := p . Path ( ) if podPath == " " { // Pod is gone. return time . Time { } , nil } st := & syscall . Stat_t { } if err := syscall . Lstat ( podPath , st ) ; err != nil { if err == syscall . ENOENT { // Pod is gone. err = nil } return time . Time { } , err } return time . Unix ( st . Ctim . Unix ( ) ) , nil }
GCMarkedTime returns the time when the pod is marked by gc .
175
16
167,111
func ( p * Pod ) Pid ( ) ( int , error ) { if pid , err := p . readIntFromFile ( " " ) ; err == nil { return pid , nil } if pid , err := p . readIntFromFile ( " " ) ; err != nil { return - 1 , err } else { return pid , nil } }
Pid returns the pid of the stage1 process that started the pod .
75
15
167,112
func ( p * Pod ) Stage1RootfsPath ( ) ( string , error ) { stage1RootfsPath := " " if p . UsesOverlay ( ) { stage1TreeStoreID , err := p . GetStage1TreeStoreID ( ) if err != nil { return " " , err } stage1RootfsPath = fmt . Sprintf ( " " , stage1TreeStoreID ) } return filepath . Join ( p . Path ( ) , stage1RootfsPath ) , nil }
Stage1RootfsPath returns the stage1 path of the pod .
107
14
167,113
func ( p * Pod ) JournalLogPath ( ) ( string , error ) { stage1RootfsPath , err := p . Stage1RootfsPath ( ) if err != nil { return " " , err } return filepath . Join ( stage1RootfsPath , " " ) , nil }
JournalLogPath returns the path to the journal log dir of the pod .
63
15
167,114
func ( p * Pod ) State ( ) string { switch { case p . isEmbryo : return Embryo case p . isPreparing : return Preparing case p . isAbortedPrepare : return AbortedPrepare case p . isPrepared : return Prepared case p . isDeleting : return Deleting case p . isExitedDeleting : return ExitedDeleting case p . isExited : // this covers p.isExitedGarbage if p . isExitedGarbage { return ExitedGarbage } return Exited case p . isGarbage : return Garbage } return Running }
State returns the current state of the pod
135
8
167,115
func ( p * Pod ) isRunning ( ) bool { // when none of these things, running! return ! p . isEmbryo && ! p . isAbortedPrepare && ! p . isPreparing && ! p . isPrepared && ! p . isExited && ! p . isExitedGarbage && ! p . isExitedDeleting && ! p . isGarbage && ! p . isDeleting && ! p . isGone }
isRunning does the annoying tests to infer if a pod is in a running state
101
16
167,116
func ( p * Pod ) PodManifestAvailable ( ) bool { if p . isPreparing || p . isAbortedPrepare || p . isDeleting { return false } return true }
PodManifestAvailable returns whether the caller should reasonably expect PodManifest to function in the pod s current state . Namely in Preparing AbortedPrepare and Deleting it s possible for the manifest to not be present
42
45
167,117
func ( p * Pod ) IsAfterRun ( ) bool { return p . isExitedDeleting || p . isDeleting || p . isExited || p . isGarbage }
IsAfterRun returns true if the pod is in a post - running state otherwise it returns false .
42
20
167,118
func ( p * Pod ) IsFinished ( ) bool { return p . isExited || p . isAbortedPrepare || p . isGarbage || p . isGone }
IsFinished returns true if the pod is in a terminal state else false .
40
16
167,119
func ( p * Pod ) AppExitCode ( appName string ) ( int , error ) { stage1RootfsPath , err := p . Stage1RootfsPath ( ) if err != nil { return - 1 , err } statusFile := common . AppStatusPathFromStage1Rootfs ( stage1RootfsPath , appName ) return p . readIntFromFile ( statusFile ) }
AppExitCode returns the app s exit code . It returns an error if the exit code file doesn t exit or the content of the file is invalid .
83
31
167,120
func StopPod ( dir string , force bool , uuid * types . UUID ) error { s1rootfs := common . Stage1RootfsPath ( dir ) if err := os . Chdir ( dir ) ; err != nil { return fmt . Errorf ( " " , err ) } ep , err := getStage1Entrypoint ( dir , stopEntrypoint ) if err != nil { return fmt . Errorf ( " " , err ) } args := [ ] string { filepath . Join ( s1rootfs , ep ) } debug ( " " , ep ) if force { args = append ( args , " " ) } args = append ( args , uuid . String ( ) ) c := exec . Cmd { Path : args [ 0 ] , Args : args , Stdout : os . Stdout , Stderr : os . Stderr , } return c . Run ( ) }
StopPod stops the given pod .
194
7
167,121
func ( c * Config ) MarshalJSON ( ) ( [ ] byte , error ) { stage0 := [ ] interface { } { } for host , auth := range c . AuthPerHost { var typ string var credentials interface { } switch h := auth . ( type ) { case * basicAuthHeaderer : typ = " " credentials = h . auth case * oAuthBearerTokenHeaderer : typ = " " credentials = h . auth case * awsAuthHeaderer : typ = " " credentials = h . auth default : return nil , errors . New ( " " ) } auth := struct { RktVersion string `json:"rktVersion"` RktKind string `json:"rktKind"` Domains [ ] string `json:"domains"` Type string `json:"type"` Credentials interface { } `json:"credentials"` } { RktVersion : " " , RktKind : " " , Domains : [ ] string { host } , Type : typ , Credentials : credentials , } stage0 = append ( stage0 , auth ) } for registry , credentials := range c . DockerCredentialsPerRegistry { dockerAuth := struct { RktVersion string `json:"rktVersion"` RktKind string `json:"rktKind"` Registries [ ] string `json:"registries"` Credentials BasicCredentials `json:"credentials"` } { RktVersion : " " , RktKind : " " , Registries : [ ] string { registry } , Credentials : credentials , } stage0 = append ( stage0 , dockerAuth ) } paths := struct { RktVersion string `json:"rktVersion"` RktKind string `json:"rktKind"` Data string `json:"data"` Stage1Images string `json:"stage1-images"` } { RktVersion : " " , RktKind : " " , Data : c . Paths . DataDir , Stage1Images : c . Paths . Stage1ImagesDir , } stage1 := struct { RktVersion string `json:"rktVersion"` RktKind string `json:"rktKind"` Name string `json:"name"` Version string `json:"version"` Location string `json:"location"` } { RktVersion : " " , RktKind : " " , Name : c . Stage1 . Name , Version : c . Stage1 . Version , Location : c . Stage1 . Location , } stage0 = append ( stage0 , paths , stage1 ) data := map [ string ] interface { } { " " : stage0 } return json . Marshal ( data ) }
MarshalJSON marshals the config for user output .
573
11
167,122
func ResolveAuthPerHost ( authPerHost map [ string ] Headerer ) map [ string ] http . Header { hostHeaders := make ( map [ string ] http . Header , len ( authPerHost ) ) for k , v := range authPerHost { hostHeaders [ k ] = v . GetHeader ( ) } return hostHeaders }
ResolveAuthPerHost takes a map of strings to Headerer and resolves the Headerers to http . Headers
75
23
167,123
func GetConfigFrom ( dirs ... string ) ( * Config , error ) { cfg := newConfig ( ) for _ , cd := range dirs { subcfg , err := GetConfigFromDir ( cd ) if err != nil { return nil , err } mergeConfigs ( cfg , subcfg ) } return cfg , nil }
GetConfigFrom gets the Config instance with configuration taken from given paths . Subsequent paths override settings from the previous paths .
72
24
167,124
func GetConfigFromDir ( dir string ) ( * Config , error ) { subcfg := newConfig ( ) if valid , err := validDir ( dir ) ; err != nil { return nil , err } else if ! valid { return subcfg , nil } if err := readConfigDir ( subcfg , dir ) ; err != nil { return nil , err } return subcfg , nil }
GetConfigFromDir gets the Config instance with configuration taken from given directory .
82
15
167,125
func WriteEnvFile ( env [ ] string , uidRange * user . UidRange , envFilePath string ) error { ef := bytes . Buffer { } for _ , v := range env { fmt . Fprintf ( & ef , " \n " , v ) } if err := os . MkdirAll ( filepath . Dir ( envFilePath ) , 0755 ) ; err != nil { return err } if err := ioutil . WriteFile ( envFilePath , ef . Bytes ( ) , 0644 ) ; err != nil { return err } if err := user . ShiftFiles ( [ ] string { envFilePath } , uidRange ) ; err != nil { return err } return nil }
WriteEnvFile creates an environment file for given app name . To ensure the minimum required environment variables by the appc spec are set to sensible defaults env should be the result of calling ComposeEnviron . The containing directory and its ancestors will be created if necessary .
156
54
167,126
func ComposeEnviron ( env types . Environment ) [ ] string { var composed [ ] string for dk , dv := range defaultEnv { if _ , exists := env . Get ( dk ) ; ! exists { composed = append ( composed , fmt . Sprintf ( " " , dk , dv ) ) } } for _ , e := range env { composed = append ( composed , fmt . Sprintf ( " " , e . Name , e . Value ) ) } return composed }
ComposeEnviron formats the environment into a slice of strings each of the form key = value . The minimum required environment variables by the appc spec will be set to sensible defaults here if they re not provided by env .
106
45
167,127
func ( f mountFlags ) String ( ) string { var s [ ] string maybeAppendFlag := func ( ff uintptr , desc string ) { if uintptr ( f ) & ff != 0 { s = append ( s , desc ) } } maybeAppendFlag ( syscall . MS_DIRSYNC , " " ) maybeAppendFlag ( syscall . MS_MANDLOCK , " " ) maybeAppendFlag ( syscall . MS_NOATIME , " " ) maybeAppendFlag ( syscall . MS_NODEV , " " ) maybeAppendFlag ( syscall . MS_NODIRATIME , " " ) maybeAppendFlag ( syscall . MS_NOEXEC , " " ) maybeAppendFlag ( syscall . MS_NOSUID , " " ) maybeAppendFlag ( syscall . MS_RDONLY , " " ) maybeAppendFlag ( syscall . MS_REC , " " ) maybeAppendFlag ( syscall . MS_RELATIME , " " ) maybeAppendFlag ( syscall . MS_SILENT , " " ) maybeAppendFlag ( syscall . MS_STRICTATIME , " " ) maybeAppendFlag ( syscall . MS_SYNCHRONOUS , " " ) maybeAppendFlag ( syscall . MS_REMOUNT , " " ) maybeAppendFlag ( syscall . MS_BIND , " " ) maybeAppendFlag ( syscall . MS_SHARED , " " ) maybeAppendFlag ( syscall . MS_PRIVATE , " " ) maybeAppendFlag ( syscall . MS_SLAVE , " " ) maybeAppendFlag ( syscall . MS_UNBINDABLE , " " ) maybeAppendFlag ( syscall . MS_MOVE , " " ) return strings . Join ( s , " " ) }
String returns a human readable representation of mountFlags based on which bits are set . E . g . for a value of syscall . MS_RDONLY|syscall . MS_BIND it will print MS_RDONLY|MS_BIND
425
54
167,128
func newValidator ( image io . ReadSeeker ) ( * validator , error ) { manifest , err := aci . ManifestFromImage ( image ) if err != nil { return nil , err } v := & validator { image : image , manifest : manifest , } return v , nil }
newValidator returns a validator instance if passed image is indeed an ACI .
63
17
167,129
func ( v * validator ) ValidateName ( imageName string ) error { name := v . ImageName ( ) if name != imageName { return fmt . Errorf ( " " , imageName , name ) } return nil }
ValidateName checks if desired image name is actually the same as the one in the image manifest .
49
20
167,130
func ( v * validator ) ValidateLabels ( labels map [ types . ACIdentifier ] string ) error { for n , rv := range labels { if av , ok := v . manifest . GetLabel ( n . String ( ) ) ; ok { if rv != av { return fmt . Errorf ( " " , n , rv , av ) } } else { return fmt . Errorf ( " " , n ) } } return nil }
ValidateLabels checks if desired image labels are actually the same as the ones in the image manifest .
97
21
167,131
func ( v * validator ) ValidateWithSignature ( ks * keystore . Keystore , sig io . ReadSeeker ) ( * openpgp . Entity , error ) { if ks == nil { return nil , nil } if _ , err := v . image . Seek ( 0 , 0 ) ; err != nil { return nil , errwrap . Wrap ( errors . New ( " " ) , err ) } if _ , err := sig . Seek ( 0 , 0 ) ; err != nil { return nil , errwrap . Wrap ( errors . New ( " " ) , err ) } entity , err := ks . CheckSignature ( v . ImageName ( ) , v . image , sig ) if err == pgperrors . ErrUnknownIssuer { log . Print ( " " ) log . Print ( " " ) } if err != nil { return nil , err } return entity , nil }
ValidateWithSignature verifies the image against a given signature file .
193
15
167,132
func Register ( distType Type , f newDistribution ) { if _ , ok := distributions [ distType ] ; ok { panic ( fmt . Errorf ( " " , distType ) ) } distributions [ distType ] = f }
Register registers a function that returns a new instance of the given distribution . This is intended to be called from the init function in packages that implement distribution functions .
49
31
167,133
func Get ( u * url . URL ) ( Distribution , error ) { c , err := parseCIMD ( u ) if err != nil { return nil , fmt . Errorf ( " " , u . String ( ) , err ) } if u . Scheme != Scheme { return nil , fmt . Errorf ( " " , u . String ( ) ) } if _ , ok := distributions [ c . Type ] ; ! ok { return nil , fmt . Errorf ( " " , c . Type ) } return distributions [ c . Type ] ( u ) }
Get returns a Distribution from the input URI . It returns an error if the uri string is wrong or referencing an unknown distribution type .
118
27
167,134
func Parse ( rawuri string ) ( Distribution , error ) { u , err := url . Parse ( rawuri ) if err != nil { return nil , fmt . Errorf ( " " , rawuri , err ) } return Get ( u ) }
Parse parses the provided distribution URI string and returns a Distribution .
54
14
167,135
func newCompletion ( w io . Writer ) func ( * cobra . Command , [ ] string ) int { return func ( cmd * cobra . Command , args [ ] string ) int { return runCompletion ( w , cmd , args ) } }
newCompletion creates a new command with a bounded writer . Writer is used to print the generated shell - completion script which is intended to be consumed by the CLI users .
54
34
167,136
func runCompletion ( w io . Writer , cmd * cobra . Command , args [ ] string ) ( exit int ) { if len ( args ) == 0 { stderr . Print ( " " ) return 254 } if len ( args ) > 1 { stderr . Print ( " " ) return 254 } // Right now only bash completion is supported, but zsh could be // supported in a future as well. completion , ok := completionShells [ args [ 0 ] ] if ! ok { stderr . Printf ( " " , args [ 0 ] ) return 254 } // Write the shell completion to the specified writer. err := completion ( w , cmd . Parent ( ) ) if err != nil { stderr . PrintE ( " " , err ) return 254 } return 0 }
runCompletion is a command handler to generate the shell script with shell completion functions . It ensures that there are enough arguments to generate the completion scripts .
169
30